2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
37 #include <sys/types.h>
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
63 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
64 #define WX_READCR 0x08 /* underlying file is at \r */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* values for exflag - it's used differently in msvcr90.dll*/
72 #define EF_CRIT_INIT 0x04
73 #define EF_UNK_UNICODE 0x08
75 static char utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
76 static char utf16_bom
[2] = { 0xff, 0xfe };
78 /* FIXME: this should be allocated dynamically */
79 #define MSVCRT_MAX_FILES 2048
80 #define MSVCRT_FD_BLOCK_SIZE 32
82 /* ioinfo structure size is different in msvcrXX.dll's */
88 CRITICAL_SECTION crit
;
91 /*********************************************************************
92 * __pioinfo (MSVCRT.@)
93 * array of pointers to ioinfo arrays [32]
95 ioinfo
* MSVCRT___pioinfo
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
] = { 0 };
97 /*********************************************************************
98 * __badioinfo (MSVCRT.@)
100 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};
102 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
103 static int MSVCRT_fdend
= 3; /* highest allocated fd */
107 CRITICAL_SECTION crit
;
110 MSVCRT_FILE MSVCRT__iob
[_IOB_ENTRIES
] = { { 0 } };
111 static file_crit
* MSVCRT_fstream
[MSVCRT_MAX_FILES
/MSVCRT_FD_BLOCK_SIZE
];
112 static int MSVCRT_max_streams
= 512, MSVCRT_stream_idx
;
114 /* INTERNAL: process umask */
115 static int MSVCRT_umask
= 0;
117 /* INTERNAL: static data for tmpnam and _wtmpname functions */
118 static int tmpnam_unique
;
120 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
121 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
122 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
123 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
125 #define TOUL(x) (ULONGLONG)(x)
126 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
127 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
128 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
129 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
131 /* This critical section protects the tables MSVCRT___pioinfo and MSVCRT_fstreams,
132 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
133 * and MSVCRT_stream_idx, from race conditions.
134 * It doesn't protect against race conditions manipulating the underlying files
135 * or flags; doing so would probably be better accomplished with per-file
136 * protection, rather than locking the whole table for every change.
138 static CRITICAL_SECTION MSVCRT_file_cs
;
139 static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
=
141 0, 0, &MSVCRT_file_cs
,
142 { &MSVCRT_file_cs_debug
.ProcessLocksList
, &MSVCRT_file_cs_debug
.ProcessLocksList
},
143 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs") }
145 static CRITICAL_SECTION MSVCRT_file_cs
= { &MSVCRT_file_cs_debug
, -1, 0, 0, 0, 0 };
146 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
147 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
149 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
151 buf
->st_dev
= buf64
->st_dev
;
152 buf
->st_ino
= buf64
->st_ino
;
153 buf
->st_mode
= buf64
->st_mode
;
154 buf
->st_nlink
= buf64
->st_nlink
;
155 buf
->st_uid
= buf64
->st_uid
;
156 buf
->st_gid
= buf64
->st_gid
;
157 buf
->st_rdev
= buf64
->st_rdev
;
158 buf
->st_size
= buf64
->st_size
;
159 buf
->st_atime
= buf64
->st_atime
;
160 buf
->st_mtime
= buf64
->st_mtime
;
161 buf
->st_ctime
= buf64
->st_ctime
;
164 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
166 buf
->st_dev
= buf64
->st_dev
;
167 buf
->st_ino
= buf64
->st_ino
;
168 buf
->st_mode
= buf64
->st_mode
;
169 buf
->st_nlink
= buf64
->st_nlink
;
170 buf
->st_uid
= buf64
->st_uid
;
171 buf
->st_gid
= buf64
->st_gid
;
172 buf
->st_rdev
= buf64
->st_rdev
;
173 buf
->st_size
= buf64
->st_size
;
174 buf
->st_atime
= buf64
->st_atime
;
175 buf
->st_mtime
= buf64
->st_mtime
;
176 buf
->st_ctime
= buf64
->st_ctime
;
179 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
181 /* 1601 to 1970 is 369 years plus 89 leap days */
182 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
184 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
185 ft
->dwHighDateTime
= ticks
>> 32;
186 ft
->dwLowDateTime
= ticks
;
189 static inline ioinfo
* msvcrt_get_ioinfo(int fd
)
192 if(fd
< MSVCRT_MAX_FILES
)
193 ret
= MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
];
195 return &MSVCRT___badioinfo
;
197 return ret
+ (fd
%MSVCRT_FD_BLOCK_SIZE
);
200 static inline MSVCRT_FILE
* msvcrt_get_file(int i
)
204 if(i
>= MSVCRT_max_streams
)
208 return &MSVCRT__iob
[i
];
210 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
];
212 MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(file_crit
));
213 if(!MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
]) {
214 ERR("out of memory\n");
215 *MSVCRT__errno() = MSVCRT_ENOMEM
;
219 ret
= MSVCRT_fstream
[i
/MSVCRT_FD_BLOCK_SIZE
] + (i
%MSVCRT_FD_BLOCK_SIZE
);
221 ret
+= i
%MSVCRT_FD_BLOCK_SIZE
;
226 static inline BOOL
msvcrt_is_valid_fd(int fd
)
228 return fd
>= 0 && fd
< MSVCRT_fdend
&& (msvcrt_get_ioinfo(fd
)->wxflag
& WX_OPEN
);
231 /* INTERNAL: Get the HANDLE for a fd
232 * This doesn't lock the table, because a failure will result in
233 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
234 * it returns a valid handle which is about to be closed, a subsequent call
235 * will fail, most likely in a sane way.
237 static HANDLE
msvcrt_fdtoh(int fd
)
239 if (!msvcrt_is_valid_fd(fd
))
241 WARN(":fd (%d) - no handle!\n",fd
);
242 *MSVCRT___doserrno() = 0;
243 *MSVCRT__errno() = MSVCRT_EBADF
;
244 return INVALID_HANDLE_VALUE
;
246 if (msvcrt_get_ioinfo(fd
)->handle
== INVALID_HANDLE_VALUE
)
247 WARN("returning INVALID_HANDLE_VALUE for %d\n", fd
);
248 return msvcrt_get_ioinfo(fd
)->handle
;
251 /* INTERNAL: free a file entry fd */
252 static void msvcrt_free_fd(int fd
)
258 fdinfo
= msvcrt_get_ioinfo(fd
);
259 old_handle
= fdinfo
->handle
;
260 if(fdinfo
!= &MSVCRT___badioinfo
)
262 fdinfo
->handle
= INVALID_HANDLE_VALUE
;
265 TRACE(":fd (%d) freed\n",fd
);
266 if (fd
< 3) /* don't use 0,1,2 for user files */
271 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
274 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
277 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
283 if (fd
== MSVCRT_fdend
- 1)
285 if (fd
< MSVCRT_fdstart
)
291 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
292 /* caller must hold the files lock */
293 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
297 if (fd
>= MSVCRT_MAX_FILES
)
299 WARN(":files exhausted!\n");
300 *MSVCRT__errno() = MSVCRT_ENFILE
;
304 fdinfo
= msvcrt_get_ioinfo(fd
);
305 if(fdinfo
== &MSVCRT___badioinfo
) {
308 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
] = MSVCRT_calloc(MSVCRT_FD_BLOCK_SIZE
, sizeof(ioinfo
));
309 if(!MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
]) {
310 WARN(":out of memory!\n");
311 *MSVCRT__errno() = MSVCRT_ENOMEM
;
315 for(i
=0; i
<MSVCRT_FD_BLOCK_SIZE
; i
++)
316 MSVCRT___pioinfo
[fd
/MSVCRT_FD_BLOCK_SIZE
][i
].handle
= INVALID_HANDLE_VALUE
;
318 fdinfo
= msvcrt_get_ioinfo(fd
);
321 fdinfo
->handle
= hand
;
322 fdinfo
->wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
325 /* locate next free slot */
326 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
327 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
329 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
330 msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
!= INVALID_HANDLE_VALUE
)
332 /* update last fd in use */
333 if (fd
>= MSVCRT_fdend
)
334 MSVCRT_fdend
= fd
+ 1;
335 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
339 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
340 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
341 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
347 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
348 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
353 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
354 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
359 /* INTERNAL: Allocate a FILE* for an fd slot */
360 /* caller must hold the files lock */
361 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
366 for (i
= 3; i
< MSVCRT_max_streams
; i
++)
368 file
= msvcrt_get_file(i
);
372 if (file
->_flag
== 0)
374 if (i
== MSVCRT_stream_idx
)
376 if (file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
378 InitializeCriticalSection(&((file_crit
*)file
)->crit
);
379 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": file_crit.crit");
390 /* INTERNAL: initialize a FILE* from an open fd */
391 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
393 TRACE(":fd (%d) allocating FILE*\n",fd
);
394 if (!msvcrt_is_valid_fd(fd
))
396 WARN(":invalid fd %d\n",fd
);
397 *MSVCRT___doserrno() = 0;
398 *MSVCRT__errno() = MSVCRT_EBADF
;
401 memset(file
, 0, sizeof(*file
));
403 file
->_flag
= stream_flags
;
405 TRACE(":got FILE* (%p)\n",file
);
409 /* INTERNAL: Create an inheritance data block (for spawned process)
410 * The inheritance block is made of:
411 * 00 int nb of file descriptor (NBFD)
412 * 04 char file flags (wxflag): repeated for each fd
413 * 4+NBFD HANDLE file handle: repeated for each fd
415 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
422 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
423 *block
= MSVCRT_calloc(*size
, 1);
429 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
430 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
432 *(unsigned*)*block
= MSVCRT_fdend
;
433 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
435 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
436 fdinfo
= msvcrt_get_ioinfo(fd
);
437 if ((fdinfo
->wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
439 *wxflag_ptr
= fdinfo
->wxflag
;
440 *handle_ptr
= fdinfo
->handle
;
445 *handle_ptr
= INVALID_HANDLE_VALUE
;
447 wxflag_ptr
++; handle_ptr
++;
452 /* INTERNAL: Set up all file descriptors,
453 * as well as default streams (stdin, stderr and stdout)
455 void msvcrt_init_io(void)
461 GetStartupInfoA(&si
);
462 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
468 count
= *(unsigned*)si
.lpReserved2
;
469 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
470 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
472 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
473 count
= min(count
, MSVCRT_MAX_FILES
);
474 for (i
= 0; i
< count
; i
++)
476 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
477 msvcrt_alloc_fd_from(*handle_ptr
, *wxflag_ptr
, i
);
479 wxflag_ptr
++; handle_ptr
++;
481 MSVCRT_fdend
= max( 3, count
);
482 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
483 if (msvcrt_get_ioinfo(MSVCRT_fdstart
)->handle
== INVALID_HANDLE_VALUE
) break;
486 if(!MSVCRT___pioinfo
[0])
487 msvcrt_alloc_fd_from(INVALID_HANDLE_VALUE
, 0, 3);
489 fdinfo
= msvcrt_get_ioinfo(0);
490 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
492 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
493 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
494 GetCurrentProcess(), &fdinfo
->handle
,
495 0, TRUE
, DUPLICATE_SAME_ACCESS
))
496 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
500 fdinfo
= msvcrt_get_ioinfo(1);
501 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
503 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
504 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
505 GetCurrentProcess(), &fdinfo
->handle
,
506 0, TRUE
, DUPLICATE_SAME_ACCESS
))
507 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
511 fdinfo
= msvcrt_get_ioinfo(2);
512 if (!(fdinfo
->wxflag
& WX_OPEN
) || fdinfo
->handle
== INVALID_HANDLE_VALUE
)
514 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
515 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
516 GetCurrentProcess(), &fdinfo
->handle
,
517 0, TRUE
, DUPLICATE_SAME_ACCESS
))
518 fdinfo
->wxflag
= WX_OPEN
| WX_TEXT
;
522 TRACE(":handles (%p)(%p)(%p)\n", msvcrt_get_ioinfo(0)->handle
,
523 msvcrt_get_ioinfo(1)->handle
, msvcrt_get_ioinfo(2)->handle
);
525 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
526 for (i
= 0; i
< 3; i
++)
528 /* FILE structs for stdin/out/err are static and never deleted */
529 MSVCRT__iob
[i
]._file
= i
;
530 MSVCRT__iob
[i
]._tmpfname
= NULL
;
531 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
533 MSVCRT_stream_idx
= 3;
536 /* INTERNAL: Flush stdio file buffer */
537 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
540 int cnt
=file
->_ptr
-file
->_base
;
541 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
542 file
->_flag
|= MSVCRT__IOERR
;
545 file
->_ptr
=file
->_base
;
546 file
->_cnt
=file
->_bufsiz
;
551 /* INTERNAL: Allocate stdio file buffer */
552 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
554 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
556 file
->_bufsiz
= MSVCRT_BUFSIZ
;
557 file
->_flag
|= MSVCRT__IOMYBUF
;
559 file
->_base
= (char*)(&file
->_charbuf
);
561 file
->_bufsiz
= sizeof(file
->_charbuf
);
563 file
->_ptr
= file
->_base
;
567 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
568 static int msvcrt_int_to_base32(int num
, char *str
)
583 *p
= (num
& 31) + '0';
585 *p
+= ('a' - '0' - 10);
592 /* INTERNAL: wide character version of msvcrt_int_to_base32 */
593 static int msvcrt_int_to_base32_w(int num
, MSVCRT_wchar_t
*str
)
608 *p
= (num
& 31) + '0';
610 *p
+= ('a' - '0' - 10);
617 /*********************************************************************
618 * __iob_func(MSVCRT.@)
620 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
622 return &MSVCRT__iob
[0];
625 /*********************************************************************
628 int CDECL
MSVCRT__access(const char *filename
, int mode
)
630 DWORD attr
= GetFileAttributesA(filename
);
632 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
634 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
636 msvcrt_set_errno(GetLastError());
639 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
641 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
647 /*********************************************************************
648 * _access_s (MSVCRT.@)
650 int CDECL
_access_s(const char *filename
, int mode
)
652 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return -1;
653 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return -1;
655 return MSVCRT__access(filename
, mode
);
658 /*********************************************************************
659 * _waccess (MSVCRT.@)
661 int CDECL
MSVCRT__waccess(const MSVCRT_wchar_t
*filename
, int mode
)
663 DWORD attr
= GetFileAttributesW(filename
);
665 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
667 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
669 msvcrt_set_errno(GetLastError());
672 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
674 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
680 /*********************************************************************
681 * _waccess_s (MSVCRT.@)
683 int CDECL
_waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
685 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return -1;
686 if (!MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0)) return -1;
688 return MSVCRT__waccess(filename
, mode
);
691 /*********************************************************************
694 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
696 DWORD oldFlags
= GetFileAttributesA(path
);
698 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
700 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
701 oldFlags
| FILE_ATTRIBUTE_READONLY
;
703 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
706 msvcrt_set_errno(GetLastError());
710 /*********************************************************************
713 int CDECL
MSVCRT__wchmod(const MSVCRT_wchar_t
*path
, int flags
)
715 DWORD oldFlags
= GetFileAttributesW(path
);
717 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
719 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
720 oldFlags
| FILE_ATTRIBUTE_READONLY
;
722 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
725 msvcrt_set_errno(GetLastError());
729 /*********************************************************************
732 int CDECL
MSVCRT__unlink(const char *path
)
734 TRACE("%s\n",debugstr_a(path
));
735 if(DeleteFileA(path
))
737 TRACE("failed (%d)\n",GetLastError());
738 msvcrt_set_errno(GetLastError());
742 /*********************************************************************
743 * _wunlink (MSVCRT.@)
745 int CDECL
MSVCRT__wunlink(const MSVCRT_wchar_t
*path
)
747 TRACE("(%s)\n",debugstr_w(path
));
748 if(DeleteFileW(path
))
750 TRACE("failed (%d)\n",GetLastError());
751 msvcrt_set_errno(GetLastError());
755 /* flush_all_buffers calls MSVCRT_fflush which calls flush_all_buffers */
756 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
758 /* INTERNAL: Flush all stream buffer */
759 static int msvcrt_flush_all_buffers(int mask
)
761 int i
, num_flushed
= 0;
765 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
766 file
= msvcrt_get_file(i
);
770 if(file
->_flag
& mask
) {
778 TRACE(":flushed (%d) handles\n",num_flushed
);
782 /*********************************************************************
783 * _flushall (MSVCRT.@)
785 int CDECL
MSVCRT__flushall(void)
787 return msvcrt_flush_all_buffers(MSVCRT__IOWRT
| MSVCRT__IOREAD
);
790 /*********************************************************************
793 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
796 msvcrt_flush_all_buffers(MSVCRT__IOWRT
);
797 } else if(file
->_flag
& MSVCRT__IOWRT
) {
800 MSVCRT__lock_file(file
);
801 res
= msvcrt_flush_buffer(file
);
802 MSVCRT__unlock_file(file
);
805 } else if(file
->_flag
& MSVCRT__IOREAD
) {
806 MSVCRT__lock_file(file
);
808 file
->_ptr
= file
->_base
;
809 MSVCRT__unlock_file(file
);
816 /*********************************************************************
819 int CDECL
MSVCRT__close(int fd
)
825 hand
= msvcrt_fdtoh(fd
);
826 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
827 if (!msvcrt_is_valid_fd(fd
)) {
831 ret
= CloseHandle(hand
) ? 0 : -1;
833 WARN(":failed-last error (%d)\n",GetLastError());
834 msvcrt_set_errno(GetLastError());
842 /*********************************************************************
845 int CDECL
MSVCRT__commit(int fd
)
847 HANDLE hand
= msvcrt_fdtoh(fd
);
849 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
850 if (hand
== INVALID_HANDLE_VALUE
)
853 if (!FlushFileBuffers(hand
))
855 if (GetLastError() == ERROR_INVALID_HANDLE
)
857 /* FlushFileBuffers fails for console handles
858 * so we ignore this error.
862 TRACE(":failed-last error (%d)\n",GetLastError());
863 msvcrt_set_errno(GetLastError());
870 /*********************************************************************
873 * MSDN isn't clear on this point, but the remarks for _pipe
874 * indicate file descriptors duplicated with _dup and _dup2 are always
877 int CDECL
MSVCRT__dup2(int od
, int nd
)
881 TRACE("(od=%d, nd=%d)\n", od
, nd
);
883 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
887 if (DuplicateHandle(GetCurrentProcess(), msvcrt_get_ioinfo(od
)->handle
,
888 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
890 int wxflag
= msvcrt_get_ioinfo(od
)->wxflag
& ~MSVCRT__O_NOINHERIT
;
892 if (msvcrt_is_valid_fd(nd
))
894 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
898 *MSVCRT__errno() = MSVCRT_EMFILE
;
902 /* _dup2 returns 0, not nd, on success */
909 msvcrt_set_errno(GetLastError());
914 *MSVCRT__errno() = MSVCRT_EBADF
;
921 /*********************************************************************
924 int CDECL
MSVCRT__dup(int od
)
930 if (MSVCRT__dup2(od
, fd
) == 0)
938 /*********************************************************************
941 int CDECL
MSVCRT__eof(int fd
)
944 LONG hcurpos
,hendpos
;
945 HANDLE hand
= msvcrt_fdtoh(fd
);
947 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
949 if (hand
== INVALID_HANDLE_VALUE
)
952 if (msvcrt_get_ioinfo(fd
)->wxflag
& WX_ATEOF
) return TRUE
;
954 /* Otherwise we do it the hard way */
955 hcurpos
= hendpos
= 0;
956 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
957 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
959 if (curpos
== endpos
&& hcurpos
== hendpos
)
961 /* FIXME: shouldn't WX_ATEOF be set here? */
965 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
969 /*********************************************************************
970 * _fcloseall (MSVCRT.@)
972 int CDECL
MSVCRT__fcloseall(void)
974 int num_closed
= 0, i
;
978 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
979 file
= msvcrt_get_file(i
);
981 if (file
->_flag
&& !MSVCRT_fclose(file
))
986 TRACE(":closed (%d) handles\n",num_closed
);
990 /* free everything on process exit */
991 void msvcrt_free_io(void)
996 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
997 * stdout, and stderr (unlike GNU), so we need to fclose() them here
998 * or they won't get flushed.
1000 MSVCRT_fclose(&MSVCRT__iob
[0]);
1001 MSVCRT_fclose(&MSVCRT__iob
[1]);
1002 MSVCRT_fclose(&MSVCRT__iob
[2]);
1004 for(i
=0; i
<sizeof(MSVCRT___pioinfo
)/sizeof(MSVCRT___pioinfo
[0]); i
++)
1005 MSVCRT_free(MSVCRT___pioinfo
[i
]);
1007 for(i
=0; i
<MSVCRT_stream_idx
; i
++)
1009 MSVCRT_FILE
*file
= msvcrt_get_file(i
);
1010 if(file
<MSVCRT__iob
|| file
>=MSVCRT__iob
+_IOB_ENTRIES
)
1012 ((file_crit
*)file
)->crit
.DebugInfo
->Spare
[0] = 0;
1013 DeleteCriticalSection(&((file_crit
*)file
)->crit
);
1017 for(i
=0; i
<sizeof(MSVCRT_fstream
)/sizeof(MSVCRT_fstream
[0]); i
++)
1018 MSVCRT_free(MSVCRT_fstream
[i
]);
1020 DeleteCriticalSection(&MSVCRT_file_cs
);
1023 /*********************************************************************
1024 * _lseeki64 (MSVCRT.@)
1026 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
1028 HANDLE hand
= msvcrt_fdtoh(fd
);
1031 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1032 if (hand
== INVALID_HANDLE_VALUE
)
1035 if (whence
< 0 || whence
> 2)
1037 *MSVCRT__errno() = MSVCRT_EINVAL
;
1041 TRACE(":fd (%d) to %s pos %s\n",
1042 fd
,wine_dbgstr_longlong(offset
),
1043 (whence
==SEEK_SET
)?"SEEK_SET":
1044 (whence
==SEEK_CUR
)?"SEEK_CUR":
1045 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
1047 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1048 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1049 ofs
.QuadPart
= offset
;
1050 if ((ofs
.u
.LowPart
= SetFilePointer(hand
, ofs
.u
.LowPart
, &ofs
.u
.HighPart
, whence
)) != INVALID_SET_FILE_POINTER
||
1051 GetLastError() == ERROR_SUCCESS
)
1053 msvcrt_get_ioinfo(fd
)->wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
1054 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1056 return ofs
.QuadPart
;
1058 TRACE(":error-last error (%d)\n",GetLastError());
1059 msvcrt_set_errno(GetLastError());
1063 /*********************************************************************
1066 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
1068 return MSVCRT__lseeki64(fd
, offset
, whence
);
1071 /*********************************************************************
1072 * _lock_file (MSVCRT.@)
1074 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
1076 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1077 _lock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1079 EnterCriticalSection(&((file_crit
*)file
)->crit
);
1082 /*********************************************************************
1083 * _unlock_file (MSVCRT.@)
1085 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
1087 if(file
>=MSVCRT__iob
&& file
<MSVCRT__iob
+_IOB_ENTRIES
)
1088 _unlock(_STREAM_LOCKS
+(file
-MSVCRT__iob
));
1090 LeaveCriticalSection(&((file_crit
*)file
)->crit
);
1093 /*********************************************************************
1094 * _locking (MSVCRT.@)
1096 * This is untested; the underlying LockFile doesn't work yet.
1098 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
1102 HANDLE hand
= msvcrt_fdtoh(fd
);
1104 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1105 if (hand
== INVALID_HANDLE_VALUE
)
1108 if (mode
< 0 || mode
> 4)
1110 *MSVCRT__errno() = MSVCRT_EINVAL
;
1114 TRACE(":fd (%d) by 0x%08x mode %s\n",
1115 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
1116 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
1117 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
1118 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
1119 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
1122 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
1124 FIXME ("Seek failed\n");
1125 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
1128 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
1131 ret
= 1; /* just to satisfy gcc */
1134 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1139 else if (mode
== MSVCRT__LK_UNLCK
)
1140 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1142 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
1143 /* FIXME - what about error settings? */
1144 return ret
? 0 : -1;
1147 /*********************************************************************
1148 * _fseeki64 (MSVCRT.@)
1150 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
1154 MSVCRT__lock_file(file
);
1155 /* Flush output if needed */
1156 if(file
->_flag
& MSVCRT__IOWRT
)
1157 msvcrt_flush_buffer(file
);
1159 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1160 offset
-= file
->_cnt
;
1161 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
1162 /* Black magic correction for CR removal */
1164 for (i
=0; i
<file
->_cnt
; i
++) {
1165 if (file
->_ptr
[i
] == '\n')
1168 /* Black magic when reading CR at buffer boundary*/
1169 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
1173 /* Discard buffered input */
1175 file
->_ptr
= file
->_base
;
1176 /* Reset direction of i/o */
1177 if(file
->_flag
& MSVCRT__IORW
) {
1178 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1180 /* Clear end of file flag */
1181 file
->_flag
&= ~MSVCRT__IOEOF
;
1182 ret
= (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1184 MSVCRT__unlock_file(file
);
1188 /*********************************************************************
1191 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1193 return MSVCRT__fseeki64( file
, offset
, whence
);
1196 /*********************************************************************
1197 * _chsize (MSVCRT.@)
1199 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1205 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1209 handle
= msvcrt_fdtoh(fd
);
1210 if (handle
!= INVALID_HANDLE_VALUE
)
1212 /* save the current file pointer */
1213 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1216 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
1219 ret
= SetEndOfFile(handle
);
1220 if (!ret
) msvcrt_set_errno(GetLastError());
1223 /* restore the file pointer */
1224 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
1229 return ret
? 0 : -1;
1232 /*********************************************************************
1233 * clearerr (MSVCRT.@)
1235 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1237 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1239 MSVCRT__lock_file(file
);
1240 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1241 MSVCRT__unlock_file(file
);
1244 /*********************************************************************
1247 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1249 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1251 MSVCRT__lock_file(file
);
1252 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1253 MSVCRT_clearerr(file
);
1254 MSVCRT__unlock_file(file
);
1257 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1259 int plus
= strchrW(mode
, '+') != NULL
;
1261 TRACE("%s\n", debugstr_w(mode
));
1266 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1267 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1270 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1271 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1274 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1275 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1278 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1282 while (*mode
&& *mode
!=',')
1286 *open_flags
|= MSVCRT__O_BINARY
;
1287 *open_flags
&= ~MSVCRT__O_TEXT
;
1290 *open_flags
|= MSVCRT__O_TEXT
;
1291 *open_flags
&= ~MSVCRT__O_BINARY
;
1294 *open_flags
|= MSVCRT__O_TEMPORARY
;
1297 *open_flags
|= MSVCRT__O_SHORT_LIVED
;
1303 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1309 static const WCHAR ccs
[] = {'c','c','s'};
1310 static const WCHAR utf8
[] = {'u','t','f','-','8'};
1311 static const WCHAR utf16le
[] = {'u','t','f','-','1','6','l','e'};
1312 static const WCHAR unicode
[] = {'u','n','i','c','o','d','e'};
1315 while(*mode
== ' ') mode
++;
1316 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs
, mode
, sizeof(ccs
)/sizeof(ccs
[0]))))
1318 mode
+= sizeof(ccs
)/sizeof(ccs
[0]);
1319 while(*mode
== ' ') mode
++;
1320 if(!MSVCRT_CHECK_PMT(*mode
== '='))
1323 while(*mode
== ' ') mode
++;
1325 if(!strncmpiW(utf8
, mode
, sizeof(utf8
)/sizeof(utf8
[0])))
1327 *open_flags
|= MSVCRT__O_U8TEXT
;
1328 mode
+= sizeof(utf8
)/sizeof(utf8
[0]);
1330 else if(!strncmpiW(utf16le
, mode
, sizeof(utf16le
)/sizeof(utf16le
[0])))
1332 *open_flags
|= MSVCRT__O_U16TEXT
;
1333 mode
+= sizeof(utf16le
)/sizeof(utf16le
[0]);
1335 else if(!strncmpiW(unicode
, mode
, sizeof(unicode
)/sizeof(unicode
[0])))
1337 *open_flags
|= MSVCRT__O_WTEXT
;
1338 mode
+= sizeof(unicode
)/sizeof(unicode
[0]);
1342 MSVCRT_INVALID_PMT(0, MSVCRT_EINVAL
);
1346 while(*mode
== ' ') mode
++;
1349 if(!MSVCRT_CHECK_PMT(*mode
== 0))
1354 /*********************************************************************
1355 * _fdopen (MSVCRT.@)
1357 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1360 MSVCRT_wchar_t
*modeW
= NULL
;
1362 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1364 ret
= MSVCRT__wfdopen(fd
, modeW
);
1370 /*********************************************************************
1371 * _wfdopen (MSVCRT.@)
1373 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1375 int open_flags
, stream_flags
;
1378 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1381 if (!(file
= msvcrt_alloc_fp()))
1383 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1388 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1394 /*********************************************************************
1395 * _filelength (MSVCRT.@)
1397 LONG CDECL
MSVCRT__filelength(int fd
)
1399 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1402 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1405 if (endPos
!= curPos
)
1406 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1413 /*********************************************************************
1414 * _filelengthi64 (MSVCRT.@)
1416 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1418 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1421 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1424 if (endPos
!= curPos
)
1425 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1432 /*********************************************************************
1433 * _fileno (MSVCRT.@)
1435 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1437 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1441 /*********************************************************************
1442 * _fstat64 (MSVCRT.@)
1444 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1448 BY_HANDLE_FILE_INFORMATION hfi
;
1449 HANDLE hand
= msvcrt_fdtoh(fd
);
1451 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1452 if (hand
== INVALID_HANDLE_VALUE
)
1457 WARN(":failed-NULL buf\n");
1458 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1462 memset(&hfi
, 0, sizeof(hfi
));
1463 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1464 type
= GetFileType(hand
);
1465 if (type
== FILE_TYPE_PIPE
)
1467 buf
->st_dev
= buf
->st_rdev
= fd
;
1468 buf
->st_mode
= S_IFIFO
;
1471 else if (type
== FILE_TYPE_CHAR
)
1473 buf
->st_dev
= buf
->st_rdev
= fd
;
1474 buf
->st_mode
= S_IFCHR
;
1477 else /* FILE_TYPE_DISK etc. */
1479 if (!GetFileInformationByHandle(hand
, &hfi
))
1481 WARN(":failed-last error (%d)\n",GetLastError());
1482 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1485 buf
->st_mode
= S_IFREG
| 0444;
1486 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1487 buf
->st_mode
|= 0222;
1488 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1489 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1491 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1492 buf
->st_mtime
= buf
->st_ctime
= dw
;
1493 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1495 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1500 /*********************************************************************
1501 * _fstati64 (MSVCRT.@)
1503 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1506 struct MSVCRT__stat64 buf64
;
1508 ret
= MSVCRT__fstat64(fd
, &buf64
);
1510 msvcrt_stat64_to_stati64(&buf64
, buf
);
1514 /*********************************************************************
1517 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1519 struct MSVCRT__stat64 buf64
;
1521 ret
= MSVCRT__fstat64(fd
, &buf64
);
1523 msvcrt_stat64_to_stat(&buf64
, buf
);
1527 /*********************************************************************
1528 * _futime64 (MSVCRT.@)
1530 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1532 HANDLE hand
= msvcrt_fdtoh(fd
);
1537 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1542 time_to_filetime( t
->actime
, &at
);
1543 time_to_filetime( t
->modtime
, &wt
);
1546 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1548 msvcrt_set_errno(GetLastError());
1554 /*********************************************************************
1555 * _futime32 (MSVCRT.@)
1557 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1561 struct MSVCRT___utimbuf64 t64
;
1562 t64
.actime
= t
->actime
;
1563 t64
.modtime
= t
->modtime
;
1564 return _futime64( fd
, &t64
);
1567 return _futime64( fd
, NULL
);
1570 /*********************************************************************
1571 * _futime (MSVCRT.@)
1574 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1576 return _futime64( fd
, t
);
1579 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1581 return _futime32( fd
, t
);
1585 /*********************************************************************
1586 * _get_osfhandle (MSVCRT.@)
1588 MSVCRT_intptr_t CDECL
MSVCRT__get_osfhandle(int fd
)
1590 HANDLE hand
= msvcrt_fdtoh(fd
);
1591 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1593 return (MSVCRT_intptr_t
)hand
;
1596 /*********************************************************************
1597 * _isatty (MSVCRT.@)
1599 int CDECL
MSVCRT__isatty(int fd
)
1601 HANDLE hand
= msvcrt_fdtoh(fd
);
1603 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1604 if (hand
== INVALID_HANDLE_VALUE
)
1607 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1610 /*********************************************************************
1611 * _mktemp (MSVCRT.@)
1613 char * CDECL
MSVCRT__mktemp(char *pattern
)
1616 char *retVal
= pattern
;
1621 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1625 id
= GetCurrentProcessId();
1629 int tempNum
= id
/ 10;
1630 *pattern
-- = id
- (tempNum
* 10) + '0';
1636 *pattern
= letter
++;
1637 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1638 GetLastError() == ERROR_FILE_NOT_FOUND
)
1640 } while(letter
<= 'z');
1644 /*********************************************************************
1645 * _wmktemp (MSVCRT.@)
1647 MSVCRT_wchar_t
* CDECL
MSVCRT__wmktemp(MSVCRT_wchar_t
*pattern
)
1650 MSVCRT_wchar_t
*retVal
= pattern
;
1652 MSVCRT_wchar_t letter
= 'a';
1655 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1659 id
= GetCurrentProcessId();
1663 int tempNum
= id
/ 10;
1664 *pattern
-- = id
- (tempNum
* 10) + '0';
1670 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1671 GetLastError() == ERROR_FILE_NOT_FOUND
)
1673 *pattern
= letter
++;
1674 } while(letter
!= '|');
1678 static unsigned split_oflags(unsigned oflags
)
1681 unsigned unsupp
; /* until we support everything */
1683 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1684 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1685 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1686 else if (oflags
& MSVCRT__O_WTEXT
) wxflags
|= WX_TEXT
;
1687 else if (oflags
& MSVCRT__O_U16TEXT
) wxflags
|= WX_TEXT
;
1688 else if (oflags
& MSVCRT__O_U8TEXT
) wxflags
|= WX_TEXT
;
1689 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1690 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1691 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1693 if ((unsupp
= oflags
& ~(
1694 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1695 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1696 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1697 MSVCRT__O_NOINHERIT
|
1698 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
|
1699 MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
1701 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1706 /*********************************************************************
1709 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1712 SECURITY_ATTRIBUTES sa
;
1713 HANDLE readHandle
, writeHandle
;
1717 *MSVCRT__errno() = MSVCRT_EINVAL
;
1721 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1722 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1723 sa
.lpSecurityDescriptor
= NULL
;
1724 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1726 unsigned int wxflags
= split_oflags(textmode
);
1730 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1734 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1742 MSVCRT__close(pfds
[0]);
1743 CloseHandle(writeHandle
);
1744 *MSVCRT__errno() = MSVCRT_EMFILE
;
1749 CloseHandle(readHandle
);
1750 CloseHandle(writeHandle
);
1751 *MSVCRT__errno() = MSVCRT_EMFILE
;
1756 msvcrt_set_errno(GetLastError());
1761 static int check_bom(HANDLE h
, int oflags
, BOOL seek
)
1763 char bom
[sizeof(utf8_bom
)];
1766 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
1768 if (!ReadFile(h
, bom
, sizeof(utf8_bom
), &r
, NULL
))
1771 if (r
==sizeof(utf8_bom
) && !memcmp(bom
, utf8_bom
, sizeof(utf8_bom
))) {
1772 oflags
|= MSVCRT__O_U8TEXT
;
1773 }else if (r
>=sizeof(utf16_bom
) && !memcmp(bom
, utf16_bom
, sizeof(utf16_bom
))) {
1775 SetFilePointer(h
, 2, NULL
, FILE_BEGIN
);
1776 oflags
|= MSVCRT__O_U16TEXT
;
1778 SetFilePointer(h
, 0, NULL
, FILE_BEGIN
);
1784 /*********************************************************************
1785 * _wsopen_s (MSVCRT.@)
1787 int CDECL
MSVCRT__wsopen_s( int *fd
, const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, int pmode
)
1789 DWORD access
= 0, creation
= 0, attrib
;
1790 SECURITY_ATTRIBUTES sa
;
1795 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1796 fd
, debugstr_w(path
), oflags
, shflags
, pmode
);
1798 if (!MSVCRT_CHECK_PMT( fd
!= NULL
)) return MSVCRT_EINVAL
;
1801 wxflag
= split_oflags(oflags
);
1802 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1804 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1805 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1806 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1809 if (oflags
& MSVCRT__O_CREAT
)
1811 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1812 FIXME(": pmode 0x%04x ignored\n", pmode
);
1814 WARN(": pmode 0x%04x ignored\n", pmode
);
1816 if (oflags
& MSVCRT__O_EXCL
)
1817 creation
= CREATE_NEW
;
1818 else if (oflags
& MSVCRT__O_TRUNC
)
1819 creation
= CREATE_ALWAYS
;
1821 creation
= OPEN_ALWAYS
;
1823 else /* no MSVCRT__O_CREAT */
1825 if (oflags
& MSVCRT__O_TRUNC
)
1826 creation
= TRUNCATE_EXISTING
;
1828 creation
= OPEN_EXISTING
;
1833 case MSVCRT__SH_DENYRW
:
1836 case MSVCRT__SH_DENYWR
:
1837 sharing
= FILE_SHARE_READ
;
1839 case MSVCRT__SH_DENYRD
:
1840 sharing
= FILE_SHARE_WRITE
;
1842 case MSVCRT__SH_DENYNO
:
1843 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1846 ERR( "Unhandled shflags 0x%x\n", shflags
);
1847 return MSVCRT_EINVAL
;
1849 attrib
= FILE_ATTRIBUTE_NORMAL
;
1851 if (oflags
& MSVCRT__O_TEMPORARY
)
1853 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1855 sharing
|= FILE_SHARE_DELETE
;
1858 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1859 sa
.lpSecurityDescriptor
= NULL
;
1860 sa
.bInheritHandle
= !(oflags
& MSVCRT__O_NOINHERIT
);
1862 if ((oflags
&(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
1863 && (creation
==OPEN_ALWAYS
|| creation
==OPEN_EXISTING
)
1864 && !(access
&GENERIC_READ
))
1866 hand
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1867 &sa
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
1868 if (hand
!= INVALID_HANDLE_VALUE
)
1870 oflags
= check_bom(hand
, oflags
, FALSE
);
1874 oflags
&= ~(MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
);
1877 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1878 if (hand
== INVALID_HANDLE_VALUE
) {
1879 WARN(":failed-last error (%d)\n",GetLastError());
1880 msvcrt_set_errno(GetLastError());
1881 return *MSVCRT__errno();
1884 if (oflags
& (MSVCRT__O_WTEXT
|MSVCRT__O_U16TEXT
|MSVCRT__O_U8TEXT
))
1886 if ((access
& GENERIC_WRITE
) && (creation
==CREATE_NEW
1887 || creation
==CREATE_ALWAYS
|| creation
==TRUNCATE_EXISTING
1888 || (creation
==OPEN_ALWAYS
&& GetLastError()==ERROR_ALREADY_EXISTS
)))
1890 if (oflags
& MSVCRT__O_U8TEXT
)
1892 DWORD written
= 0, tmp
;
1894 while(written
!=sizeof(utf8_bom
) && WriteFile(hand
, (char*)utf8_bom
+written
,
1895 sizeof(utf8_bom
)-written
, &tmp
, NULL
))
1897 if (written
!= sizeof(utf8_bom
)) {
1898 WARN("error writting BOM\n");
1900 msvcrt_set_errno(GetLastError());
1901 return *MSVCRT__errno();
1906 DWORD written
= 0, tmp
;
1908 while(written
!=sizeof(utf16_bom
) && WriteFile(hand
, (char*)utf16_bom
+written
,
1909 sizeof(utf16_bom
)-written
, &tmp
, NULL
))
1911 if (written
!= sizeof(utf16_bom
))
1913 WARN("error writting BOM\n");
1915 msvcrt_set_errno(GetLastError());
1916 return *MSVCRT__errno();
1920 else if (access
& GENERIC_READ
)
1921 oflags
= check_bom(hand
, oflags
, TRUE
);
1924 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1926 return *MSVCRT__errno();
1928 if (oflags
& MSVCRT__O_WTEXT
)
1929 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
|EF_UNK_UNICODE
;
1930 else if (oflags
& MSVCRT__O_U16TEXT
)
1931 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF16
;
1932 else if (oflags
& MSVCRT__O_U8TEXT
)
1933 msvcrt_get_ioinfo(*fd
)->exflag
|= EF_UTF8
;
1935 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1939 /*********************************************************************
1940 * _wsopen (MSVCRT.@)
1942 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
*path
, int oflags
, int shflags
, ... )
1947 if (oflags
& MSVCRT__O_CREAT
)
1951 __ms_va_start(ap
, shflags
);
1952 pmode
= va_arg(ap
, int);
1958 MSVCRT__wsopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1962 /*********************************************************************
1963 * _sopen_s (MSVCRT.@)
1965 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1967 MSVCRT_wchar_t
*pathW
;
1970 if(!MSVCRT_CHECK_PMT(path
&& (pathW
= msvcrt_wstrdupa(path
))))
1971 return MSVCRT_EINVAL
;
1973 ret
= MSVCRT__wsopen_s(fd
, pathW
, oflags
, shflags
, pmode
);
1978 /*********************************************************************
1981 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1986 if (oflags
& MSVCRT__O_CREAT
)
1990 __ms_va_start(ap
, shflags
);
1991 pmode
= va_arg(ap
, int);
1997 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
2001 /*********************************************************************
2004 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
2008 if (flags
& MSVCRT__O_CREAT
)
2011 __ms_va_start(ap
, flags
);
2012 pmode
= va_arg(ap
, int);
2014 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2017 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
2020 /*********************************************************************
2023 int CDECL
MSVCRT__wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
2027 if (flags
& MSVCRT__O_CREAT
)
2030 __ms_va_start(ap
, flags
);
2031 pmode
= va_arg(ap
, int);
2033 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
2036 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
2039 /*********************************************************************
2042 int CDECL
MSVCRT__creat(const char *path
, int flags
)
2044 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2045 return MSVCRT__open(path
, usedFlags
);
2048 /*********************************************************************
2049 * _wcreat (MSVCRT.@)
2051 int CDECL
MSVCRT__wcreat(const MSVCRT_wchar_t
*path
, int flags
)
2053 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
2054 return MSVCRT__wopen(path
, usedFlags
);
2057 /*********************************************************************
2058 * _open_osfhandle (MSVCRT.@)
2060 int CDECL
MSVCRT__open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
2064 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
2065 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2066 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
2067 * text - it never sets MSVCRT__O_BINARY.
2069 /* don't let split_oflags() decide the mode if no mode is passed */
2070 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
2071 oflags
|= MSVCRT__O_BINARY
;
2073 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
2074 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
2078 /*********************************************************************
2081 int CDECL
MSVCRT__rmtmp(void)
2083 int num_removed
= 0, i
;
2087 for (i
= 3; i
< MSVCRT_stream_idx
; i
++) {
2088 file
= msvcrt_get_file(i
);
2090 if (file
->_tmpfname
)
2092 MSVCRT_fclose(file
);
2099 TRACE(":removed (%d) temp files\n",num_removed
);
2103 /*********************************************************************
2106 * When reading \r as last character in text mode, read() positions
2107 * the file pointer on the \r character while getc() goes on to
2110 static int read_i(int fd
, void *buf
, unsigned int count
)
2113 char *bufstart
= buf
;
2114 HANDLE hand
= msvcrt_fdtoh(fd
);
2115 ioinfo
*fdinfo
= msvcrt_get_ioinfo(fd
);
2120 if (fdinfo
->wxflag
& WX_READEOF
) {
2121 fdinfo
->wxflag
|= WX_ATEOF
;
2122 TRACE("already at EOF, returning 0\n");
2125 /* Don't trace small reads, it gets *very* annoying */
2127 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2128 if (hand
== INVALID_HANDLE_VALUE
)
2131 /* Reading single bytes in O_TEXT mode makes things slow
2132 * So read big chunks
2134 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
2136 if (count
!= 0 && num_read
== 0)
2138 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2139 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
2141 else if (fdinfo
->wxflag
& WX_TEXT
)
2144 if (bufstart
[num_read
-1] == '\r')
2148 fdinfo
->wxflag
&= ~WX_READCR
;
2149 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
2153 fdinfo
->wxflag
|= WX_READCR
;
2158 fdinfo
->wxflag
&= ~WX_READCR
;
2159 for (i
=0, j
=0; i
<num_read
; i
++)
2161 /* in text mode, a ctrl-z signals EOF */
2162 if (bufstart
[i
] == 0x1a)
2164 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2165 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
2168 /* in text mode, strip \r if followed by \n.
2169 * BUG: should save state across calls somehow, so CR LF that
2170 * straddles buffer boundary gets recognized properly?
2172 if ((bufstart
[i
] != '\r')
2173 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
2174 bufstart
[j
++] = bufstart
[i
];
2181 if (GetLastError() == ERROR_BROKEN_PIPE
)
2183 TRACE(":end-of-pipe\n");
2184 fdinfo
->wxflag
|= (WX_ATEOF
|WX_READEOF
);
2189 TRACE(":failed-last error (%d)\n",GetLastError());
2195 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
2199 /*********************************************************************
2202 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
2205 num_read
= read_i(fd
, buf
, count
);
2209 /*********************************************************************
2210 * _setmode (MSVCRT.@)
2212 int CDECL
MSVCRT__setmode(int fd
,int mode
)
2214 int ret
= msvcrt_get_ioinfo(fd
)->wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
2215 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
2216 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
2217 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
2218 msvcrt_get_ioinfo(fd
)->wxflag
|= WX_TEXT
;
2220 msvcrt_get_ioinfo(fd
)->wxflag
&= ~WX_TEXT
;
2224 /*********************************************************************
2225 * _stat64 (MSVCRT.@)
2227 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
2230 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2231 unsigned short mode
= ALL_S_IREAD
;
2234 TRACE(":file (%s) buf(%p)\n",path
,buf
);
2236 plen
= strlen(path
);
2237 while (plen
&& path
[plen
-1]==' ')
2240 if (plen
&& (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2242 *MSVCRT__errno() = MSVCRT_ENOENT
;
2246 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
2248 TRACE("failed (%d)\n",GetLastError());
2249 *MSVCRT__errno() = MSVCRT_ENOENT
;
2253 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2255 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
2256 Bon 011120: This FIXME seems incorrect
2257 Also a letter as first char isn't enough to be classified
2260 if (isalpha(*path
)&& (*(path
+1)==':'))
2261 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
2263 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2265 /* Dir, or regular file? */
2266 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2267 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2270 mode
|= MSVCRT__S_IFREG
;
2272 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2274 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
2275 (tolower(path
[plen
-3]) << 16);
2276 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
2277 mode
|= ALL_S_IEXEC
;
2281 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2282 mode
|= ALL_S_IWRITE
;
2284 buf
->st_mode
= mode
;
2286 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2287 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2289 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2290 buf
->st_mtime
= buf
->st_ctime
= dw
;
2291 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2292 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2293 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2297 /*********************************************************************
2298 * _stati64 (MSVCRT.@)
2300 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2303 struct MSVCRT__stat64 buf64
;
2305 ret
= MSVCRT_stat64(path
, &buf64
);
2307 msvcrt_stat64_to_stati64(&buf64
, buf
);
2311 /*********************************************************************
2314 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2316 struct MSVCRT__stat64 buf64
;
2318 ret
= MSVCRT_stat64( path
, &buf64
);
2320 msvcrt_stat64_to_stat(&buf64
, buf
);
2324 /*********************************************************************
2325 * _wstat64 (MSVCRT.@)
2327 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2330 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2331 unsigned short mode
= ALL_S_IREAD
;
2334 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2336 plen
= strlenW(path
);
2337 while (plen
&& path
[plen
-1]==' ')
2340 if(plen
&& (path
[plen
-1]=='\\' || path
[plen
-1]=='/'))
2342 *MSVCRT__errno() = MSVCRT_ENOENT
;
2346 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2348 TRACE("failed (%d)\n",GetLastError());
2349 *MSVCRT__errno() = MSVCRT_ENOENT
;
2353 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2355 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2356 if (MSVCRT_iswalpha(*path
))
2357 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2359 buf
->st_dev
= buf
->st_rdev
= MSVCRT__getdrive() - 1;
2361 /* Dir, or regular file? */
2362 if (hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
2363 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2366 mode
|= MSVCRT__S_IFREG
;
2368 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2370 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2371 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2372 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2373 mode
|= ALL_S_IEXEC
;
2377 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2378 mode
|= ALL_S_IWRITE
;
2380 buf
->st_mode
= mode
;
2382 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2383 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2385 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2386 buf
->st_mtime
= buf
->st_ctime
= dw
;
2387 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2388 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2389 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2393 /*********************************************************************
2394 * _wstati64 (MSVCRT.@)
2396 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2399 struct MSVCRT__stat64 buf64
;
2401 ret
= MSVCRT__wstat64(path
, &buf64
);
2403 msvcrt_stat64_to_stati64(&buf64
, buf
);
2407 /*********************************************************************
2410 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2413 struct MSVCRT__stat64 buf64
;
2415 ret
= MSVCRT__wstat64( path
, &buf64
);
2416 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2420 /*********************************************************************
2423 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
2425 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2428 /*********************************************************************
2429 * _telli64 (MSVCRT.@)
2431 __int64 CDECL
_telli64(int fd
)
2433 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2436 /*********************************************************************
2437 * _tempnam (MSVCRT.@)
2439 char * CDECL
MSVCRT__tempnam(const char *dir
, const char *prefix
)
2441 char tmpbuf
[MAX_PATH
];
2442 const char *tmp_dir
= MSVCRT_getenv("TMP");
2444 if (tmp_dir
) dir
= tmp_dir
;
2446 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2447 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2449 TRACE("got name (%s)\n",tmpbuf
);
2450 DeleteFileA(tmpbuf
);
2451 return MSVCRT__strdup(tmpbuf
);
2453 TRACE("failed (%d)\n",GetLastError());
2457 /*********************************************************************
2458 * _wtempnam (MSVCRT.@)
2460 MSVCRT_wchar_t
* CDECL
MSVCRT__wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2462 static const MSVCRT_wchar_t tmpW
[] = {'T','M','P',0};
2463 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2464 const MSVCRT_wchar_t
*tmp_dir
= MSVCRT__wgetenv(tmpW
);
2466 if (tmp_dir
) dir
= tmp_dir
;
2468 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2469 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2471 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2472 DeleteFileW(tmpbuf
);
2473 return MSVCRT__wcsdup(tmpbuf
);
2475 TRACE("failed (%d)\n",GetLastError());
2479 /*********************************************************************
2482 int CDECL
MSVCRT__umask(int umask
)
2484 int old_umask
= MSVCRT_umask
;
2485 TRACE("(%d)\n",umask
);
2486 MSVCRT_umask
= umask
;
2490 /*********************************************************************
2491 * _utime64 (MSVCRT.@)
2493 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2495 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2499 int retVal
= _futime64(fd
, t
);
2506 /*********************************************************************
2507 * _utime32 (MSVCRT.@)
2509 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2513 struct MSVCRT___utimbuf64 t64
;
2514 t64
.actime
= t
->actime
;
2515 t64
.modtime
= t
->modtime
;
2516 return _utime64( path
, &t64
);
2519 return _utime64( path
, NULL
);
2522 /*********************************************************************
2526 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2528 return _utime64( path
, t
);
2531 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2533 return _utime32( path
, t
);
2537 /*********************************************************************
2538 * _wutime64 (MSVCRT.@)
2540 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2542 int fd
= MSVCRT__wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2546 int retVal
= _futime64(fd
, t
);
2553 /*********************************************************************
2554 * _wutime32 (MSVCRT.@)
2556 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2560 struct MSVCRT___utimbuf64 t64
;
2561 t64
.actime
= t
->actime
;
2562 t64
.modtime
= t
->modtime
;
2563 return _wutime64( path
, &t64
);
2566 return _wutime64( path
, NULL
);
2569 /*********************************************************************
2570 * _wutime (MSVCRT.@)
2573 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2575 return _wutime64( path
, t
);
2578 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2580 return _wutime32( path
, t
);
2584 /*********************************************************************
2587 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2590 ioinfo
*info
= msvcrt_get_ioinfo(fd
);
2591 HANDLE hand
= info
->handle
;
2593 /* Don't trace small writes, it gets *very* annoying */
2596 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2598 if (hand
== INVALID_HANDLE_VALUE
)
2600 *MSVCRT__errno() = MSVCRT_EBADF
;
2604 if (((info
->exflag
&EF_UTF8
) || (info
->exflag
&EF_UTF16
)) && count
&1)
2606 *MSVCRT__errno() = MSVCRT_EINVAL
;
2610 /* If appending, go to EOF */
2611 if (info
->wxflag
& WX_APPEND
)
2612 MSVCRT__lseek(fd
, 0, FILE_END
);
2614 if (!(info
->wxflag
& WX_TEXT
))
2616 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2617 && (num_written
== count
))
2619 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2620 hand
, GetLastError());
2621 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2625 unsigned int i
, j
, nr_lf
, size
;
2628 const char *s
= buf
, *buf_start
= buf
;
2630 if (!(info
->exflag
& (EF_UTF8
|EF_UTF16
)))
2632 /* find number of \n */
2633 for (nr_lf
=0, i
=0; i
<count
; i
++)
2639 if ((q
= p
= MSVCRT_malloc(size
)))
2641 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2650 FIXME("Malloc failed\n");
2662 else if (info
->exflag
& EF_UTF16
)
2664 for (nr_lf
=0, i
=0; i
<count
; i
+=2)
2665 if (s
[i
]=='\n' && s
[i
+1]==0)
2670 if ((q
= p
= MSVCRT_malloc(size
)))
2672 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2674 if (s
[i
]=='\n' && s
[i
+1]==0)
2685 FIXME("Malloc failed\n");
2701 for(nr_lf
=0, i
=0; i
<count
; i
+=2)
2702 if (s
[i
]=='\n' && s
[i
+1]==0)
2705 conv_len
= WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)buf
, count
/2, NULL
, 0, NULL
, NULL
);
2707 msvcrt_set_errno(GetLastError());
2712 size
= conv_len
+nr_lf
;
2713 if((p
= MSVCRT_malloc(count
+nr_lf
*2+size
)))
2715 for (s
=buf
, i
=0, j
=0; i
<count
; i
++)
2717 if (s
[i
]=='\n' && s
[i
+1]==0)
2725 q
= p
+count
+nr_lf
*2;
2726 WideCharToMultiByte(CP_UTF8
, 0, (WCHAR
*)p
, count
/2+nr_lf
,
2727 p
+count
+nr_lf
*2, conv_len
+nr_lf
, NULL
, NULL
);
2731 FIXME("Malloc failed\n");
2738 if (!WriteFile(hand
, q
, size
, &num_written
, NULL
))
2742 if (num_written
!= size
)
2744 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2745 fd
, hand
, GetLastError(), num_written
);
2746 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2747 return s
- buf_start
;
2755 /*********************************************************************
2758 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2762 MSVCRT__lock_file(file
);
2763 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2764 if (len
== sizeof(val
)) {
2765 MSVCRT__unlock_file(file
);
2769 file
->_flag
|= MSVCRT__IOERR
;
2770 MSVCRT__unlock_file(file
);
2774 /*********************************************************************
2777 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2781 MSVCRT__lock_file(file
);
2783 MSVCRT_free(file
->_tmpfname
);
2784 file
->_tmpfname
= NULL
;
2785 /* flush stdio buffers */
2786 if(file
->_flag
& MSVCRT__IOWRT
)
2787 MSVCRT_fflush(file
);
2788 if(file
->_flag
& MSVCRT__IOMYBUF
)
2789 MSVCRT_free(file
->_base
);
2791 r
=MSVCRT__close(file
->_file
);
2794 MSVCRT__unlock_file(file
);
2796 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2799 /*********************************************************************
2802 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2804 return file
->_flag
& MSVCRT__IOEOF
;
2807 /*********************************************************************
2810 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2812 return file
->_flag
& MSVCRT__IOERR
;
2815 /*********************************************************************
2816 * _filbuf (MSVCRT.@)
2818 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2821 MSVCRT__lock_file(file
);
2823 /* Allocate buffer if needed */
2824 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
))
2825 msvcrt_alloc_buffer(file
);
2827 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2828 if(file
->_flag
& MSVCRT__IORW
)
2829 file
->_flag
|= MSVCRT__IOREAD
;
2831 MSVCRT__unlock_file(file
);
2836 if(file
->_flag
& MSVCRT__IONBF
) {
2838 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2839 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2840 MSVCRT__unlock_file(file
);
2844 MSVCRT__unlock_file(file
);
2847 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2849 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2851 MSVCRT__unlock_file(file
);
2856 file
->_ptr
= file
->_base
+1;
2857 c
= *(unsigned char *)file
->_base
;
2858 MSVCRT__unlock_file(file
);
2863 /*********************************************************************
2866 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2871 MSVCRT__lock_file(file
);
2874 i
= (unsigned char *)file
->_ptr
++;
2877 j
= MSVCRT__filbuf(file
);
2879 MSVCRT__unlock_file(file
);
2883 /*********************************************************************
2884 * _fgetchar (MSVCRT.@)
2886 int CDECL
MSVCRT__fgetchar(void)
2888 return MSVCRT_fgetc(MSVCRT_stdin
);
2891 /*********************************************************************
2894 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2896 int cc
= MSVCRT_EOF
;
2897 char * buf_start
= s
;
2899 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2900 file
,file
->_file
,s
,size
);
2902 MSVCRT__lock_file(file
);
2904 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2909 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2911 TRACE(":nothing read\n");
2912 MSVCRT__unlock_file(file
);
2915 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2918 TRACE(":got %s\n", debugstr_a(buf_start
));
2919 MSVCRT__unlock_file(file
);
2923 /*********************************************************************
2926 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2927 * the CR from CR/LF combinations
2929 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2933 MSVCRT__lock_file(file
);
2934 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
))
2941 for(i
=0; i
<sizeof(wc
); i
++)
2951 j
= MSVCRT__filbuf(file
);
2954 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2957 MSVCRT__unlock_file(file
);
2964 MSVCRT__unlock_file(file
);
2968 c
= MSVCRT_fgetc(file
);
2969 if ((get_locinfo()->mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2971 FIXME("Treat Multibyte characters\n");
2974 MSVCRT__unlock_file(file
);
2975 if (c
== MSVCRT_EOF
)
2978 return (MSVCRT_wint_t
)c
;
2981 /*********************************************************************
2984 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2991 MSVCRT__lock_file(file
);
2992 for (j
=0; j
<sizeof(int); j
++) {
2993 k
= MSVCRT_fgetc(file
);
2994 if (k
== MSVCRT_EOF
) {
2995 file
->_flag
|= MSVCRT__IOEOF
;
2996 MSVCRT__unlock_file(file
);
3002 MSVCRT__unlock_file(file
);
3006 /*********************************************************************
3009 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
3011 return MSVCRT_fgetwc(file
);
3014 /*********************************************************************
3015 * _fgetwchar (MSVCRT.@)
3017 MSVCRT_wint_t CDECL
MSVCRT__fgetwchar(void)
3019 return MSVCRT_fgetwc(MSVCRT_stdin
);
3022 /*********************************************************************
3023 * getwchar (MSVCRT.@)
3025 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
3027 return MSVCRT__fgetwchar();
3030 /*********************************************************************
3033 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
3035 int cc
= MSVCRT_WEOF
;
3036 MSVCRT_wchar_t
* buf_start
= s
;
3038 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3039 file
,file
->_file
,s
,size
);
3041 MSVCRT__lock_file(file
);
3043 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
3048 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
3050 TRACE(":nothing read\n");
3051 MSVCRT__unlock_file(file
);
3054 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
3057 TRACE(":got %s\n", debugstr_w(buf_start
));
3058 MSVCRT__unlock_file(file
);
3062 /*********************************************************************
3065 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3067 MSVCRT_size_t wrcnt
=size
* nmemb
;
3072 MSVCRT__lock_file(file
);
3074 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
3075 memcpy(file
->_ptr
, ptr
, pcnt
);
3080 ptr
= (const char*)ptr
+ pcnt
;
3081 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3082 if(file
->_flag
& MSVCRT__IORW
) {
3083 file
->_flag
|= MSVCRT__IOWRT
;
3085 MSVCRT__unlock_file(file
);
3092 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
))
3093 msvcrt_alloc_buffer(file
);
3096 res
=msvcrt_flush_buffer(file
);
3098 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
3101 file
->_flag
|= MSVCRT__IOERR
;
3104 written
+= pwritten
;
3108 MSVCRT__unlock_file(file
);
3109 return written
/ size
;
3112 /*********************************************************************
3115 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3117 MSVCRT_wchar_t mwc
=wc
;
3118 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
3123 /*********************************************************************
3124 * _fputwchar (MSVCRT.@)
3126 MSVCRT_wint_t CDECL
MSVCRT__fputwchar(MSVCRT_wint_t wc
)
3128 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
3131 /*********************************************************************
3132 * _wfsopen (MSVCRT.@)
3134 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
3137 int open_flags
, stream_flags
, fd
;
3139 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
3141 /* map mode string to open() flags. "man fopen" for possibilities. */
3142 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
3146 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3149 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
3151 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
3158 TRACE(":got (%p)\n",file
);
3159 if (fd
>= 0 && !file
)
3165 /*********************************************************************
3166 * _fsopen (MSVCRT.@)
3168 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
3171 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3173 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
3174 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3175 *MSVCRT__errno() = MSVCRT_EINVAL
;
3178 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3181 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
3182 *MSVCRT__errno() = MSVCRT_EINVAL
;
3186 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
3193 /*********************************************************************
3196 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
3198 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3201 /*********************************************************************
3202 * fopen_s (MSVCRT.@)
3204 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
3205 const char *filename
, const char *mode
)
3207 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3208 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3209 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3211 *pFile
= MSVCRT_fopen(filename
, mode
);
3214 return *MSVCRT__errno();
3218 /*********************************************************************
3219 * _wfopen (MSVCRT.@)
3221 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
3223 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
3226 /*********************************************************************
3227 * _wfopen_s (MSVCRT.@)
3229 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
3230 const MSVCRT_wchar_t
*mode
)
3232 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3233 if (!MSVCRT_CHECK_PMT(filename
!= NULL
)) return MSVCRT_EINVAL
;
3234 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3236 *pFile
= MSVCRT__wfopen(filename
, mode
);
3239 return *MSVCRT__errno();
3243 /*********************************************************************
3244 * _flsbuf (MSVCRT.@)
3246 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
3248 /* Flush output buffer */
3249 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3250 msvcrt_alloc_buffer(file
);
3252 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
3253 if(file
->_flag
& MSVCRT__IORW
)
3254 file
->_flag
|= MSVCRT__IOWRT
;
3262 res
= msvcrt_flush_buffer(file
);
3266 res
= msvcrt_flush_buffer(file
);
3269 return res
? res
: c
&0xff;
3273 /* set _cnt to 0 for unbuffered FILEs */
3275 len
= MSVCRT__write(file
->_file
, &cc
, 1);
3278 file
->_flag
|= MSVCRT__IOERR
;
3283 /*********************************************************************
3286 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
3290 MSVCRT__lock_file(file
);
3296 res
= msvcrt_flush_buffer(file
);
3297 MSVCRT__unlock_file(file
);
3298 return res
? res
: c
;
3301 MSVCRT__unlock_file(file
);
3305 res
= MSVCRT__flsbuf(c
, file
);
3306 MSVCRT__unlock_file(file
);
3311 /*********************************************************************
3312 * _fputchar (MSVCRT.@)
3314 int CDECL
MSVCRT__fputchar(int c
)
3316 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3319 /*********************************************************************
3322 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
3324 MSVCRT_size_t rcnt
=size
* nmemb
;
3325 MSVCRT_size_t read
=0;
3331 MSVCRT__lock_file(file
);
3333 /* first buffered data */
3335 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
3336 memcpy(ptr
, file
->_ptr
, pcnt
);
3341 ptr
= (char*)ptr
+ pcnt
;
3342 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
3343 if(file
->_flag
& MSVCRT__IORW
) {
3344 file
->_flag
|= MSVCRT__IOREAD
;
3346 MSVCRT__unlock_file(file
);
3353 /* Fill the buffer on small reads.
3354 * TODO: Use a better buffering strategy.
3356 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
3357 if (file
->_bufsiz
== 0) {
3358 msvcrt_alloc_buffer(file
);
3360 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
3361 file
->_ptr
= file
->_base
;
3362 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
3363 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3364 if (i
> 0 && i
< file
->_cnt
) {
3365 msvcrt_get_ioinfo(file
->_file
)->wxflag
&= ~WX_ATEOF
;
3366 file
->_flag
&= ~MSVCRT__IOEOF
;
3369 memcpy(ptr
, file
->_ptr
, i
);
3374 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
3378 ptr
= (char *)ptr
+i
;
3379 /* expose feof condition in the flags
3380 * MFC tests file->_flag for feof, and doesn't call feof())
3382 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_ATEOF
)
3383 file
->_flag
|= MSVCRT__IOEOF
;
3386 file
->_flag
|= MSVCRT__IOERR
;
3393 MSVCRT__unlock_file(file
);
3397 /*********************************************************************
3398 * _wfreopen (MSVCRT.@)
3401 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
3405 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
3408 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
3412 MSVCRT_fclose(file
);
3413 file
= MSVCRT__wfsopen(path
, mode
, MSVCRT__SH_DENYNO
);
3419 /*********************************************************************
3420 * _wfreopen_s (MSVCRT.@)
3422 int CDECL
MSVCRT__wfreopen_s(MSVCRT_FILE
** pFile
,
3423 const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
3425 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3426 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
3427 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3428 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3430 *pFile
= MSVCRT__wfreopen(path
, mode
, file
);
3433 return *MSVCRT__errno();
3437 /*********************************************************************
3438 * freopen (MSVCRT.@)
3441 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
3444 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
3446 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
3447 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
3453 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
3460 /*********************************************************************
3461 * freopen_s (MSVCRT.@)
3463 int CDECL
MSVCRT_freopen_s(MSVCRT_FILE
** pFile
,
3464 const char *path
, const char *mode
, MSVCRT_FILE
* file
)
3466 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
)) return MSVCRT_EINVAL
;
3467 if (!MSVCRT_CHECK_PMT(path
!= NULL
)) return MSVCRT_EINVAL
;
3468 if (!MSVCRT_CHECK_PMT(mode
!= NULL
)) return MSVCRT_EINVAL
;
3469 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3471 *pFile
= MSVCRT_freopen(path
, mode
, file
);
3474 return *MSVCRT__errno();
3478 /*********************************************************************
3479 * fsetpos (MSVCRT.@)
3481 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3485 MSVCRT__lock_file(file
);
3486 /* Note that all this has been lifted 'as is' from fseek */
3487 if(file
->_flag
& MSVCRT__IOWRT
)
3488 msvcrt_flush_buffer(file
);
3490 /* Discard buffered input */
3492 file
->_ptr
= file
->_base
;
3494 /* Reset direction of i/o */
3495 if(file
->_flag
& MSVCRT__IORW
) {
3496 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
3499 ret
= (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3500 MSVCRT__unlock_file(file
);
3504 /*********************************************************************
3505 * _ftelli64 (MSVCRT.@)
3507 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
3509 /* TODO: just call fgetpos and return lower half of result */
3513 MSVCRT__lock_file(file
);
3514 pos
= _telli64(file
->_file
);
3516 MSVCRT__unlock_file(file
);
3520 if( file
->_flag
& MSVCRT__IOWRT
) {
3521 off
= file
->_ptr
- file
->_base
;
3524 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3525 /* Black magic correction for CR removal */
3527 for (i
=0; i
<file
->_cnt
; i
++) {
3528 if (file
->_ptr
[i
] == '\n')
3531 /* Black magic when reading CR at buffer boundary*/
3532 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3538 MSVCRT__unlock_file(file
);
3542 /*********************************************************************
3545 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
3547 return MSVCRT__ftelli64(file
);
3550 /*********************************************************************
3551 * fgetpos (MSVCRT.@)
3553 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3557 MSVCRT__lock_file(file
);
3558 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
3560 MSVCRT__unlock_file(file
);
3564 if( file
->_flag
& MSVCRT__IOWRT
) {
3565 off
= file
->_ptr
- file
->_base
;
3568 if (msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
) {
3569 /* Black magic correction for CR removal */
3571 for (i
=0; i
<file
->_cnt
; i
++) {
3572 if (file
->_ptr
[i
] == '\n')
3575 /* Black magic when reading CR at buffer boundary*/
3576 if(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_READCR
)
3582 MSVCRT__unlock_file(file
);
3586 /*********************************************************************
3589 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
3591 MSVCRT_size_t len
= strlen(s
);
3594 MSVCRT__lock_file(file
);
3595 ret
= MSVCRT_fwrite(s
, sizeof(*s
), len
, file
) == len
? 0 : MSVCRT_EOF
;
3596 MSVCRT__unlock_file(file
);
3600 /*********************************************************************
3603 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
3605 MSVCRT_size_t i
, len
= strlenW(s
);
3608 MSVCRT__lock_file(file
);
3609 if (!(msvcrt_get_ioinfo(file
->_file
)->wxflag
& WX_TEXT
)) {
3610 ret
= MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3611 MSVCRT__unlock_file(file
);
3614 for (i
=0; i
<len
; i
++) {
3615 if (((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
3616 || MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
) {
3617 MSVCRT__unlock_file(file
);
3622 MSVCRT__unlock_file(file
);
3626 /*********************************************************************
3627 * getchar (MSVCRT.@)
3629 int CDECL
MSVCRT_getchar(void)
3631 return MSVCRT_fgetc(MSVCRT_stdin
);
3634 /*********************************************************************
3637 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
3639 return MSVCRT_fgetc(file
);
3642 /*********************************************************************
3645 char * CDECL
MSVCRT_gets(char *buf
)
3648 char * buf_start
= buf
;
3650 MSVCRT__lock_file(MSVCRT_stdin
);
3651 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
3652 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
3653 if(cc
!= '\r') *buf
++ = (char)cc
;
3657 TRACE("got '%s'\n", buf_start
);
3658 MSVCRT__unlock_file(MSVCRT_stdin
);
3662 /*********************************************************************
3665 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
3668 MSVCRT_wchar_t
* ws
= buf
;
3670 MSVCRT__lock_file(MSVCRT_stdin
);
3671 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
3672 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3675 *buf
++ = (MSVCRT_wchar_t
)cc
;
3679 TRACE("got %s\n", debugstr_w(ws
));
3680 MSVCRT__unlock_file(MSVCRT_stdin
);
3684 /*********************************************************************
3687 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3689 return MSVCRT_fputc(c
, file
);
3692 /*********************************************************************
3693 * putchar (MSVCRT.@)
3695 int CDECL
MSVCRT_putchar(int c
)
3697 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3700 /*********************************************************************
3701 * _putwch (MSVCRT.@)
3703 int CDECL
MSVCRT__putwch(int c
)
3705 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
3708 /*********************************************************************
3711 int CDECL
MSVCRT_puts(const char *s
)
3713 MSVCRT_size_t len
= strlen(s
);
3716 MSVCRT__lock_file(MSVCRT_stdout
);
3717 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
3718 MSVCRT__unlock_file(MSVCRT_stdout
);
3722 ret
= MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3723 MSVCRT__unlock_file(MSVCRT_stdout
);
3727 /*********************************************************************
3730 int CDECL
MSVCRT__putws(const MSVCRT_wchar_t
*s
)
3732 static const MSVCRT_wchar_t nl
= '\n';
3733 MSVCRT_size_t len
= strlenW(s
);
3736 MSVCRT__lock_file(MSVCRT_stdout
);
3737 if(MSVCRT_fwrite(s
, sizeof(*s
), len
, MSVCRT_stdout
) != len
) {
3738 MSVCRT__unlock_file(MSVCRT_stdout
);
3742 ret
= MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3743 MSVCRT__unlock_file(MSVCRT_stdout
);
3747 /*********************************************************************
3750 int CDECL
MSVCRT_remove(const char *path
)
3752 TRACE("(%s)\n",path
);
3753 if (DeleteFileA(path
))
3755 TRACE(":failed (%d)\n",GetLastError());
3756 msvcrt_set_errno(GetLastError());
3760 /*********************************************************************
3761 * _wremove (MSVCRT.@)
3763 int CDECL
MSVCRT__wremove(const MSVCRT_wchar_t
*path
)
3765 TRACE("(%s)\n",debugstr_w(path
));
3766 if (DeleteFileW(path
))
3768 TRACE(":failed (%d)\n",GetLastError());
3769 msvcrt_set_errno(GetLastError());
3773 /*********************************************************************
3776 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3778 TRACE(":from %s to %s\n",oldpath
,newpath
);
3779 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3781 TRACE(":failed (%d)\n",GetLastError());
3782 msvcrt_set_errno(GetLastError());
3786 /*********************************************************************
3787 * _wrename (MSVCRT.@)
3789 int CDECL
MSVCRT__wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3791 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3792 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3794 TRACE(":failed (%d)\n",GetLastError());
3795 msvcrt_set_errno(GetLastError());
3799 /*********************************************************************
3800 * setvbuf (MSVCRT.@)
3802 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3804 MSVCRT__lock_file(file
);
3806 MSVCRT_free(file
->_base
);
3810 if(mode
== MSVCRT__IOFBF
) {
3811 file
->_flag
&= ~MSVCRT__IONBF
;
3812 file
->_base
= file
->_ptr
= buf
;
3814 file
->_bufsiz
= size
;
3817 file
->_flag
|= MSVCRT__IONBF
;
3819 MSVCRT__unlock_file(file
);
3823 /*********************************************************************
3826 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3828 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3831 /*********************************************************************
3834 char * CDECL
MSVCRT_tmpnam(char *s
)
3841 thread_data_t
*data
= msvcrt_get_thread_data();
3843 if(!data
->tmpnam_buffer
)
3844 data
->tmpnam_buffer
= MSVCRT_malloc(MAX_PATH
);
3846 s
= data
->tmpnam_buffer
;
3849 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3850 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3851 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3853 size
= msvcrt_int_to_base32(tmpnam_unique
++, tmpstr
);
3854 memcpy(p
, tmpstr
, size
);
3856 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3857 GetLastError() == ERROR_FILE_NOT_FOUND
)
3863 /*********************************************************************
3864 * _wtmpnam (MSVCRT.@)
3866 MSVCRT_wchar_t
* CDECL
MSVCRT_wtmpnam(MSVCRT_wchar_t
*s
)
3868 static const MSVCRT_wchar_t format
[] = {'\\','s','%','s','.',0};
3869 MSVCRT_wchar_t tmpstr
[16];
3873 thread_data_t
*data
= msvcrt_get_thread_data();
3875 if(!data
->wtmpnam_buffer
)
3876 data
->wtmpnam_buffer
= MSVCRT_malloc(sizeof(MSVCRT_wchar_t
[MAX_PATH
]));
3878 s
= data
->wtmpnam_buffer
;
3881 msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr
);
3882 p
= s
+ MSVCRT__snwprintf(s
, MAX_PATH
, format
, tmpstr
);
3883 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3885 size
= msvcrt_int_to_base32_w(tmpnam_unique
++, tmpstr
);
3886 memcpy(p
, tmpstr
, size
*sizeof(MSVCRT_wchar_t
));
3888 if (GetFileAttributesW(s
) == INVALID_FILE_ATTRIBUTES
&&
3889 GetLastError() == ERROR_FILE_NOT_FOUND
)
3895 /*********************************************************************
3896 * tmpfile (MSVCRT.@)
3898 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3900 char *filename
= MSVCRT_tmpnam(NULL
);
3902 MSVCRT_FILE
* file
= NULL
;
3905 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
,
3906 MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
3907 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3909 if (msvcrt_init_fp(file
, fd
, MSVCRT__IORW
) == -1)
3914 else file
->_tmpfname
= MSVCRT__strdup(filename
);
3917 if(fd
!= -1 && !file
)
3923 /*********************************************************************
3924 * tmpfile_s (MSVCRT.@)
3926 int CDECL
MSVCRT_tmpfile_s(MSVCRT_FILE
** file
)
3928 if (!MSVCRT_CHECK_PMT(file
!= NULL
)) return MSVCRT_EINVAL
;
3930 *file
= MSVCRT_tmpfile();
3934 static int puts_clbk_file_a(void *file
, int len
, const char *str
)
3936 return MSVCRT_fwrite(str
, sizeof(char), len
, file
);
3939 static int puts_clbk_file_w(void *file
, int len
, const MSVCRT_wchar_t
*str
)
3941 return MSVCRT_fwrite(str
, sizeof(MSVCRT_wchar_t
), len
, file
);
3944 /*********************************************************************
3945 * vfprintf (MSVCRT.@)
3947 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3951 MSVCRT__lock_file(file
);
3952 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
3953 MSVCRT__unlock_file(file
);
3958 /*********************************************************************
3959 * vfprintf_s (MSVCRT.@)
3961 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3965 if(!MSVCRT_CHECK_PMT(file
!= NULL
)) return -1;
3967 MSVCRT__lock_file(file
);
3968 ret
= pf_printf_a(puts_clbk_file_a
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
3969 MSVCRT__unlock_file(file
);
3974 /*********************************************************************
3975 * vfwprintf (MSVCRT.@)
3977 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3981 MSVCRT__lock_file(file
);
3982 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
3983 MSVCRT__unlock_file(file
);
3988 /*********************************************************************
3989 * vfwprintf_s (MSVCRT.@)
3991 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3995 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
3997 MSVCRT__lock_file(file
);
3998 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, NULL
, FALSE
, TRUE
, arg_clbk_valist
, NULL
, &valist
);
3999 MSVCRT__unlock_file(file
);
4004 /*********************************************************************
4005 * _vfwprintf_l (MSVCRT.@)
4007 int CDECL
MSVCRT__vfwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
,
4008 MSVCRT__locale_t locale
, __ms_va_list valist
)
4012 if (!MSVCRT_CHECK_PMT( file
!= NULL
)) return -1;
4014 MSVCRT__lock_file(file
);
4015 ret
= pf_printf_w(puts_clbk_file_w
, file
, format
, locale
, FALSE
, FALSE
, arg_clbk_valist
, NULL
, &valist
);
4016 MSVCRT__unlock_file(file
);
4021 /*********************************************************************
4022 * vprintf (MSVCRT.@)
4024 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
4026 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
4029 /*********************************************************************
4030 * vprintf_s (MSVCRT.@)
4032 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
4034 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
4037 /*********************************************************************
4038 * vwprintf (MSVCRT.@)
4040 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4042 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
4045 /*********************************************************************
4046 * vwprintf_s (MSVCRT.@)
4048 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
4050 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
4053 /*********************************************************************
4054 * fprintf (MSVCRT.@)
4056 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
4058 __ms_va_list valist
;
4060 __ms_va_start(valist
, format
);
4061 res
= MSVCRT_vfprintf(file
, format
, valist
);
4062 __ms_va_end(valist
);
4066 /*********************************************************************
4067 * fprintf_s (MSVCRT.@)
4069 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
4071 __ms_va_list valist
;
4073 __ms_va_start(valist
, format
);
4074 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
4075 __ms_va_end(valist
);
4079 /*********************************************************************
4080 * fwprintf (MSVCRT.@)
4082 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4084 __ms_va_list valist
;
4086 __ms_va_start(valist
, format
);
4087 res
= MSVCRT_vfwprintf(file
, format
, valist
);
4088 __ms_va_end(valist
);
4092 /*********************************************************************
4093 * fwprintf_s (MSVCRT.@)
4095 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
4097 __ms_va_list valist
;
4099 __ms_va_start(valist
, format
);
4100 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
4101 __ms_va_end(valist
);
4105 /*********************************************************************
4106 * _fwprintf_l (MSVCRT.@)
4108 int CDECL
MSVCRT__fwprintf_l(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, ...)
4110 __ms_va_list valist
;
4112 __ms_va_start(valist
, locale
);
4113 res
= MSVCRT__vfwprintf_l(file
, format
, locale
, valist
);
4114 __ms_va_end(valist
);
4118 /*********************************************************************
4121 int CDECL
MSVCRT_printf(const char *format
, ...)
4123 __ms_va_list valist
;
4125 __ms_va_start(valist
, format
);
4126 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
4127 __ms_va_end(valist
);
4131 /*********************************************************************
4132 * printf_s (MSVCRT.@)
4134 int CDECL
MSVCRT_printf_s(const char *format
, ...)
4136 __ms_va_list valist
;
4138 __ms_va_start(valist
, format
);
4139 res
= MSVCRT_vprintf_s(format
, valist
);
4140 __ms_va_end(valist
);
4144 /*********************************************************************
4147 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
4149 if (c
== MSVCRT_EOF
)
4152 MSVCRT__lock_file(file
);
4153 if(file
->_bufsiz
== 0) {
4154 msvcrt_alloc_buffer(file
);
4157 if(file
->_ptr
>file
->_base
) {
4161 MSVCRT_clearerr(file
);
4162 MSVCRT__unlock_file(file
);
4166 MSVCRT__unlock_file(file
);
4170 /*********************************************************************
4171 * ungetwc (MSVCRT.@)
4173 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
4175 MSVCRT_wchar_t mwc
= wc
;
4176 unsigned char * pp
= (unsigned char *)&mwc
;
4179 if (wc
== MSVCRT_WEOF
)
4182 MSVCRT__lock_file(file
);
4183 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
4184 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
)) {
4185 MSVCRT__unlock_file(file
);
4190 MSVCRT__unlock_file(file
);
4194 /*********************************************************************
4195 * wprintf (MSVCRT.@)
4197 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
4199 __ms_va_list valist
;
4201 __ms_va_start(valist
, format
);
4202 res
= MSVCRT_vwprintf(format
, valist
);
4203 __ms_va_end(valist
);
4207 /*********************************************************************
4208 * wprintf_s (MSVCRT.@)
4210 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
4212 __ms_va_list valist
;
4214 __ms_va_start(valist
, format
);
4215 res
= MSVCRT_vwprintf_s(format
, valist
);
4216 __ms_va_end(valist
);
4220 /*********************************************************************
4221 * _getmaxstdio (MSVCRT.@)
4223 int CDECL
MSVCRT__getmaxstdio(void)
4225 return MSVCRT_max_streams
;
4228 /*********************************************************************
4229 * _setmaxstdio (MSVCRT.@)
4231 int CDECL
MSVCRT__setmaxstdio(int newmax
)
4233 TRACE("%d\n", newmax
);
4235 if(newmax
<_IOB_ENTRIES
|| newmax
>MSVCRT_MAX_FILES
|| newmax
<MSVCRT_stream_idx
)
4238 MSVCRT_max_streams
= newmax
;
4239 return MSVCRT_max_streams
;