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"
49 #include <sys/utime.h>
52 int *__p__fmode(void);
53 int *__p___mb_cur_max(void);
58 #define _IOCOMMIT 0x4000
86 /* _access() bit flags FIXME: incomplete */
87 /* defined in crt/io.h */
89 /* values for wxflag in file descriptor */
92 #define WX_READNL 0x04 /* read started with \n */
93 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
95 #define WX_READCR 0x08 /* underlying file is at \r */
96 #define WX_DONTINHERIT 0x10
97 #define WX_APPEND 0x20
101 /* values for exflag - it's used differently in msvcr90.dll*/
103 #define EF_UTF16 0x02
104 #define EF_CRIT_INIT 0x04
105 #define EF_UNK_UNICODE 0x08
107 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
108 static char utf16_bom
[2] = { 0xff, 0xfe };
110 /* FIXME: this should be allocated dynamically */
111 #define MSVCRT_MAX_FILES 2048
112 #define MSVCRT_FD_BLOCK_SIZE 32
114 #define MSVCRT_INTERNAL_BUFSIZ 4096
116 /* ioinfo structure size is different in msvcrXX.dll's */
119 unsigned char wxflag
;
122 CRITICAL_SECTION crit
;
125 /*********************************************************************
126 * __pioinfo (MSVCRT.@)
127 * array of pointers to ioinfo arrays [32]
129 ioinfo
* __pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
131 /*********************************************************************
132 * __badioinfo (MSVCRT.@)
134 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
136 static int fdstart
= 3; /* first unallocated fd */
137 static int fdend
= 3; /* highest allocated fd */
141 CRITICAL_SECTION crit
;
144 FILE _iob
[_IOB_ENTRIES
] = { { 0 } };
145 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { NULL
};
146 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
148 /* INTERNAL: process umask */
149 static int MSVCRT_umask
= 0;
151 /* INTERNAL: static data for tmpnam and _wtmpname functions */
152 static int tmpnam_unique
;
154 /* This critical section protects the tables __pioinfo and fstreams,
155 * and their related indexes, fdstart, fdend,
156 * and MSVCRT_stream_idx, from race conditions.
157 * It doesn't protect against race conditions manipulating the underlying files
158 * or flags; doing so would probably be better accomplished with per-file
159 * protection, rather than locking the whole table for every change.
161 static CRITICAL_SECTION MSVCRT_file_cs
;
162 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
164 0, 0, &MSVCRT_file_cs
,
165 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
166 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
168 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
169 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
170 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
172 static inline ioinfo
* get_ioinfo_nolock(int fd
)
175 if(fd
>=0 && fd
<MSVCRT_MAX_FILES
)
176 ret
= __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
180 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
183 static inline ioinfo
* get_ioinfo(int fd
)
185 ioinfo
*ret
= get_ioinfo_nolock(fd
);
186 if(ret
->exflag
& EF_CRIT_INIT
)
187 EnterCriticalSection(&ret
->crit
);
191 static inline void release_ioinfo(ioinfo
*info
)
193 if(info
->exflag
& EF_CRIT_INIT
)
194 LeaveCriticalSection(&info
->crit
);
197 static inline FILE* msvcrt_get_file(int i
)
201 if(i
>= MSVCRT_max_streams
)
207 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
209 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
210 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
211 ERR("out of memory\n");
216 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
218 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
223 static inline BOOL
is_valid_fd(int fd
)
225 return fd
>= 0 && fd
< fdend
&& (get_ioinfo_nolock(fd
)->wxflag
& WX_OPEN
);
228 /* INTERNAL: Get the HANDLE for a fd
229 * This doesn't lock the table, because a failure will result in
230 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
231 * it returns a valid handle which is about to be closed, a subsequent call
232 * will fail, most likely in a sane way.
234 /*static*/ HANDLE
fdtoh(int fd
)
236 if (!is_valid_fd(fd
))
238 WARN(":fd (%d) - no handle!\n",fd
);
241 return INVALID_HANDLE_VALUE
;
243 //if (get_ioinfo_nolock(fd)->handle == INVALID_HANDLE_VALUE)
244 //FIXME("returning INVALID_HANDLE_VALUE for %d\n", fd);
245 return get_ioinfo_nolock(fd
)->handle
;
248 /* INTERNAL: free a file entry fd */
249 static void msvcrt_free_fd(int fd
)
254 fdinfo
= get_ioinfo_nolock(fd
);
255 if(fdinfo
!= &__badioinfo
)
257 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
260 TRACE(":fd (%d) freed\n",fd
);
267 SetStdHandle(STD_INPUT_HANDLE
, 0);
270 SetStdHandle(STD_OUTPUT_HANDLE
, 0);
273 SetStdHandle(STD_ERROR_HANDLE
, 0);
285 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
286 /* caller must hold the files lock */
287 static int msvcrt_set_fd(HANDLE hand
, int flag
, int fd
)
291 if (fd
>= MSVCRT_MAX_FILES
)
293 WARN(":files exhausted!\n");
298 fdinfo
= get_ioinfo_nolock(fd
);
299 if(fdinfo
== &__badioinfo
) {
302 __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
303 if(!__pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
304 WARN(":out of memory!\n");
309 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
310 __pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
312 fdinfo
= get_ioinfo_nolock(fd
);
315 fdinfo
->handle
= hand
;
316 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
| WX_PIPE
| WX_TTY
));
317 fdinfo
->lookahead
[0] = '\n';
318 fdinfo
->lookahead
[1] = '\n';
319 fdinfo
->lookahead
[2] = '\n';
320 if(!(fdinfo
->exflag
& EF_CRIT_INIT
))
321 InitializeCriticalSection(&fdinfo
->crit
);
322 fdinfo
->exflag
= EF_CRIT_INIT
;
324 /* locate next free slot */
325 if (fd
== fdstart
&& fd
== fdend
)
328 while (fdstart
< fdend
&&
329 get_ioinfo_nolock(fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
331 /* update last fd in use */
334 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
338 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
339 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
340 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
346 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
347 /*static*/ int msvcrt_alloc_fd(HANDLE hand
, int flag
)
352 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
353 ret
= msvcrt_set_fd(hand
, flag
, fdstart
);
358 /* INTERNAL: Allocate a FILE* for an fd slot */
359 /* caller must hold the files lock */
360 static FILE* msvcrt_alloc_fp(void)
365 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
367 file
= msvcrt_get_file(i
);
371 if (file
->_flag
== 0)
373 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
381 /* INTERNAL: initialize a FILE* from an open fd */
382 static int msvcrt_init_fp(FILE* file
, int fd
, unsigned stream_flags
)
384 TRACE(":fd (%d) allocating FILE*\n",fd
);
385 if (!is_valid_fd(fd
))
387 WARN(":invalid fd %d\n",fd
);
392 file
->_ptr
= file
->_base
= NULL
;
395 file
->_flag
= stream_flags
;
396 file
->_tmpfname
= NULL
;
398 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
399 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
401 TRACE(":got FILE* (%p)\n",file
);
405 /* INTERNAL: Create an inheritance data block (for spawned process)
406 * The inheritance block is made of:
407 * 00 int nb of file descriptor (NBFD)
408 * 04 char file flags (wxflag): repeated for each fd
409 * 4+NBFD HANDLE file handle: repeated for each fd
411 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
418 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
419 *block
= calloc(1, *size
);
425 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
426 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
428 *(unsigned*)*block
= fdend
;
429 for (fd
= 0; fd
< fdend
; fd
++)
431 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
432 fdinfo
= get_ioinfo_nolock(fd
);
433 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
435 *wxflag_ptr
= fdinfo
->wxflag
;
436 *handle_ptr
= fdinfo
->handle
;
441 *handle_ptr
= INVALID_HANDLE_VALUE
;
443 wxflag_ptr
++; handle_ptr
++;
448 /* INTERNAL: Set up all file descriptors,
449 * as well as default streams (stdin, stderr and stdout)
451 void msvcrt_init_io(void)
457 GetStartupInfoA(&si
);
458 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
464 count
= *(unsigned*)si
.lpReserved2
;
465 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
466 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
468 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
469 count
= min(count
, MSVCRT_MAX_FILES
);
470 for (i
= 0; i
< count
; i
++)
472 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
473 msvcrt_set_fd(*handle_ptr
, *wxflag_ptr
, i
);
475 wxflag_ptr
++; handle_ptr
++;
477 fdend
= max( 3, count
);
478 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
479 if (get_ioinfo_nolock(fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
482 fdinfo
= get_ioinfo_nolock(STDIN_FILENO
);
483 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
484 HANDLE h
= GetStdHandle(STD_INPUT_HANDLE
);
485 DWORD type
= GetFileType(h
);
487 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
488 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDIN_FILENO
);
491 fdinfo
= get_ioinfo_nolock(STDOUT_FILENO
);
492 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
493 HANDLE h
= GetStdHandle(STD_OUTPUT_HANDLE
);
494 DWORD type
= GetFileType(h
);
496 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
497 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDOUT_FILENO
);
500 fdinfo
= get_ioinfo_nolock(STDERR_FILENO
);
501 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
) {
502 HANDLE h
= GetStdHandle(STD_ERROR_HANDLE
);
503 DWORD type
= GetFileType(h
);
505 msvcrt_set_fd(h
, WX_OPEN
|WX_TEXT
|((type
&0xf)==FILE_TYPE_CHAR
? WX_TTY
: 0)
506 |((type
&0xf)==FILE_TYPE_PIPE
? WX_PIPE
: 0), STDERR_FILENO
);
509 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO
)->handle
,
510 get_ioinfo_nolock(STDOUT_FILENO
)->handle
,
511 get_ioinfo_nolock(STDERR_FILENO
)->handle
);
513 memset(_iob
,0,3*sizeof(FILE));
514 for (i
= 0; i
< 3; i
++)
516 /* FILE structs for stdin/out/err are static and never deleted */
518 _iob
[i
]._tmpfname
= NULL
;
519 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
521 MSVCRT_stream_idx
= 3;
524 /* INTERNAL: Flush stdio file buffer */
525 static int msvcrt_flush_buffer(FILE* file
)
527 if((file
->_flag
& (_IOREAD
|_IOWRT
)) == _IOWRT
&&
528 file
->_flag
& (_IOMYBUF
|_USERBUF
)) {
529 int cnt
=file
->_ptr
-file
->_base
;
530 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
531 file
->_flag
|= _IOERR
;
535 if(file
->_flag
& _IORW
)
536 file
->_flag
&= ~_IOWRT
;
538 #ifdef __REACTOS__ /* CORE-11949 */
539 file
->_ptr
=file
->_base
;
544 #ifndef __REACTOS__ /* CORE-11949 */
545 file
->_ptr
=file
->_base
;
551 /*********************************************************************
554 int CDECL
_isatty(int fd
)
556 TRACE(":fd (%d)\n",fd
);
558 return get_ioinfo_nolock(fd
)->wxflag
& WX_TTY
;
561 /* INTERNAL: Allocate stdio file buffer */
562 /*static*/ BOOL
msvcrt_alloc_buffer(FILE* file
)
564 if((file
->_file
==STDOUT_FILENO
|| file
->_file
==STDERR_FILENO
)
565 && _isatty(file
->_file
))
568 file
->_base
= calloc(1, MSVCRT_INTERNAL_BUFSIZ
);
570 file
->_bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
571 file
->_flag
|= _IOMYBUF
;
573 file
->_base
= (char*)(&file
->_charbuf
);
575 file
->_flag
|= _IONBF
;
577 file
->_ptr
= file
->_base
;
582 /* INTERNAL: Allocate temporary buffer for stdout and stderr */
583 static BOOL
add_std_buffer(FILE *file
)
585 static char buffers
[2][BUFSIZ
];
587 if((file
->_file
!=STDOUT_FILENO
&& file
->_file
!=STDERR_FILENO
)
588 || (file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
589 || !_isatty(file
->_file
))
592 file
->_ptr
= file
->_base
= buffers
[file
->_file
== STDOUT_FILENO
? 0 : 1];
593 file
->_bufsiz
= file
->_cnt
= BUFSIZ
;
594 file
->_flag
|= _USERBUF
;
598 /* INTERNAL: Removes temporary buffer from stdout or stderr */
599 /* Only call this function when add_std_buffer returned TRUE */
600 static void remove_std_buffer(FILE *file
)
602 msvcrt_flush_buffer(file
);
603 file
->_ptr
= file
->_base
= NULL
;
604 file
->_bufsiz
= file
->_cnt
= 0;
605 file
->_flag
&= ~_USERBUF
;
608 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
609 static int msvcrt_int_to_base32(int num
, char *str
)
624 *p
= (num
& 31) + '0';
626 *p
+= ('a' - '0' - 10);
633 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
634 static int msvcrt_int_to_base32_w(int num
, wchar_t *str
)
649 *p
= (num
& 31) + '0';
651 *p
+= ('a' - '0' - 10);
658 /* INTERNAL: Create a wide string from an ascii string */
659 wchar_t *msvcrt_wstrdupa(const char *str
)
661 const unsigned int len
= strlen(str
) + 1 ;
662 wchar_t *wstr
= malloc(len
* sizeof (wchar_t));
665 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
,str
,len
,wstr
,len
);
669 /*********************************************************************
670 * __iob_func(MSVCRT.@)
672 FILE * CDECL
__iob_func(void)
677 /*********************************************************************
680 int CDECL
_access(const char *filename
, int mode
)
682 DWORD attr
= GetFileAttributesA(filename
);
684 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
686 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
688 _dosmaperr(GetLastError());
691 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
693 _set_errno(ERROR_ACCESS_DENIED
);
699 /*********************************************************************
700 * _access_s (MSVCRT.@)
702 int CDECL
_access_s(const char *filename
, int mode
)
704 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
705 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
711 return _access(filename
, mode
);
714 /*********************************************************************
715 * _waccess (MSVCRT.@)
717 int CDECL
_waccess(const wchar_t *filename
, int mode
)
719 DWORD attr
= GetFileAttributesW(filename
);
721 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
723 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
725 _dosmaperr(GetLastError());
728 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
730 _set_errno(ERROR_ACCESS_DENIED
);
736 /*********************************************************************
737 * _waccess_s (MSVCRT.@)
739 int CDECL
_waccess_s(const wchar_t *filename
, int mode
)
741 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
742 !MSVCRT_CHECK_PMT((mode
& ~(R_OK
| W_OK
)) == 0))
748 return _waccess(filename
, mode
);
751 /*********************************************************************
754 int CDECL
_chmod(const char *path
, int flags
)
756 DWORD oldFlags
= GetFileAttributesA(path
);
758 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
760 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
761 oldFlags
| FILE_ATTRIBUTE_READONLY
;
763 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
766 _dosmaperr(GetLastError());
770 /*********************************************************************
773 int CDECL
_wchmod(const wchar_t *path
, int flags
)
775 DWORD oldFlags
= GetFileAttributesW(path
);
777 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
779 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
780 oldFlags
| FILE_ATTRIBUTE_READONLY
;
782 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
785 _dosmaperr(GetLastError());
789 /*********************************************************************
792 int CDECL
_unlink(const char *path
)
794 TRACE("%s\n",debugstr_a(path
));
795 if(DeleteFileA(path
))
797 TRACE("failed (%d)\n",GetLastError());
798 _dosmaperr(GetLastError());
802 /*********************************************************************
803 * _wunlink (MSVCRT.@)
805 int CDECL
_wunlink(const wchar_t *path
)
807 TRACE("(%s)\n",debugstr_w(path
));
808 if(DeleteFileW(path
))
810 TRACE("failed (%d)\n",GetLastError());
811 _dosmaperr(GetLastError());
815 /*********************************************************************
818 int CDECL
_commit(int fd
)
820 ioinfo
*info
= get_ioinfo(fd
);
823 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
824 if (info
->handle
== INVALID_HANDLE_VALUE
)
826 else if (!FlushFileBuffers(info
->handle
))
828 if (GetLastError() == ERROR_INVALID_HANDLE
)
830 /* FlushFileBuffers fails for console handles
831 * so we ignore this error.
837 TRACE(":failed-last error (%d)\n",GetLastError());
838 _dosmaperr(GetLastError());
848 release_ioinfo(info
);
852 /* _flushall calls fflush which calls _flushall */
853 int CDECL
fflush(FILE* file
);
855 /* INTERNAL: Flush all stream buffer */
856 static int msvcrt_flush_all_buffers(int mask
)
858 int i
, num_flushed
= 0;
862 for (i
= 0; i
< MSVCRT_stream_idx
; i
++) {
863 file
= msvcrt_get_file(i
);
867 if(file
->_flag
& mask
) {
875 TRACE(":flushed (%d) handles\n",num_flushed
);
879 /*********************************************************************
880 * _flushall (MSVCRT.@)
882 int CDECL
_flushall(void)
884 return msvcrt_flush_all_buffers(_IOWRT
| _IOREAD
);
887 /*********************************************************************
890 int CDECL
fflush(FILE* file
)
893 msvcrt_flush_all_buffers(_IOWRT
);
894 } else if(file
->_flag
& _IOWRT
) {
898 res
= msvcrt_flush_buffer(file
);
900 if(!res && (file->_flag & _IOCOMMIT))
901 res = _commit(file->_file) ? EOF : 0;
906 } else if(file
->_flag
& _IOREAD
) {
909 file
->_ptr
= file
->_base
;
917 /*********************************************************************
920 int CDECL
_close(int fd
)
922 ioinfo
*info
= get_ioinfo(fd
);
926 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
927 if (!(info
->wxflag
& WX_OPEN
)) {
930 ret
= CloseHandle(info
->handle
) ? 0 : -1;
933 WARN(":failed-last error (%d)\n",GetLastError());
934 _dosmaperr(GetLastError());
939 release_ioinfo(info
);
943 /*********************************************************************
946 * MSDN isn't clear on this point, but the remarks for _pipe
947 * indicate file descriptors duplicated with _dup and _dup2 are always
950 int CDECL
_dup2(int od
, int nd
)
954 TRACE("(od=%d, nd=%d)\n", od
, nd
);
956 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && is_valid_fd(od
))
960 if (DuplicateHandle(GetCurrentProcess(), get_ioinfo_nolock(od
)->handle
,
961 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
963 int wxflag
= get_ioinfo_nolock(od
)->wxflag
& ~_O_NOINHERIT
;
967 ret
= msvcrt_set_fd(handle
, wxflag
, nd
);
975 /* _dup2 returns 0, not nd, on success */
982 _dosmaperr(GetLastError());
994 /*********************************************************************
997 int CDECL
_dup(int od
)
1003 if (_dup2(od
, fd
) == 0)
1011 /*********************************************************************
1014 int CDECL
_eof(int fd
)
1016 ioinfo
*info
= get_ioinfo(fd
);
1017 DWORD curpos
,endpos
;
1018 LONG hcurpos
,hendpos
;
1020 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1022 if (info
->handle
== INVALID_HANDLE_VALUE
)
1024 release_ioinfo(info
);
1028 if (info
->wxflag
& WX_ATEOF
)
1030 release_ioinfo(info
);
1034 /* Otherwise we do it the hard way */
1035 hcurpos
= hendpos
= 0;
1036 curpos
= SetFilePointer(info
->handle
, 0, &hcurpos
, FILE_CURRENT
);
1037 endpos
= SetFilePointer(info
->handle
, 0, &hendpos
, FILE_END
);
1039 if (curpos
== endpos
&& hcurpos
== hendpos
)
1041 /* FIXME: shouldn't WX_ATEOF be set here? */
1042 release_ioinfo(info
);
1046 SetFilePointer(info
->handle
, curpos
, &hcurpos
, FILE_BEGIN
);
1047 release_ioinfo(info
);
1051 /*********************************************************************
1052 * _fcloseall (MSVCRT.@)
1054 int CDECL
_fcloseall(void)
1056 int num_closed
= 0, i
;
1060 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
1061 file
= msvcrt_get_file(i
);
1063 if (file
->_flag
&& !fclose(file
))
1068 TRACE(":closed (%d) handles\n",num_closed
);
1072 /* free everything on process exit */
1073 void msvcrt_free_io(void)
1081 for(i
=0; i
<sizeof(__pioinfo
)/sizeof(__pioinfo
[0]); i
++)
1086 for(j
=0; j
<MSVCRT_FD_BLOCK_SIZE
; j
++)
1088 if(__pioinfo
[i
][j
].exflag
& EF_CRIT_INIT
)
1089 DeleteCriticalSection(&__pioinfo
[i
][j
].crit
);
1094 for(j
=0; j
<MSVCRT_stream_idx
; j
++)
1096 FILE *file
= msvcrt_get_file(j
);
1097 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
1099 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1100 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1104 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1105 free(MSVCRT_fstream
[i
]);
1108 /*********************************************************************
1109 * _lseeki64 (MSVCRT.@)
1111 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
1113 ioinfo
*info
= get_ioinfo(fd
);
1116 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1118 if (info
->handle
== INVALID_HANDLE_VALUE
)
1120 release_ioinfo(info
);
1124 if (whence
< 0 || whence
> 2)
1126 release_ioinfo(info
);
1131 TRACE(":fd (%d) to %s pos %s\n",
1132 fd
,wine_dbgstr_longlong(offset
),
1133 (whence
==SEEK_SET
)?"SEEK_SET":
1134 (whence
==SEEK_CUR
)?"SEEK_CUR":
1135 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1137 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1138 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1139 ofs
.QuadPart
= offset
;
1140 if ((ofs
.u
.LowPart
= SetFilePointer(info
->handle
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1141 GetLastError() == ERROR_SUCCESS
)
1143 info
->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1144 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1146 release_ioinfo(info
);
1147 return ofs
.QuadPart
;
1149 release_ioinfo(info
);
1150 TRACE(":error-last error (%d)\n",GetLastError());
1151 _dosmaperr(GetLastError());
1155 /*********************************************************************
1158 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
1160 return (LONG
)_lseeki64(fd
, offset
, whence
);
1163 /*********************************************************************
1164 * _lock_file (MSVCRT.@)
1166 void CDECL
_lock_file(FILE *file
)
1168 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1169 _lock(_STREAM_LOCKS
+(file
-_iob
));
1170 /* ReactOS: string streams dont need to be locked */
1171 else if(!(file
->_flag
& _IOSTRG
))
1172 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1175 /*********************************************************************
1176 * _unlock_file (MSVCRT.@)
1178 void CDECL
_unlock_file(FILE *file
)
1180 if(file
>=_iob
&& file
<_iob
+_IOB_ENTRIES
)
1181 _unlock(_STREAM_LOCKS
+(file
-_iob
));
1182 /* ReactOS: string streams dont need to be locked */
1183 else if(!(file
->_flag
& _IOSTRG
))
1184 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1188 /*********************************************************************
1189 * _locking (MSVCRT.@)
1191 * This is untested; the underlying LockFile doesn't work yet.
1193 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
1195 ioinfo
*info
= get_ioinfo(fd
);
1199 TRACE(":fd (%d) handle (%p)\n", fd
, info
->handle
);
1200 if (info
->handle
== INVALID_HANDLE_VALUE
)
1202 release_ioinfo(info
);
1206 if (mode
< 0 || mode
> 4)
1208 release_ioinfo(info
);
1213 TRACE(":fd (%d) by 0x%08x mode %s\n",
1214 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
1215 (mode
==_LK_LOCK
)?"_LK_LOCK":
1216 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
1217 (mode
==_LK_RLCK
)?"_LK_RLCK":
1218 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
1221 if ((cur_locn
= SetFilePointer(info
->handle
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1223 release_ioinfo(info
);
1224 FIXME ("Seek failed\n");
1225 *_errno() = EINVAL
; /* FIXME */
1228 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
1231 ret
= 1; /* just to satisfy gcc */
1234 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1239 else if (mode
== _LK_UNLCK
)
1240 ret
= UnlockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1242 ret
= LockFile(info
->handle
, cur_locn
, 0L, nbytes
, 0L);
1243 /* FIXME - what about error settings? */
1244 release_ioinfo(info
);
1245 return ret
? 0 : -1;
1248 /*********************************************************************
1249 * _fseeki64 (MSVCRT.@)
1251 int CDECL
_fseeki64(FILE* file
, __int64 offset
, int whence
)
1256 /* Flush output if needed */
1257 if(file
->_flag
& _IOWRT
)
1258 msvcrt_flush_buffer(file
);
1260 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
1262 offset
+= _ftelli64(file
);
1265 /* Discard buffered input */
1267 file
->_ptr
= file
->_base
;
1268 /* Reset direction of i/o */
1269 if(file
->_flag
& _IORW
) {
1270 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
1272 /* Clear end of file flag */
1273 file
->_flag
&= ~_IOEOF
;
1274 ret
= (_lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1280 /*********************************************************************
1283 int CDECL
fseek(FILE* file
, long offset
, int whence
)
1285 return _fseeki64( file
, offset
, whence
);
1288 /*********************************************************************
1289 * _chsize_s (MSVCRT.@)
1291 int CDECL
_chsize_s(int fd
, __int64 size
)
1297 TRACE("(fd=%d, size=%s)\n", fd
, wine_dbgstr_longlong(size
));
1299 if (!MSVCRT_CHECK_PMT(size
>= 0)) return EINVAL
;
1301 info
= get_ioinfo(fd
);
1302 if (info
->handle
!= INVALID_HANDLE_VALUE
)
1304 /* save the current file pointer */
1305 cur
= _lseeki64(fd
, 0, SEEK_CUR
);
1308 pos
= _lseeki64(fd
, size
, SEEK_SET
);
1311 ret
= SetEndOfFile(info
->handle
);
1312 if (!ret
) _dosmaperr(GetLastError());
1315 /* restore the file pointer */
1316 _lseeki64(fd
, cur
, SEEK_SET
);
1320 release_ioinfo(info
);
1321 return ret
? 0 : *_errno();
1324 /*********************************************************************
1325 * _chsize (MSVCRT.@)
1327 int CDECL
_chsize(int fd
, long size
)
1329 /* _chsize_s returns errno on failure but _chsize should return -1 */
1330 return _chsize_s( fd
, size
) == 0 ? 0 : -1;
1333 /*********************************************************************
1334 * clearerr (MSVCRT.@)
1336 void CDECL
clearerr(FILE* file
)
1338 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1341 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1345 /*********************************************************************
1348 void CDECL
rewind(FILE* file
)
1350 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1353 fseek(file
, 0L, SEEK_SET
);
1358 static int msvcrt_get_flags(const wchar_t* mode
, int *open_flags
, int* stream_flags
)
1360 int plus
= strchrW(mode
, '+') != NULL
;
1362 TRACE("%s\n", debugstr_w(mode
));
1364 while(*mode
== ' ') mode
++;
1369 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1370 *stream_flags
= plus
? _IORW
: _IOREAD
;
1373 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1374 *stream_flags
= plus
? _IORW
: _IOWRT
;
1377 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1378 *stream_flags
= plus
? _IORW
: _IOWRT
;
1381 MSVCRT_INVALID_PMT(0, EINVAL
);
1385 *stream_flags
|= _commode
;
1387 while (*mode
&& *mode
!=',')
1391 *open_flags
|= _O_BINARY
;
1392 *open_flags
&= ~_O_TEXT
;
1395 *open_flags
|= _O_TEXT
;
1396 *open_flags
&= ~_O_BINARY
;
1399 *open_flags
|= _O_TEMPORARY
;
1402 *open_flags
|= _O_SHORT_LIVED
;
1405 *stream_flags
|= _IOCOMMIT
;
1408 *stream_flags
&= ~_IOCOMMIT
;
1411 *open_flags
|= _O_NOINHERIT
;
1420 FIXME("ignoring cache optimization flag: %c\n", mode
[-1]);
1423 ERR("incorrect mode flag: %c\n", mode
[-1]);
1429 static const WCHAR ccs
[] = {'c','c','s'};
1430 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1431 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1432 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1435 while(*mode
== ' ') mode
++;
1436 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1438 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1439 while(*mode
== ' ') mode
++;
1440 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1443 while(*mode
== ' ') mode
++;
1445 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1447 *open_flags
|= _O_U8TEXT
;
1448 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1450 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1452 *open_flags
|= _O_U16TEXT
;
1453 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1455 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1457 *open_flags
|= _O_WTEXT
;
1458 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1462 MSVCRT_INVALID_PMT(0, EINVAL
);
1466 while(*mode
== ' ') mode
++;
1469 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1474 /*********************************************************************
1475 * _fdopen (MSVCRT.@)
1477 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1480 wchar_t *modeW
= NULL
;
1482 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1484 ret
= _wfdopen(fd
, modeW
);
1490 /*********************************************************************
1491 * _wfdopen (MSVCRT.@)
1493 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1495 int open_flags
, stream_flags
;
1498 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1501 if (!(file
= msvcrt_alloc_fp()))
1503 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1508 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1514 /*********************************************************************
1515 * _filelength (MSVCRT.@)
1517 LONG CDECL
_filelength(int fd
)
1519 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1522 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1525 if (endPos
!= curPos
)
1526 _lseek(fd
, curPos
, SEEK_SET
);
1533 /*********************************************************************
1534 * _filelengthi64 (MSVCRT.@)
1536 __int64 CDECL
_filelengthi64(int fd
)
1538 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1541 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1544 if (endPos
!= curPos
)
1545 _lseeki64(fd
, curPos
, SEEK_SET
);
1552 /*********************************************************************
1553 * _fileno (MSVCRT.@)
1555 int CDECL
_fileno(FILE* file
)
1557 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1561 /*********************************************************************
1562 * _get_osfhandle (MSVCRT.@)
1564 intptr_t CDECL
_get_osfhandle(int fd
)
1566 HANDLE hand
= fdtoh(fd
);
1567 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1569 return (intptr_t)hand
;
1572 /*********************************************************************
1573 * _mktemp (MSVCRT.@)
1575 char * CDECL
_mktemp(char *pattern
)
1578 char *retVal
= pattern
;
1586 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1590 id
= GetCurrentProcessId();
1594 int tempNum
= id
/ 10;
1595 *pattern
-- = id
- (tempNum
* 10) + '0';
1601 *pattern
= letter
++;
1602 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
)
1604 } while(letter
<= 'z');
1608 /*********************************************************************
1609 * _wmktemp (MSVCRT.@)
1611 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1614 wchar_t *retVal
= pattern
;
1616 wchar_t letter
= 'a';
1619 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1623 id
= GetCurrentProcessId();
1627 int tempNum
= id
/ 10;
1628 *pattern
-- = id
- (tempNum
* 10) + '0';
1634 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1635 GetLastError() == ERROR_FILE_NOT_FOUND
)
1637 *pattern
= letter
++;
1638 } while(letter
!= '|');
1642 /*static*/ unsigned split_oflags(unsigned oflags
)
1645 unsigned unsupp
; /* until we support everything */
1647 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1648 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1649 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1650 else if (oflags
& _O_WTEXT
) wxflags
|= WX_TEXT
;
1651 else if (oflags
& _O_U16TEXT
) wxflags
|= WX_TEXT
;
1652 else if (oflags
& _O_U8TEXT
) wxflags
|= WX_TEXT
;
1653 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1654 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1655 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1657 if ((unsupp
= oflags
& ~(
1658 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1659 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1660 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1662 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
|
1663 _O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
1665 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1670 /*********************************************************************
1673 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1676 SECURITY_ATTRIBUTES sa
;
1677 HANDLE readHandle
, writeHandle
;
1685 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1686 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1687 sa
.lpSecurityDescriptor
= NULL
;
1688 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1690 unsigned int wxflags
= split_oflags(textmode
);
1694 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1698 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1707 CloseHandle(writeHandle
);
1713 CloseHandle(readHandle
);
1714 CloseHandle(writeHandle
);
1720 _dosmaperr(GetLastError());
1725 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1727 char bom
[sizeof(utf8_bom
)];
1730 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1732 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1735 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1736 oflags
|= _O_U8TEXT
;
1737 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1739 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1740 oflags
|= _O_U16TEXT
;
1742 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1748 /*********************************************************************
1749 * _wsopen_s (MSVCRT.@)
1751 int CDECL
_wsopen_s( int *fd
, const wchar_t* path
, int oflags
, int shflags
, int pmode
)
1753 DWORD access
= 0, creation
= 0, attrib
;
1754 SECURITY_ATTRIBUTES sa
;
1755 DWORD sharing
, type
;
1759 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1760 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1762 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return EINVAL
;
1765 wxflag
= split_oflags(oflags
);
1766 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1768 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1769 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1770 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1773 if (oflags
& _O_CREAT
)
1775 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1776 FIXME(": pmode 0x%04x ignored\n", pmode
);
1778 WARN(": pmode 0x%04x ignored\n", pmode
);
1780 if (oflags
& _O_EXCL
)
1781 creation
= CREATE_NEW
;
1782 else if (oflags
& _O_TRUNC
)
1783 creation
= CREATE_ALWAYS
;
1785 creation
= OPEN_ALWAYS
;
1787 else /* no _O_CREAT */
1789 if (oflags
& _O_TRUNC
)
1790 creation
= TRUNCATE_EXISTING
;
1792 creation
= OPEN_EXISTING
;
1801 sharing
= FILE_SHARE_READ
;
1804 sharing
= FILE_SHARE_WRITE
;
1807 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1810 ERR( "Unhandled shflags 0x%x\n", shflags
);
1813 attrib
= FILE_ATTRIBUTE_NORMAL
;
1815 if (oflags
& _O_TEMPORARY
)
1817 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1819 sharing
|= FILE_SHARE_DELETE
;
1822 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1823 sa
.lpSecurityDescriptor
= NULL
;
1824 sa
.bInheritHandle
= !(oflags
& _O_NOINHERIT
);
1826 if ((oflags
&(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1827 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1828 && !(access
&GENERIC_READ
))
1830 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1831 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1832 if (hand
!= INVALID_HANDLE_VALUE
)
1834 oflags
= check_bom(hand
, oflags
, FALSE
);
1838 oflags
&= ~(_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
);
1841 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1842 if (hand
== INVALID_HANDLE_VALUE
) {
1843 WARN(":failed-last error (%d)\n",GetLastError());
1844 _dosmaperr(GetLastError());
1848 if (oflags
& (_O_WTEXT
|_O_U16TEXT
|_O_U8TEXT
))
1850 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1851 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1852 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1854 if (oflags
& _O_U8TEXT
)
1856 DWORD written
= 0, tmp
;
1858 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1859 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1861 if (written
!= sizeof(utf8_bom
)) {
1862 WARN("error writing BOM\n");
1864 _dosmaperr(GetLastError());
1870 DWORD written
= 0, tmp
;
1872 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1873 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1875 if (written
!= sizeof(utf16_bom
))
1877 WARN("error writing BOM\n");
1879 _dosmaperr(GetLastError());
1884 else if (access
& GENERIC_READ
)
1885 oflags
= check_bom(hand
, oflags
, TRUE
);
1888 type
= GetFileType(hand
);
1889 if (type
== FILE_TYPE_CHAR
)
1891 else if (type
== FILE_TYPE_PIPE
)
1894 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1898 if (oflags
& _O_WTEXT
)
1899 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1900 else if (oflags
& _O_U16TEXT
)
1901 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF16
;
1902 else if (oflags
& _O_U8TEXT
)
1903 get_ioinfo_nolock(*fd
)->exflag
|= EF_UTF8
;
1905 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1909 /*********************************************************************
1910 * _wsopen (MSVCRT.@)
1912 int CDECL
_wsopen( const wchar_t *path
, int oflags
, int shflags
, ... )
1917 if (oflags
& _O_CREAT
)
1921 __ms_va_start(ap
, shflags
);
1922 pmode
= va_arg(ap
, int);
1928 _wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1932 /*********************************************************************
1933 * _sopen_s (MSVCRT.@)
1935 int CDECL
_sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1940 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1943 ret
= _wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1948 /*********************************************************************
1951 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1956 if (oflags
& _O_CREAT
)
1960 va_start(ap
, shflags
);
1961 pmode
= va_arg(ap
, int);
1967 _sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1971 /*********************************************************************
1974 int CDECL
_open( const char *path
, int flags
, ... )
1978 if (flags
& _O_CREAT
)
1981 va_start(ap
, flags
);
1982 pmode
= va_arg(ap
, int);
1984 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1987 return _sopen( path
, flags
, _SH_DENYNO
);
1990 /*********************************************************************
1993 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1997 if (flags
& _O_CREAT
)
2000 va_start(ap
, flags
);
2001 pmode
= va_arg(ap
, int);
2003 return _wsopen( path
, flags
, _SH_DENYNO
, pmode
);
2006 return _wsopen( path
, flags
, _SH_DENYNO
);
2009 /*********************************************************************
2012 int CDECL
_creat(const char *path
, int flags
)
2014 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2015 return _open(path
, usedFlags
);
2018 /*********************************************************************
2019 * _wcreat (MSVCRT.@)
2021 int CDECL
_wcreat(const wchar_t *path
, int flags
)
2023 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
2024 return _wopen(path
, usedFlags
);
2027 /*********************************************************************
2028 * _open_osfhandle (MSVCRT.@)
2030 int CDECL
_open_osfhandle(intptr_t handle
, int oflags
)
2035 /* _O_RDONLY (0) always matches, so set the read flag
2036 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2037 * file, so set the write flag. It also only sets _O_TEXT if it wants
2038 * text - it never sets _O_BINARY.
2040 /* don't let split_oflags() decide the mode if no mode is passed */
2041 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
2042 oflags
|= _O_BINARY
;
2044 flags
= GetFileType((HANDLE
)handle
);
2045 if (flags
==FILE_TYPE_UNKNOWN
&& GetLastError()!=NO_ERROR
)
2047 _dosmaperr(GetLastError());
2051 if (flags
== FILE_TYPE_CHAR
)
2053 else if (flags
== FILE_TYPE_PIPE
)
2057 flags
|= split_oflags(oflags
);
2059 fd
= msvcrt_alloc_fd((HANDLE
)handle
, flags
);
2060 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, flags
);
2064 /*********************************************************************
2067 int CDECL
_rmtmp(void)
2069 int num_removed
= 0, i
;
2073 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2074 file
= msvcrt_get_file(i
);
2076 if (file
->_tmpfname
)
2085 TRACE(":removed (%d) temp files\n",num_removed
);
2089 static inline int get_utf8_char_len(char ch
)
2091 if((ch
&0xf8) == 0xf0)
2093 else if((ch
&0xf0) == 0xe0)
2095 else if((ch
&0xe0) == 0xc0)
2100 /*********************************************************************
2101 * (internal) read_utf8
2103 static int read_utf8(int fd
, wchar_t *buf
, unsigned int count
)
2105 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2106 HANDLE hand
= fdinfo
->handle
;
2107 char min_buf
[4], *readbuf
, lookahead
;
2108 DWORD readbuf_size
, pos
=0, num_read
=1, char_len
, i
, j
;
2110 /* make the buffer big enough to hold at least one character */
2111 /* read bytes have to fit to output and lookahead buffers */
2113 readbuf_size
= count
< 4 ? 4 : count
;
2114 if(readbuf_size
<=4 || !(readbuf
= malloc(readbuf_size
))) {
2119 if(fdinfo
->lookahead
[0] != '\n') {
2120 readbuf
[pos
++] = fdinfo
->lookahead
[0];
2121 fdinfo
->lookahead
[0] = '\n';
2123 if(fdinfo
->lookahead
[1] != '\n') {
2124 readbuf
[pos
++] = fdinfo
->lookahead
[1];
2125 fdinfo
->lookahead
[1] = '\n';
2127 if(fdinfo
->lookahead
[2] != '\n') {
2128 readbuf
[pos
++] = fdinfo
->lookahead
[2];
2129 fdinfo
->lookahead
[2] = '\n';
2134 /* NOTE: this case is broken in native dll, reading
2135 * sometimes fails when small buffer is passed
2138 if(!pos
&& !ReadFile(hand
, readbuf
, 1, &num_read
, NULL
)) {
2139 if (GetLastError() == ERROR_BROKEN_PIPE
) {
2140 fdinfo
->wxflag
|= WX_ATEOF
;
2143 _dosmaperr(GetLastError());
2146 }else if(!num_read
) {
2147 fdinfo
->wxflag
|= WX_ATEOF
;
2153 char_len
= get_utf8_char_len(readbuf
[0]);
2155 if(ReadFile(hand
, readbuf
+pos
, char_len
-pos
, &num_read
, NULL
))
2159 if(readbuf
[0] == '\n')
2160 fdinfo
->wxflag
|= WX_READNL
;
2162 fdinfo
->wxflag
&= ~WX_READNL
;
2164 if(readbuf
[0] == 0x1a) {
2165 fdinfo
->wxflag
|= WX_ATEOF
;
2169 if(readbuf
[0] == '\r') {
2170 if(!ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || num_read
!=1)
2172 else if(lookahead
== '\n')
2176 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2177 fdinfo
->lookahead
[0] = lookahead
;
2179 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2184 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2185 _dosmaperr(GetLastError());
2192 if(!ReadFile(hand
, readbuf
+pos
, readbuf_size
-pos
, &num_read
, NULL
)) {
2195 }else if(GetLastError() == ERROR_BROKEN_PIPE
) {
2196 fdinfo
->wxflag
|= WX_ATEOF
;
2197 if (readbuf
!= min_buf
) free(readbuf
);
2200 _dosmaperr(GetLastError());
2201 if (readbuf
!= min_buf
) free(readbuf
);
2204 }else if(!pos
&& !num_read
) {
2205 fdinfo
->wxflag
|= WX_ATEOF
;
2206 if (readbuf
!= min_buf
) free(readbuf
);
2211 if(readbuf
[0] == '\n')
2212 fdinfo
->wxflag
|= WX_READNL
;
2214 fdinfo
->wxflag
&= ~WX_READNL
;
2216 /* Find first byte of last character (may be incomplete) */
2217 for(i
=pos
-1; i
>0 && i
>pos
-4; i
--)
2218 if((readbuf
[i
]&0xc0) != 0x80)
2220 char_len
= get_utf8_char_len(readbuf
[i
]);
2221 if(char_len
+i
<= pos
)
2224 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
)) {
2226 fdinfo
->lookahead
[0] = readbuf
[i
];
2228 fdinfo
->lookahead
[1] = readbuf
[i
+1];
2230 fdinfo
->lookahead
[2] = readbuf
[i
+2];
2232 SetFilePointer(fdinfo
->handle
, i
-pos
, NULL
, FILE_CURRENT
);
2236 for(i
=0, j
=0; i
<pos
; i
++) {
2237 if(readbuf
[i
] == 0x1a) {
2238 fdinfo
->wxflag
|= WX_ATEOF
;
2242 /* strip '\r' if followed by '\n' */
2243 if(readbuf
[i
] == '\r' && i
+1==pos
) {
2244 if(fdinfo
->lookahead
[0] != '\n' || !ReadFile(hand
, &lookahead
, 1, &num_read
, NULL
) || !num_read
) {
2245 readbuf
[j
++] = '\r';
2246 }else if(lookahead
== '\n' && j
==0) {
2247 readbuf
[j
++] = '\n';
2249 if(lookahead
!= '\n')
2250 readbuf
[j
++] = '\r';
2252 if(fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2253 fdinfo
->lookahead
[0] = lookahead
;
2255 SetFilePointer(fdinfo
->handle
, -1, NULL
, FILE_CURRENT
);
2257 }else if(readbuf
[i
]!='\r' || readbuf
[i
+1]!='\n') {
2258 readbuf
[j
++] = readbuf
[i
];
2263 if(!(num_read
= MultiByteToWideChar(CP_UTF8
, 0, readbuf
, pos
, buf
, count
))) {
2264 _dosmaperr(GetLastError());
2265 if (readbuf
!= min_buf
) free(readbuf
);
2269 if (readbuf
!= min_buf
) free(readbuf
);
2273 /*********************************************************************
2276 * When reading \r as last character in text mode, read() positions
2277 * the file pointer on the \r character while getc() goes on to
2280 static int read_i(int fd
, void *buf
, unsigned int count
)
2282 DWORD num_read
, utf16
;
2283 char *bufstart
= buf
;
2284 HANDLE hand
= fdtoh(fd
);
2285 ioinfo
*fdinfo
= get_ioinfo_nolock(fd
);
2290 if (fdinfo
->wxflag
& WX_ATEOF
) {
2291 TRACE("already at EOF, returning 0\n");
2294 /* Don't trace small reads, it gets *very* annoying */
2296 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2297 if (hand
== INVALID_HANDLE_VALUE
)
2303 utf16
= (fdinfo
->exflag
& EF_UTF16
) != 0;
2304 if (((fdinfo
->exflag
&EF_UTF8
) || utf16
) && count
&1)
2310 if((fdinfo
->wxflag
&WX_TEXT
) && (fdinfo
->exflag
&EF_UTF8
))
2311 return read_utf8(fd
, buf
, count
);
2313 if (fdinfo
->lookahead
[0]!='\n' || ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2315 if (fdinfo
->lookahead
[0] != '\n')
2317 bufstart
[0] = fdinfo
->lookahead
[0];
2318 fdinfo
->lookahead
[0] = '\n';
2322 bufstart
[1] = fdinfo
->lookahead
[1];
2323 fdinfo
->lookahead
[1] = '\n';
2326 if(count
>1+utf16
&& ReadFile(hand
, bufstart
+1+utf16
, count
-1-utf16
, &num_read
, NULL
))
2327 num_read
+= 1+utf16
;
2332 if(utf16
&& (num_read
&1))
2334 /* msvcr90 uses uninitialized value from the buffer in this case */
2335 /* msvcrt ignores additional data */
2336 ERR("got odd number of bytes in UTF16 mode\n");
2340 if (count
!= 0 && num_read
== 0)
2342 fdinfo
->wxflag
|= WX_ATEOF
;
2343 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2345 else if (fdinfo
->wxflag
& WX_TEXT
)
2349 if (bufstart
[0]=='\n' && (!utf16
|| bufstart
[1]==0))
2350 fdinfo
->wxflag
|= WX_READNL
;
2352 fdinfo
->wxflag
&= ~WX_READNL
;
2354 for (i
=0, j
=0; i
<num_read
; i
+=1+utf16
)
2356 /* in text mode, a ctrl-z signals EOF */
2357 if (bufstart
[i
]==0x1a && (!utf16
|| bufstart
[i
+1]==0))
2359 fdinfo
->wxflag
|= WX_ATEOF
;
2360 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2364 /* in text mode, strip \r if followed by \n */
2365 if (bufstart
[i
]=='\r' && (!utf16
|| bufstart
[i
+1]==0) && i
+1+utf16
==num_read
)
2370 lookahead
[1] = '\n';
2371 if (ReadFile(hand
, lookahead
, 1+utf16
, &len
, NULL
) && len
)
2373 if(lookahead
[0]=='\n' && (!utf16
|| lookahead
[1]==0) && j
==0)
2375 bufstart
[j
++] = '\n';
2376 if(utf16
) bufstart
[j
++] = 0;
2380 if(lookahead
[0]!='\n' || (utf16
&& lookahead
[1]!=0))
2382 bufstart
[j
++] = '\r';
2383 if(utf16
) bufstart
[j
++] = 0;
2386 if (fdinfo
->wxflag
& (WX_PIPE
| WX_TTY
))
2388 if (lookahead
[0]=='\n' && (!utf16
|| !lookahead
[1]))
2390 bufstart
[j
++] = '\n';
2391 if (utf16
) bufstart
[j
++] = 0;
2395 fdinfo
->lookahead
[0] = lookahead
[0];
2396 fdinfo
->lookahead
[1] = lookahead
[1];
2400 SetFilePointer(fdinfo
->handle
, -1-utf16
, NULL
, FILE_CURRENT
);
2405 bufstart
[j
++] = '\r';
2406 if(utf16
) bufstart
[j
++] = 0;
2409 else if((bufstart
[i
]!='\r' || (utf16
&& bufstart
[i
+1]!=0))
2410 || (bufstart
[i
+1+utf16
]!='\n' || (utf16
&& bufstart
[i
+3]!=0)))
2412 bufstart
[j
++] = bufstart
[i
];
2413 if(utf16
) bufstart
[j
++] = bufstart
[i
+1];
2421 if (GetLastError() == ERROR_BROKEN_PIPE
)
2423 TRACE(":end-of-pipe\n");
2424 fdinfo
->wxflag
|= WX_ATEOF
;
2429 TRACE(":failed-last error (%d)\n",GetLastError());
2435 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2439 /*********************************************************************
2442 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
2445 num_read
= read_i(fd
, buf
, count
);
2449 /*********************************************************************
2450 * _setmode (MSVCRT.@)
2452 int CDECL
_setmode(int fd
,int mode
)
2454 int ret
= get_ioinfo_nolock(fd
)->wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
2455 if(ret
==_O_TEXT
&& (get_ioinfo_nolock(fd
)->exflag
& (EF_UTF8
|EF_UTF16
)))
2458 if(mode
!=_O_TEXT
&& mode
!=_O_BINARY
&& mode
!=_O_WTEXT
2459 && mode
!=_O_U16TEXT
&& mode
!=_O_U8TEXT
) {
2464 if(mode
== _O_BINARY
) {
2465 get_ioinfo_nolock(fd
)->wxflag
&= ~WX_TEXT
;
2466 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2470 get_ioinfo_nolock(fd
)->wxflag
|= WX_TEXT
;
2472 get_ioinfo_nolock(fd
)->exflag
&= ~(EF_UTF8
|EF_UTF16
);
2473 else if(mode
== _O_U8TEXT
)
2474 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF16
) | EF_UTF8
;
2476 get_ioinfo_nolock(fd
)->exflag
= (get_ioinfo_nolock(fd
)->exflag
& ~EF_UTF8
) | EF_UTF16
;
2482 /*********************************************************************
2485 long CDECL
_tell(int fd
)
2487 return _lseek(fd
, 0, SEEK_CUR
);
2490 /*********************************************************************
2491 * _telli64 (MSVCRT.@)
2493 __int64 CDECL
_telli64(int fd
)
2495 return _lseeki64(fd
, 0, SEEK_CUR
);
2498 /*********************************************************************
2499 * _tempnam (MSVCRT.@)
2501 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2503 char tmpbuf
[MAX_PATH
];
2504 const char *tmp_dir
= getenv("TMP");
2506 if (tmp_dir
) dir
= tmp_dir
;
2508 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2509 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2511 TRACE("got name (%s)\n",tmpbuf
);
2512 DeleteFileA(tmpbuf
);
2513 return _strdup(tmpbuf
);
2515 TRACE("failed (%d)\n",GetLastError());
2519 /*********************************************************************
2520 * _wtempnam (MSVCRT.@)
2522 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2524 wchar_t tmpbuf
[MAX_PATH
];
2526 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2527 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2529 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2530 DeleteFileW(tmpbuf
);
2531 return _wcsdup(tmpbuf
);
2533 TRACE("failed (%d)\n",GetLastError());
2537 /*********************************************************************
2540 int CDECL
_umask(int umask
)
2542 int old_umask
= MSVCRT_umask
;
2543 TRACE("(%d)\n",umask
);
2544 MSVCRT_umask
= umask
;
2548 /*********************************************************************
2551 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2554 ioinfo
*info
= get_ioinfo_nolock(fd
);
2555 HANDLE hand
= info
->handle
;
2557 /* Don't trace small writes, it gets *very* annoying */
2560 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2562 if (hand
== INVALID_HANDLE_VALUE
)
2568 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2574 /* If appending, go to EOF */
2575 if (info
->wxflag
& WX_APPEND
)
2576 _lseek(fd
, 0, FILE_END
);
2578 if (!(info
->wxflag
& WX_TEXT
))
2580 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2581 && (num_written
== count
))
2583 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2584 hand
, GetLastError());
2589 unsigned int i
, j
, nr_lf
, size
;
2592 const char *s
= buf
, *buf_start
= buf
;
2594 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2596 /* find number of \n */
2597 for (nr_lf
=0, i
=0; i
<count
; i
++)
2603 if ((q
= p
= malloc(size
)))
2605 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2614 FIXME("Malloc failed\n");
2626 else if (info
->exflag
& EF_UTF16
)
2628 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2629 if (s
[i
]=='\n' && s
[i
+1]==0)
2634 if ((q
= p
= malloc(size
)))
2636 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2638 if (s
[i
]=='\n' && s
[i
+1]==0)
2649 FIXME("Malloc failed\n");
2665 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2666 if (s
[i
]=='\n' && s
[i
+1]==0)
2669 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2671 _dosmaperr(GetLastError());
2676 size
= conv_len
+nr_lf
;
2677 if((p
= malloc(count
+nr_lf
*2+size
)))
2679 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2681 if (s
[i
]=='\n' && s
[i
+1]==0)
2689 q
= p
+count
+nr_lf
*2;
2690 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2691 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2695 FIXME("Malloc failed\n");
2702 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2706 if (num_written
!= size
)
2708 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2709 fd
, hand
, GetLastError(), num_written
);
2711 return s
- buf_start
;
2719 /*********************************************************************
2722 int CDECL
_putw(int val
, FILE* file
)
2727 len
= _write(file
->_file
, &val
, sizeof(val
));
2728 if (len
== sizeof(val
)) {
2733 file
->_flag
|= _IOERR
;
2738 /*********************************************************************
2741 int CDECL
fclose(FILE* file
)
2747 free(file
->_tmpfname
);
2748 file
->_tmpfname
= NULL
;
2749 /* flush stdio buffers */
2750 if(file
->_flag
& _IOWRT
)
2752 if(file
->_flag
& _IOMYBUF
)
2755 r
=_close(file
->_file
);
2759 if(file
<_iob
|| file
>=_iob
+_IOB_ENTRIES
)
2760 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
2762 if(file
== msvcrt_get_file(MSVCRT_stream_idx
-1)) {
2763 while(MSVCRT_stream_idx
>3 && !file
->_flag
) {
2764 MSVCRT_stream_idx
--;
2765 file
= msvcrt_get_file(MSVCRT_stream_idx
-1);
2769 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2772 /*********************************************************************
2775 int CDECL
feof(FILE* file
)
2777 return file
->_flag
& _IOEOF
;
2780 /*********************************************************************
2783 int CDECL
ferror(FILE* file
)
2785 return file
->_flag
& _IOERR
;
2788 /*********************************************************************
2789 * _filbuf (MSVCRT.@)
2791 int CDECL
_filbuf(FILE* file
)
2796 if(file
->_flag
& _IOSTRG
) {
2801 /* Allocate buffer if needed */
2802 if(!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
2803 msvcrt_alloc_buffer(file
);
2805 if(!(file
->_flag
& _IOREAD
)) {
2806 if(file
->_flag
& _IORW
)
2807 file
->_flag
|= _IOREAD
;
2814 if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
2816 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2817 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2825 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2827 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2834 file
->_ptr
= file
->_base
+1;
2835 c
= *(unsigned char *)file
->_base
;
2841 /*********************************************************************
2844 int CDECL
fgetc(FILE* file
)
2852 i
= (unsigned char *)file
->_ptr
++;
2861 /*********************************************************************
2862 * _fgetchar (MSVCRT.@)
2864 int CDECL
_fgetchar(void)
2866 return fgetc(stdin
);
2869 /*********************************************************************
2872 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2875 char * buf_start
= s
;
2877 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2878 file
,file
->_file
,s
,size
);
2882 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2887 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2889 TRACE(":nothing read\n");
2893 if ((cc
!= EOF
) && (size
> 1))
2896 TRACE(":got %s\n", debugstr_a(buf_start
));
2901 /*********************************************************************
2904 wint_t CDECL
fgetwc(FILE* file
)
2911 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
2912 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
2915 for(p
=(char*)&ret
; (wint_t*)p
<&ret
+1; p
++) {
2924 char mbs
[MB_LEN_MAX
];
2930 if(isleadbyte((unsigned char)mbs
[0])) {
2941 if(!len
|| mbtowc(&ret
, mbs
, len
)==-1)
2949 /*********************************************************************
2952 int CDECL
_getw(FILE* file
)
2960 for (j
=0; j
<sizeof(int); j
++) {
2963 file
->_flag
|= _IOEOF
;
2974 /*********************************************************************
2977 wint_t CDECL
getwc(FILE* file
)
2979 return fgetwc(file
);
2982 /*********************************************************************
2983 * _fgetwchar (MSVCRT.@)
2985 wint_t CDECL
_fgetwchar(void)
2987 return fgetwc(stdin
);
2990 /*********************************************************************
2991 * getwchar (MSVCRT.@)
2993 wint_t CDECL
getwchar(void)
2995 return _fgetwchar();
2998 /*********************************************************************
3001 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
3004 wchar_t * buf_start
= s
;
3006 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3007 file
,file
->_file
,s
,size
);
3011 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
3016 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3018 TRACE(":nothing read\n");
3022 if ((cc
!= WEOF
) && (size
> 1))
3025 TRACE(":got %s\n", debugstr_w(buf_start
));
3030 /*********************************************************************
3033 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3035 size_t wrcnt
=size
* nmemb
;
3044 if(file
->_cnt
< 0) {
3045 WARN("negative file->_cnt value in %p\n", file
);
3046 file
->_flag
|= MSVCRT__IOERR
;
3051 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3052 memcpy(file
->_ptr
, ptr
, pcnt
);
3057 ptr
= (const char*)ptr
+ pcnt
;
3058 } else if((file
->_flag
& _IONBF
)
3059 || ((file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= file
->_bufsiz
)
3060 || (!(file
->_flag
& (_IOMYBUF
| _USERBUF
)) && wrcnt
>= MSVCRT_INTERNAL_BUFSIZ
)) {
3064 if(file
->_flag
& _IONBF
)
3066 else if(!(file
->_flag
& (_IOMYBUF
| _USERBUF
)))
3067 bufsiz
= MSVCRT_INTERNAL_BUFSIZ
;
3069 bufsiz
= file
->_bufsiz
;
3071 pcnt
= (wrcnt
/ bufsiz
) * bufsiz
;
3073 if(msvcrt_flush_buffer(file
) == EOF
)
3076 if(_write(file
->_file
, ptr
, pcnt
) <= 0) {
3077 file
->_flag
|= _IOERR
;
3082 ptr
= (const char*)ptr
+ pcnt
;
3084 if(_flsbuf(*(const char*)ptr
, file
) == EOF
)
3088 ptr
= (const char*)ptr
+ 1;
3093 return written
/ size
;
3096 /*********************************************************************
3098 * FORKED for ReactOS, don't sync with Wine!
3100 * - http://jira.reactos.org/browse/CORE-6495
3101 * - http://bugs.winehq.org/show_bug.cgi?id=8598
3103 wint_t CDECL
fputwc(wchar_t c
, FILE* stream
)
3105 /* If this is a real file stream (and not some temporary one for
3106 sprintf-like functions), check whether it is opened in text mode.
3107 In this case, we have to perform an implicit conversion to ANSI. */
3108 if (!(stream
->_flag
& _IOSTRG
) && get_ioinfo_nolock(stream
->_file
)->wxflag
& WX_TEXT
)
3110 /* Convert to multibyte in text mode */
3111 char mbc
[MB_LEN_MAX
];
3114 mb_return
= wctomb(mbc
, c
);
3119 /* Output all characters */
3120 if (fwrite(mbc
, mb_return
, 1, stream
) != 1)
3125 if (fwrite(&c
, sizeof(c
), 1, stream
) != 1)
3132 /*********************************************************************
3133 * _fputwchar (MSVCRT.@)
3135 wint_t CDECL
_fputwchar(wint_t wc
)
3137 return fputwc(wc
, stdout
);
3140 /*********************************************************************
3141 * _wfsopen (MSVCRT.@)
3143 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
3146 int open_flags
, stream_flags
, fd
;
3148 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3150 /* map mode string to open() flags. "man fopen" for possibilities. */
3151 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3155 fd
= _wsopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
3158 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3160 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3167 TRACE(":got (%p)\n",file
);
3168 if (fd
>= 0 && !file
)
3174 /*********************************************************************
3175 * _fsopen (MSVCRT.@)
3177 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
3180 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3182 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3183 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3187 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3190 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3195 ret
= _wfsopen(pathW
, modeW
, share
);
3202 /*********************************************************************
3205 FILE * CDECL
fopen(const char *path
, const char *mode
)
3207 return _fsopen( path
, mode
, _SH_DENYNO
);
3210 /*********************************************************************
3211 * fopen_s (MSVCRT.@)
3213 int CDECL
fopen_s(FILE** pFile
,
3214 const char *filename
, const char *mode
)
3216 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return EINVAL
;
3217 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return EINVAL
;
3218 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return EINVAL
;
3220 *pFile
= fopen(filename
, mode
);
3227 /*********************************************************************
3228 * _wfopen (MSVCRT.@)
3230 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
3232 return _wfsopen( path
, mode
, _SH_DENYNO
);
3235 /*********************************************************************
3236 * _wfopen_s (MSVCRT.@)
3238 int CDECL
_wfopen_s(FILE** pFile
, const wchar_t *filename
,
3239 const wchar_t *mode
)
3241 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
3242 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
3247 *pFile
= _wfopen(filename
, mode
);
3254 /* fputc calls _flsbuf which calls fputc */
3255 int CDECL
_flsbuf(int c
, FILE* file
);
3257 /*********************************************************************
3260 int CDECL
fputc(int c
, FILE* file
)
3270 res
= msvcrt_flush_buffer(file
);
3272 return res
? res
: c
;
3279 res
= _flsbuf(c
, file
);
3285 /*********************************************************************
3286 * _fputchar (MSVCRT.@)
3288 int CDECL
_fputchar(int c
)
3290 return fputc(c
, stdout
);
3293 /*********************************************************************
3296 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
3298 size_t rcnt
=size
* nmemb
;
3307 /* first buffered data */
3309 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3310 memcpy(ptr
, file
->_ptr
, pcnt
);
3315 ptr
= (char*)ptr
+ pcnt
;
3316 } else if(!(file
->_flag
& _IOREAD
)) {
3317 if(file
->_flag
& _IORW
) {
3318 file
->_flag
|= _IOREAD
;
3325 if(rcnt
>0 && !(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
)))
3326 msvcrt_alloc_buffer(file
);
3331 if (!file
->_cnt
&& rcnt
<BUFSIZ
&& (file
->_flag
& (_IOMYBUF
| _USERBUF
))) {
3332 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
3333 file
->_ptr
= file
->_base
;
3334 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3335 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3336 if (i
> 0 && i
< file
->_cnt
) {
3337 get_ioinfo_nolock(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3338 file
->_flag
&= ~_IOEOF
;
3341 memcpy(ptr
, file
->_ptr
, i
);
3345 } else if (rcnt
> INT_MAX
) {
3346 i
= _read(file
->_file
, ptr
, INT_MAX
);
3347 } else if (rcnt
< BUFSIZ
) {
3348 i
= _read(file
->_file
, ptr
, rcnt
);
3350 i
= _read(file
->_file
, ptr
, rcnt
- BUFSIZ
/2);
3354 ptr
= (char *)ptr
+i
;
3355 /* expose feof condition in the flags
3356 * MFC tests file->_flag for feof, and doesn't call feof())
3358 if (get_ioinfo_nolock(file
->_file
)->wxflag
& WX_ATEOF
)
3359 file
->_flag
|= _IOEOF
;
3362 file
->_flag
|= _IOERR
;
3373 /*********************************************************************
3374 * _wfreopen (MSVCRT.@)
3377 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
, FILE* file
)
3379 int open_flags
, stream_flags
, fd
;
3381 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
? file
->_file
: -1);
3384 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
3389 /* map mode string to open() flags. "man fopen" for possibilities. */
3390 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3394 fd
= _wopen(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
3397 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
3400 WARN(":failed-last error (%d)\n",GetLastError());
3401 _dosmaperr(GetLastError());
3410 /*********************************************************************
3411 * freopen (MSVCRT.@)
3414 FILE* CDECL
freopen(const char *path
, const char *mode
, FILE* file
)
3417 wchar_t *pathW
= NULL
, *modeW
= NULL
;
3419 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3420 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3426 ret
= _wfreopen(pathW
, modeW
, file
);
3433 /*********************************************************************
3434 * fsetpos (MSVCRT.@)
3436 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
3441 /* Note that all this has been lifted 'as is' from fseek */
3442 if(file
->_flag
& _IOWRT
)
3443 msvcrt_flush_buffer(file
);
3445 /* Discard buffered input */
3447 file
->_ptr
= file
->_base
;
3449 /* Reset direction of i/o */
3450 if(file
->_flag
& _IORW
) {
3451 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
3454 ret
= (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3459 /*********************************************************************
3460 * _ftelli64 (MSVCRT.@)
3462 __int64 CDECL
_ftelli64(FILE* file
)
3467 pos
= _telli64(file
->_file
);
3472 if(file
->_flag
& (_IOMYBUF
| _USERBUF
)) {
3473 if(file
->_flag
& _IOWRT
) {
3474 pos
+= file
->_ptr
- file
->_base
;
3476 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
3479 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3483 } else if(!file
->_cnt
) { /* nothing to do */
3484 } else if(_lseeki64(file
->_file
, 0, SEEK_END
)==pos
) {
3488 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
3489 for(i
=0; i
<file
->_cnt
; i
++)
3490 if(file
->_ptr
[i
] == '\n')
3496 if(_lseeki64(file
->_file
, pos
, SEEK_SET
) != pos
) {
3501 pos
-= file
->_bufsiz
;
3502 pos
+= file
->_ptr
- file
->_base
;
3504 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
) {
3505 if(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_READNL
)
3508 for(p
=file
->_base
; p
<file
->_ptr
; p
++)
3519 /*********************************************************************
3522 LONG CDECL
ftell(FILE* file
)
3524 return (LONG
)_ftelli64(file
);
3527 /*********************************************************************
3528 * fgetpos (MSVCRT.@)
3530 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
3532 *pos
= _ftelli64(file
);
3538 /*********************************************************************
3541 int CDECL
fputs(const char *s
, FILE* file
)
3543 size_t len
= strlen(s
);
3547 ret
= fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : EOF
;
3552 /*********************************************************************
3555 int CDECL
fputws(const wchar_t *s
, FILE* file
)
3557 size_t i
, len
= strlenW(s
);
3562 if (!(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3563 ret
= fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
3568 tmp_buf
= add_std_buffer(file
);
3569 for (i
=0; i
<len
; i
++) {
3570 if(fputwc(s
[i
], file
) == WEOF
) {
3571 if(tmp_buf
) remove_std_buffer(file
);
3577 if(tmp_buf
) remove_std_buffer(file
);
3582 /*********************************************************************
3583 * getchar (MSVCRT.@)
3585 int CDECL
getchar(void)
3587 return fgetc(stdin
);
3590 /*********************************************************************
3593 int CDECL
getc(FILE* file
)
3598 /*********************************************************************
3601 char * CDECL
gets(char *buf
)
3604 char * buf_start
= buf
;
3607 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
3609 if(cc
!= '\r') *buf
++ = (char)cc
;
3613 TRACE("got '%s'\n", buf_start
);
3614 _unlock_file(stdin
);
3618 /*********************************************************************
3621 wchar_t* CDECL
_getws(wchar_t* buf
)
3627 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
3631 *buf
++ = (wchar_t)cc
;
3635 TRACE("got %s\n", debugstr_w(ws
));
3636 _unlock_file(stdin
);
3640 /*********************************************************************
3643 int CDECL
putc(int c
, FILE* file
)
3645 return fputc(c
, file
);
3648 /*********************************************************************
3649 * putchar (MSVCRT.@)
3651 int CDECL
putchar(int c
)
3653 return fputc(c
, stdout
);
3656 /*********************************************************************
3657 * _putwch (MSVCRT.@)
3659 wint_t CDECL
_putwch(wchar_t c
)
3661 return fputwc(c
, stdout
);
3664 /*********************************************************************
3667 int CDECL
puts(const char *s
)
3669 size_t len
= strlen(s
);
3673 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3674 _unlock_file(stdout
);
3678 ret
= fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
3679 _unlock_file(stdout
);
3683 /*********************************************************************
3686 int CDECL
_putws(const wchar_t *s
)
3688 static const wchar_t nl
= '\n';
3689 size_t len
= strlenW(s
);
3693 if(fwrite(s
, sizeof(*s
), len
, stdout
) != len
) {
3694 _unlock_file(stdout
);
3698 ret
= fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
3699 _unlock_file(stdout
);
3703 /*********************************************************************
3706 int CDECL
remove(const char *path
)
3708 TRACE("(%s)\n",path
);
3709 if (DeleteFileA(path
))
3711 TRACE(":failed (%d)\n",GetLastError());
3712 _dosmaperr(GetLastError());
3716 /*********************************************************************
3717 * _wremove (MSVCRT.@)
3719 int CDECL
_wremove(const wchar_t *path
)
3721 TRACE("(%s)\n",debugstr_w(path
));
3722 if (DeleteFileW(path
))
3724 TRACE(":failed (%d)\n",GetLastError());
3725 _dosmaperr(GetLastError());
3729 /*********************************************************************
3732 int CDECL
rename(const char *oldpath
,const char *newpath
)
3734 TRACE(":from %s to %s\n",oldpath
,newpath
);
3735 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3737 TRACE(":failed (%d)\n",GetLastError());
3738 _dosmaperr(GetLastError());
3742 /*********************************************************************
3743 * _wrename (MSVCRT.@)
3745 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
3747 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3748 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3750 TRACE(":failed (%d)\n",GetLastError());
3751 _dosmaperr(GetLastError());
3755 /*********************************************************************
3756 * setvbuf (MSVCRT.@)
3758 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3760 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3761 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| mode
==_IOFBF
|| mode
==_IOLBF
)) return -1;
3762 if(!MSVCRT_CHECK_PMT(mode
==_IONBF
|| (size
>=2 && size
<=INT_MAX
))) return -1;
3767 if(file
->_flag
& _IOMYBUF
)
3769 file
->_flag
&= ~(_IONBF
| _IOMYBUF
| _USERBUF
);
3772 if(mode
== _IONBF
) {
3773 file
->_flag
|= _IONBF
;
3774 file
->_base
= file
->_ptr
= (char*)&file
->_charbuf
;
3777 file
->_base
= file
->_ptr
= buf
;
3778 file
->_flag
|= _USERBUF
;
3779 file
->_bufsiz
= size
;
3781 file
->_base
= file
->_ptr
= malloc(size
);
3788 file
->_flag
|= _IOMYBUF
;
3789 file
->_bufsiz
= size
;
3795 /*********************************************************************
3798 void CDECL
setbuf(FILE* file
, char *buf
)
3800 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3803 /*********************************************************************
3806 char * CDECL
tmpnam(char *s
)
3813 thread_data_t
*data
= msvcrt_get_thread_data();
3815 if(!data
->tmpnam_buffer
)
3816 data
->tmpnam_buffer
= malloc(MAX_PATH
);
3818 s
= data
->tmpnam_buffer
;
3821 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3822 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3823 for (count
= 0; count
< TMP_MAX
; count
++)
3825 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3826 memcpy(p
, tmpstr
, size
);
3828 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3829 GetLastError() == ERROR_FILE_NOT_FOUND
)
3835 /*********************************************************************
3836 * _wtmpnam (MSVCRT.@)
3838 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3840 static const wchar_t format
[] = {'\\','s','%','s','.',0};
3845 thread_data_t
*data
= msvcrt_get_thread_data();
3847 if(!data
->wtmpnam_buffer
)
3848 data
->wtmpnam_buffer
= malloc(sizeof(wchar_t[MAX_PATH
]));
3850 s
= data
->wtmpnam_buffer
;
3853 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3854 p
= s
+ _snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3855 for (count
= 0; count
< TMP_MAX
; count
++)
3857 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3858 memcpy(p
, tmpstr
, size
*sizeof(wchar_t));
3860 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3861 GetLastError() == ERROR_FILE_NOT_FOUND
)
3867 /*********************************************************************
3868 * tmpfile (MSVCRT.@)
3870 FILE* CDECL
tmpfile(void)
3872 char *filename
= tmpnam(NULL
);
3877 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
,
3878 _S_IREAD
| _S_IWRITE
);
3879 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3881 if (msvcrt_init_fp(file
, fd
, _IORW
) == -1)
3886 else file
->_tmpfname
= _strdup(filename
);
3889 if(fd
!= -1 && !file
)
3895 /*********************************************************************
3898 int CDECL
ungetc(int c
, FILE * file
)
3900 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return EOF
;
3902 if (c
== EOF
|| !(file
->_flag
&_IOREAD
||
3903 (file
->_flag
&_IORW
&& !(file
->_flag
&_IOWRT
))))
3907 if((!(file
->_flag
& (_IONBF
| _IOMYBUF
| _USERBUF
))
3908 && msvcrt_alloc_buffer(file
))
3909 || (!file
->_cnt
&& file
->_ptr
==file
->_base
))
3912 if(file
->_ptr
>file
->_base
) {
3914 if(file
->_flag
& _IOSTRG
) {
3915 if(*file
->_ptr
!= c
) {
3925 file
->_flag
|= _IOREAD
;
3934 /*********************************************************************
3935 * ungetwc (MSVCRT.@)
3937 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3946 if((get_ioinfo_nolock(file
->_file
)->exflag
& (EF_UTF8
| EF_UTF16
))
3947 || !(get_ioinfo_nolock(file
->_file
)->wxflag
& WX_TEXT
)) {
3948 unsigned char * pp
= (unsigned char *)&mwc
;
3951 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3952 if(pp
[i
] != ungetc(pp
[i
],file
)) {
3958 char mbs
[MB_LEN_MAX
];
3961 len
= wctomb(mbs
, mwc
);
3967 for(len
--; len
>=0; len
--) {
3968 if(mbs
[len
] != ungetc(mbs
[len
], file
)) {
3981 /*********************************************************************
3982 * _getmaxstdio (MSVCRT.@)
3984 int CDECL
_getmaxstdio(void)
3986 return MSVCRT_max_streams
;
3989 /*********************************************************************
3990 * _setmaxstdio (MSVCRT.@)
3992 int CDECL
_setmaxstdio(int newmax
)
3994 TRACE("%d\n", newmax
);
3996 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
3999 MSVCRT_max_streams
= newmax
;
4000 return MSVCRT_max_streams
;