ReactOS 0.4.16-dev-2104-gb84fa49
file.c
Go to the documentation of this file.
1/*
2 * msvcrt.dll file functions
3 *
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
10 *
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.
15 *
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.
20 *
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
24 *
25 */
26
27#include <direct.h>
28#include <fcntl.h>
29#include <io.h>
30#include <share.h>
31#include <stdarg.h>
32#include <sys/locking.h>
33#include <sys/types.h>
34#include <sys/utime.h>
35#include <time.h>
36#include <limits.h>
37
38#include "windef.h"
39#include "winbase.h"
40#include "wincon.h"
41#include "winternl.h"
42#include "winnls.h"
43#include "msvcrt.h"
44#include "mtdll.h"
45#include "wine/asm.h"
46#include "wine/debug.h"
47
49
50#undef _fstat
51#undef _fstati64
52#undef _stat
53#undef _stati64
54#undef _wstat
55#undef _wstati64
56#undef _fstat32
57#undef _fstat32i64
58#undef _stat32i64
59#undef _stat32
60#undef _wstat32
61#undef _wstat32i64
62#undef _fstat64i32
63#undef _fstat64
64#undef _stat64
65#undef _stat64i32
66#undef _wstat64i32
67#undef _wstat64
68int __cdecl _wstat64(const wchar_t*, struct _stat64*);
69
70/* for stat mode, permissions apply to all,owner and group */
71#define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
72#define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
73#define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
74
75/* _access() bit flags FIXME: incomplete */
76#define MSVCRT_W_OK 0x02
77#define MSVCRT_R_OK 0x04
78
79/* values for wxflag in file descriptor */
80#define WX_OPEN 0x01
81#define WX_ATEOF 0x02
82#define WX_READNL 0x04 /* read started with \n */
83#define WX_PIPE 0x08
84#define WX_DONTINHERIT 0x10
85#define WX_APPEND 0x20
86#define WX_TTY 0x40
87#define WX_TEXT 0x80
88
89static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
90static char utf16_bom[2] = { 0xff, 0xfe };
91
92#define MSVCRT_INTERNAL_BUFSIZ 4096
93
95{
99};
100
101#if _MSVCR_VER >= 140
102
103#define MSVCRT_MAX_FILES 8192
104#define MSVCRT_FD_BLOCK_SIZE 64
105
106typedef struct {
107 CRITICAL_SECTION crit;
109 __int64 startpos;
110 unsigned char wxflag;
111 char textmode;
112 char lookahead[3];
113 unsigned int unicode : 1;
114 unsigned int utf8translations : 1;
115 unsigned int dbcsBufferUsed : 1;
116 char dbcsBuffer[MB_LEN_MAX];
117} ioinfo;
118
119/*********************************************************************
120 * __badioinfo (MSVCRT.@)
121 */
123#else
124
125#define MSVCRT_MAX_FILES 2048
126#define MSVCRT_FD_BLOCK_SIZE 32
127
128typedef struct {
130 unsigned char wxflag;
131 char lookahead[3];
134#if _MSVCR_VER >= 80
135 char textmode : 7;
136 char unicode : 1;
137 char pipech2[2];
138 __int64 startpos;
139 BOOL utf8translations;
140 char dbcsBuffer[1];
141 BOOL dbcsBufferUsed;
142#endif
143} ioinfo;
144
145/*********************************************************************
146 * __badioinfo (MSVCRT.@)
147 */
149#endif
150
151/*********************************************************************
152 * __pioinfo (MSVCRT.@)
153 * array of pointers to ioinfo arrays [32]
154 */
156
157#if _MSVCR_VER >= 80
158
159#if _MSVCR_VER >= 140
160static inline BOOL ioinfo_is_crit_init(ioinfo *info)
161{
162 return TRUE;
163}
164
165static inline void ioinfo_set_crit_init(ioinfo *info)
166{
167}
168#else
169static inline BOOL ioinfo_is_crit_init(ioinfo *info)
170{
171 return info->exflag & 1;
172}
173
174static inline void ioinfo_set_crit_init(ioinfo *info)
175{
176 info->exflag |= 1;
177}
178#endif
179
180static inline enum textmode ioinfo_get_textmode(ioinfo *info)
181{
182 return info->textmode;
183}
184
185static inline void ioinfo_set_textmode(ioinfo *info, enum textmode mode)
186{
187 info->textmode = mode;
188}
189
190static inline void ioinfo_set_unicode(ioinfo *info, BOOL unicode)
191{
192 info->unicode = !!unicode;
193}
194#else
195
196#define EF_UTF8 0x01
197#define EF_UTF16 0x02
198#define EF_CRIT_INIT 0x04
199#define EF_UNK_UNICODE 0x08
200
202{
203 return info->exflag & EF_CRIT_INIT;
204}
205
206static inline void ioinfo_set_crit_init(ioinfo *info)
207{
208 info->exflag |= EF_CRIT_INIT;
209}
210
212{
213 if (info->exflag & EF_UTF8)
214 return TEXTMODE_UTF8;
215 if (info->exflag & EF_UTF16)
216 return TEXTMODE_UTF16LE;
217 return TEXTMODE_ANSI;
218}
219
220static inline void ioinfo_set_textmode(ioinfo *info, enum textmode mode)
221{
222 info->exflag &= EF_CRIT_INIT | EF_UNK_UNICODE;
223 switch (mode)
224 {
225 case TEXTMODE_ANSI:
226 break;
227 case TEXTMODE_UTF8:
228 info->exflag |= EF_UTF8;
229 break;
230 case TEXTMODE_UTF16LE:
231 info->exflag |= EF_UTF16;
232 break;
233 }
234}
235
236static inline void ioinfo_set_unicode(ioinfo *info, BOOL unicode)
237{
238 if (unicode)
239 info->exflag |= EF_UNK_UNICODE;
240 else
241 info->exflag &= ~EF_UNK_UNICODE;
242}
243#endif
244
245typedef struct {
248} file_crit;
249
250#if _MSVCR_VER >= 140
252
253static FILE* iob_get_file(int i)
254{
255 return &MSVCRT__iob[i].file;
256}
257
259{
260 return &((file_crit*)f)->crit;
261}
262#else
264
265static FILE* iob_get_file(int i)
266{
267 return &MSVCRT__iob[i];
268}
269
271{
272 if (f < iob_get_file(0) || f >= iob_get_file(_IOB_ENTRIES))
273 return &((file_crit*)f)->crit;
274 return NULL;
275}
276#endif
277
280
281/* INTERNAL: process umask */
282static int MSVCRT_umask = 0;
283
284/* INTERNAL: static data for tmpnam and _wtmpname functions */
287
288#define TOUL(x) (ULONGLONG)(x)
289static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
290static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
291static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
292static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
293
294/* This critical section protects the MSVCRT_fstreams table
295 * and MSVCRT_stream_idx from race conditions. It also
296 * protects fd critical sections creation code.
297 */
300{
301 0, 0, &MSVCRT_file_cs,
303 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
304};
305static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
306#define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
307#define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
308
309static void msvcrt_stat64_to_stat(const struct _stat64 *buf64, struct _stat *buf)
310{
311 buf->st_dev = buf64->st_dev;
312 buf->st_ino = buf64->st_ino;
313 buf->st_mode = buf64->st_mode;
314 buf->st_nlink = buf64->st_nlink;
315 buf->st_uid = buf64->st_uid;
316 buf->st_gid = buf64->st_gid;
317 buf->st_rdev = buf64->st_rdev;
318 buf->st_size = buf64->st_size;
319 buf->st_atime = buf64->st_atime;
320 buf->st_mtime = buf64->st_mtime;
321 buf->st_ctime = buf64->st_ctime;
322}
323
324static void msvcrt_stat64_to_stati64(const struct _stat64 *buf64, struct _stati64 *buf)
325{
326 buf->st_dev = buf64->st_dev;
327 buf->st_ino = buf64->st_ino;
328 buf->st_mode = buf64->st_mode;
329 buf->st_nlink = buf64->st_nlink;
330 buf->st_uid = buf64->st_uid;
331 buf->st_gid = buf64->st_gid;
332 buf->st_rdev = buf64->st_rdev;
333 buf->st_size = buf64->st_size;
334 buf->st_atime = buf64->st_atime;
335 buf->st_mtime = buf64->st_mtime;
336 buf->st_ctime = buf64->st_ctime;
337}
338
339static void msvcrt_stat64_to_stat32(const struct _stat64 *buf64, struct _stat32 *buf)
340{
341 buf->st_dev = buf64->st_dev;
342 buf->st_ino = buf64->st_ino;
343 buf->st_mode = buf64->st_mode;
344 buf->st_nlink = buf64->st_nlink;
345 buf->st_uid = buf64->st_uid;
346 buf->st_gid = buf64->st_gid;
347 buf->st_rdev = buf64->st_rdev;
348 buf->st_size = buf64->st_size;
349 buf->st_atime = buf64->st_atime;
350 buf->st_mtime = buf64->st_mtime;
351 buf->st_ctime = buf64->st_ctime;
352}
353
354static void msvcrt_stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
355{
356 buf->st_dev = buf64->st_dev;
357 buf->st_ino = buf64->st_ino;
358 buf->st_mode = buf64->st_mode;
359 buf->st_nlink = buf64->st_nlink;
360 buf->st_uid = buf64->st_uid;
361 buf->st_gid = buf64->st_gid;
362 buf->st_rdev = buf64->st_rdev;
363 buf->st_size = buf64->st_size;
364 buf->st_atime = buf64->st_atime;
365 buf->st_mtime = buf64->st_mtime;
366 buf->st_ctime = buf64->st_ctime;
367}
368
369static void msvcrt_stat64_to_stat32i64(const struct _stat64 *buf64, struct _stat32i64 *buf)
370{
371 buf->st_dev = buf64->st_dev;
372 buf->st_ino = buf64->st_ino;
373 buf->st_mode = buf64->st_mode;
374 buf->st_nlink = buf64->st_nlink;
375 buf->st_uid = buf64->st_uid;
376 buf->st_gid = buf64->st_gid;
377 buf->st_rdev = buf64->st_rdev;
378 buf->st_size = buf64->st_size;
379 buf->st_atime = buf64->st_atime;
380 buf->st_mtime = buf64->st_mtime;
381 buf->st_ctime = buf64->st_ctime;
382}
383
385{
386 /* 1601 to 1970 is 369 years plus 89 leap days */
387 static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
388
389 __int64 ticks = (time + secs_1601_to_1970) * 10000000;
390 ft->dwHighDateTime = ticks >> 32;
391 ft->dwLowDateTime = ticks;
392}
393
394static inline ioinfo* get_ioinfo_nolock(int fd)
395{
396 ioinfo *ret = NULL;
397 if(fd>=0 && fd<MSVCRT_MAX_FILES)
399 if(!ret)
400 return &MSVCRT___badioinfo;
401
402 return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
403}
404
405static inline void init_ioinfo_cs(ioinfo *info)
406{
408 LOCK_FILES();
412 }
413 UNLOCK_FILES();
414 }
415}
416
417static inline ioinfo* get_ioinfo(int fd)
418{
420 if(ret == &MSVCRT___badioinfo)
421 return ret;
424 return ret;
425}
426
427static inline BOOL alloc_pioinfo_block(int fd)
428{
429 ioinfo *block;
430 int i;
431
432 if(fd<0 || fd>=MSVCRT_MAX_FILES)
433 {
434 *_errno() = ENFILE;
435 return FALSE;
436 }
437
439 if(!block)
440 {
441 WARN(":out of memory!\n");
442 *_errno() = ENOMEM;
443 return FALSE;
444 }
445 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
446 {
447 block[i].handle = INVALID_HANDLE_VALUE;
449 {
450 /* Initialize crit section on block allocation for _MSVC_VER >= 140,
451 * ioinfo_is_crit_init() is always TRUE. */
453 }
454 }
456 {
457 if (ioinfo_is_crit_init(&block[0]))
458 {
459 for(i = 0; i < MSVCRT_FD_BLOCK_SIZE; ++i)
461 }
462 free(block);
463 }
464 return TRUE;
465}
466
467static inline ioinfo* get_ioinfo_alloc_fd(int fd)
468{
469 ioinfo *ret;
470
471 ret = get_ioinfo(fd);
472 if(ret != &MSVCRT___badioinfo)
473 return ret;
474
476 return &MSVCRT___badioinfo;
477
478 return get_ioinfo(fd);
479}
480
481static inline ioinfo* get_ioinfo_alloc(int *fd)
482{
483 int i;
484
485 *fd = -1;
486 for(i=0; i<MSVCRT_MAX_FILES; i++)
487 {
489
491 {
493 return &MSVCRT___badioinfo;
495 }
496
498 if(TryEnterCriticalSection(&info->crit))
499 {
500 if(info->handle == INVALID_HANDLE_VALUE)
501 {
502 *fd = i;
503 return info;
504 }
506 }
507 }
508
509 WARN(":files exhausted!\n");
510 *_errno() = ENFILE;
511 return &MSVCRT___badioinfo;
512}
513
514static inline void release_ioinfo(ioinfo *info)
515{
518}
519
520static inline FILE* msvcrt_get_file(int i)
521{
522 file_crit *ret;
523
524 if(i >= MSVCRT_max_streams)
525 return NULL;
526
527 if(i < _IOB_ENTRIES)
528 return iob_get_file(i);
529
531 if(!ret) {
534 ERR("out of memory\n");
535 *_errno() = ENOMEM;
536 return NULL;
537 }
538
540 } else
542
543 return &ret->file;
544}
545
546/* INTERNAL: free a file entry fd */
547static void msvcrt_free_fd(int fd)
548{
549 ioinfo *fdinfo = get_ioinfo(fd);
550
551 if(fdinfo != &MSVCRT___badioinfo)
552 {
554 fdinfo->wxflag = 0;
555 }
556 TRACE(":fd (%d) freed\n",fd);
557
558 if (fd < 3)
559 {
560 switch (fd)
561 {
562 case 0:
564 break;
565 case 1:
567 break;
568 case 2:
570 break;
571 }
572 }
573 release_ioinfo(fdinfo);
574}
575
576static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
577{
578 fdinfo->handle = hand;
580 fdinfo->lookahead[0] = '\n';
581 fdinfo->lookahead[1] = '\n';
582 fdinfo->lookahead[2] = '\n';
583 ioinfo_set_unicode(fdinfo, FALSE);
585
586 if (hand != MSVCRT_NO_CONSOLE)
587 {
588 switch (fdinfo-MSVCRT___pioinfo[0])
589 {
590 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
591 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
592 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
593 }
594 }
595}
596
597/* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
598static int msvcrt_alloc_fd(HANDLE hand, int flag)
599{
600 int fd;
602
603 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
604
606 return -1;
607
608 msvcrt_set_fd(info, hand, flag);
610 return fd;
611}
612
613/* INTERNAL: Allocate a FILE* for an fd slot */
614/* caller must hold the files lock */
616{
617 int i = 0;
618 FILE *file;
619
620#if _MSVCR_VER >= 140
621 i = 3;
622#endif
623 for (; i < MSVCRT_max_streams; i++)
624 {
626 if (!file)
627 return NULL;
628
629 if (file->_flag == 0)
630 {
631 if (i == MSVCRT_stream_idx)
632 {
634 if (cs)
635 {
637 cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
638 }
640 }
641 return file;
642 }
643 }
644
645 return NULL;
646}
647
648/* INTERNAL: initialize a FILE* from an open fd */
649static int msvcrt_init_fp(FILE* file, int fd, unsigned stream_flags)
650{
651 TRACE(":fd (%d) allocating FILE*\n",fd);
652 if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
653 {
654 WARN(":invalid fd %d\n",fd);
655 *__doserrno() = 0;
656 *_errno() = EBADF;
657 return -1;
658 }
659 file->_ptr = file->_base = NULL;
660 file->_cnt = 0;
661 file->_file = fd;
662 file->_flag = stream_flags;
663 file->_tmpfname = NULL;
664
665 TRACE(":got FILE* (%p)\n",file);
666 return 0;
667}
668
669/* INTERNAL: Create an inheritance data block (for spawned process)
670 * The inheritance block is made of:
671 * 00 int nb of file descriptor (NBFD)
672 * 04 char file flags (wxflag): repeated for each fd
673 * 4+NBFD HANDLE file handle: repeated for each fd
674 */
676{
677 int fd, last_fd;
678 char* wxflag_ptr;
679 HANDLE* handle_ptr;
680 ioinfo* fdinfo;
681
682 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
684 break;
685 last_fd++;
686
687 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
688 *block = calloc(1, *size);
689 if (!*block)
690 {
691 *size = 0;
692 return FALSE;
693 }
694 wxflag_ptr = (char*)*block + sizeof(unsigned);
695 handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
696
697 *(unsigned*)*block = last_fd;
698 for (fd = 0; fd < last_fd; fd++)
699 {
700 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
701 fdinfo = get_ioinfo_nolock(fd);
702 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
703 {
704 *wxflag_ptr = fdinfo->wxflag;
705 *handle_ptr = fdinfo->handle;
706 }
707 else
708 {
709 *wxflag_ptr = 0;
710 *handle_ptr = INVALID_HANDLE_VALUE;
711 }
712 wxflag_ptr++; handle_ptr++;
713 }
714 return TRUE;
715}
716
717/* INTERNAL: Set up all file descriptors,
718 * as well as default streams (stdin, stderr and stdout)
719 */
721{
722 STARTUPINFOA si;
723 int i;
724 ioinfo *fdinfo;
725
726 GetStartupInfoA(&si);
727 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
728 {
729 BYTE* wxflag_ptr;
730 HANDLE* handle_ptr;
731 unsigned int count;
732
733 count = *(unsigned*)si.lpReserved2;
734 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
735 handle_ptr = (HANDLE*)(wxflag_ptr + count);
736
737 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
739 for (i = 0; i < count; i++)
740 {
741 if ((*wxflag_ptr & WX_OPEN) && GetFileType(*handle_ptr) != FILE_TYPE_UNKNOWN)
742 {
743 fdinfo = get_ioinfo_alloc_fd(i);
744 if(fdinfo != &MSVCRT___badioinfo)
745 msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
746 release_ioinfo(fdinfo);
747 }
748
749 wxflag_ptr++; handle_ptr++;
750 }
751 }
752
754 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
758
759 if (type == FILE_TYPE_UNKNOWN) {
761 flags |= WX_TTY;
762 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
763 flags |= WX_TTY;
764 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
765 flags |= WX_PIPE;
766 }
767
768 msvcrt_set_fd(fdinfo, h, flags);
769 }
770 release_ioinfo(fdinfo);
771
773 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
777
778 if (type == FILE_TYPE_UNKNOWN) {
780 flags |= WX_TTY;
781 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
782 flags |= WX_TTY;
783 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
784 flags |= WX_PIPE;
785 }
786
787 msvcrt_set_fd(fdinfo, h, flags);
788 }
789 release_ioinfo(fdinfo);
790
792 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
796
797 if (type == FILE_TYPE_UNKNOWN) {
799 flags |= WX_TTY;
800 } else if ((type & 0xf) == FILE_TYPE_CHAR) {
801 flags |= WX_TTY;
802 } else if ((type & 0xf) == FILE_TYPE_PIPE) {
803 flags |= WX_PIPE;
804 }
805
806 msvcrt_set_fd(fdinfo, h, flags);
807 }
808 release_ioinfo(fdinfo);
809
810 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO)->handle,
813
814 for (i = 0; i < 3; i++)
815 {
816 FILE *f = iob_get_file(i);
818
819 /* FILE structs for stdin/out/err are static and never deleted */
822 f->_tmpfname = NULL;
823 f->_flag = (i == 0) ? _IOREAD : _IOWRT;
824
825 if (cs)
826 {
828 cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
829 }
830 }
832}
833
834/* INTERNAL: Flush stdio file buffer */
836{
837 int ret = 0;
838
839 if((file->_flag & (_IOREAD|_IOWRT)) == _IOWRT &&
840 file->_flag & (_IOMYBUF|MSVCRT__USERBUF)) {
841 int cnt=file->_ptr-file->_base;
842 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
843 file->_flag |= _IOERR;
844 ret = EOF;
845 } else if(file->_flag & _IORW) {
846 file->_flag &= ~_IOWRT;
847 }
848 }
849
850 file->_ptr=file->_base;
851 file->_cnt=0;
852 return ret;
853}
854
855/*********************************************************************
856 * _isatty (MSVCRT.@)
857 */
859{
860 TRACE(":fd (%d)\n",fd);
861
863}
864
865/* INTERNAL: Allocate stdio file buffer */
867{
868#if _MSVCR_VER >= 140
869 if((file->_file==STDOUT_FILENO && _isatty(file->_file))
870 || file->_file == STDERR_FILENO)
871 return FALSE;
872#else
873 if((file->_file==STDOUT_FILENO || file->_file==STDERR_FILENO)
874 && _isatty(file->_file))
875 return FALSE;
876#endif
877
879 if(file->_base) {
880 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
881 file->_flag |= _IOMYBUF;
882 } else {
883 file->_base = (char*)(&file->_charbuf);
884 file->_bufsiz = 2;
885 file->_flag |= MSVCRT__NOBUF;
886 }
887 file->_ptr = file->_base;
888 file->_cnt = 0;
889 return TRUE;
890}
891
892/* INTERNAL: Allocate temporary buffer for stdout and stderr */
894{
895 static char buffers[2][BUFSIZ];
896
897 if((file->_file!=STDOUT_FILENO && file->_file!=STDERR_FILENO)
898 || (file->_flag & (MSVCRT__NOBUF | _IOMYBUF | MSVCRT__USERBUF))
899 || !_isatty(file->_file))
900 return FALSE;
901
902 file->_ptr = file->_base = buffers[file->_file == STDOUT_FILENO ? 0 : 1];
903 file->_bufsiz = file->_cnt = BUFSIZ;
904 file->_flag |= MSVCRT__USERBUF;
905 return TRUE;
906}
907
908/* INTERNAL: Removes temporary buffer from stdout or stderr */
909/* Only call this function when add_std_buffer returned TRUE */
911{
913 file->_ptr = file->_base = NULL;
914 file->_bufsiz = file->_cnt = 0;
915 file->_flag &= ~MSVCRT__USERBUF;
916}
917
918/* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
919static int msvcrt_int_to_base32(int num, char *str)
920{
921 char *p;
922 int n = num;
923 int digits = 0;
924
925 while (n != 0)
926 {
927 n >>= 5;
928 digits++;
929 }
930 p = str + digits;
931 *p = 0;
932 while (--p >= str)
933 {
934 *p = (num & 31) + '0';
935 if (*p > '9')
936 *p += ('a' - '0' - 10);
937 num >>= 5;
938 }
939
940 return digits;
941}
942
943/* INTERNAL: wide character version of msvcrt_int_to_base32 */
944static int msvcrt_int_to_base32_w(int num, wchar_t *str)
945{
946 wchar_t *p;
947 int n = num;
948 int digits = 0;
949
950 while (n != 0)
951 {
952 n >>= 5;
953 digits++;
954 }
955 p = str + digits;
956 *p = 0;
957 while (--p >= str)
958 {
959 *p = (num & 31) + '0';
960 if (*p > '9')
961 *p += ('a' - '0' - 10);
962 num >>= 5;
963 }
964
965 return digits;
966}
967
968/*********************************************************************
969 * __iob_func (MSVCRT.@)
970 */
971#undef __iob_func
973{
974 return iob_get_file(0);
975}
976
977#if _MSVCR_VER >= 140
978/*********************************************************************
979 * __acrt_iob_func(UCRTBASE.@)
980 */
981FILE * CDECL __acrt_iob_func(unsigned idx)
982{
983 return iob_get_file(idx);
984}
985#endif
986
987/*********************************************************************
988 * _access (MSVCRT.@)
989 */
990int CDECL _access(const char *filename, int mode)
991{
992 wchar_t *filenameW = NULL;
993 int ret;
994
995 if (filename && !(filenameW = wstrdupa_utf8(filename))) return -1;
998 return ret;
999}
1000
1001/*********************************************************************
1002 * _access_s (MSVCRT.@)
1003 */
1004int CDECL _access_s(const char *filename, int mode)
1005{
1006 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *_errno();
1007 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *_errno();
1008
1009 if (_access(filename, mode) == -1)
1010 return *_errno();
1011 return 0;
1012}
1013
1014/*********************************************************************
1015 * _waccess (MSVCRT.@)
1016 */
1017int CDECL _waccess(const wchar_t *filename, int mode)
1018{
1020
1021 TRACE("(%s,%d) %ld\n", debugstr_w(filename), mode, attr);
1022
1024 {
1026 return -1;
1027 }
1029 {
1031 return -1;
1032 }
1033 return 0;
1034}
1035
1036/*********************************************************************
1037 * _waccess_s (MSVCRT.@)
1038 */
1039int CDECL _waccess_s(const wchar_t *filename, int mode)
1040{
1041 if (!MSVCRT_CHECK_PMT(filename != NULL)) return *_errno();
1042 if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *_errno();
1043
1044 if (_waccess(filename, mode) == -1)
1045 return *_errno();
1046 return 0;
1047}
1048
1049/*********************************************************************
1050 * _chmod (MSVCRT.@)
1051 */
1052int CDECL _chmod(const char *path, int flags)
1053{
1054 wchar_t *pathW = NULL;
1055 int ret;
1056
1057 if (path && !(pathW = wstrdupa_utf8(path))) return -1;
1058 ret = _wchmod(pathW, flags);
1059 free(pathW);
1060 return ret;
1061}
1062
1063/*********************************************************************
1064 * _wchmod (MSVCRT.@)
1065 */
1066int CDECL _wchmod(const wchar_t *path, int flags)
1067{
1068 DWORD oldFlags = GetFileAttributesW(path);
1069
1070 if (oldFlags != INVALID_FILE_ATTRIBUTES)
1071 {
1072 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
1073 oldFlags | FILE_ATTRIBUTE_READONLY;
1074
1075 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
1076 return 0;
1077 }
1079 return -1;
1080}
1081
1082/*********************************************************************
1083 * _unlink (MSVCRT.@)
1084 */
1085int CDECL _unlink(const char *path)
1086{
1087 wchar_t *pathW = NULL;
1088 int ret;
1089
1090 if (path && !(pathW = wstrdupa_utf8(path))) return -1;
1091 ret = _wunlink(pathW);
1092 free(pathW);
1093 return ret;
1094}
1095
1096/*********************************************************************
1097 * _wunlink (MSVCRT.@)
1098 */
1099int CDECL _wunlink(const wchar_t *path)
1100{
1101 TRACE("(%s)\n", debugstr_w(path));
1102 if(DeleteFileW(path))
1103 return 0;
1104 TRACE("failed (%ld)\n", GetLastError());
1106 return -1;
1107}
1108
1109/*********************************************************************
1110 * _commit (MSVCRT.@)
1111 */
1113{
1115 int ret;
1116
1117 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1118
1119 if (info->handle == INVALID_HANDLE_VALUE)
1120 ret = -1;
1121 else if (!FlushFileBuffers(info->handle))
1122 {
1124 {
1125 /* FlushFileBuffers fails for console handles
1126 * so we ignore this error.
1127 */
1128 ret = 0;
1129 }
1130 else
1131 {
1132 TRACE(":failed-last error (%ld)\n", GetLastError());
1134 ret = -1;
1135 }
1136 }
1137 else
1138 {
1139 TRACE(":ok\n");
1140 ret = 0;
1141 }
1142
1144 return ret;
1145}
1146
1147/* INTERNAL: Flush all stream buffer */
1149{
1150 int i, num_flushed = 0;
1151 FILE *file;
1152
1153 LOCK_FILES();
1154 for (i = 0; i < MSVCRT_stream_idx; i++) {
1156
1157 if (file->_flag)
1158 {
1159 if(file->_flag & mask) {
1160 fflush(file);
1161 num_flushed++;
1162 }
1163 }
1164 }
1165 UNLOCK_FILES();
1166
1167 TRACE(":flushed (%d) handles\n",num_flushed);
1168 return num_flushed;
1169}
1170
1171/*********************************************************************
1172 * _flushall (MSVCRT.@)
1173 */
1175{
1177}
1178
1179/*********************************************************************
1180 * fflush (MSVCRT.@)
1181 */
1183{
1184 int ret;
1185
1186 if(!file) {
1188 ret = 0;
1189 } else {
1193 }
1194
1195 return ret;
1196}
1197
1198/*********************************************************************
1199 * _fflush_nolock (MSVCRT.@)
1200 */
1202{
1203 int res;
1204
1205 if(!file) {
1207 return 0;
1208 }
1209
1211 if(!res && (file->_flag & MSVCRT__IOCOMMIT))
1212 res = _commit(file->_file) ? EOF : 0;
1213 return res;
1214}
1215
1216/*********************************************************************
1217 * _close (MSVCRT.@)
1218 */
1220{
1222 int ret;
1223
1224 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1225
1226 if (fd == MSVCRT_NO_CONSOLE_FD) {
1227 *_errno() = EBADF;
1228 ret = -1;
1229 } else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, EBADF)) {
1230 ret = -1;
1231 } else if (fd == STDOUT_FILENO &&
1234 ret = 0;
1235 } else if (fd == STDERR_FILENO &&
1238 ret = 0;
1239 } else {
1240 ret = CloseHandle(info->handle) ? 0 : -1;
1242 if (ret) {
1243 WARN(":failed-last error (%ld)\n", GetLastError());
1245 }
1246 }
1248 return ret;
1249}
1250
1251/*********************************************************************
1252 * _dup2 (MSVCRT.@)
1253 * NOTES
1254 * MSDN isn't clear on this point, but the remarks for _pipe
1255 * indicate file descriptors duplicated with _dup and _dup2 are always
1256 * inheritable.
1257 */
1258int CDECL _dup2(int od, int nd)
1259{
1260 ioinfo *info_od, *info_nd;
1261 int ret;
1262
1263 TRACE("(od=%d, nd=%d)\n", od, nd);
1264
1265 if (od < nd)
1266 {
1267 info_od = get_ioinfo(od);
1268 info_nd = get_ioinfo_alloc_fd(nd);
1269 }
1270 else
1271 {
1272 info_nd = get_ioinfo_alloc_fd(nd);
1273 info_od = get_ioinfo(od);
1274 }
1275
1276 if (info_nd == &MSVCRT___badioinfo)
1277 {
1278 *_errno() = EBADF;
1279 ret = -1;
1280 }
1281 else if (info_od->wxflag & WX_OPEN)
1282 {
1283 HANDLE handle;
1284
1287 {
1288 int wxflag = info_od->wxflag & ~WX_DONTINHERIT;
1289
1290 if (info_nd->wxflag & WX_OPEN)
1291 _close(nd);
1292
1293 msvcrt_set_fd(info_nd, handle, wxflag);
1294 /* _dup2 returns 0, not nd, on success */
1295 ret = 0;
1296 }
1297 else
1298 {
1299 ret = -1;
1301 }
1302 }
1303 else
1304 {
1305 *_errno() = EBADF;
1306 ret = -1;
1307 }
1308
1309 release_ioinfo(info_od);
1310 release_ioinfo(info_nd);
1311 return ret;
1312}
1313
1314/*********************************************************************
1315 * _dup (MSVCRT.@)
1316 */
1317int CDECL _dup(int od)
1318{
1319 int fd, ret;
1321
1322 if (_dup2(od, fd) == 0)
1323 ret = fd;
1324 else
1325 ret = -1;
1327 return ret;
1328}
1329
1330/*********************************************************************
1331 * _eof (MSVCRT.@)
1332 */
1333int CDECL _eof(int fd)
1334{
1336 DWORD curpos,endpos;
1337 LONG hcurpos,hendpos;
1338
1339 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1340
1341 if (info->handle == INVALID_HANDLE_VALUE)
1342 {
1344 return -1;
1345 }
1346
1347 if (info->wxflag & WX_ATEOF)
1348 {
1350 return TRUE;
1351 }
1352
1353 /* Otherwise we do it the hard way */
1354 hcurpos = hendpos = 0;
1355 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1356 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1357
1358 if (curpos == endpos && hcurpos == hendpos)
1359 {
1360 /* FIXME: shouldn't WX_ATEOF be set here? */
1362 return TRUE;
1363 }
1364
1365 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1367 return FALSE;
1368}
1369
1370/*********************************************************************
1371 * _fcloseall (MSVCRT.@)
1372 */
1374{
1375 int num_closed = 0, i;
1376 FILE *file;
1377
1378 LOCK_FILES();
1379 for (i = 3; i < MSVCRT_stream_idx; i++) {
1381
1382 if (file->_flag && !fclose(file))
1383 num_closed++;
1384 }
1385 UNLOCK_FILES();
1386
1387 TRACE(":closed (%d) handles\n",num_closed);
1388 return num_closed;
1389}
1390
1391/* free everything on process exit */
1393{
1394 unsigned int i;
1395 int j;
1396
1397 _flushall();
1398 _fcloseall();
1399
1400 for(i=0; i<ARRAY_SIZE(MSVCRT___pioinfo); i++)
1401 {
1402 if(!MSVCRT___pioinfo[i])
1403 continue;
1404
1405 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1406 {
1409 }
1411 }
1412
1413 for(j=0; j<MSVCRT_stream_idx; j++)
1414 {
1417
1418 if(cs)
1419 {
1420 cs->DebugInfo->Spare[0] = 0;
1422 }
1423 }
1424
1425 for(i=0; i<ARRAY_SIZE(MSVCRT_fstream); i++)
1427}
1428
1429/*********************************************************************
1430 * _lseeki64 (MSVCRT.@)
1431 */
1433{
1435 LARGE_INTEGER ofs;
1436
1437 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1438
1439 if (info->handle == INVALID_HANDLE_VALUE)
1440 {
1441 *_errno() = EBADF;
1443 return -1;
1444 }
1445
1446 if (whence < 0 || whence > 2)
1447 {
1449 *_errno() = EINVAL;
1450 return -1;
1451 }
1452
1453 TRACE(":fd (%d) to %#I64x pos %s\n",
1454 fd, offset, (whence == SEEK_SET) ? "SEEK_SET" :
1455 (whence == SEEK_CUR) ? "SEEK_CUR" :
1456 (whence == SEEK_END) ? "SEEK_END" : "UNKNOWN");
1457
1458 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1459 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1460 ofs.QuadPart = offset;
1461 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1463 {
1464 info->wxflag &= ~WX_ATEOF;
1465 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1466
1468 return ofs.QuadPart;
1469 }
1471 TRACE(":error-last error (%ld)\n", GetLastError());
1473 return -1;
1474}
1475
1476/*********************************************************************
1477 * _lseek (MSVCRT.@)
1478 */
1480{
1481 return _lseeki64(fd, offset, whence);
1482}
1483
1484/*********************************************************************
1485 * _lock_file (MSVCRT.@)
1486 */
1488{
1490 if (!cs)
1492 else
1494}
1495
1496/*********************************************************************
1497 * _unlock_file (MSVCRT.@)
1498 */
1500{
1502 if (!cs)
1504 else
1506}
1507
1508/*********************************************************************
1509 * _locking (MSVCRT.@)
1510 *
1511 * This is untested; the underlying LockFile doesn't work yet.
1512 */
1513int CDECL _locking(int fd, int mode, __msvcrt_long nbytes)
1514{
1516 BOOL ret;
1517 DWORD cur_locn;
1518
1519 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1520 if (info->handle == INVALID_HANDLE_VALUE)
1521 {
1523 return -1;
1524 }
1525
1526 if (mode < 0 || mode > 4)
1527 {
1529 *_errno() = EINVAL;
1530 return -1;
1531 }
1532
1533 TRACE(":fd (%d) by %#lx mode %s\n",
1534 fd, nbytes, (mode == _LK_UNLCK) ? "_LK_UNLCK" :
1535 (mode == _LK_LOCK) ? "_LK_LOCK" :
1536 (mode == _LK_NBLCK) ? "_LK_NBLCK" :
1537 (mode == _LK_RLCK) ? "_LK_RLCK" :
1538 (mode == _LK_NBRLCK) ? "_LK_NBRLCK" :
1539 "UNKNOWN");
1540
1541 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1542 {
1544 FIXME("Seek failed\n");
1545 *_errno() = EINVAL; /* FIXME */
1546 return -1;
1547 }
1548 if (mode == _LK_LOCK || mode == _LK_RLCK)
1549 {
1550 int nretry = 10;
1551 ret = 1; /* just to satisfy gcc */
1552 while (nretry--)
1553 {
1554 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1555 if (ret) break;
1556 Sleep(1);
1557 }
1558 }
1559 else if (mode == _LK_UNLCK)
1560 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1561 else
1562 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1563 /* FIXME - what about error settings? */
1565 return ret ? 0 : -1;
1566}
1567
1568/*********************************************************************
1569 * _fseeki64 (MSVCRT.@)
1570 */
1572{
1573 int ret;
1574
1576 ret = _fseeki64_nolock(file, offset, whence);
1578
1579 return ret;
1580}
1581
1582/*********************************************************************
1583 * _fseeki64_nolock (MSVCRT.@)
1584 */
1586{
1587 int ret;
1588
1589 if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
1590 whence = SEEK_SET;
1592 }
1593
1594 /* Flush output if needed */
1596 /* Reset direction of i/o */
1597 if(file->_flag & _IORW) {
1598 file->_flag &= ~(_IOREAD|_IOWRT);
1599 }
1600 /* Clear end of file flag */
1601 file->_flag &= ~_IOEOF;
1602 ret = (_lseeki64(file->_file,offset,whence) == -1)?-1:0;
1603
1604 return ret;
1605}
1606
1607/*********************************************************************
1608 * fseek (MSVCRT.@)
1609 */
1611{
1612 return _fseeki64( file, offset, whence );
1613}
1614
1615/*********************************************************************
1616 * _fseek_nolock (MSVCRT.@)
1617 */
1619{
1620 return _fseeki64_nolock( file, offset, whence );
1621}
1622
1623/*********************************************************************
1624 * _chsize_s (MSVCRT.@)
1625 */
1627{
1628 ioinfo *info;
1629 __int64 cur, pos;
1630 BOOL ret = FALSE;
1631
1632 TRACE("(fd=%d, size=%#I64x)\n", fd, size);
1633
1634 if (!MSVCRT_CHECK_PMT(size >= 0)) return EINVAL;
1635
1636
1637 info = get_ioinfo(fd);
1638 if (info->handle != INVALID_HANDLE_VALUE)
1639 {
1640 /* save the current file pointer */
1641 cur = _lseeki64(fd, 0, SEEK_CUR);
1642 if (cur >= 0)
1643 {
1645 if (pos >= 0)
1646 {
1647 ret = SetEndOfFile(info->handle);
1649 }
1650
1651 /* restore the file pointer */
1653 }
1654 }
1655
1657 return ret ? 0 : *_errno();
1658}
1659
1660/*********************************************************************
1661 * _chsize (MSVCRT.@)
1662 */
1664{
1665 /* _chsize_s returns errno on failure but _chsize should return -1 */
1666 return _chsize_s( fd, size ) == 0 ? 0 : -1;
1667}
1668
1669/*********************************************************************
1670 * clearerr (MSVCRT.@)
1671 */
1673{
1674 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1675
1677 file->_flag &= ~(_IOERR | _IOEOF);
1679}
1680
1681/*********************************************************************
1682 * clearerr_s (MSVCRT.@)
1683 */
1685{
1686 TRACE(":file (%p)\n",file);
1687
1688 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
1689
1691 file->_flag &= ~(_IOERR | _IOEOF);
1693 return 0;
1694}
1695
1696#if defined(__i386__)
1697/* Stack preserving thunk for rewind
1698 * needed for the UIO mod for Fallout: New Vegas
1699 */
1700__ASM_GLOBAL_FUNC(rewind_preserve_stack,
1701 "pushl 4(%esp)\n\t"
1702 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1703 "call "__ASM_NAME("rewind") "\n\t"
1704 "addl $4,%esp\n\t"
1705 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
1706 "ret")
1707#endif
1708
1709/*********************************************************************
1710 * rewind (MSVCRT.@)
1711 */
1713{
1714 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1715
1718 clearerr(file);
1720}
1721
1722static int msvcrt_get_flags(const wchar_t* mode, int *open_flags, int* stream_flags)
1723{
1724 int plus = wcschr(mode, '+') != NULL;
1725
1726 TRACE("%s\n", debugstr_w(mode));
1727
1728 while(*mode == ' ') mode++;
1729
1730 switch(*mode++)
1731 {
1732 case 'R': case 'r':
1733 *open_flags = plus ? _O_RDWR : _O_RDONLY;
1734 *stream_flags = plus ? _IORW : _IOREAD;
1735 break;
1736 case 'W': case 'w':
1737 *open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1738 *stream_flags = plus ? _IORW : _IOWRT;
1739 break;
1740 case 'A': case 'a':
1741 *open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1742 *stream_flags = plus ? _IORW : _IOWRT;
1743 break;
1744 default:
1746 return -1;
1747 }
1748
1749 *stream_flags |= MSVCRT__commode;
1750
1751 while (*mode && *mode!=',')
1752 switch (*mode++)
1753 {
1754 case 'B': case 'b':
1755 *open_flags |= _O_BINARY;
1756 *open_flags &= ~_O_TEXT;
1757 break;
1758 case 't':
1759 *open_flags |= _O_TEXT;
1760 *open_flags &= ~_O_BINARY;
1761 break;
1762#if _MSVCR_VER>=140
1763 case 'x':
1764 if(!MSVCRT_CHECK_PMT((*open_flags & (_O_CREAT | _O_APPEND)) == _O_CREAT))
1765 return -1;
1766 *open_flags |= _O_EXCL;
1767 break;
1768#endif
1769 case 'D':
1770 *open_flags |= _O_TEMPORARY;
1771 break;
1772 case 'T':
1773 *open_flags |= _O_SHORT_LIVED;
1774 break;
1775 case 'c':
1776 *stream_flags |= MSVCRT__IOCOMMIT;
1777 break;
1778 case 'n':
1779 *stream_flags &= ~MSVCRT__IOCOMMIT;
1780 break;
1781 case 'N':
1782 *open_flags |= _O_NOINHERIT;
1783 break;
1784 case '+':
1785 case ' ':
1786 case 'a':
1787 case 'w':
1788 break;
1789 case 'S':
1790 if (!(*open_flags & _O_RANDOM))
1791 *open_flags |= _O_SEQUENTIAL;
1792 break;
1793 case 'R':
1794 if (!(*open_flags & _O_SEQUENTIAL))
1795 *open_flags |= _O_RANDOM;
1796 break;
1797 default:
1798 ERR("incorrect mode flag: %c\n", mode[-1]);
1799 break;
1800 }
1801
1802 if(*mode == ',')
1803 {
1804 mode++;
1805 while(*mode == ' ') mode++;
1806 if(!MSVCRT_CHECK_PMT(!wcsncmp(L"ccs", mode, 3)))
1807 return -1;
1808 mode += 3;
1809 while(*mode == ' ') mode++;
1810 if(!MSVCRT_CHECK_PMT(*mode == '='))
1811 return -1;
1812 mode++;
1813 while(*mode == ' ') mode++;
1814
1815 if(!_wcsnicmp(L"utf-8", mode, 5))
1816 {
1817 *open_flags |= _O_U8TEXT;
1818 mode += 5;
1819 }
1820 else if(!_wcsnicmp(L"utf-16le", mode, 8))
1821 {
1822 *open_flags |= _O_U16TEXT;
1823 mode += 8;
1824 }
1825 else if(!_wcsnicmp(L"unicode", mode, 7))
1826 {
1827 *open_flags |= _O_WTEXT;
1828 mode += 7;
1829 }
1830 else
1831 {
1833 return -1;
1834 }
1835
1836 while(*mode == ' ') mode++;
1837 }
1838
1839 if(!MSVCRT_CHECK_PMT(*mode == 0))
1840 return -1;
1841 return 0;
1842}
1843
1844/*********************************************************************
1845 * _fdopen (MSVCRT.@)
1846 */
1847FILE* CDECL _fdopen(int fd, const char *mode)
1848{
1849 FILE *ret;
1850 wchar_t *modeW = NULL;
1851
1852 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1853
1854 ret = _wfdopen(fd, modeW);
1855
1856 free(modeW);
1857 return ret;
1858}
1859
1860/*********************************************************************
1861 * _wfdopen (MSVCRT.@)
1862 */
1863FILE* CDECL _wfdopen(int fd, const wchar_t *mode)
1864{
1865 int open_flags, stream_flags;
1866 FILE* file;
1867
1868 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1869
1870 LOCK_FILES();
1871 if (!(file = msvcrt_alloc_fp()))
1872 file = NULL;
1873 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1874 {
1875 file->_flag = 0;
1876 file = NULL;
1877 }
1878 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1879 UNLOCK_FILES();
1880
1881 return file;
1882}
1883
1884/*********************************************************************
1885 * _filelength (MSVCRT.@)
1886 */
1888{
1889 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1890 if (curPos != -1)
1891 {
1892 LONG endPos = _lseek(fd, 0, SEEK_END);
1893 if (endPos != -1)
1894 {
1895 if (endPos != curPos)
1896 _lseek(fd, curPos, SEEK_SET);
1897 return endPos;
1898 }
1899 }
1900 return -1;
1901}
1902
1903/*********************************************************************
1904 * _filelengthi64 (MSVCRT.@)
1905 */
1907{
1908 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1909 if (curPos != -1)
1910 {
1911 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1912 if (endPos != -1)
1913 {
1914 if (endPos != curPos)
1915 _lseeki64(fd, curPos, SEEK_SET);
1916 return endPos;
1917 }
1918 }
1919 return -1;
1920}
1921
1922/*********************************************************************
1923 * _fileno (MSVCRT.@)
1924 */
1926{
1927 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1928 return file->_file;
1929}
1930
1931/*********************************************************************
1932 * _fstat64 (MSVCRT.@)
1933 */
1934int CDECL _fstat64(int fd, struct _stat64* buf)
1935{
1937 DWORD dw;
1938 DWORD type;
1939
1940 TRACE(":fd (%d) stat (%p)\n", fd, buf);
1941 if (info->handle == INVALID_HANDLE_VALUE)
1942 {
1944 return -1;
1945 }
1946
1947 if (!buf)
1948 {
1949 WARN(":failed-NULL buf\n");
1952 return -1;
1953 }
1954
1955 memset(buf, 0, sizeof(struct _stat64));
1956 type = GetFileType(info->handle);
1957 if (type == FILE_TYPE_PIPE)
1958 {
1959 buf->st_dev = buf->st_rdev = fd;
1960 buf->st_mode = _S_IFIFO;
1961 buf->st_nlink = 1;
1962 }
1963 else if (type == FILE_TYPE_CHAR)
1964 {
1965 buf->st_dev = buf->st_rdev = fd;
1966 buf->st_mode = _S_IFCHR;
1967 buf->st_nlink = 1;
1968 }
1969 else /* FILE_TYPE_DISK etc. */
1970 {
1971 FILE_BASIC_INFORMATION basic_info;
1975
1976 if ((status = NtQueryInformationFile( info->handle, &io, &basic_info, sizeof(basic_info), FileBasicInformation )) ||
1977 (status = NtQueryInformationFile( info->handle, &io, &std_info, sizeof(std_info), FileStandardInformation )))
1978 {
1979 WARN(":failed-error %lx\n", status);
1982 return -1;
1983 }
1984 buf->st_mode = _S_IFREG | 0444;
1985 if (!(basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY))
1986 buf->st_mode |= 0222;
1987 buf->st_size = std_info.EndOfFile.QuadPart;
1989 buf->st_atime = dw;
1991 buf->st_mtime = buf->st_ctime = dw;
1992 buf->st_nlink = std_info.NumberOfLinks;
1993 TRACE(":dwFileAttributes = %#lx, mode set to %#x\n",
1994 basic_info.FileAttributes, buf->st_mode);
1995 }
1997 return 0;
1998}
1999
2000/*********************************************************************
2001 * _fstati64 (MSVCRT.@)
2002 */
2003int CDECL _fstati64(int fd, struct _stati64* buf)
2004{
2005 int ret;
2006 struct _stat64 buf64;
2007
2008 ret = _fstat64(fd, &buf64);
2009 if (!ret)
2011 return ret;
2012}
2013
2014/*********************************************************************
2015 * _fstat (MSVCRT.@)
2016 */
2017int CDECL _fstat(int fd, struct _stat* buf)
2018{ int ret;
2019 struct _stat64 buf64;
2020
2021 ret = _fstat64(fd, &buf64);
2022 if (!ret)
2023 msvcrt_stat64_to_stat(&buf64, buf);
2024 return ret;
2025}
2026
2027/*********************************************************************
2028 * _fstat32 (MSVCR80.@)
2029 */
2030int CDECL _fstat32(int fd, struct _stat32* buf)
2031{
2032 int ret;
2033 struct _stat64 buf64;
2034
2035 ret = _fstat64(fd, &buf64);
2036 if (!ret)
2038 return ret;
2039}
2040
2041/*********************************************************************
2042 * _fstat32i64 (MSVCR80.@)
2043 */
2045{
2046 int ret;
2047 struct _stat64 buf64;
2048
2049 ret = _fstat64(fd, &buf64);
2050 if (!ret)
2052 return ret;
2053}
2054
2055/*********************************************************************
2056 * _fstat64i32 (MSVCR80.@)
2057 */
2059{
2060 int ret;
2061 struct _stat64 buf64;
2062
2063 ret = _fstat64(fd, &buf64);
2064 if (!ret)
2066 return ret;
2067}
2068
2069/*********************************************************************
2070 * _futime64 (MSVCRT.@)
2071 */
2072int CDECL _futime64(int fd, struct __utimbuf64 *t)
2073{
2075 FILETIME at, wt;
2076
2077 if (!t)
2078 {
2079 time_to_filetime( _time64(NULL), &at );
2080 wt = at;
2081 }
2082 else
2083 {
2084 time_to_filetime( t->actime, &at );
2085 time_to_filetime( t->modtime, &wt );
2086 }
2087
2088 if (!SetFileTime(info->handle, NULL, &at, &wt))
2089 {
2092 return -1 ;
2093 }
2095 return 0;
2096}
2097
2098/*********************************************************************
2099 * _futime32 (MSVCRT.@)
2100 */
2101int CDECL _futime32(int fd, struct __utimbuf32 *t)
2102{
2103 if (t)
2104 {
2105 struct __utimbuf64 t64;
2106 t64.actime = t->actime;
2107 t64.modtime = t->modtime;
2108 return _futime64( fd, &t64 );
2109 }
2110 else
2111 return _futime64( fd, NULL );
2112}
2113
2114/*********************************************************************
2115 * _get_osfhandle (MSVCRT.@)
2116 */
2118{
2120 TRACE(":fd (%d) handle (%p)\n",fd,hand);
2121
2122 if(hand == INVALID_HANDLE_VALUE)
2123 *_errno() = EBADF;
2124 return (intptr_t)hand;
2125}
2126
2127/*********************************************************************
2128 * _mktemp_s (MSVCRT.@)
2129 */
2130int CDECL _mktemp_s(char *pattern, size_t size)
2131{
2132 DWORD len, wlen, xno, id;
2133 wchar_t *pathW;
2134
2136 return EINVAL;
2137
2138 for(len=0; len<size; len++)
2139 if(!pattern[len])
2140 break;
2141 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
2142 if(size)
2143 pattern[0] = 0;
2144 return EINVAL;
2145 }
2146
2147 for(xno=1; xno<=6; xno++)
2148 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
2149 return EINVAL;
2150
2151 id = GetCurrentProcessId();
2152 for(xno=1; xno<6; xno++) {
2153 pattern[len-xno] = id%10 + '0';
2154 id /= 10;
2155 }
2156
2157 if(!(pathW = wstrdupa_utf8(pattern))) return *_errno();
2158 wlen = wcslen(pathW);
2159 for(pathW[wlen-6]='a'; pathW[wlen-6]<='z'; pathW[wlen-6]++) {
2161 pattern[len-6] = pathW[wlen-6];
2162 free(pathW);
2163 return 0;
2164 }
2165 }
2166 free(pathW);
2167
2168 pattern[0] = 0;
2169 *_errno() = EEXIST;
2170 return EEXIST;
2171}
2172
2173/*********************************************************************
2174 * _mktemp (MSVCRT.@)
2175 */
2176char * CDECL _mktemp(char *pattern)
2177{
2178 wchar_t *pathW, *p;
2179 int numX = 0;
2180 char *retVal = pattern;
2181 int id;
2182 char letter = 'a';
2183
2184 if(!pattern)
2185 return NULL;
2186
2187 while(*pattern)
2188 numX = (*pattern++ == 'X')? numX + 1 : 0;
2189 if (numX < 6)
2190 return NULL;
2191 pattern--;
2192 id = GetCurrentProcessId();
2193 numX = 6;
2194 while(numX--)
2195 {
2196 int tempNum = id / 10;
2197 *pattern-- = id - (tempNum * 10) + '0';
2198 id = tempNum;
2199 }
2200 pattern++;
2201 if (!(pathW = wstrdupa_utf8(retVal)))
2202 return NULL;
2203 p = pathW + wcslen(pathW) - 6;
2204 do
2205 {
2206 *p = letter++;
2208 {
2209 *pattern = *p;
2210 free(pathW);
2211 return retVal;
2212 }
2213 } while(letter <= 'z');
2214 free(pathW);
2215 return NULL;
2216}
2217
2218/*********************************************************************
2219 * _wmktemp_s (MSVCRT.@)
2220 */
2221int CDECL _wmktemp_s(wchar_t *pattern, size_t size)
2222{
2223 DWORD len, xno, id;
2224
2226 return EINVAL;
2227
2228 for(len=0; len<size; len++)
2229 if(!pattern[len])
2230 break;
2231 if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
2232 if(size)
2233 pattern[0] = 0;
2234 return EINVAL;
2235 }
2236
2237 for(xno=1; xno<=6; xno++)
2238 if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
2239 return EINVAL;
2240
2241 id = GetCurrentProcessId();
2242 for(xno=1; xno<6; xno++) {
2243 pattern[len-xno] = id%10 + '0';
2244 id /= 10;
2245 }
2246
2247 for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
2249 return 0;
2250 }
2251
2252 pattern[0] = 0;
2253 *_errno() = EEXIST;
2254 return EEXIST;
2255}
2256
2257/*********************************************************************
2258 * _wmktemp (MSVCRT.@)
2259 */
2260wchar_t * CDECL _wmktemp(wchar_t *pattern)
2261{
2262 int numX = 0;
2263 wchar_t *retVal = pattern;
2264 int id;
2265 wchar_t letter = 'a';
2266
2267 if(!pattern)
2268 return NULL;
2269
2270 while(*pattern)
2271 numX = (*pattern++ == 'X')? numX + 1 : 0;
2272 if (numX < 6)
2273 return NULL;
2274 pattern--;
2275 id = GetCurrentProcessId();
2276 numX = 6;
2277 while(numX--)
2278 {
2279 int tempNum = id / 10;
2280 *pattern-- = id - (tempNum * 10) + '0';
2281 id = tempNum;
2282 }
2283 pattern++;
2284 do
2285 {
2287 return retVal;
2288 *pattern = letter++;
2289 } while(letter != '|');
2290 return NULL;
2291}
2292
2293static unsigned split_oflags(unsigned oflags)
2294{
2295 int wxflags = 0;
2296 unsigned unsupp; /* until we support everything */
2297
2298 if (oflags & _O_APPEND) wxflags |= WX_APPEND;
2299 if (oflags & _O_BINARY) {/* Nothing to do */}
2300 else if (oflags & _O_TEXT) wxflags |= WX_TEXT;
2301 else if (oflags & _O_WTEXT) wxflags |= WX_TEXT;
2302 else if (oflags & _O_U16TEXT) wxflags |= WX_TEXT;
2303 else if (oflags & _O_U8TEXT) wxflags |= WX_TEXT;
2304 else
2305 {
2306 int fmode;
2307 _get_fmode(&fmode);
2308 if (!(fmode & _O_BINARY)) wxflags |= WX_TEXT; /* default to TEXT*/
2309 }
2310 if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
2311
2312 if ((unsupp = oflags & ~(_O_BINARY | _O_TEXT | _O_APPEND | _O_TRUNC | _O_EXCL | _O_CREAT |
2315 ERR(":unsupported oflags %#x\n",unsupp);
2316
2317 return wxflags;
2318}
2319
2320/*********************************************************************
2321 * _pipe (MSVCRT.@)
2322 */
2323int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
2324{
2325 int ret = -1;
2327 HANDLE readHandle, writeHandle;
2328
2329 if (!pfds)
2330 {
2331 *_errno() = EINVAL;
2332 return -1;
2333 }
2334
2335 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
2336 sa.bInheritHandle = !(textmode & _O_NOINHERIT);
2337 sa.lpSecurityDescriptor = NULL;
2338 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
2339 {
2340 unsigned int wxflags = split_oflags(textmode);
2341 int fd;
2342
2343 fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
2344 if (fd != -1)
2345 {
2346 pfds[0] = fd;
2347 fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
2348 if (fd != -1)
2349 {
2350 pfds[1] = fd;
2351 ret = 0;
2352 }
2353 else
2354 {
2355 _close(pfds[0]);
2356 CloseHandle(writeHandle);
2357 *_errno() = EMFILE;
2358 }
2359 }
2360 else
2361 {
2362 CloseHandle(readHandle);
2363 CloseHandle(writeHandle);
2364 *_errno() = EMFILE;
2365 }
2366 }
2367 else
2369
2370 return ret;
2371}
2372
2373static int check_bom(HANDLE h, int oflags, BOOL seek)
2374{
2375 char bom[sizeof(utf8_bom)];
2376 DWORD r;
2377
2378 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
2379 return oflags;
2380
2381 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
2382 oflags = (oflags & ~(_O_WTEXT | _O_U16TEXT)) | _O_U8TEXT;
2383 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
2384 if (seek && r>2)
2386 oflags = (oflags & ~(_O_WTEXT | _O_U8TEXT)) | _O_U16TEXT;
2387 }else if (seek) {
2389 }
2390
2391 return oflags;
2392}
2393
2394/*********************************************************************
2395 * _wsopen_dispatch (UCRTBASE.@)
2396 */
2397int CDECL _wsopen_dispatch( const wchar_t* path, int oflags, int shflags, int pmode,
2398 int *fd, int secure )
2399{
2400 DWORD access = 0, creation = 0, attrib;
2403 int wxflag;
2404 HANDLE hand;
2405
2406 TRACE("path: (%s) oflags: %#x shflags: %#x pmode: %#x fd*: %p secure: %d\n",
2407 debugstr_w(path), oflags, shflags, pmode, fd, secure);
2408
2409 if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
2410 *fd = -1;
2411 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
2412
2413 wxflag = split_oflags(oflags);
2414 switch (oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
2415 {
2416 case _O_RDONLY: access |= GENERIC_READ; break;
2417 case _O_WRONLY: access |= GENERIC_WRITE; break;
2418 case _O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
2419 }
2420
2421 if (oflags & _O_CREAT)
2422 {
2423 if (secure && !MSVCRT_CHECK_PMT(!(pmode & ~(_S_IREAD | _S_IWRITE))))
2424 return EINVAL;
2425
2426 if (oflags & _O_EXCL)
2427 creation = CREATE_NEW;
2428 else if (oflags & _O_TRUNC)
2429 creation = CREATE_ALWAYS;
2430 else
2431 creation = OPEN_ALWAYS;
2432 }
2433 else /* no _O_CREAT */
2434 {
2435 if (oflags & _O_TRUNC)
2436 creation = TRUNCATE_EXISTING;
2437 else
2438 creation = OPEN_EXISTING;
2439 }
2440
2441 switch( shflags )
2442 {
2443 case _SH_DENYRW:
2444 sharing = 0L;
2445 break;
2446 case _SH_DENYWR:
2448 break;
2449 case _SH_DENYRD:
2451 break;
2452 case _SH_DENYNO:
2454 break;
2455 default:
2456 ERR( "Unhandled shflags %#x\n", shflags );
2457 return EINVAL;
2458 }
2459
2460 if (!(pmode & ~MSVCRT_umask & _S_IWRITE))
2461 attrib = FILE_ATTRIBUTE_READONLY;
2462 else
2463 attrib = FILE_ATTRIBUTE_NORMAL;
2464
2465 if (oflags & _O_TEMPORARY)
2466 {
2467 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
2468 access |= DELETE;
2470 }
2471
2472 if (oflags & _O_RANDOM)
2473 attrib |= FILE_FLAG_RANDOM_ACCESS;
2474 if (oflags & _O_SEQUENTIAL)
2475 attrib |= FILE_FLAG_SEQUENTIAL_SCAN;
2476 if (oflags & _O_SHORT_LIVED)
2477 attrib |= FILE_ATTRIBUTE_TEMPORARY;
2478
2479 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
2480 sa.lpSecurityDescriptor = NULL;
2481 sa.bInheritHandle = !(oflags & _O_NOINHERIT);
2482
2483 if ((oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
2484 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
2485 && !(access&GENERIC_READ))
2486 {
2489 if (hand != INVALID_HANDLE_VALUE)
2490 {
2491 oflags = check_bom(hand, oflags, FALSE);
2492 CloseHandle(hand);
2493 }
2494 }
2495
2496 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
2497 if (hand == INVALID_HANDLE_VALUE) {
2498 WARN(":failed-last error (%ld)\n", GetLastError());
2500 return *_errno();
2501 }
2502
2503 if (oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
2504 {
2505 LARGE_INTEGER size = {{0}};
2506
2507 if ((access & GENERIC_WRITE) && (creation==OPEN_EXISTING || creation==OPEN_ALWAYS))
2508 GetFileSizeEx(hand, &size);
2509
2510 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
2511 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
2512 || ((creation==OPEN_EXISTING || creation==OPEN_ALWAYS) && !size.QuadPart)))
2513 {
2514 if (oflags & _O_U8TEXT)
2515 {
2516 DWORD written = 0, tmp;
2517
2518 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
2519 sizeof(utf8_bom)-written, &tmp, NULL))
2520 written += tmp;
2521 if (written != sizeof(utf8_bom)) {
2522 WARN("error writing BOM\n");
2523 CloseHandle(hand);
2525 return *_errno();
2526 }
2527 }
2528 else
2529 {
2530 DWORD written = 0, tmp;
2531
2532 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
2533 sizeof(utf16_bom)-written, &tmp, NULL))
2534 written += tmp;
2535 if (written != sizeof(utf16_bom))
2536 {
2537 WARN("error writing BOM\n");
2538 CloseHandle(hand);
2540 return *_errno();
2541 }
2542 oflags |= _O_U16TEXT;
2543 }
2544 }
2545 else if (access & GENERIC_READ)
2546 oflags = check_bom(hand, oflags, TRUE);
2547 }
2548
2549 type = GetFileType(hand);
2550 if (type == FILE_TYPE_CHAR)
2551 wxflag |= WX_TTY;
2552 else if (type == FILE_TYPE_PIPE)
2553 wxflag |= WX_PIPE;
2554
2555 *fd = msvcrt_alloc_fd(hand, wxflag);
2556 if (*fd == -1)
2557 return *_errno();
2558
2559 if (oflags & _O_WTEXT)
2561
2562 if (oflags & _O_U16TEXT)
2564 else if (oflags & _O_U8TEXT)
2566
2567 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
2568 return 0;
2569}
2570
2571
2572/*********************************************************************
2573 * _wsopen_s (MSVCRT.@)
2574 */
2575int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int pmode )
2576{
2577 return _wsopen_dispatch( path, oflags, shflags, pmode, fd, 1 );
2578}
2579
2580/*********************************************************************
2581 * _wsopen (MSVCRT.@)
2582 */
2583int WINAPIV _wsopen( const wchar_t *path, int oflags, int shflags, ... )
2584{
2585 int pmode;
2586 int fd;
2587
2588 if (oflags & _O_CREAT)
2589 {
2590 va_list ap;
2591
2592 va_start(ap, shflags);
2593 pmode = va_arg(ap, int);
2594 va_end(ap);
2595 }
2596 else
2597 pmode = 0;
2598
2599 return _wsopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2600}
2601
2602
2603/*********************************************************************
2604 * _sopen_dispatch (UCRTBASE.@)
2605 */
2606int CDECL _sopen_dispatch( const char *path, int oflags, int shflags,
2607 int pmode, int *fd, int secure)
2608{
2609 wchar_t *pathW = NULL;
2610 int ret;
2611
2612 if (!MSVCRT_CHECK_PMT(fd != NULL))
2613 return EINVAL;
2614 *fd = -1;
2615 if (path && !(pathW = wstrdupa_utf8(path))) return *_errno();
2616
2617 ret = _wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
2618 free(pathW);
2619 return ret;
2620}
2621
2622/*********************************************************************
2623 * _sopen_s (MSVCRT.@)
2624 */
2625int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
2626{
2627 return _sopen_dispatch(path, oflags, shflags, pmode, fd, 1);
2628}
2629
2630/*********************************************************************
2631 * _sopen (MSVCRT.@)
2632 */
2633int WINAPIV _sopen( const char *path, int oflags, int shflags, ... )
2634{
2635 int pmode;
2636 int fd;
2637
2638 if (oflags & _O_CREAT)
2639 {
2640 va_list ap;
2641
2642 va_start(ap, shflags);
2643 pmode = va_arg(ap, int);
2644 va_end(ap);
2645 }
2646 else
2647 pmode = 0;
2648
2649 return _sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
2650}
2651
2652/*********************************************************************
2653 * _open (MSVCRT.@)
2654 */
2655int WINAPIV _open( const char *path, int flags, ... )
2656{
2657 va_list ap;
2658
2659 if (flags & _O_CREAT)
2660 {
2661 int pmode;
2662 va_start(ap, flags);
2663 pmode = va_arg(ap, int);
2664 va_end(ap);
2665 return _sopen( path, flags, _SH_DENYNO, pmode );
2666 }
2667 else
2668 return _sopen( path, flags, _SH_DENYNO);
2669}
2670
2671/*********************************************************************
2672 * _wopen (MSVCRT.@)
2673 */
2674int WINAPIV _wopen(const wchar_t *path,int flags,...)
2675{
2676 va_list ap;
2677
2678 if (flags & _O_CREAT)
2679 {
2680 int pmode;
2681 va_start(ap, flags);
2682 pmode = va_arg(ap, int);
2683 va_end(ap);
2684 return _wsopen( path, flags, _SH_DENYNO, pmode );
2685 }
2686 else
2687 return _wsopen( path, flags, _SH_DENYNO);
2688}
2689
2690/*********************************************************************
2691 * _creat (MSVCRT.@)
2692 */
2693int CDECL _creat(const char *path, int pmode)
2694{
2695 int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
2696 return _open(path, flags, pmode);
2697}
2698
2699/*********************************************************************
2700 * _wcreat (MSVCRT.@)
2701 */
2702int CDECL _wcreat(const wchar_t *path, int pmode)
2703{
2704 int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
2705 return _wopen(path, flags, pmode);
2706}
2707
2708/*********************************************************************
2709 * _open_osfhandle (MSVCRT.@)
2710 */
2712{
2713 DWORD flags;
2714 int fd;
2715
2716 /* _O_RDONLY (0) always matches, so set the read flag
2717 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2718 * file, so set the write flag. It also only sets _O_TEXT if it wants
2719 * text - it never sets _O_BINARY.
2720 */
2721 /* don't let split_oflags() decide the mode if no mode is passed */
2722 if (!(oflags & (_O_BINARY | _O_TEXT)))
2723 oflags |= _O_BINARY;
2724
2727 {
2729 return -1;
2730 }
2731
2732 if (flags == FILE_TYPE_CHAR)
2733 flags = WX_TTY;
2734 else if (flags == FILE_TYPE_PIPE)
2735 flags = WX_PIPE;
2736 else
2737 flags = 0;
2738 flags |= split_oflags(oflags);
2739
2741 TRACE(":handle (%Iu) fd (%d) flags %#lx\n", handle, fd, flags);
2742 return fd;
2743}
2744
2745/*********************************************************************
2746 * _rmtmp (MSVCRT.@)
2747 */
2748int CDECL _rmtmp(void)
2749{
2750 int num_removed = 0, i;
2751 FILE *file;
2752
2753 LOCK_FILES();
2754 for (i = 3; i < MSVCRT_stream_idx; i++) {
2756
2757 if (file->_tmpfname)
2758 {
2759 fclose(file);
2760 num_removed++;
2761 }
2762 }
2763 UNLOCK_FILES();
2764
2765 if (num_removed)
2766 TRACE(":removed (%d) temp files\n",num_removed);
2767 return num_removed;
2768}
2769
2770static inline int get_utf8_char_len(char ch)
2771{
2772 if((ch&0xf8) == 0xf0)
2773 return 4;
2774 else if((ch&0xf0) == 0xe0)
2775 return 3;
2776 else if((ch&0xe0) == 0xc0)
2777 return 2;
2778 return 1;
2779}
2780
2781/*********************************************************************
2782 * (internal) read_utf8
2783 */
2784static int read_utf8(ioinfo *fdinfo, wchar_t *buf, unsigned int count)
2785{
2786 HANDLE hand = fdinfo->handle;
2787 char min_buf[4], *readbuf, lookahead;
2788 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2789
2790 /* make the buffer big enough to hold at least one character */
2791 /* read bytes have to fit to output and lookahead buffers */
2792 count /= 2;
2793 readbuf_size = count < 4 ? 4 : count;
2794 if(readbuf_size<=4 || !(readbuf = malloc(readbuf_size))) {
2795 readbuf_size = 4;
2796 readbuf = min_buf;
2797 }
2798
2799 if(fdinfo->lookahead[0] != '\n') {
2800 readbuf[pos++] = fdinfo->lookahead[0];
2801 fdinfo->lookahead[0] = '\n';
2802
2803 if(fdinfo->lookahead[1] != '\n') {
2804 readbuf[pos++] = fdinfo->lookahead[1];
2805 fdinfo->lookahead[1] = '\n';
2806
2807 if(fdinfo->lookahead[2] != '\n') {
2808 readbuf[pos++] = fdinfo->lookahead[2];
2809 fdinfo->lookahead[2] = '\n';
2810 }
2811 }
2812 }
2813
2814 /* NOTE: this case is broken in native dll, reading
2815 * sometimes fails when small buffer is passed
2816 */
2817 if(count < 4) {
2818 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2820 fdinfo->wxflag |= WX_ATEOF;
2821 return 0;
2822 }else {
2825 *_errno() = EBADF;
2826 return -1;
2827 }
2828 }else if(!num_read) {
2829 fdinfo->wxflag |= WX_ATEOF;
2830 return 0;
2831 }else {
2832 pos++;
2833 }
2834
2835 char_len = get_utf8_char_len(readbuf[0]);
2836 if(char_len>pos) {
2837 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2838 pos += num_read;
2839 }
2840
2841 if(readbuf[0] == '\n')
2842 fdinfo->wxflag |= WX_READNL;
2843 else
2844 fdinfo->wxflag &= ~WX_READNL;
2845
2846 if(readbuf[0] == 0x1a) {
2847 fdinfo->wxflag |= WX_ATEOF;
2848 return 0;
2849 }
2850
2851 if(readbuf[0] == '\r') {
2852 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2853 buf[0] = '\r';
2854 else if(lookahead == '\n')
2855 buf[0] = '\n';
2856 else {
2857 buf[0] = '\r';
2858 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2859 fdinfo->lookahead[0] = lookahead;
2860 else
2861 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2862 }
2863 return 2;
2864 }
2865
2866 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2868 return -1;
2869 }
2870
2871 return num_read*2;
2872 }
2873
2874 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2875 if(pos) {
2876 num_read = 0;
2877 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2878 fdinfo->wxflag |= WX_ATEOF;
2879 if (readbuf != min_buf) free(readbuf);
2880 return 0;
2881 }else {
2884 *_errno() = EBADF;
2885 if (readbuf != min_buf) free(readbuf);
2886 return -1;
2887 }
2888 }else if(!pos && !num_read) {
2889 fdinfo->wxflag |= WX_ATEOF;
2890 if (readbuf != min_buf) free(readbuf);
2891 return 0;
2892 }
2893
2894 pos += num_read;
2895 if(readbuf[0] == '\n')
2896 fdinfo->wxflag |= WX_READNL;
2897 else
2898 fdinfo->wxflag &= ~WX_READNL;
2899
2900 /* Find first byte of last character (may be incomplete) */
2901 for(i=pos-1; i>0 && i>pos-4; i--)
2902 if((readbuf[i]&0xc0) != 0x80)
2903 break;
2904 char_len = get_utf8_char_len(readbuf[i]);
2905 if(char_len+i <= pos)
2906 i += char_len;
2907
2908 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2909 if(i < pos)
2910 fdinfo->lookahead[0] = readbuf[i];
2911 if(i+1 < pos)
2912 fdinfo->lookahead[1] = readbuf[i+1];
2913 if(i+2 < pos)
2914 fdinfo->lookahead[2] = readbuf[i+2];
2915 }else if(i < pos) {
2917 }
2918 pos = i;
2919
2920 for(i=0, j=0; i<pos; i++) {
2921 if(readbuf[i] == 0x1a) {
2922 fdinfo->wxflag |= WX_ATEOF;
2923 break;
2924 }
2925
2926 /* strip '\r' if followed by '\n' */
2927 if(readbuf[i] == '\r' && i+1==pos) {
2928 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2929 readbuf[j++] = '\r';
2930 }else if(lookahead == '\n' && j==0) {
2931 readbuf[j++] = '\n';
2932 }else {
2933 if(lookahead != '\n')
2934 readbuf[j++] = '\r';
2935
2936 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2937 fdinfo->lookahead[0] = lookahead;
2938 else
2939 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2940 }
2941 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2942 readbuf[j++] = readbuf[i];
2943 }
2944 }
2945 pos = j;
2946
2947 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2949 if (readbuf != min_buf) free(readbuf);
2950 return -1;
2951 }
2952
2953 if (readbuf != min_buf) free(readbuf);
2954 return num_read*2;
2955}
2956
2957/*********************************************************************
2958 * (internal) read_i
2959 *
2960 * When reading \r as last character in text mode, read() positions
2961 * the file pointer on the \r character while getc() goes on to
2962 * the following \n
2963 */
2964static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
2965{
2966 DWORD num_read, utf16;
2967 char *bufstart = buf;
2968
2969 if (count == 0)
2970 return 0;
2971
2972 if (fdinfo->wxflag & WX_ATEOF) {
2973 TRACE("already at EOF, returning 0\n");
2974 return 0;
2975 }
2976 /* Don't trace small reads, it gets *very* annoying */
2977 if (count > 4)
2978 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2979 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2980 {
2981 *_errno() = EBADF;
2982 return -1;
2983 }
2984
2985 utf16 = ioinfo_get_textmode(fdinfo) == TEXTMODE_UTF16LE;
2986 if (ioinfo_get_textmode(fdinfo) != TEXTMODE_ANSI && count&1)
2987 {
2988 *_errno() = EINVAL;
2989 return -1;
2990 }
2991
2992 if((fdinfo->wxflag&WX_TEXT) && ioinfo_get_textmode(fdinfo) == TEXTMODE_UTF8)
2993 return read_utf8(fdinfo, buf, count);
2994
2995 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2996 {
2997 if (fdinfo->lookahead[0] != '\n')
2998 {
2999 bufstart[0] = fdinfo->lookahead[0];
3000 fdinfo->lookahead[0] = '\n';
3001
3002 if (utf16)
3003 {
3004 bufstart[1] = fdinfo->lookahead[1];
3005 fdinfo->lookahead[1] = '\n';
3006 }
3007
3008 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
3009 num_read += 1+utf16;
3010 else
3011 num_read = 1+utf16;
3012 }
3013
3014 if(utf16 && (num_read&1))
3015 {
3016 /* msvcr90 uses uninitialized value from the buffer in this case */
3017 /* msvcrt ignores additional data */
3018 ERR("got odd number of bytes in UTF16 mode\n");
3019 num_read--;
3020 }
3021
3022 if (count != 0 && num_read == 0)
3023 {
3024 fdinfo->wxflag |= WX_ATEOF;
3025 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
3026 }
3027 else if (fdinfo->wxflag & WX_TEXT)
3028 {
3029 DWORD i, j;
3030
3031 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
3032 fdinfo->wxflag |= WX_READNL;
3033 else
3034 fdinfo->wxflag &= ~WX_READNL;
3035
3036 for (i=0, j=0; i<num_read; i+=1+utf16)
3037 {
3038 /* in text mode, a ctrl-z signals EOF */
3039 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
3040 {
3041 fdinfo->wxflag |= WX_ATEOF;
3042 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
3043 break;
3044 }
3045
3046 /* in text mode, strip \r if followed by \n */
3047 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
3048 {
3049 char lookahead[2];
3050 DWORD len;
3051
3052 lookahead[1] = '\n';
3053 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
3054 {
3055 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
3056 {
3057 bufstart[j++] = '\n';
3058 if(utf16) bufstart[j++] = 0;
3059 }
3060 else
3061 {
3062 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
3063 {
3064 bufstart[j++] = '\r';
3065 if(utf16) bufstart[j++] = 0;
3066 }
3067
3068 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
3069 {
3070 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
3071 {
3072 bufstart[j++] = '\n';
3073 if (utf16) bufstart[j++] = 0;
3074 }
3075 else
3076 {
3077 fdinfo->lookahead[0] = lookahead[0];
3078 fdinfo->lookahead[1] = lookahead[1];
3079 }
3080 }
3081 else
3082 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
3083 }
3084 }
3085 else
3086 {
3087 bufstart[j++] = '\r';
3088 if(utf16) bufstart[j++] = 0;
3089 }
3090 }
3091 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
3092 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
3093 {
3094 bufstart[j++] = bufstart[i];
3095 if(utf16) bufstart[j++] = bufstart[i+1];
3096 }
3097 }
3098 num_read = j;
3099 }
3100 }
3101 else
3102 {
3104 {
3105 TRACE(":end-of-pipe\n");
3106 fdinfo->wxflag |= WX_ATEOF;
3107 return 0;
3108 }
3109 else
3110 {
3111 TRACE(":failed-last error (%ld)\n", GetLastError());
3114 *_errno() = EBADF;
3115 return -1;
3116 }
3117 }
3118
3119 if (count > 4)
3120 TRACE("(%lu), %s\n", num_read, debugstr_an(buf, num_read));
3121 return num_read;
3122}
3123
3124/*********************************************************************
3125 * _read (MSVCRT.@)
3126 */
3127int CDECL _read(int fd, void *buf, unsigned int count)
3128{
3129 ioinfo *info;
3130 int num_read;
3131
3132 if(fd == MSVCRT_NO_CONSOLE_FD) {
3133 *_errno() = EBADF;
3134 return -1;
3135 }
3136
3137 info = get_ioinfo(fd);
3138 num_read = read_i(fd, info, buf, count);
3140 return num_read;
3141}
3142
3143/*********************************************************************
3144 * _setmode (MSVCRT.@)
3145 */
3146int CDECL _setmode(int fd,int mode)
3147{
3149 int ret = info->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY;
3150
3152 ret = _O_WTEXT;
3153
3155 && mode!=_O_U16TEXT && mode!=_O_U8TEXT) {
3156 *_errno() = EINVAL;
3158 return -1;
3159 }
3160
3161 if(info == &MSVCRT___badioinfo) {
3162 *_errno() = EBADF;
3163 return EOF;
3164 }
3165
3166 if(mode == _O_BINARY) {
3167 info->wxflag &= ~WX_TEXT;
3170 return ret;
3171 }
3172
3173 info->wxflag |= WX_TEXT;
3174 if(mode == _O_TEXT)
3176 else if(mode == _O_U8TEXT)
3178 else
3180
3182 return ret;
3183}
3184
3185/*********************************************************************
3186 * _stat64 (MSVCRT.@)
3187 */
3188int CDECL _stat64(const char* path, struct _stat64 * buf)
3189{
3190 wchar_t *pathW = NULL;
3191 int ret;
3192
3193 if (path && !(pathW = wstrdupa_utf8(path))) return -1;
3194 ret = _wstat64(pathW, buf);
3195 free(pathW);
3196 return ret;
3197}
3198
3199/*********************************************************************
3200 * _stati64 (MSVCRT.@)
3201 */
3202int CDECL _stati64(const char* path, struct _stati64 * buf)
3203{
3204 int ret;
3205 struct _stat64 buf64;
3206
3207 ret = _stat64(path, &buf64);
3208 if (!ret)
3210 return ret;
3211}
3212
3213/*********************************************************************
3214 * _stat (MSVCRT.@)
3215 */
3216int CDECL _stat(const char* path, struct _stat * buf)
3217{
3218 int ret;
3219 struct _stat64 buf64;
3220
3221 ret = _stat64( path, &buf64);
3222 if (!ret)
3223 msvcrt_stat64_to_stat(&buf64, buf);
3224 return ret;
3225}
3226
3227#if _MSVCR_VER >= 80
3228
3229/*********************************************************************
3230 * _stat32 (MSVCR80.@)
3231 */
3232int CDECL _stat32(const char *path, struct _stat32 *buf)
3233{
3234 int ret;
3235 struct _stat64 buf64;
3236
3237 ret = _stat64(path, &buf64);
3238 if (!ret)
3240 return ret;
3241}
3242
3243/*********************************************************************
3244 * _stat32i64 (MSVCR80.@)
3245 */
3246int CDECL _stat32i64(const char *path, struct _stat32i64 *buf)
3247{
3248 int ret;
3249 struct _stat64 buf64;
3250
3251 ret = _stat64(path, &buf64);
3252 if (!ret)
3254 return ret;
3255}
3256
3257/*********************************************************************
3258 * _stat64i32 (MSVCR80.@)
3259 */
3260int CDECL _stat64i32(const char* path, struct _stat64i32 *buf)
3261{
3262 int ret;
3263 struct _stat64 buf64;
3264
3265 ret = _stat64(path, &buf64);
3266 if (!ret)
3268 return ret;
3269}
3270
3271#endif /* _MSVCR_VER >= 80 */
3272
3273/*********************************************************************
3274 * _wstat64 (MSVCRT.@)
3275 */
3276int CDECL _wstat64(const wchar_t* path, struct _stat64 * buf)
3277{
3278 DWORD dw;
3280 unsigned short mode = ALL_S_IREAD;
3281 int plen;
3282
3283 TRACE(":file (%s) buf(%p)\n", debugstr_w(path), buf);
3284
3285 plen = wcslen(path);
3286 while (plen && path[plen-1]==' ')
3287 plen--;
3288
3289 if (plen==2 && path[1]==':')
3290 {
3291 *_errno() = ENOENT;
3292 return -1;
3293 }
3294
3295#if _MSVCR_VER<140
3296 if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
3297 {
3298 *_errno() = ENOENT;
3299 return -1;
3300 }
3301#endif
3302
3304 {
3305 TRACE("failed (%ld)\n", GetLastError());
3306 *_errno() = ENOENT;
3307 return -1;
3308 }
3309
3310 memset(buf,0,sizeof(struct _stat64));
3311
3312 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
3313 if (iswalpha(*path) && path[1] == ':')
3314 buf->st_dev = buf->st_rdev = towupper(*path) - 'A'; /* drive num */
3315 else
3316 buf->st_dev = buf->st_rdev = _getdrive() - 1;
3317
3318 /* Dir, or regular file? */
3320 mode |= (_S_IFDIR | ALL_S_IEXEC);
3321 else
3322 {
3323 mode |= _S_IFREG;
3324 /* executable? */
3325 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
3326 {
3327 ULONGLONG ext = towlower(path[plen-1]) | (towlower(path[plen-2]) << 16) |
3328 ((ULONGLONG)towlower(path[plen-3]) << 32);
3329 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
3330 mode |= ALL_S_IEXEC;
3331 }
3332 }
3333
3335 mode |= ALL_S_IWRITE;
3336
3337 buf->st_mode = mode;
3338 buf->st_nlink = 1;
3339 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
3341 buf->st_atime = dw;
3343 buf->st_mtime = buf->st_ctime = dw;
3344 TRACE("%d %d %#I64x %I64d %I64d %I64d\n", buf->st_mode, buf->st_nlink,
3345 buf->st_size, buf->st_atime, buf->st_mtime, buf->st_ctime);
3346 return 0;
3347}
3348
3349/*********************************************************************
3350 * _wstati64 (MSVCRT.@)
3351 */
3352int CDECL _wstati64(const wchar_t* path, struct _stati64 * buf)
3353{
3354 int ret;
3355 struct _stat64 buf64;
3356
3357 ret = _wstat64(path, &buf64);
3358 if (!ret)
3360 return ret;
3361}
3362
3363/*********************************************************************
3364 * _wstat (MSVCRT.@)
3365 */
3366int CDECL _wstat(const wchar_t* path, struct _stat * buf)
3367{
3368 int ret;
3369 struct _stat64 buf64;
3370
3371 ret = _wstat64( path, &buf64 );
3372 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
3373 return ret;
3374}
3375
3376#if _MSVCR_VER >= 80
3377
3378/*********************************************************************
3379 * _wstat32 (MSVCR80.@)
3380 */
3381int CDECL _wstat32(const wchar_t *path, struct _stat32 *buf)
3382{
3383 int ret;
3384 struct _stat64 buf64;
3385
3386 ret = _wstat64(path, &buf64);
3387 if (!ret)
3389 return ret;
3390}
3391
3392/*********************************************************************
3393 * _wstat32i64 (MSVCR80.@)
3394 */
3395int CDECL _wstat32i64(const wchar_t *path, struct _stat32i64 *buf)
3396{
3397 int ret;
3398 struct _stat64 buf64;
3399
3400 ret = _wstat64(path, &buf64);
3401 if (!ret)
3403 return ret;
3404}
3405
3406/*********************************************************************
3407 * _wstat64i32 (MSVCR80.@)
3408 */
3409int CDECL _wstat64i32(const wchar_t *path, struct _stat64i32 *buf)
3410{
3411 int ret;
3412 struct _stat64 buf64;
3413
3414 ret = _wstat64(path, &buf64);
3415 if (!ret)
3417 return ret;
3418}
3419
3420#endif /* _MSVCR_VER >= 80 */
3421
3422/*********************************************************************
3423 * _tell (MSVCRT.@)
3424 */
3426{
3427 return _lseek(fd, 0, SEEK_CUR);
3428}
3429
3430/*********************************************************************
3431 * _telli64 (MSVCRT.@)
3432 */
3434{
3435 return _lseeki64(fd, 0, SEEK_CUR);
3436}
3437
3438/*********************************************************************
3439 * _tempnam (MSVCRT.@)
3440 */
3441char * CDECL _tempnam(const char *dir, const char *prefix)
3442{
3443 wchar_t *dirW = NULL, *prefixW = NULL, *retW;
3444 char *ret;
3445
3446 if (dir && !(dirW = wstrdupa_utf8(dir))) return NULL;
3447 if (prefix && !(prefixW = wstrdupa_utf8(prefix)))
3448 {
3449 free(dirW);
3450 return NULL;
3451 }
3452 retW = _wtempnam(dirW, prefixW);
3453 free(dirW);
3454 free(prefixW);
3455 /* TODO: don't do the conversion */
3456 ret = astrdupw_utf8(retW);
3457 free(retW);
3458 return ret;
3459}
3460
3461/*********************************************************************
3462 * _wtempnam (MSVCRT.@)
3463 */
3464wchar_t * CDECL _wtempnam(const wchar_t *dir, const wchar_t *prefix)
3465{
3466 wchar_t tmpbuf[MAX_PATH];
3467 const wchar_t *tmp_dir = _wgetenv(L"TMP");
3468
3469 if (tmp_dir) dir = tmp_dir;
3470
3471 TRACE("dir (%s) prefix (%s)\n", debugstr_w(dir), debugstr_w(prefix));
3472 /* TODO: use whole prefix */
3473 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
3474 {
3475 TRACE("got name (%s)\n", debugstr_w(tmpbuf));
3476 DeleteFileW(tmpbuf);
3477 return _wcsdup(tmpbuf);
3478 }
3479 TRACE("failed (%ld)\n", GetLastError());
3480 return NULL;
3481}
3482
3483/*********************************************************************
3484 * _umask (MSVCRT.@)
3485 */
3487{
3488 int old_umask = MSVCRT_umask;
3489 TRACE("(%d)\n",umask);
3491 return old_umask;
3492}
3493
3494/*********************************************************************
3495 * _utime64 (MSVCRT.@)
3496 */
3497int CDECL _utime64(const char* path, struct __utimbuf64 *t)
3498{
3499 int fd = _open(path, _O_WRONLY | _O_BINARY);
3500
3501 if (fd > 0)
3502 {
3503 int retVal = _futime64(fd, t);
3504 _close(fd);
3505 return retVal;
3506 }
3507 return -1;
3508}
3509
3510/*********************************************************************
3511 * _utime32 (MSVCRT.@)
3512 */
3513int CDECL _utime32(const char* path, struct __utimbuf32 *t)
3514{
3515 if (t)
3516 {
3517 struct __utimbuf64 t64;
3518 t64.actime = t->actime;
3519 t64.modtime = t->modtime;
3520 return _utime64( path, &t64 );
3521 }
3522 else
3523 return _utime64( path, NULL );
3524}
3525
3526/*********************************************************************
3527 * _wutime64 (MSVCRT.@)
3528 */
3529int CDECL _wutime64(const wchar_t* path, struct __utimbuf64 *t)
3530{
3531 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
3532
3533 if (fd > 0)
3534 {
3535 int retVal = _futime64(fd, t);
3536 _close(fd);
3537 return retVal;
3538 }
3539 return -1;
3540}
3541
3542/*********************************************************************
3543 * _wutime32 (MSVCRT.@)
3544 */
3545int CDECL _wutime32(const wchar_t* path, struct __utimbuf32 *t)
3546{
3547 if (t)
3548 {
3549 struct __utimbuf64 t64;
3550 t64.actime = t->actime;
3551 t64.modtime = t->modtime;
3552 return _wutime64( path, &t64 );
3553 }
3554 else
3555 return _wutime64( path, NULL );
3556}
3557
3558/*********************************************************************
3559 * _write (MSVCRT.@)
3560 */
3561int CDECL _write(int fd, const void* buf, unsigned int count)
3562{
3564 HANDLE hand = info->handle;
3565 DWORD num_written, i;
3566 BOOL console = FALSE;
3567
3569 {
3570 *_errno() = EBADF;
3572 return -1;
3573 }
3574
3576 {
3577 *_errno() = EINVAL;
3579 return -1;
3580 }
3581
3582 /* If appending, go to EOF */
3583 if (info->wxflag & WX_APPEND)
3584 _lseek(fd, 0, FILE_END);
3585
3586 if (!(info->wxflag & WX_TEXT))
3587 {
3588 if (!WriteFile(hand, buf, count, &num_written, NULL)
3589 || num_written != count)
3590 {
3591 TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
3592 hand, GetLastError());
3595 *_errno() = EBADF;
3596 num_written = -1;
3597 }
3598
3600 return num_written;
3601 }
3602
3603 if (_isatty(fd)) console = VerifyConsoleIoHandle(hand);
3604 for (i = 0; i < count;)
3605 {
3606 const char *s = buf;
3607 char lfbuf[2048];
3608 DWORD j = 0;
3609
3610 if (ioinfo_get_textmode(info) == TEXTMODE_ANSI && console)
3611 {
3612 char conv[sizeof(lfbuf)];
3613 size_t len = 0;
3614
3615#if _MSVCR_VER >= 80
3616 if (info->dbcsBufferUsed)
3617 {
3618 conv[j++] = info->dbcsBuffer[0];
3619 info->dbcsBufferUsed = FALSE;
3620 conv[j++] = s[i++];
3621 len++;
3622 }
3623#endif
3624
3625 for (; i < count && j < sizeof(conv)-1 &&
3626 len < (sizeof(lfbuf) - 1) / sizeof(WCHAR); i++, j++, len++)
3627 {
3628 if (isleadbyte((unsigned char)s[i]))
3629 {
3630 conv[j++] = s[i++];
3631
3632 if (i == count)
3633 {
3634#if _MSVCR_VER >= 80
3635 info->dbcsBuffer[0] = conv[j-1];
3636 info->dbcsBufferUsed = TRUE;
3637 break;
3638#else
3639 *_errno() = EINVAL;
3641 return -1;
3642#endif
3643 }
3644 }
3645 else if (s[i] == '\n')
3646 {
3647 conv[j++] = '\r';
3648 len++;
3649 }
3650 conv[j] = s[i];
3651 }
3652
3653 len = mbstowcs((WCHAR*)lfbuf, conv, len);
3654 if (len == -1)
3655 {
3658 return -1;
3659 }
3660 j = len * 2;
3661 }
3663 {
3664 for (j = 0; i < count && j < sizeof(lfbuf)-1; i++, j++)
3665 {
3666 if (s[i] == '\n')
3667 lfbuf[j++] = '\r';
3668 lfbuf[j] = s[i];
3669 }
3670 }
3671 else if (ioinfo_get_textmode(info) == TEXTMODE_UTF16LE || console)
3672 {
3673 for (j = 0; i < count && j < sizeof(lfbuf)-3; i++, j++)
3674 {
3675 if (s[i] == '\n' && !s[i+1])
3676 {
3677 lfbuf[j++] = '\r';
3678 lfbuf[j++] = 0;
3679 }
3680 lfbuf[j++] = s[i++];
3681 lfbuf[j] = s[i];
3682 }
3683 }
3684 else
3685 {
3686 char conv[sizeof(lfbuf)/4];
3687
3688 for (j = 0; i < count && j < sizeof(conv)-3; i++, j++)
3689 {
3690 if (s[i] == '\n' && !s[i+1])
3691 {
3692 conv[j++] = '\r';
3693 conv[j++] = 0;
3694 }
3695 conv[j++] = s[i++];
3696 conv[j] = s[i];
3697 }
3698
3699 j = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)conv, j/2, lfbuf, sizeof(lfbuf), NULL, NULL);
3700 if (!j)
3701 {
3704 return -1;
3705 }
3706 }
3707
3708 if (console)
3709 {
3710 j = j/2;
3711 if (!WriteConsoleW(hand, lfbuf, j, &num_written, NULL))
3712 num_written = -1;
3713 }
3714 else if (!WriteFile(hand, lfbuf, j, &num_written, NULL))
3715 {
3716 num_written = -1;
3717 }
3718
3719 if (num_written != j)
3720 {
3721 TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%ld)\n", fd,
3722 hand, GetLastError());
3725 *_errno() = EBADF;
3727 return -1;
3728 }
3729 }
3730
3732 return count;
3733}
3734
3735/*********************************************************************
3736 * _putw (MSVCRT.@)
3737 */
3739{
3740 int len;
3741
3743 len = _write(file->_file, &val, sizeof(val));
3744 if (len == sizeof(val)) {
3746 return val;
3747 }
3748
3749 file->_flag |= _IOERR;
3751 return EOF;
3752}
3753
3754/*********************************************************************
3755 * fclose (MSVCRT.@)
3756 */
3758{
3759 int ret;
3760
3761 if (!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
3762
3766
3767 return ret;
3768}
3769
3770/*********************************************************************
3771 * _fclose_nolock (MSVCRT.@)
3772 */
3774{
3775 int r, flag;
3776
3777 if (!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
3778
3779 if(!(file->_flag & (_IOREAD | _IOWRT | _IORW)))
3780 {
3781 file->_flag = 0;
3782 return EOF;
3783 }
3784
3785 flag = file->_flag;
3786 free(file->_tmpfname);
3787 file->_tmpfname = NULL;
3788 /* flush stdio buffers */
3789 if(file->_flag & _IOWRT)
3791 if(file->_flag & _IOMYBUF)
3792 free(file->_base);
3793
3794 r=_close(file->_file);
3795 file->_flag = 0;
3796
3797 return ((r == -1) || (flag & _IOERR) ? EOF : 0);
3798}
3799
3800/*********************************************************************
3801 * feof (MSVCRT.@)
3802 */
3804{
3805 return file->_flag & _IOEOF;
3806}
3807
3808/*********************************************************************
3809 * ferror (MSVCRT.@)
3810 */
3812{
3813 return file->_flag & _IOERR;
3814}
3815
3816/*********************************************************************
3817 * _filbuf (MSVCRT.@)
3818 */
3820{
3821 unsigned char c;
3822
3823 if(file->_flag & _IOSTRG)
3824 return EOF;
3825
3826 /* Allocate buffer if needed */
3827 if(!(file->_flag & (MSVCRT__NOBUF | _IOMYBUF | MSVCRT__USERBUF)))
3829
3830 if(!(file->_flag & _IOREAD)) {
3831 if(file->_flag & _IORW)
3832 file->_flag |= _IOREAD;
3833 else
3834 return EOF;
3835 }
3836
3837 if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
3838 int r;
3839 if ((r = _read(file->_file,&c,1)) != 1) {
3840 file->_flag |= (r == 0) ? _IOEOF : _IOERR;
3841 return EOF;
3842 }
3843
3844 return c;
3845 } else {
3846 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
3847 if(file->_cnt<=0) {
3848 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
3849 file->_cnt = 0;
3850 return EOF;
3851 }
3852
3853 file->_cnt--;
3854 file->_ptr = file->_base+1;
3855 c = *(unsigned char *)file->_base;
3856 return c;
3857 }
3858}
3859
3860/*********************************************************************
3861 * fgetc (MSVCRT.@)
3862 */
3864{
3865 int ret;
3866
3870
3871 return ret;
3872}
3873
3874/*********************************************************************
3875 * _fgetc_nolock (MSVCRT.@)
3876 */
3878{
3879 unsigned char *i;
3880 unsigned int j;
3881
3882 if (file->_cnt>0) {
3883 file->_cnt--;
3884 i = (unsigned char *)file->_ptr++;
3885 j = *i;
3886 } else
3887 j = _filbuf(file);
3888
3889 return j;
3890}
3891
3892/*********************************************************************
3893 * _fgetchar (MSVCRT.@)
3894 */
3896{
3897 return fgetc(stdin);
3898}
3899
3900/*********************************************************************
3901 * fgets (MSVCRT.@)
3902 */
3903char * CDECL fgets(char *s, int size, FILE* file)
3904{
3905 int cc = EOF;
3906 char * buf_start = s;
3907
3908 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3909 file,file->_file,s,size);
3910
3912
3913 while ((size >1) && (cc = _fgetc_nolock(file)) != EOF && cc != '\n')
3914 {
3915 *s++ = (char)cc;
3916 size --;
3917 }
3918 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
3919 {
3920 TRACE(":nothing read\n");
3922 return NULL;
3923 }
3924 if ((cc != EOF) && (size > 1))
3925 *s++ = cc;
3926 *s = '\0';
3927 TRACE(":got %s\n", debugstr_a(buf_start));
3929 return buf_start;
3930}
3931
3932/*********************************************************************
3933 * fgetwc (MSVCRT.@)
3934 */
3936{
3937 wint_t ret;
3938
3942
3943 return ret;
3944}
3945
3946/*********************************************************************
3947 * _fgetwc_nolock (MSVCRT.@)
3948 */
3950{
3951 wint_t ret;
3952 int ch;
3953
3955 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3956 char *p;
3957
3958 for(p=(char*)&ret; (wint_t*)p<&ret+1; p++) {
3960 if(ch == EOF) {
3961 ret = WEOF;
3962 break;
3963 }
3964 *p = (char)ch;
3965 }
3966 }else {
3967 char mbs[MB_LEN_MAX];
3968 int len = 0;
3969
3971 if(ch != EOF) {
3972 mbs[0] = (char)ch;
3973 if(isleadbyte((unsigned char)mbs[0])) {
3975 if(ch != EOF) {
3976 mbs[1] = (char)ch;
3977 len = 2;
3978 }
3979 }else {
3980 len = 1;
3981 }
3982 }
3983
3984 if(!len || mbtowc(&ret, mbs, len)==-1)
3985 ret = WEOF;
3986 }
3987
3988 return ret;
3989}
3990
3991/*********************************************************************
3992 * _getw (MSVCRT.@)
3993 */
3995{
3996 char *ch;
3997 int i, k;
3998 unsigned int j;
3999 ch = (char *)&i;
4000
4002 for (j=0; j<sizeof(int); j++) {
4003 k = _fgetc_nolock(file);
4004 if (k == EOF) {
4005 file->_flag |= _IOEOF;
4007 return EOF;
4008 }
4009 ch[j] = k;
4010 }
4011
4013 return i;
4014}
4015
4016/*********************************************************************
4017 * getwc (MSVCRT.@)
4018 */
4020{
4021 return fgetwc(file);
4022}
4023
4024/*********************************************************************
4025 * _fgetwchar (MSVCRT.@)
4026 */
4028{
4029 return fgetwc(stdin);
4030}
4031
4032/*********************************************************************
4033 * getwchar (MSVCRT.@)
4034 */
4036{
4037 return _fgetwchar();
4038}
4039
4040/*********************************************************************
4041 * fgetws (MSVCRT.@)
4042 */
4043wchar_t * CDECL fgetws(wchar_t *s, int size, FILE* file)
4044{
4045 wint_t cc = WEOF;
4046 wchar_t * buf_start = s;
4047
4048 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
4049 file,file->_file,s,size);
4050
4052
4053 while ((size >1) && (cc = _fgetwc_nolock(file)) != WEOF && cc != '\n')
4054 {
4055 *s++ = cc;
4056 size --;
4057 }
4058 if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
4059 {
4060 TRACE(":nothing read\n");
4062 return NULL;
4063 }
4064 if ((cc != WEOF) && (size > 1))
4065 *s++ = cc;
4066 *s = 0;
4067 TRACE(":got %s\n", debugstr_w(buf_start));
4069 return buf_start;
4070}
4071
4072/*********************************************************************
4073 * _flsbuf (MSVCRT.@)
4074 */
4076{
4077 /* Flush output buffer */
4078 if(!(file->_flag & (MSVCRT__NOBUF | _IOMYBUF | MSVCRT__USERBUF))) {
4080 }
4081
4082 if(!(file->_flag & _IOWRT)) {
4083 if(!(file->_flag & _IORW)) {
4084 file->_flag |= _IOERR;
4085 *_errno() = EBADF;
4086 return EOF;
4087 }
4088 file->_flag |= _IOWRT;
4089 }
4090 if(file->_flag & _IOREAD) {
4091 if(!(file->_flag & _IOEOF)) {
4092 file->_flag |= _IOERR;
4093 return EOF;
4094 }
4095 file->_cnt = 0;
4096 file->_ptr = file->_base;
4097 file->_flag &= ~(_IOREAD | _IOEOF);
4098 }
4099
4100 if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
4101 int res = 0;
4102
4103 if(file->_cnt <= 0) {
4105 if(res)
4106 return res;
4107 file->_flag |= _IOWRT;
4108 file->_cnt=file->_bufsiz;
4109 }
4110 *file->_ptr++ = c;
4111 file->_cnt--;
4112 return c&0xff;
4113 } else {
4114 unsigned char cc=c;
4115 int len;
4116 /* set _cnt to 0 for unbuffered FILEs */
4117 file->_cnt = 0;
4118 len = _write(file->_file, &cc, 1);
4119 if (len == 1)
4120 return c & 0xff;
4121 file->_flag |= _IOERR;
4122 return EOF;
4123 }
4124}
4125
4126/*********************************************************************
4127 * fwrite (MSVCRT.@)
4128 */
4129size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
4130{
4131 size_t ret;
4132
4134 ret = _fwrite_nolock(ptr, size, nmemb, file);
4136
4137 return ret;
4138}
4139
4140/*********************************************************************
4141 * _fwrite_nolock (MSVCRT.@)
4142 */
4143size_t CDECL _fwrite_nolock(const void *ptr, size_t size, size_t nmemb, FILE* file)
4144{
4145 size_t wrcnt=size * nmemb;
4146 int written = 0;
4147 if (size == 0)
4148 return 0;
4149
4150 while(wrcnt) {
4151 if(file->_cnt < 0) {
4152 WARN("negative file->_cnt value in %p\n", file);
4153 file->_flag |= _IOERR;
4154 break;
4155 } else if(file->_cnt) {
4156 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
4157 memcpy(file->_ptr, ptr, pcnt);
4158 file->_cnt -= pcnt;
4159 file->_ptr += pcnt;
4160 written += pcnt;
4161 wrcnt -= pcnt;
4162 ptr = (const char*)ptr + pcnt;
4163 } else if((file->_flag & MSVCRT__NOBUF)
4164 || ((file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
4165 || (!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
4166 size_t pcnt;
4167 int bufsiz;
4168
4169 if(file->_flag & MSVCRT__NOBUF)
4170 bufsiz = 1;
4171 else if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)))
4172 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
4173 else
4174 bufsiz = file->_bufsiz;
4175
4176 pcnt = (wrcnt / bufsiz) * bufsiz;
4177
4179 break;
4180
4181 if(_write(file->_file, ptr, pcnt) <= 0) {
4182 file->_flag |= _IOERR;
4183 break;
4184 }
4185 written += pcnt;
4186 wrcnt -= pcnt;
4187 ptr = (const char*)ptr + pcnt;
4188 } else {
4189 if(_flsbuf(*(const char*)ptr, file) == EOF)
4190 break;
4191 written++;
4192 wrcnt--;
4193 ptr = (const char*)ptr + 1;
4194 }
4195 }
4196
4197 return written / size;
4198}
4199
4200/*********************************************************************
4201 * fputwc (MSVCRT.@)
4202 */
4204{
4205 wint_t ret;
4206
4208 ret = _fputwc_nolock(wc, file);
4210
4211 return ret;
4212}
4213
4214/*********************************************************************
4215 * _fputwc_nolock (MSVCRT.@)
4216 */
4218{
4219 wchar_t mwc=wc;
4220 ioinfo *fdinfo;
4221 wint_t ret;
4222
4223 fdinfo = get_ioinfo_nolock(file->_file);
4224
4225 if((fdinfo->wxflag&WX_TEXT) && ioinfo_get_textmode(fdinfo) == TEXTMODE_ANSI) {
4226 char buf[MB_LEN_MAX];
4227 int char_len;
4228
4229 char_len = wctomb(buf, mwc);
4230 if(char_len!=-1 && _fwrite_nolock(buf, char_len, 1, file)==1)
4231 ret = wc;
4232 else
4233 ret = WEOF;
4234 }else if(_fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
4235 ret = wc;
4236 }else {
4237 ret = WEOF;
4238 }
4239
4240 return ret;
4241}
4242
4243/*********************************************************************
4244 * _fputwchar (MSVCRT.@)
4245 */
4247{
4248 return fputwc(wc, stdout);
4249}
4250
4251/*********************************************************************
4252 * _wfsopen (MSVCRT.@)
4253 */
4254FILE * CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
4255{
4256 FILE* file;
4257 int open_flags, stream_flags, fd;
4258
4259 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
4260
4261 /* map mode string to open() flags. "man fopen" for possibilities. */
4262 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4263 return NULL;
4264
4265 LOCK_FILES();
4266 fd = _wsopen(path, open_flags, share, _S_IREAD | _S_IWRITE);
4267 if (fd < 0)
4268 file = NULL;
4269 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
4270 != -1)
4271 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
4272 else if (file)
4273 {
4274 file->_flag = 0;
4275 file = NULL;
4276 }
4277
4278 TRACE(":got (%p)\n",file);
4279 if (fd >= 0 && !file)
4280 _close(fd);
4281 UNLOCK_FILES();
4282 return file;
4283}
4284
4285/*********************************************************************
4286 * _fsopen (MSVCRT.@)
4287 */
4288FILE * CDECL _fsopen(const char *path, const char *mode, int share)
4289{
4290 wchar_t *pathW = NULL, *modeW = NULL;
4291 FILE *ret;
4292
4293 if (path && !(pathW = wstrdupa_utf8(path))) return NULL;
4294 if (mode && !(modeW = wstrdupa_utf8(mode)))
4295 {
4296 free(pathW);
4297 return NULL;
4298 }
4299
4300 ret = _wfsopen(pathW, modeW, share);
4301
4302 free(pathW);
4303 free(modeW);
4304 return ret;
4305}
4306
4307/*********************************************************************
4308 * fopen (MSVCRT.@)
4309 */
4310FILE * CDECL fopen(const char *path, const char *mode)
4311{
4312 return _fsopen( path, mode, _SH_DENYNO );
4313}
4314
4315/*********************************************************************
4316 * fopen_s (MSVCRT.@)
4317 */
4319 const char *filename, const char *mode)
4320{
4321 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4322 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
4323 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4324
4325 *pFile = fopen(filename, mode);
4326
4327 if(!*pFile)
4328 return *_errno();
4329 return 0;
4330}
4331
4332/*********************************************************************
4333 * _wfopen (MSVCRT.@)
4334 */
4335FILE * CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
4336{
4337 return _wfsopen( path, mode, _SH_DENYNO );
4338}
4339
4340/*********************************************************************
4341 * _wfopen_s (MSVCRT.@)
4342 */
4343int CDECL _wfopen_s(FILE** pFile, const wchar_t *filename,
4344 const wchar_t *mode)
4345{
4346 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4347 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
4348 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4349
4351
4352 if(!*pFile)
4353 return *_errno();
4354 return 0;
4355}
4356
4357/*********************************************************************
4358 * fputc (MSVCRT.@)
4359 */
4361{
4362 int ret;
4363
4365 ret = _fputc_nolock(c, file);
4367
4368 return ret;
4369}
4370
4371/*********************************************************************
4372 * _fputc_nolock (MSVCRT.@)
4373 */
4375{
4376 int res;
4377
4378 if(file->_cnt>0) {
4379 *file->_ptr++=c;
4380 file->_cnt--;
4381 if (c == '\n')
4382 {
4384 return res ? res : c;
4385 }
4386 else {
4387 return c & 0xff;
4388 }
4389 } else {
4390 res = _flsbuf(c, file);
4391 return res;
4392 }
4393}
4394
4395/*********************************************************************
4396 * _fputchar (MSVCRT.@)
4397 */
4399{
4400 return fputc(c, stdout);
4401}
4402
4403/*********************************************************************
4404 * fread (MSVCRT.@)
4405 */
4406size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
4407{
4408 size_t ret;
4409
4411 ret = _fread_nolock(ptr, size, nmemb, file);
4413
4414 return ret;
4415}
4416
4417/*********************************************************************
4418 * _fread_nolock (MSVCRT.@)
4419 */
4420size_t CDECL _fread_nolock(void *ptr, size_t size, size_t nmemb, FILE* file)
4421{
4422 size_t rcnt=size * nmemb;
4423 size_t read=0;
4424 size_t pread=0;
4425
4426 if(!rcnt)
4427 return 0;
4428
4429 /* first buffered data */
4430 if(file->_cnt>0) {
4431 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
4432 memcpy(ptr, file->_ptr, pcnt);
4433 file->_cnt -= pcnt;
4434 file->_ptr += pcnt;
4435 read += pcnt ;
4436 rcnt -= pcnt ;
4437 ptr = (char*)ptr + pcnt;
4438 } else if(!(file->_flag & _IOREAD )) {
4439 if(file->_flag & _IORW) {
4440 file->_flag |= _IOREAD;
4441 } else {
4442 return 0;
4443 }
4444 }
4445
4446 if(rcnt>0 && !(file->_flag & (MSVCRT__NOBUF | _IOMYBUF | MSVCRT__USERBUF)))
4448
4449 while(rcnt>0)
4450 {
4451 int i;
4452 if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
4453 i = _read(file->_file, file->_base, file->_bufsiz);
4454 file->_ptr = file->_base;
4455 if (i != -1) {
4456 file->_cnt = i;
4457 if (i > rcnt) i = rcnt;
4458 }
4459 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
4460 if (i > 0 && i < file->_cnt) {
4461 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
4462 file->_flag &= ~_IOEOF;
4463 }
4464 if (i > 0) {
4465 memcpy(ptr, file->_ptr, i);
4466 file->_cnt -= i;
4467 file->_ptr += i;
4468 }
4469 } else if (rcnt > INT_MAX) {
4470 i = _read(file->_file, ptr, INT_MAX);
4471 } else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
4472 i = _read(file->_file, ptr, rcnt);
4473 } else {
4474 i = _read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
4475 }
4476 pread += i;
4477 rcnt -= i;
4478 ptr = (char *)ptr+i;
4479 /* expose feof condition in the flags
4480 * MFC tests file->_flag for feof, and doesn't call feof())
4481 */
4482 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
4483 file->_flag |= _IOEOF;
4484 else if (i == -1)
4485 {
4486 file->_flag |= _IOERR;
4487 pread = 0;
4488 rcnt = 0;
4489 }
4490 if (i < 1) break;
4491 }
4492 read+=pread;
4493 return read / size;
4494}
4495
4496#if _MSVCR_VER >= 80
4497
4498/*********************************************************************
4499 * fread_s (MSVCR80.@)
4500 */
4501size_t CDECL fread_s(void *buf, size_t buf_size, size_t elem_size,
4502 size_t count, FILE *stream)
4503{
4504 size_t ret;
4505
4506 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4507 if(buf && buf_size)
4508 memset(buf, 0, buf_size);
4509 return 0;
4510 }
4511 if(!elem_size || !count) return 0;
4512
4514 ret = _fread_nolock_s(buf, buf_size, elem_size, count, stream);
4516
4517 return ret;
4518}
4519
4520/*********************************************************************
4521 * _fread_nolock_s (MSVCR80.@)
4522 */
4523size_t CDECL _fread_nolock_s(void *buf, size_t buf_size, size_t elem_size,
4524 size_t count, FILE *stream)
4525{
4526 size_t bytes_left, buf_pos;
4527
4528 TRACE("(%p %Iu %Iu %Iu %p)\n", buf, buf_size, elem_size, count, stream);
4529
4530 if(!MSVCRT_CHECK_PMT(stream != NULL)) {
4531 if(buf && buf_size)
4532 memset(buf, 0, buf_size);
4533 return 0;
4534 }
4535 if(!elem_size || !count) return 0;
4536 if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
4537 if(!MSVCRT_CHECK_PMT(SIZE_MAX/count >= elem_size)) return 0;
4538
4539 bytes_left = elem_size*count;
4540 buf_pos = 0;
4541 while(bytes_left) {
4542 if(stream->_cnt > 0) {
4543 size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
4544
4545 if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, ERANGE)) {
4546 memset(buf, 0, buf_size);
4547 return 0;
4548 }
4549
4550 _fread_nolock((char*)buf+buf_pos, 1, size, stream);
4551 buf_pos += size;
4552 bytes_left -= size;
4553 }else {
4554 int c = _filbuf(stream);
4555
4556 if(c == EOF)
4557 break;
4558
4559 if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, ERANGE)) {
4560 memset(buf, 0, buf_size);
4561 return 0;
4562 }
4563
4564 ((char*)buf)[buf_pos++] = c;
4565 bytes_left--;
4566 }
4567 }
4568
4569 return buf_pos/elem_size;
4570}
4571
4572#endif /* _MSVCR_VER >= 80 */
4573
4574/*********************************************************************
4575 * _wfreopen (MSVCRT.@)
4576 *
4577 */
4578FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE* file)
4579{
4580 int open_flags, stream_flags, fd;
4581
4582 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
4583
4584 LOCK_FILES();
4585 if (file)
4586 {
4587 fclose(file);
4588 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
4589 file = NULL;
4590 else if((fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE)) < 0)
4591 file = NULL;
4592 else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
4593 {
4594 file->_flag = 0;
4595 file = NULL;
4596 }
4597 }
4598 UNLOCK_FILES();
4599 return file;
4600}
4601
4602/*********************************************************************
4603 * _wfreopen_s (MSVCRT.@)
4604 */
4606 const wchar_t *path, const wchar_t *mode, FILE* file)
4607{
4608 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4609 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
4610 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4611 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
4612
4614
4615 if(!*pFile)
4616 return *_errno();
4617 return 0;
4618}
4619
4620/*********************************************************************
4621 * freopen (MSVCRT.@)
4622 *
4623 */
4624FILE* CDECL freopen(const char *path, const char *mode, FILE* file)
4625{
4626 FILE *ret;
4627 wchar_t *pathW = NULL, *modeW = NULL;
4628
4629 if (path && !(pathW = wstrdupa_utf8(path))) return NULL;
4630 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
4631 {
4632 free(pathW);
4633 return NULL;
4634 }
4635
4636 ret = _wfreopen(pathW, modeW, file);
4637
4638 free(pathW);
4639 free(modeW);
4640 return ret;
4641}
4642
4643/*********************************************************************
4644 * freopen_s (MSVCRT.@)
4645 */
4647 const char *path, const char *mode, FILE* file)
4648{
4649 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
4650 if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
4651 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
4652 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
4653
4654 *pFile = freopen(path, mode, file);
4655
4656 if(!*pFile)
4657 return *_errno();
4658 return 0;
4659}
4660
4661/*********************************************************************
4662 * fsetpos (MSVCRT.@)
4663 */
4665{
4666 return _fseeki64(file,*pos,SEEK_SET);
4667}
4668
4669/*********************************************************************
4670 * _ftelli64 (MSVCRT.@)
4671 */
4673{
4674 __int64 ret;
4675
4679
4680 return ret;
4681}
4682
4683/*********************************************************************
4684 * _ftelli64_nolock (MSVCRT.@)
4685 */
4687{
4688 __int64 pos;
4689
4690 pos = _telli64(file->_file);
4691 if(pos == -1)
4692 return -1;
4693 if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
4694 if(file->_flag & _IOWRT) {
4695 pos += file->_ptr - file->_base;
4696
4697 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4698 char *p;
4699
4700 for(p=file->_base; p<file->_ptr; p++)
4701 if(*p == '\n')
4702 pos++;
4703 }
4704 } else if(!file->_cnt) { /* nothing to do */
4705 } else if(_lseeki64(file->_file, 0, SEEK_END)==pos) {
4706 int i;
4707
4708 pos -= file->_cnt;
4709 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4710 for(i=0; i<file->_cnt; i++)
4711 if(file->_ptr[i] == '\n')
4712 pos--;
4713 }
4714 } else {
4715 char *p;
4716
4717 if(_lseeki64(file->_file, pos, SEEK_SET) != pos)
4718 return -1;
4719
4720 pos -= file->_bufsiz;
4721 pos += file->_ptr - file->_base;
4722
4723 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
4724 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
4725 pos--;
4726
4727 for(p=file->_base; p<file->_ptr; p++)
4728 if(*p == '\n')
4729 pos++;
4730 }
4731 }
4732 }
4733
4734 return pos;
4735}
4736
4737/*********************************************************************
4738 * ftell (MSVCRT.@)
4739 */
4741{
4742 return _ftelli64(file);
4743}
4744
4745#if _MSVCR_VER >= 80
4746/*********************************************************************
4747 * _ftell_nolock (MSVCR80.@)
4748 */
4750{
4751 return _ftelli64_nolock(file);
4752}
4753#endif
4754
4755/*********************************************************************
4756 * fgetpos (MSVCRT.@)
4757 */
4759{
4760 *pos = _ftelli64(file);
4761 if(*pos == -1)
4762 return -1;
4763 return 0;
4764}
4765
4766/*********************************************************************
4767 * fputs (MSVCRT.@)
4768 */
4769int CDECL fputs(const char *s, FILE* file)
4770{
4771 size_t len = strlen(s);
4772 int ret;
4773
4775 ret = _fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : EOF;
4777 return ret;
4778}
4779
4780/*********************************************************************
4781 * fputws (MSVCRT.@)
4782 */
4783int CDECL fputws(const wchar_t *s, FILE* file)
4784{
4785 size_t i, len = wcslen(s);
4786 BOOL tmp_buf;
4787 int ret;
4788
4790 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
4791 ret = _fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : EOF;
4793 return ret;
4794 }
4795
4796 tmp_buf = add_std_buffer(file);
4797 for (i=0; i<len; i++) {
4798 if(_fputwc_nolock(s[i], file) == WEOF) {
4799 if(tmp_buf) remove_std_buffer(file);
4801 return WEOF;
4802 }
4803 }
4804
4805 if(tmp_buf) remove_std_buffer(file);
4807 return 0;
4808}
4809
4810/*********************************************************************
4811 * getchar (MSVCRT.@)
4812 */
4814{
4815 return fgetc(stdin);
4816}
4817
4818/*********************************************************************
4819 * getc (MSVCRT.@)
4820 */
4822{
4823 return fgetc(file);
4824}
4825
4826/*********************************************************************
4827 * gets_s (MSVCR80.@)
4828 */
4829char * CDECL gets_s(char *buf, size_t len)
4830{
4831 char *buf_start = buf;
4832 int cc;
4833
4834 if (!MSVCRT_CHECK_PMT(buf != NULL)) return NULL;
4835 if (!MSVCRT_CHECK_PMT(len != 0)) return NULL;
4836
4838 for(cc = _fgetc_nolock(stdin);
4839 len != 0 && cc != EOF && cc != '\n';
4841 {
4842 if (cc != '\r')
4843 {
4844 *buf++ = (char)cc;
4845 len--;
4846 }
4847 }
4849
4850 if (!len)
4851 {
4852 *buf_start = 0;
4854 return NULL;
4855 }
4856
4857 if ((cc == EOF) && (buf_start == buf))
4858 {
4859 TRACE(":nothing read\n");
4860 return NULL;
4861 }
4862 *buf = '\0';
4863
4864 TRACE("got '%s'\n", buf_start);
4865 return buf_start;
4866}
4867
4868/*********************************************************************
4869 * gets (MSVCRT.@)
4870 */
4871char * CDECL gets(char *buf)
4872{
4873 return gets_s(buf, -1);
4874}
4875
4876/*********************************************************************
4877 * _getws (MSVCRT.@)
4878 */
4879wchar_t* CDECL _getws(wchar_t* buf)
4880{
4881 wint_t cc;
4882 wchar_t* ws = buf;
4883
4885 for (cc = _fgetwc_nolock(stdin); cc != WEOF && cc != '\n';
4887 {
4888 if (cc != '\r')
4889 *buf++ = (wchar_t)cc;
4890 }
4892
4893 if ((cc == WEOF) && (ws == buf))
4894 {
4895 TRACE(":nothing read\n");
4896 return NULL;
4897 }
4898 *buf = '\0';
4899
4900 TRACE("got %s\n", debugstr_w(ws));
4901 return ws;
4902}
4903
4904/*********************************************************************
4905 * putc (MSVCRT.@)
4906 */
4908{
4909 return fputc(c, file);
4910}
4911
4912/*********************************************************************
4913 * putchar (MSVCRT.@)
4914 */
4916{
4917 return fputc(c, stdout);
4918}
4919
4920/*********************************************************************
4921 * puts (MSVCRT.@)
4922 */
4923int CDECL puts(const char *s)
4924{
4925 size_t len = strlen(s);
4926 int ret;
4927
4929 if(_fwrite_nolock(s, sizeof(*s), len, stdout) != len) {
4931 return EOF;
4932 }
4933
4934 ret = _fwrite_nolock("\n",1,1,stdout) == 1 ? 0 : EOF;
4936 return ret;
4937}
4938
4939/*********************************************************************
4940 * _putws (MSVCRT.@)
4941 */
4942int CDECL _putws(const wchar_t *s)
4943{
4944 int ret;
4945
4947 ret = fputws(s, stdout);
4948 if(ret >= 0)
4949 ret = _fputwc_nolock('\n', stdout);
4951 return ret >= 0 ? 0 : WEOF;
4952}
4953
4954/*********************************************************************
4955 * remove (MSVCRT.@)
4956 */
4957int CDECL remove(const char *path)
4958{
4959 return _unlink(path);
4960}
4961
4962/*********************************************************************
4963 * _wremove (MSVCRT.@)
4964 */
4965int CDECL _wremove(const wchar_t *path)
4966{
4967 return _wunlink(path);
4968}
4969
4970/*********************************************************************
4971 * rename (MSVCRT.@)
4972 */
4973int CDECL rename(const char *oldpath,const char *newpath)
4974{
4975 wchar_t *oldpathW = NULL, *newpathW = NULL;
4976 int ret;
4977
4978 if (oldpath && !(oldpathW = wstrdupa_utf8(oldpath))) return -1;
4979 if (newpath && !(newpathW = wstrdupa_utf8(newpath)))
4980 {
4981 free(oldpathW);
4982 return -1;
4983 }
4984 ret = _wrename(oldpathW, newpathW);
4985 free(oldpathW);
4986 free(newpathW);
4987 return ret;
4988}
4989
4990/*********************************************************************
4991 * _wrename (MSVCRT.@)
4992 */
4993int CDECL _wrename(const wchar_t *oldpath,const wchar_t *newpath)
4994{
4995 TRACE(":from %s to %s\n", debugstr_w(oldpath), debugstr_w(newpath));
4996 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
4997 return 0;
4998 TRACE(":failed (%ld)\n", GetLastError());
5000 return -1;
5001}
5002
5003/*********************************************************************
5004 * setvbuf (MSVCRT.@)
5005 */
5006int CDECL setvbuf(FILE* file, char *buf, int mode, size_t size)
5007{
5008 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
5009 if(!MSVCRT_CHECK_PMT(mode==_IONBF || mode==_IOFBF || mode==_IOLBF)) return -1;
5010 if(!MSVCRT_CHECK_PMT(mode==_IONBF || (size>=2 && size<=INT_MAX))) return -1;
5011
5013
5015 if(file->_flag & _IOMYBUF)
5016 free(file->_base);
5017 file->_flag &= ~(MSVCRT__NOBUF | _IOMYBUF | MSVCRT__USERBUF);
5018 file->_cnt = 0;
5019
5020 if(mode == _IONBF) {
5021 file->_flag |= MSVCRT__NOBUF;
5022 file->_base = file->_ptr = (char*)&file->_charbuf;
5023 file->_bufsiz = 2;
5024 }else if(buf) {
5025 file->_base = file->_ptr = buf;
5026 file->_flag |= MSVCRT__USERBUF;
5027 file->_bufsiz = size;
5028 }else {
5029 file->_base = file->_ptr = malloc(size);
5030 if(!file->_base) {
5031 file->_bufsiz = 0;
5033 return -1;
5034 }
5035
5036 file->_flag |= _IOMYBUF;
5037 file->_bufsiz = size;
5038 }
5040 return 0;
5041}
5042
5043/*********************************************************************
5044 * setbuf (MSVCRT.@)
5045 */
5046void CDECL setbuf(FILE* file, char *buf)
5047{
5049}
5050
5051static int tmpnam_helper(char *s, size_t size, LONG *tmpnam_unique, int tmp_max)
5052{
5053 char tmpstr[8];
5054 char *p = s;
5055 int digits;
5056
5057 if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
5058
5059 if (size < 3) {
5060 if (size) *s = 0;
5061 *_errno() = ERANGE;
5062 return ERANGE;
5063 }
5064 *p++ = '\\';
5065 *p++ = 's';
5066 size -= 2;
5068 if (digits+1 > size) {
5069 *s = 0;
5070 *_errno() = ERANGE;
5071 return ERANGE;
5072 }
5073 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5074 p += digits;
5075 *p++ = '.';
5076 size -= digits+1;
5077
5078 while(1) {
5079 while ((digits = *tmpnam_unique)+1 < tmp_max) {
5081 break;
5082 }
5083
5085 if (digits+1 > size) {
5086 *s = 0;
5087 *_errno() = ERANGE;
5088 return ERANGE;
5089 }
5090 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5091 p[digits] = 0;
5092
5095 break;
5096 }
5097 return 0;
5098}
5099
5100int CDECL tmpnam_s(char *s, size_t size)
5101{
5103}
5104
5105/*********************************************************************
5106 * tmpnam (MSVCRT.@)
5107 */
5108char * CDECL tmpnam(char *s)
5109{
5110 if (!s) {
5112
5113 if(!data->tmpnam_buffer)
5114 data->tmpnam_buffer = malloc(MAX_PATH);
5115
5116 s = data->tmpnam_buffer;
5117 }
5118
5119 return tmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
5120}
5121
5122static int wtmpnam_helper(wchar_t *s, size_t size, LONG *tmpnam_unique, int tmp_max)
5123{
5124 wchar_t tmpstr[8];
5125 wchar_t *p = s;
5126 int digits;
5127
5128 if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
5129
5130 if (size < 3) {
5131 if (size) *s = 0;
5132 *_errno() = ERANGE;
5133 return ERANGE;
5134 }
5135 *p++ = '\\';
5136 *p++ = 's';
5137 size -= 2;
5139 if (digits+1 > size) {
5140 *s = 0;
5141 *_errno() = ERANGE;
5142 return ERANGE;
5143 }
5144 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5145 p += digits;
5146 *p++ = '.';
5147 size -= digits+1;
5148
5149 while(1) {
5150 while ((digits = *tmpnam_unique)+1 < tmp_max) {
5152 break;
5153 }
5154
5156 if (digits+1 > size) {
5157 *s = 0;
5158 *_errno() = ERANGE;
5159 return ERANGE;
5160 }
5161 memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
5162 p[digits] = 0;
5163
5166 break;
5167 }
5168 return 0;
5169}
5170
5171/*********************************************************************
5172 * _wtmpnam_s (MSVCRT.@)
5173 */
5174int CDECL _wtmpnam_s(wchar_t *s, size_t size)
5175{
5177}
5178
5179/*********************************************************************
5180 * _wtmpnam (MSVCRT.@)
5181 */
5182wchar_t * CDECL _wtmpnam(wchar_t *s)
5183{
5184 if (!s) {
5186
5187 if(!data->wtmpnam_buffer)
5188 data->wtmpnam_buffer = malloc(sizeof(wchar_t[MAX_PATH]));
5189
5190 s = data->wtmpnam_buffer;
5191 }
5192
5193 return wtmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
5194}
5195
5196/*********************************************************************
5197 * tmpfile (MSVCRT.@)
5198 */
5200{
5201 char *filename = _tempnam(",", "t");
5202 int fd;
5203 FILE* file = NULL;
5204
5205 LOCK_FILES();
5208 if (fd != -1 && (file = msvcrt_alloc_fp()))
5209 {
5210 if (msvcrt_init_fp(file, fd, _IORW) == -1)
5211 {
5212 file->_flag = 0;
5213 file = NULL;
5214 }
5215 else file->_tmpfname = _strdup(filename);
5216 }
5217
5218 if(fd != -1 && !file)
5219 _close(fd);
5220 free(filename);
5221 UNLOCK_FILES();
5222 return file;
5223}
5224
5225/*********************************************************************
5226 * tmpfile_s (MSVCRT.@)
5227 */
5229{
5230 if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
5231
5232 *file = tmpfile();
5233 return 0;
5234}
5235
5236static int puts_clbk_file_a(void *file, int len, const char *str)
5237{
5238 return fwrite(str, sizeof(char), len, file);
5239}
5240
5241static int puts_clbk_file_w(void *file, int len, const wchar_t *str)
5242{
5243 int i, ret;
5244
5246
5247 if(!(get_ioinfo_nolock(((FILE*)file)->_file)->wxflag & WX_TEXT)) {
5248 ret = _fwrite_nolock(str, sizeof(wchar_t), len, file);
5250 return ret;
5251 }
5252
5253 for(i=0; i<len; i++) {
5254 if(_fputwc_nolock(str[i], file) == WEOF) {
5256 return -1;
5257 }
5258 }
5259
5261 return len;
5262}
5263
5264static int vfprintf_helper(DWORD options, FILE* file, const char *format,
5266{
5267 printf_arg args_ctx[_ARGMAX+1];
5268 BOOL tmp_buf;
5269 int ret;
5270
5271 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5272 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5273
5275 memset(args_ctx, 0, sizeof(args_ctx));
5277 if(ret < 0) {
5279 *_errno() = EINVAL;
5280 return ret;
5281 } else if(!ret)
5282 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5283 }
5284
5286 tmp_buf = add_std_buffer(file);
5290 if(tmp_buf) remove_std_buffer(file);
5292
5293 return ret;
5294}
5295
5296static int vfwprintf_helper(DWORD options, FILE* file, const wchar_t *format,
5298{
5299 printf_arg args_ctx[_ARGMAX+1];
5300 BOOL tmp_buf;
5301 int ret;
5302
5303 if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
5304 if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
5305
5307 memset(args_ctx, 0, sizeof(args_ctx));
5309 if(ret < 0) {
5311 *_errno() = EINVAL;
5312 return ret;
5313 } else if(!ret)
5314 options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
5315 }
5316
5318 tmp_buf = add_std_buffer(file);
5322 if(tmp_buf) remove_std_buffer(file);
5324
5325 return ret;
5326}
5327
5328/*********************************************************************
5329 * _vfprintf_s_l (MSVCRT.@)
5330 */
5333{
5335}
5336
5337/*********************************************************************
5338 * _vfwprintf_s_l (MSVCRT.@)
5339 */
5340int CDECL _vfwprintf_s_l(FILE* file, const wchar_t *format,
5342{
5344}
5345
5346/*********************************************************************
5347 * vfprintf (MSVCRT.@)
5348 */
5350{
5351 return vfprintf_helper(0, file, format, NULL, valist);
5352}
5353
5354/*********************************************************************
5355 * vfprintf_s (MSVCRT.@)
5356 */
5358{
5359 return _vfprintf_s_l(file, format, NULL, valist);
5360}
5361
5362/*********************************************************************
5363 * vfwprintf (MSVCRT.@)
5364 */
5366{
5367 return vfwprintf_helper(0, file, format, NULL, valist);
5368}
5369
5370/*********************************************************************
5371 * vfwprintf_s (MSVCRT.@)
5372 */
5374{
5376}
5377
5378#if _MSVCR_VER >= 140
5379
5380/*********************************************************************
5381 * __stdio_common_vfprintf (UCRTBASE.@)
5382 */
5383int CDECL _stdio_common_vfprintf(unsigned __int64 options, FILE *file, const char *format,
5385{
5387 FIXME("options %#I64x not handled\n", options);
5388
5390}
5391
5392/*********************************************************************
5393 * __stdio_common_vfprintf_p (UCRTBASE.@)
5394 */
5395int CDECL __stdio_common_vfprintf_p(unsigned __int64 options, FILE *file, const char *format,
5397{
5399 FIXME("options %#I64x not handled\n", options);
5400
5403}
5404
5405
5406/*********************************************************************
5407 * __stdio_common_vfprintf_s (UCRTBASE.@)
5408 */
5409int CDECL __stdio_common_vfprintf_s(unsigned __int64 options, FILE *file, const char *format,
5411{
5413 FIXME("options %#I64x not handled\n", options);
5414
5417}
5418
5419/*********************************************************************
5420 * __stdio_common_vfwprintf (UCRTBASE.@)
5421 */
5422int CDECL __stdio_common_vfwprintf(unsigned __int64 options, FILE *file, const wchar_t *format,
5424{
5426 FIXME("options %#I64x not handled\n", options);
5427
5429}
5430
5431/*********************************************************************
5432 * __stdio_common_vfwprintf_p (UCRTBASE.@)
5433 */
5434int CDECL __stdio_common_vfwprintf_p(unsigned __int64 options, FILE *file, const wchar_t *format,
5436{
5438 FIXME("options %#I64x not handled\n", options);
5439
5442}
5443
5444
5445/*********************************************************************
5446 * __stdio_common_vfwprintf_s (UCRTBASE.@)
5447 */
5448int CDECL __stdio_common_vfwprintf_s(unsigned __int64 options, FILE *file, const wchar_t *format,
5450{
5452 FIXME("options %#I64x not handled\n", options);
5453
5456}
5457
5458#endif /* _MSVCR_VER >= 140 */
5459
5460/*********************************************************************
5461 * _vfprintf_l (MSVCRT.@)
5462 */
5465{
5466 return vfprintf_helper(0, file, format, locale, valist);
5467}
5468
5469/*********************************************************************
5470 * _vfwprintf_l (MSVCRT.@)
5471 */
5472int CDECL _vfwprintf_l(FILE* file, const wchar_t *format,
5474{
5476}
5477
5478/*********************************************************************
5479 * _vfprintf_p_l (MSVCRT.@)
5480 */
5483{
5486}
5487
5488/*********************************************************************
5489 * _vfprintf_p (MSVCRT.@)
5490 */
5492{
5493 return _vfprintf_p_l(file, format, NULL, valist);
5494}
5495
5496/*********************************************************************
5497 * _vfwprintf_p_l (MSVCRT.@)
5498 */
5499int CDECL _vfwprintf_p_l(FILE* file, const wchar_t *format,
5501{
5504}
5505
5506/*********************************************************************
5507 * _vfwprintf_p (MSVCRT.@)
5508 */
5510{
5512}
5513
5514/*********************************************************************
5515 * vprintf (MSVCRT.@)
5516 */
5518{
5519 return vfprintf(stdout,format,valist);
5520}
5521
5522/*********************************************************************
5523 * vprintf_s (MSVCRT.@)
5524 */
5526{
5528}
5529
5530/*********************************************************************
5531 * vwprintf (MSVCRT.@)
5532 */
5533int CDECL vwprintf(const wchar_t *format, va_list valist)
5534{
5535 return vfwprintf(stdout,format,valist);
5536}
5537
5538/*********************************************************************
5539 * vwprintf_s (MSVCRT.@)
5540 */
5542{
5544}
5545
5546/*********************************************************************
5547 * fprintf (MSVCRT.@)
5548 */
5549int WINAPIV fprintf(FILE* file, const char *format, ...)
5550{
5552 int res;
5555 va_end(valist);
5556 return res;
5557}
5558
5559/*********************************************************************
5560 * fprintf_s (MSVCRT.@)
5561 */
5562int WINAPIV fprintf_s(FILE* file, const char *format, ...)
5563{
5565 int res;
5568 va_end(valist);
5569 return res;
5570}
5571
5572/*********************************************************************
5573 * _fprintf_l (MSVCRT.@)
5574 */
5576{
5578 int res;
5581 va_end(valist);
5582 return res;
5583}
5584
5585
5586/*********************************************************************
5587 * _fprintf_p (MSVCRT.@)
5588 */
5589int WINAPIV _fprintf_p(FILE* file, const char *format, ...)
5590{
5592 int res;
5595 va_end(valist);
5596 return res;
5597}
5598
5599/*********************************************************************
5600 * _fprintf_p_l (MSVCRT.@)
5601 */
5603{
5605 int res;
5608 va_end(valist);
5609 return res;
5610}
5611
5612/*********************************************************************
5613 * _fprintf_s_l (MSVCRT.@)
5614 */
5616{
5618 int res;
5621 va_end(valist);
5622 return res;
5623}
5624
5625/*********************************************************************
5626 * fwprintf (MSVCRT.@)
5627 */
5628int WINAPIV fwprintf(FILE* file, const wchar_t *format, ...)
5629{
5631 int res;
5634 va_end(valist);
5635 return res;
5636}
5637
5638/*********************************************************************
5639 * fwprintf_s (MSVCRT.@)
5640 */
5641int WINAPIV fwprintf_s(FILE* file, const wchar_t *format, ...)
5642{
5644 int res;
5647 va_end(valist);
5648 return res;
5649}
5650
5651/*********************************************************************
5652 * _fwprintf_l (MSVCRT.@)
5653 */
5655{
5657 int res;
5660 va_end(valist);
5661 return res;
5662}
5663
5664/*********************************************************************
5665 * _fwprintf_p (MSVCRT.@)
5666 */
5667int WINAPIV _fwprintf_p(FILE* file, const wchar_t *format, ...)
5668{
5670 int res;
5673 va_end(valist);
5674 return res;
5675}
5676
5677/*********************************************************************
5678 * _fwprintf_p_l (MSVCRT.@)
5679 */
5681{
5683 int res;
5686 va_end(valist);
5687 return res;
5688}
5689
5690/*********************************************************************
5691 * _fwprintf_s_l (MSVCRT.@)
5692 */
5694{
5696 int res;
5699 va_end(valist);
5700 return res;
5701}
5702
5703/*********************************************************************
5704 * printf (MSVCRT.@)
5705 */
5706int WINAPIV printf(const char *format, ...)
5707{
5709 int res;
5712 va_end(valist);
5713 return res;
5714}
5715
5716/*********************************************************************
5717 * printf_s (MSVCRT.@)
5718 */
5719int WINAPIV printf_s(const char *format, ...)
5720{
5722 int res;
5725 va_end(valist);
5726 return res;
5727}
5728
5729/*********************************************************************
5730 * ungetc (MSVCRT.@)
5731 */
5733{
5734 int ret;
5735
5736 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
5737
5741
5742 return ret;
5743}
5744
5745/*********************************************************************
5746 * _ungetc_nolock (MSVCRT.@)
5747 */
5749{
5750 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
5751
5752 if (c == EOF || !(file->_flag&_IOREAD ||
5753 (file->_flag&_IORW && !(file->_flag&_IOWRT))))
5754 return EOF;
5755
5756 if((!(file->_flag & (MSVCRT__NOBUF | _IOMYBUF | MSVCRT__USERBUF))
5758 || (!file->_cnt && file->_ptr==file->_base))
5759 file->_ptr++;
5760
5761 if(file->_ptr>file->_base) {
5762 file->_ptr--;
5763 if(file->_flag & _IOSTRG) {
5764 if(*file->_ptr != c) {
5765 file->_ptr++;
5766 return EOF;
5767 }
5768 }else {
5769 *file->_ptr = c;
5770 }
5771 file->_cnt++;
5772 file->_flag &= ~(_IOERR | _IOEOF);
5773 file->_flag |= _IOREAD;
5774 return c;
5775 }
5776
5777 return EOF;
5778}
5779
5780/*********************************************************************
5781 * ungetwc (MSVCRT.@)
5782 */
5784{
5785 wint_t ret;
5786
5787 if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
5788
5790 ret = _ungetwc_nolock(wc, file);
5792
5793 return ret;
5794}
5795
5796/*********************************************************************
5797 * _ungetwc_nolock (MSVCRT.@)
5798 */
5800{
5801 wchar_t mwc = wc;
5802
5803 if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
5804 if (wc == WEOF)
5805 return WEOF;
5806
5808 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
5809 unsigned char * pp = (unsigned char *)&mwc;
5810 int i;
5811
5812 for(i=sizeof(wchar_t)-1;i>=0;i--) {
5813 if(pp[i] != _ungetc_nolock(pp[i],file))
5814 return WEOF;
5815 }
5816 }else {
5817 char mbs[MB_LEN_MAX];
5818 int len;
5819
5820 len = wctomb(mbs, mwc);
5821 if(len == -1)
5822 return WEOF;
5823
5824 for(len--; len>=0; len--) {
5825 if(mbs[len] != _ungetc_nolock(mbs[len], file))
5826 return WEOF;
5827 }
5828 }
5829
5830 return mwc;
5831}
5832
5833/*********************************************************************
5834 * wprintf (MSVCRT.@)
5835 */
5836int WINAPIV wprintf(const wchar_t *format, ...)
5837{
5839 int res;
5842 va_end(valist);
5843 return res;
5844}
5845
5846/*********************************************************************
5847 * wprintf_s (MSVCRT.@)
5848 */
5849int WINAPIV wprintf_s(const wchar_t *format, ...)
5850{
5852 int res;
5855 va_end(valist);
5856 return res;
5857}
5858
5859/*********************************************************************
5860 * _getmaxstdio (MSVCRT.@)
5861 */
5863{
5864 return MSVCRT_max_streams;
5865}
5866
5867/*********************************************************************
5868 * _setmaxstdio (MSVCRT.@)
5869 */
5870int CDECL _setmaxstdio(int newmax)
5871{
5872 TRACE("%d\n", newmax);
5873
5874 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
5875 return -1;
5876
5877 MSVCRT_max_streams = newmax;
5878 return MSVCRT_max_streams;
5879}
5880
5881#if _MSVCR_VER >= 140
5882/*********************************************************************
5883 * _get_stream_buffer_pointers (UCRTBASE.@)
5884 */
5886 char*** ptr, int** count)
5887{
5888 if (base)
5889 *base = &file->_base;
5890 if (ptr)
5891 *ptr = &file->_ptr;
5892 if (count)
5893 *count = &file->_cnt;
5894 return 0;
5895}
5896#endif
#define _flsbuf
Definition: _flswbuf.c:10
#define read
Definition: acwin.h:96
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
unsigned int dir
Definition: maze.c:112
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
LONG NTSTATUS
Definition: precomp.h:26
#define ARRAY_SIZE(A)
Definition: main.h:20
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define FIXME(fmt,...)
Definition: precomp.h:53
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
#define _S_IFCHR
Definition: stat.h:21
#define _S_IFREG
Definition: stat.h:27
#define _S_IFDIR
Definition: stat.h:23
#define _S_IWRITE
Definition: stat.h:85
#define _S_IREAD
Definition: stat.h:84
#define _S_IFIFO
Definition: stat.h:20
#define SEEK_END
Definition: cabinet.c:29
#define _setmode(fd, mode)
Definition: cat.c:21
Definition: _locale.h:75
#define NO_ERROR
Definition: dderror.h:5
#define free
Definition: debug_ros.c:5
#define _strdup
Definition: debug_ros.c:7
#define malloc
Definition: debug_ros.c:4
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned int idx
Definition: utils.c:41
#define _O_SEQUENTIAL
Definition: cabinet.h:43
#define _O_RDWR
Definition: cabinet.h:39
#define _O_SHORT_LIVED
Definition: cabinet.h:49
#define _O_BINARY
Definition: cabinet.h:51
#define _O_NOINHERIT
Definition: cabinet.h:45
#define _O_TEMPORARY
Definition: cabinet.h:44
#define _O_APPEND
Definition: cabinet.h:41
#define _O_RDONLY
Definition: cabinet.h:37
#define _O_TEXT
Definition: cabinet.h:50
#define _O_TRUNC
Definition: cabinet.h:47
#define _O_CREAT
Definition: cabinet.h:46
#define _O_EXCL
Definition: cabinet.h:48
#define _O_RANDOM
Definition: cabinet.h:42
#define _O_WRONLY
Definition: cabinet.h:38
#define CDECL
Definition: compat.h:29
#define CloseHandle
Definition: compat.h:739
#define wcschr
Definition: compat.h:17
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define FILE_BEGIN
Definition: compat.h:761
#define INVALID_SET_FILE_POINTER
Definition: compat.h:732
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetFilePointer
Definition: compat.h:743
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
static __inline const char * debugstr_an(const char *s, int n)
Definition: compat.h:55
#define GetCurrentProcess()
Definition: compat.h:759
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
#define CreateFileW
Definition: compat.h:741
#define GetFileSizeEx
Definition: compat.h:757
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
#define FILE_SHARE_READ
Definition: compat.h:136
static const WCHAR *const ext[]
Definition: module.c:53
BOOL WINAPI DECLSPEC_HOTPATCH VerifyConsoleIoHandle(HANDLE hIoHandle)
Definition: console.c:1112
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1215
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleW(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1447
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
VOID WINAPI GetStartupInfoA(IN LPSTARTUPINFOA lpStartupInfo)
Definition: proc.c:1320
BOOL WINAPI InitializeCriticalSectionEx(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount, IN DWORD flags)
Definition: sync.c:107
UINT WINAPI DECLSPEC_HOTPATCH GetTempFileNameW(LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer)
Definition: file.c:2355
BOOL WINAPI DECLSPEC_HOTPATCH WriteFile(HANDLE file, LPCVOID buffer, DWORD count, LPDWORD result, LPOVERLAPPED overlapped)
Definition: file.c:3896
BOOL WINAPI DECLSPEC_HOTPATCH UnlockFile(HANDLE file, DWORD offset_low, DWORD offset_high, DWORD count_low, DWORD count_high)
Definition: file.c:3863
BOOL WINAPI DECLSPEC_HOTPATCH FlushFileBuffers(HANDLE file)
Definition: file.c:3027
DWORD WINAPI DECLSPEC_HOTPATCH GetFileType(HANDLE file)
Definition: file.c:3221
BOOL WINAPI DECLSPEC_HOTPATCH DeleteFileW(LPCWSTR path)
Definition: file.c:1003
DWORD WINAPI DECLSPEC_HOTPATCH GetFileAttributesA(LPCSTR name)
Definition: file.c:1659
BOOL WINAPI DECLSPEC_HOTPATCH SetFileAttributesW(LPCWSTR name, DWORD attributes)
Definition: file.c:2918
BOOL WINAPI DECLSPEC_HOTPATCH GetFileAttributesExW(LPCWSTR name, GET_FILEEX_INFO_LEVELS level, void *ptr)
Definition: file.c:1721
BOOL WINAPI DECLSPEC_HOTPATCH LockFile(HANDLE file, DWORD offset_low, DWORD offset_high, DWORD count_low, DWORD count_high)
Definition: file.c:3306
BOOL WINAPI DECLSPEC_HOTPATCH SetEndOfFile(HANDLE file)
Definition: file.c:3651
BOOL WINAPI DECLSPEC_HOTPATCH SetFileTime(HANDLE file, const FILETIME *ctime, const FILETIME *atime, const FILETIME *mtime)
Definition: file.c:3819
DWORD WINAPI DECLSPEC_HOTPATCH GetFileAttributesW(LPCWSTR name)
Definition: file.c:1671
BOOL WINAPI MoveFileExW(const WCHAR *source, const WCHAR *dest, DWORD flag)
Definition: file.c:2531
unsigned char ch[4][2]
Definition: console.c:118
wchar_t * msvcrt_wstrdupa(const char *str)
Definition: data.c:373
unsigned int MSVCRT__commode
Definition: data.c:33
int CDECL _get_fmode(int *mode)
Definition: data.c:287
int CDECL _getdrive(void)
Definition: dir.c:819
wchar_t *CDECL _wgetenv(const wchar_t *name)
Definition: environ.c:254
void __cdecl _invalid_parameter(const wchar_t *expr, const wchar_t *func, const wchar_t *file, unsigned int line, uintptr_t arg)
Definition: errno.c:461
__msvcrt_ulong *CDECL __doserrno(void)
Definition: errno.c:223
int *CDECL _errno(void)
Definition: errno.c:215
__msvcrt_long CDECL ftell(FILE *file)
Definition: file.c:4740
int CDECL _wutime32(const wchar_t *path, struct __utimbuf32 *t)
Definition: file.c:3545
int CDECL vprintf(const char *format, va_list valist)
Definition: file.c:5517
#define EF_UTF8
Definition: file.c:196
int CDECL clearerr_s(FILE *file)
Definition: file.c:1684
int CDECL vfwprintf(FILE *file, const wchar_t *format, va_list valist)
Definition: file.c:5365
wint_t CDECL _fgetwc_nolock(FILE *file)
Definition: file.c:3949
FILE *CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
Definition: file.c:4254
#define WX_ATEOF
Definition: file.c:81
int CDECL _dup2(int od, int nd)
Definition: file.c:1258
int CDECL _vfprintf_s_l(FILE *file, const char *format, _locale_t locale, va_list valist)
Definition: file.c:5331
#define ALL_S_IEXEC
Definition: file.c:73
static FILE * msvcrt_alloc_fp(void)
Definition: file.c:615
void CDECL clearerr(FILE *file)
Definition: file.c:1672
wchar_t *CDECL _getws(wchar_t *buf)
Definition: file.c:4879
int CDECL _fgetchar(void)
Definition: file.c:3895
char *CDECL _tempnam(const char *dir, const char *prefix)
Definition: file.c:3441
int WINAPIV _fprintf_s_l(FILE *file, const char *format, _locale_t locale,...)
Definition: file.c:5615
static int puts_clbk_file_w(void *file, int len, const wchar_t *str)
Definition: file.c:5241
static void msvcrt_stat64_to_stat(const struct _stat64 *buf64, struct _stat *buf)
Definition: file.c:309
wint_t CDECL ungetwc(wint_t wc, FILE *file)
Definition: file.c:5783
int CDECL _wutime64(const wchar_t *path, struct __utimbuf64 *t)
Definition: file.c:3529
int CDECL _fflush_nolock(FILE *file)
Definition: file.c:1201
static void remove_std_buffer(FILE *file)
Definition: file.c:910
static ioinfo * get_ioinfo_alloc_fd(int fd)
Definition: file.c:467
static int puts_clbk_file_a(void *file, int len, const char *str)
Definition: file.c:5236
int CDECL _read(int fd, void *buf, unsigned int count)
Definition: file.c:3127
#define LOCK_FILES()
Definition: file.c:306
int CDECL _wcreat(const wchar_t *path, int pmode)
Definition: file.c:2702
int CDECL _vfwprintf_s_l(FILE *file, const wchar_t *format, _locale_t locale, va_list valist)
Definition: file.c:5340
static int read_utf8(ioinfo *fdinfo, wchar_t *buf, unsigned int count)
Definition: file.c:2784
int CDECL puts(const char *s)
Definition: file.c:4923
FILE *CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE *file)
Definition: file.c:4578
static int MSVCRT_umask
Definition: file.c:282
ioinfo MSVCRT___badioinfo
Definition: file.c:148
static const ULONGLONG WCCMD
Definition: file.c:291
int WINAPIV _wopen(const wchar_t *path, int flags,...)
Definition: file.c:2674
int CDECL _close(int fd)
Definition: file.c:1219
static int vfwprintf_helper(DWORD options, FILE *file, const wchar_t *format, _locale_t locale, va_list valist)
Definition: file.c:5296
void msvcrt_init_io(void)
Definition: file.c:720
int CDECL _setmaxstdio(int newmax)
Definition: file.c:5870
int WINAPIV _fwprintf_p(FILE *file, const wchar_t *format,...)
Definition: file.c:5667
static file_crit * MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE]
Definition: file.c:278
int CDECL putc(int c, FILE *file)
Definition: file.c:4907
int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
Definition: file.c:2323
int WINAPIV _fprintf_l(FILE *file, const char *format, _locale_t locale,...)
Definition: file.c:5575
static CRITICAL_SECTION MSVCRT_file_cs
Definition: file.c:298
int CDECL fclose(FILE *file)
Definition: file.c:3757
static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
Definition: file.c:299
#define WX_OPEN
Definition: file.c:80
wint_t CDECL _fputwc_nolock(wint_t wc, FILE *file)
Definition: file.c:4217
int CDECL _chmod(const char *path, int flags)
Definition: file.c:1052
int CDECL _access_s(const char *filename, int mode)
Definition: file.c:1004
int CDECL _wchmod(const wchar_t *path, int flags)
Definition: file.c:1066
int CDECL ungetc(int c, FILE *file)
Definition: file.c:5732
static int msvcrt_get_flags(const wchar_t *mode, int *open_flags, int *stream_flags)
Definition: file.c:1722
FILE *CDECL _fsopen(const char *path, const char *mode, int share)
Definition: file.c:4288
#define WX_READNL
Definition: file.c:82
int CDECL fseek(FILE *file, __msvcrt_long offset, int whence)
Definition: file.c:1610
ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE]
Definition: file.c:155
static LONG tmpnam_unique
Definition: file.c:285
int CDECL _locking(int fd, int mode, __msvcrt_long nbytes)
Definition: file.c:1513
#define MSVCRT_W_OK
Definition: file.c:76
void CDECL _lock_file(FILE *file)
Definition: file.c:1487
static int wtmpnam_helper(wchar_t *s, size_t size, LONG *tmpnam_unique, int tmp_max)
Definition: file.c:5122
int WINAPIV fprintf(FILE *file, const char *format,...)
Definition: file.c:5549
wchar_t *CDECL _wtmpnam(wchar_t *s)
Definition: file.c:5182
__int64 CDECL _ftelli64(FILE *file)
Definition: file.c:4672
int CDECL _wsopen_s(int *fd, const wchar_t *path, int oflags, int shflags, int pmode)
Definition: file.c:2575
#define MSVCRT_MAX_FILES
Definition: file.c:125
int CDECL _vfwprintf_p(FILE *file, const wchar_t *format, va_list valist)
Definition: file.c:5509
static void ioinfo_set_crit_init(ioinfo *info)
Definition: file.c:206
int CDECL _sopen_s(int *fd, const char *path, int oflags, int shflags, int pmode)
Definition: file.c:2625
#define WX_TTY
Definition: file.c:86
int CDECL vwprintf_s(const wchar_t *format, va_list valist)
Definition: file.c:5541
int CDECL _wsopen_dispatch(const wchar_t *path, int oflags, int shflags, int pmode, int *fd, int secure)
Definition: file.c:2397
static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
Definition: file.c:576
int CDECL _fclose_nolock(FILE *file)
Definition: file.c:3773
wchar_t *CDECL fgetws(wchar_t *s, int size, FILE *file)
Definition: file.c:4043
int CDECL _commit(int fd)
Definition: file.c:1112
wint_t CDECL fputwc(wint_t wc, FILE *file)
Definition: file.c:4203
static void init_ioinfo_cs(ioinfo *info)
Definition: file.c:405
wint_t CDECL getwchar(void)
Definition: file.c:4035
static int MSVCRT_max_streams
Definition: file.c:279
static unsigned split_oflags(unsigned oflags)
Definition: file.c:2293
static int check_bom(HANDLE h, int oflags, BOOL seek)
Definition: file.c:2373
int CDECL _sopen_dispatch(const char *path, int oflags, int shflags, int pmode, int *fd, int secure)
Definition: file.c:2606
int WINAPIV _fwprintf_s_l(FILE *file, const wchar_t *format, _locale_t locale,...)
Definition: file.c:5693
FILE *CDECL freopen(const char *path, const char *mode, FILE *file)
Definition: file.c:4624
size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE *file)
Definition: file.c:4406
static int msvcrt_flush_buffer(FILE *file)
Definition: file.c:835
char *CDECL tmpnam(char *s)
Definition: file.c:5108
int CDECL _putw(int val, FILE *file)
Definition: file.c:3738
int WINAPIV printf_s(const char *format,...)
Definition: file.c:5719
__int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
Definition: file.c:1432
static LONG tmpnam_s_unique
Definition: file.c:286
#define TOUL(x)
Definition: file.c:288
int CDECL vfprintf_s(FILE *file, const char *format, va_list valist)
Definition: file.c:5357
static BOOL msvcrt_alloc_buffer(FILE *file)
Definition: file.c:866
static void ioinfo_set_textmode(ioinfo *info, enum textmode mode)
Definition: file.c:220
static int tmpnam_helper(char *s, size_t size, LONG *tmpnam_unique, int tmp_max)
Definition: file.c:5051
char *CDECL gets(char *buf)
Definition: file.c:4871
int CDECL _dup(int od)
Definition: file.c:1317
int CDECL _write(int fd, const void *buf, unsigned int count)
Definition: file.c:3561
BOOL msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
Definition: file.c:675
int WINAPIV _wsopen(const wchar_t *path, int oflags, int shflags,...)
Definition: file.c:2583
wchar_t *CDECL _wtempnam(const wchar_t *dir, const wchar_t *prefix)
Definition: file.c:3464
wint_t CDECL _fputwchar(wint_t wc)
Definition: file.c:4246
wint_t CDECL fgetwc(FILE *file)
Definition: file.c:3935
int CDECL _umask(int umask)
Definition: file.c:3486
static BOOL ioinfo_is_crit_init(ioinfo *info)
Definition: file.c:201
static enum textmode ioinfo_get_textmode(ioinfo *info)
Definition: file.c:211
int CDECL vfprintf(FILE *file, const char *format, va_list valist)
Definition: file.c:5349
static char utf8_bom[3]
Definition: file.c:89
int CDECL _unlink(const char *path)
Definition: file.c:1085
int CDECL _fputchar(int c)
Definition: file.c:4398
static void msvcrt_stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
Definition: file.c:354
#define EF_UTF16
Definition: file.c:197
int CDECL getc(FILE *file)
Definition: file.c:4821
int CDECL _open_osfhandle(intptr_t handle, int oflags)
Definition: file.c:2711
static char utf16_bom[2]
Definition: file.c:90
int CDECL _wunlink(const wchar_t *path)
Definition: file.c:1099
int WINAPIV _fprintf_p_l(FILE *file, const char *format, _locale_t locale,...)
Definition: file.c:5602
int CDECL _wrename(const wchar_t *oldpath, const wchar_t *newpath)
Definition: file.c:4993
int CDECL _eof(int fd)
Definition: file.c:1333
char *CDECL fgets(char *s, int size, FILE *file)
Definition: file.c:3903
int CDECL feof(FILE *file)
Definition: file.c:3803
static FILE * iob_get_file(int i)
Definition: file.c:265
static void msvcrt_free_fd(int fd)
Definition: file.c:547
int CDECL _futime32(int fd, struct __utimbuf32 *t)
Definition: file.c:2101
int WINAPIV _sopen(const char *path, int oflags, int shflags,...)
Definition: file.c:2633
int WINAPIV _fwprintf_l(FILE *file, const wchar_t *format, _locale_t locale,...)
Definition: file.c:5654
errno_t CDECL freopen_s(FILE **pFile, const char *path, const char *mode, FILE *file)
Definition: file.c:4646
int CDECL putchar(int c)
Definition: file.c:4915
int WINAPIV _fwprintf_p_l(FILE *file, const wchar_t *format, _locale_t locale,...)
Definition: file.c:5680
FILE *CDECL _fdopen(int fd, const char *mode)
Definition: file.c:1847
char *CDECL gets_s(char *buf, size_t len)
Definition: file.c:4829
intptr_t CDECL _get_osfhandle(int fd)
Definition: file.c:2117
int CDECL rename(const char *oldpath, const char *newpath)
Definition: file.c:4973
int CDECL _fcloseall(void)
Definition: file.c:1373
#define WX_DONTINHERIT
Definition: file.c:84
int WINAPIV _fprintf_p(FILE *file, const char *format,...)
Definition: file.c:5589
int CDECL _waccess_s(const wchar_t *filename, int mode)
Definition: file.c:1039
void msvcrt_free_io(void)
Definition: file.c:1392
int CDECL _waccess(const wchar_t *filename, int mode)
Definition: file.c:1017
static int msvcrt_alloc_fd(HANDLE hand, int flag)
Definition: file.c:598
int CDECL _mktemp_s(char *pattern, size_t size)
Definition: file.c:2130
__int64 CDECL _ftelli64_nolock(FILE *file)
Definition: file.c:4686
int CDECL _chsize_s(int fd, __int64 size)
Definition: file.c:1626
FILE *CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
Definition: file.c:4335
static void time_to_filetime(__time64_t time, FILETIME *ft)
Definition: file.c:384
int CDECL fsetpos(FILE *file, fpos_t *pos)
Definition: file.c:4664
size_t CDECL _fwrite_nolock(const void *ptr, size_t size, size_t nmemb, FILE *file)
Definition: file.c:4143
int CDECL _vfprintf_p(FILE *file, const char *format, va_list valist)
Definition: file.c:5491
__msvcrt_long CDECL _tell(int fd)
Definition: file.c:3425
int CDECL vwprintf(const wchar_t *format, va_list valist)
Definition: file.c:5533
int WINAPIV fwprintf_s(FILE *file, const wchar_t *format,...)
Definition: file.c:5641
int CDECL _chsize(int fd, __msvcrt_long size)
Definition: file.c:1663
int CDECL _getmaxstdio(void)
Definition: file.c:5862
int CDECL _fseek_nolock(FILE *file, __msvcrt_long offset, int whence)
Definition: file.c:1618
static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
Definition: file.c:2964
int CDECL fputs(const char *s, FILE *file)
Definition: file.c:4769
size_t CDECL _fread_nolock(void *ptr, size_t size, size_t nmemb, FILE *file)
Definition: file.c:4420
static ioinfo * get_ioinfo_nolock(int fd)
Definition: file.c:394
int CDECL tmpnam_s(char *s, size_t size)
Definition: file.c:5100
int WINAPIV fwprintf(FILE *file, const wchar_t *format,...)
Definition: file.c:5628
static ioinfo * get_ioinfo(int fd)
Definition: file.c:417
FILE *CDECL fopen(const char *path, const char *mode)
Definition: file.c:4310
#define EF_CRIT_INIT
Definition: file.c:198
int CDECL _vfprintf_l(FILE *file, const char *format, _locale_t locale, va_list valist)
Definition: file.c:5463
void CDECL rewind(FILE *file)
Definition: file.c:1712
int CDECL fputws(const wchar_t *s, FILE *file)
Definition: file.c:4783
#define MSVCRT_R_OK
Definition: file.c:77
wint_t CDECL _ungetwc_nolock(wint_t wc, FILE *file)
Definition: file.c:5799
int CDECL _fseeki64_nolock(FILE *file, __int64 offset, int whence)
Definition: file.c:1585
__int64 CDECL _telli64(int fd)
Definition: file.c:3433
int CDECL _utime64(const char *path, struct __utimbuf64 *t)
Definition: file.c:3497
textmode
Definition: file.c:95
@ TEXTMODE_ANSI
Definition: file.c:96
@ TEXTMODE_UTF16LE
Definition: file.c:98
@ TEXTMODE_UTF8
Definition: file.c:97
int CDECL _access(const char *filename, int mode)
Definition: file.c:990
#define UNLOCK_FILES()
Definition: file.c:307
int WINAPIV wprintf_s(const wchar_t *format,...)
Definition: file.c:5849
int CDECL _getw(FILE *file)
Definition: file.c:3994
FILE *CDECL __iob_func(void)
Definition: file.c:972
int CDECL tmpfile_s(FILE **file)
Definition: file.c:5228
int CDECL fgetc(FILE *file)
Definition: file.c:3863
int CDECL _ungetc_nolock(int c, FILE *file)
Definition: file.c:5748
static int msvcrt_flush_all_buffers(int mask)
Definition: file.c:1148
void CDECL setbuf(FILE *file, char *buf)
Definition: file.c:5046
int WINAPIV fprintf_s(FILE *file, const char *format,...)
Definition: file.c:5562
int CDECL _vfwprintf_l(FILE *file, const wchar_t *format, _locale_t locale, va_list valist)
Definition: file.c:5472
FILE *CDECL tmpfile(void)
Definition: file.c:5199
int CDECL fflush(FILE *file)
Definition: file.c:1182
void CDECL _unlock_file(FILE *file)
Definition: file.c:1499
__msvcrt_long CDECL _filelength(int fd)
Definition: file.c:1887
static void msvcrt_stat64_to_stat32i64(const struct _stat64 *buf64, struct _stat32i64 *buf)
Definition: file.c:369
#define ALL_S_IREAD
Definition: file.c:71
int CDECL _wfreopen_s(FILE **pFile, const wchar_t *path, const wchar_t *mode, FILE *file)
Definition: file.c:4605
FILE MSVCRT__iob[_IOB_ENTRIES]
Definition: file.c:263
#define WX_PIPE
Definition: file.c:83
int CDECL _utime32(const char *path, struct __utimbuf32 *t)
Definition: file.c:3513
static FILE * msvcrt_get_file(int i)
Definition: file.c:520
static void release_ioinfo(ioinfo *info)
Definition: file.c:514
int CDECL _wremove(const wchar_t *path)
Definition: file.c:4965
static BOOL add_std_buffer(FILE *file)
Definition: file.c:893
__int64 CDECL _filelengthi64(int fd)
Definition: file.c:1906
static CRITICAL_SECTION * file_get_cs(FILE *f)
Definition: file.c:270
int CDECL _rmtmp(void)
Definition: file.c:2748
int CDECL _fstat32(int fd, struct _stat32 *buf)
Definition: file.c:2030
int CDECL fgetpos(FILE *file, fpos_t *pos)
Definition: file.c:4758
int CDECL _creat(const char *path, int pmode)
Definition: file.c:2693
static const ULONGLONG WCBAT
Definition: file.c:290
#define WX_TEXT
Definition: file.c:87
int CDECL vfwprintf_s(FILE *file, const wchar_t *format, va_list valist)
Definition: file.c:5373
#define ALL_S_IWRITE
Definition: file.c:72
#define EF_UNK_UNICODE
Definition: file.c:199
__msvcrt_long CDECL _lseek(int fd, __msvcrt_long offset, int whence)
Definition: file.c:1479
static const ULONGLONG WCCOM
Definition: file.c:292
int CDECL _fstat32i64(int fd, struct _stat32i64 *buf)
Definition: file.c:2044
static int get_utf8_char_len(char ch)
Definition: file.c:2770
int WINAPIV _open(const char *path, int flags,...)
Definition: file.c:2655
int CDECL _fileno(FILE *file)
Definition: file.c:1925
static int msvcrt_int_to_base32(int num, char *str)
Definition: file.c:919
int CDECL _futime64(int fd, struct __utimbuf64 *t)
Definition: file.c:2072
#define MSVCRT_INTERNAL_BUFSIZ
Definition: file.c:92
int CDECL _fseeki64(FILE *file, __int64 offset, int whence)
Definition: file.c:1571
static void ioinfo_set_unicode(ioinfo *info, BOOL unicode)
Definition: file.c:236
wint_t CDECL _fgetwchar(void)
Definition: file.c:4027
static void msvcrt_stat64_to_stat32(const struct _stat64 *buf64, struct _stat32 *buf)
Definition: file.c:339
static int msvcrt_init_fp(FILE *file, int fd, unsigned stream_flags)
Definition: file.c:649
int CDECL ferror(FILE *file)
Definition: file.c:3811
static int msvcrt_int_to_base32_w(int num, wchar_t *str)
Definition: file.c:944
int CDECL vprintf_s(const char *format, va_list valist)
Definition: file.c:5525
int CDECL _vfwprintf_p_l(FILE *file, const wchar_t *format, _locale_t locale, va_list valist)
Definition: file.c:5499
int CDECL _isatty(int fd)
Definition: file.c:858
int CDECL fputc(int c, FILE *file)
Definition: file.c:4360
static const ULONGLONG WCEXE
Definition: file.c:289
FILE *CDECL _wfdopen(int fd, const wchar_t *mode)
Definition: file.c:1863
int CDECL _wtmpnam_s(wchar_t *s, size_t size)
Definition: file.c:5174
int CDECL fopen_s(FILE **pFile, const char *filename, const char *mode)
Definition: file.c:4318
int CDECL _flushall(void)
Definition: file.c:1174
char *CDECL _mktemp(char *pattern)
Definition: file.c:2176
int CDECL _putws(const wchar_t *s)
Definition: file.c:4942
static ioinfo * get_ioinfo_alloc(int *fd)
Definition: file.c:481
#define MSVCRT_FD_BLOCK_SIZE
Definition: file.c:126
int CDECL setvbuf(FILE *file, char *buf, int mode, size_t size)
Definition: file.c:5006
static BOOL alloc_pioinfo_block(int fd)
Definition: file.c:427
int CDECL _wmktemp_s(wchar_t *pattern, size_t size)
Definition: file.c:2221
int CDECL _vfprintf_p_l(FILE *file, const char *format, _locale_t locale, va_list valist)
Definition: file.c:5481
int CDECL _filbuf(FILE *file)
Definition: file.c:3819
#define WX_APPEND
Definition: file.c:85
int CDECL getchar(void)
Definition: file.c:4813
int CDECL _wfopen_s(FILE **pFile, const wchar_t *filename, const wchar_t *mode)
Definition: file.c:4343
size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE *file)
Definition: file.c:4129
static int MSVCRT_stream_idx
Definition: file.c:279
static int vfprintf_helper(DWORD options, FILE *file, const char *format, _locale_t locale, va_list valist)
Definition: file.c:5264
static void msvcrt_stat64_to_stati64(const struct _stat64 *buf64, struct _stati64 *buf)
Definition: file.c:324
int errno_t
Definition: corecrt.h:249
int intptr_t
Definition: corecrt.h:176
long __msvcrt_long
Definition: corecrt.h:167
#define __cdecl
Definition: corecrt.h:121
#define _ARGMAX
Definition: corecrt.h:157
unsigned short wint_t
Definition: corecrt.h:243
#define __int64
Definition: corecrt.h:72
#define WEOF
#define stdout
#define stdin
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2983
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
_ACRTIMP int __cdecl wcsncmp(const wchar_t *, const wchar_t *, size_t)
Definition: wcs.c:518
_ACRTIMP int __cdecl _wcsnicmp(const wchar_t *, const wchar_t *, size_t)
Definition: wcs.c:195
_ACRTIMP wchar_t *__cdecl _wcsdup(const wchar_t *) __WINE_DEALLOC(free) __WINE_MALLOC
Definition: wcs.c:81
#define ENOENT
Definition: errno.h:25
#define EEXIST
Definition: errno.h:39
#define EINVAL
Definition: errno.h:44
#define ENFILE
Definition: errno.h:45
#define EMFILE
Definition: errno.h:46
#define ENOMEM
Definition: errno.h:35
#define ERANGE
Definition: errno.h:55
#define EBADF
Definition: errno.h:32
#define _O_WTEXT
Definition: fcntl.h:30
#define _O_U8TEXT
Definition: fcntl.h:32
#define _O_U16TEXT
Definition: fcntl.h:31
static int umask(int fd)
Definition: io.h:31
#define SIZE_MAX
Definition: limits.h:49
#define MB_LEN_MAX
Definition: limits.h:7
#define INT_MAX
Definition: limits.h:26
#define _SH_DENYRW
Definition: share.h:37
#define _SH_DENYRD
Definition: share.h:39
#define _SH_DENYNO
Definition: share.h:40
#define _SH_DENYWR
Definition: share.h:38
#define va_end(v)
Definition: stdarg.h:28
#define va_arg(v, l)
Definition: stdarg.h:27
#define va_start(v, l)
Definition: stdarg.h:26
_ACRTIMP size_t __cdecl _fread_nolock_s(void *, size_t, size_t, size_t, FILE *)
Definition: fread.cpp:69
#define _IOMYBUF
Definition: stdio.h:17
_ACRTIMP size_t __cdecl fread_s(void *, size_t, size_t, size_t, FILE *)
Definition: fread.cpp:30
#define _IOWRT
Definition: stdio.h:16
#define SEEK_CUR
Definition: stdio.h:44
#define EOF
Definition: stdio.h:33
#define _IOEOF
Definition: stdio.h:18
#define _IOREAD
Definition: stdio.h:15
#define BUFSIZ
Definition: stdio.h:40
#define _IORW
Definition: stdio.h:21
#define _IOSTRG
Definition: stdio.h:20
#define _IONBF
Definition: stdio.h:30
#define STDOUT_FILENO
Definition: stdio.h:25
_ACRTIMP __msvcrt_long __cdecl _ftell_nolock(FILE *)
Definition: ftell.cpp:379
#define _IOLBF
Definition: stdio.h:31
#define TMP_MAX
Definition: stdio.h:35
#define TMP_MAX_S
Definition: stdio.h:36
#define STDERR_FILENO
Definition: stdio.h:26
#define _IOERR
Definition: stdio.h:19
#define _IOFBF
Definition: stdio.h:29
#define STDIN_FILENO
Definition: stdio.h:24
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1592
#define _LK_LOCK
Definition: locking.h:27
#define _LK_UNLCK
Definition: locking.h:26
#define _LK_RLCK
Definition: locking.h:29
#define _LK_NBRLCK
Definition: locking.h:30
#define _LK_NBLCK
Definition: locking.h:28
#define _fstat64
Definition: stat.h:181
#define _stat64i32
Definition: stat.h:183
#define _wstat64i32
Definition: stat.h:184
#define _wstat64
Definition: stat.h:185
_ACRTIMP int __cdecl _wstat32i64(const wchar_t *, struct _stat32i64 *)
Definition: stat.cpp:542
#define _stat64
Definition: stat.h:182
_ACRTIMP int __cdecl _wstat32(const wchar_t *, struct _stat32 *)
Definition: stat.cpp:537
#define _fstat64i32
Definition: stat.h:180
_ACRTIMP __time64_t __cdecl _time64(__time64_t *)
Definition: time.c:780
char * va_list
Definition: vadefs.h:50
void CDECL _lock(int locknum)
Definition: lock.c:85
void CDECL _unlock(int locknum)
Definition: lock.c:114
thread_data_t *CDECL msvcrt_get_thread_data(void)
Definition: thread.c:45
static wchar_t * wstrdupa_utf8(const char *str)
Definition: msvcrt.h:440
static char * astrdupw_utf8(const wchar_t *wstr)
Definition: msvcrt.h:452
#define UCRTBASE_PRINTF_MASK
Definition: msvcrt.h:401
int create_positional_ctx_w(void *, const wchar_t *, va_list)
int pf_printf_a(puts_clbk_a, void *, const char *, _locale_t, DWORD, args_clbk, void *, va_list *)
#define MSVCRT_NO_CONSOLE_FD
Definition: msvcrt.h:327
#define MSVCRT_PRINTF_POSITIONAL_PARAMS
Definition: msvcrt.h:409
#define MSVCRT_INVALID_PMT(x, err)
Definition: msvcrt.h:376
int create_positional_ctx_a(void *, const char *, va_list)
#define MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
Definition: msvcrt.h:410
int pf_printf_w(puts_clbk_w, void *, const wchar_t *, _locale_t, DWORD, args_clbk, void *, va_list *)
printf_arg arg_clbk_valist(void *, int, int, va_list *)
Definition: wcs.c:847
#define __acrt_iob_func(idx)
Definition: msvcrt.h:332
#define MSVCRT__NOBUF
Definition: msvcrt.h:51
printf_arg arg_clbk_positional(void *, int, int, va_list *)
Definition: wcs.c:870
#define MSVCRT__IOCOMMIT
Definition: msvcrt.h:337
#define MSVCRT_NO_CONSOLE
Definition: msvcrt.h:328
#define MSVCRT_CHECK_PMT(x)
Definition: msvcrt.h:378
#define MSVCRT_CHECK_PMT_ERR(x, err)
Definition: msvcrt.h:377
#define MSVCRT__USERBUF
Definition: msvcrt.h:336
#define _STREAM_LOCKS
Definition: mtdll.h:52
#define _IOB_ENTRIES
Definition: mtdll.h:55
unsigned char
Definition: typeof.h:29
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
#define MOVEFILE_COPY_ALLOWED
Definition: filesup.h:29
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define printf
Definition: freeldr.h:97
@ FileBasicInformation
Definition: from_kernel.h:65
MdFileObject pFile
FxCollectionEntry * cur
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLdouble s
Definition: gl.h:2039
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble GLdouble t
Definition: gl.h:2047
GLdouble n
Definition: glext.h:7729
GLuint res
Definition: glext.h:9613
GLsizeiptr size
Definition: glext.h:5919
GLintptr offset
Definition: glext.h:5920
const GLubyte * c
Definition: glext.h:8905
GLenum GLint GLuint mask
Definition: glext.h:6028
GLubyte * pattern
Definition: glext.h:7787
GLfloat f
Definition: glext.h:7540
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLbitfield flags
Definition: glext.h:7161
GLuint GLfloat * val
Definition: glext.h:7180
GLuint GLint GLboolean GLint GLenum access
Definition: glext.h:7866
GLfloat GLfloat p
Definition: glext.h:8902
GLuint GLuint num
Definition: glext.h:9618
GLenum GLsizei len
Definition: glext.h:6722
const GLuint * buffers
Definition: glext.h:5916
GLuint id
Definition: glext.h:5910
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean flag
Definition: glfuncs.h:52
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define cs
Definition: i386-dis.c:442
BOOLEAN NTAPI RtlTimeToSecondsSince1970(PLARGE_INTEGER Time, PULONG ElapsedSeconds)
REFIID LPVOID DWORD_PTR dw
Definition: atlbase.h:40
#define InterlockedCompareExchangePointer
Definition: interlocked.h:144
#define InterlockedCompareExchange
Definition: interlocked.h:119
const char * filename
Definition: ioapi.h:137
uint32_t cc
Definition: isohybrid.c:75
#define SEEK_SET
Definition: jmemansi.c:26
static const int digits[]
Definition: decode.c:71
#define c
Definition: ke_i.h:80
#define debugstr_a
Definition: kernel32.h:31
#define debugstr_w
Definition: kernel32.h:32
if(dx< 0)
Definition: linetemp.h:194
@ GetFileExInfoStandard
Definition: minwinbase.h:356
__u16 time
Definition: mkdosfs.c:8
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define CREATE_ALWAYS
Definition: disk.h:72
#define TRUNCATE_EXISTING
Definition: disk.h:71
#define FILE_FLAG_RANDOM_ACCESS
Definition: disk.h:44
#define FILE_FLAG_DELETE_ON_CLOSE
Definition: disk.h:42
#define CREATE_NEW
Definition: disk.h:69
#define OPEN_ALWAYS
Definition: disk.h:70
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
static PVOID ptr
Definition: dispmode.c:27
#define wctomb
Definition: wctomb.c:31
static const WCHAR filenameW[]
Definition: amstream.c:41
static va_list valist
Definition: printf.c:46
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK io
Definition: file.c:100
static unsigned(__cdecl *hash_bstr)(bstr_t s)
static int secure
Definition: server.c:157
int CDECL _get_stream_buffer_pointers(FILE *, char ***, char ***, int **)
Definition: _file.cpp:153
#define __ASM_NAME(name)
Definition: config.h:934
ssize_t pread(int fd, void *buf, size_t count, off_t offset)
#define __ASM_GLOBAL_FUNC(name, code)
Definition: port.h:201
#define min(a, b)
Definition: monoChain.cc:55
int k
Definition: mpi.c:3369
int remove
Definition: msacm.c:1366
BOOL WINAPI CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
Definition: npipe.c:117
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
NTSYSAPI NTSTATUS NTAPI NtQueryInformationFile(IN HANDLE hFile, OUT PIO_STATUS_BLOCK pIoStatusBlock, OUT PVOID FileInformationBuffer, IN ULONG FileInformationBufferLength, IN FILE_INFORMATION_CLASS FileInfoClass)
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
#define DELETE
Definition: nt_native.h:57
#define GENERIC_WRITE
Definition: nt_native.h:90
#define FILE_ATTRIBUTE_TEMPORARY
Definition: nt_native.h:708
static ULONG POBJECT_ATTRIBUTES PIO_STATUS_BLOCK ULONG sharing
Definition: pipe.c:70
_Must_inspect_result_ _Out_ LPSIZE psize
Definition: ntgdi.h:1569
int __cdecl __stdio_common_vfwprintf_s(unsigned __int64 const options, FILE *const stream, wchar_t const *const format, _locale_t const locale, va_list const arglist)
Definition: output.cpp:88
int __cdecl __stdio_common_vfwprintf_p(unsigned __int64 const options, FILE *const stream, wchar_t const *const format, _locale_t const locale, va_list const arglist)
Definition: output.cpp:112
int __cdecl __stdio_common_vfwprintf(unsigned __int64 const options, FILE *const stream, wchar_t const *const format, _locale_t const locale, va_list const arglist)
Definition: output.cpp:64
int __cdecl __stdio_common_vfprintf_p(unsigned __int64 const options, FILE *const stream, char const *const format, _locale_t const locale, va_list const arglist)
Definition: output.cpp:100
int __cdecl __stdio_common_vfprintf_s(unsigned __int64 const options, FILE *const stream, char const *const format, _locale_t const locale, va_list const arglist)
Definition: output.cpp:76
long LONG
Definition: pedump.c:60
#define calloc
Definition: rosglue.h:14
const WCHAR * str
#define WINAPIV
Definition: sdbpapi.h:64
#define iswalpha(_c)
Definition: ctype.h:664
#define _fgetc_nolock(_stream)
Definition: stdio.h:1142
#define _fputc_nolock(_c, _stream)
Definition: stdio.h:1143
#define getwc(_stm)
Definition: stdio.h:1131
#define isleadbyte(_c)
Definition: wchar.h:598
#define CP_UTF8
Definition: nls.h:20
__int64 __time64_t
Definition: corecrt.h:619
_wmktemp
Definition: corecrt_wio.h:154
__int64 fpos_t
Definition: stdio.h:73
mbstowcs
Definition: stdlib.h:925
#define _stat
Definition: stat.h:146
#define _stati64
Definition: stat.h:147
#define _fstat
Definition: stat.h:144
#define _wstati64
Definition: stat.h:149
#define _wstat
Definition: stat.h:148
#define _fstati64
Definition: stat.h:145
#define msvcrt_set_errno
Definition: heap.c:50
static int fd
Definition: io.c:51
int seek(void *fd, ulong off, int mode)
Definition: pe.c:51
#define memset(x, y, z)
Definition: compat.h:39
#define mbtowc(wp, cp, len)
Definition: wchar.h:155
#define wchar_t
Definition: wchar.h:102
#define towlower(c)
Definition: wctype.h:97
#define towupper(c)
Definition: wctype.h:99
#define FileStandardInformation
Definition: propsheet.cpp:61
const char * ws
Definition: skip_ws.cpp:7
#define TRACE(s)
Definition: solgame.cpp:4
DWORD dwHighDateTime
Definition: mapidefs.h:66
DWORD dwLowDateTime
Definition: mapidefs.h:65
LARGE_INTEGER LastWriteTime
Definition: nt_native.h:944
LARGE_INTEGER LastAccessTime
Definition: nt_native.h:943
__time64_t modtime
Definition: utime.h:42
__time64_t actime
Definition: utime.h:41
Definition: stat.h:80
Definition: stat.h:136
__int64 st_size
Definition: stat.h:78
__time64_t st_ctime
Definition: stat.h:147
__time64_t st_mtime
Definition: stat.h:146
short st_uid
Definition: stat.h:141
__time64_t st_atime
Definition: stat.h:145
_dev_t st_dev
Definition: stat.h:137
short st_gid
Definition: stat.h:142
unsigned short st_mode
Definition: stat.h:139
short st_nlink
Definition: stat.h:140
_dev_t st_rdev
Definition: stat.h:143
_ino_t st_ino
Definition: stat.h:138
Definition: stat.h:52
Definition: stat.h:122
Definition: cookie.c:202
FILE file
Definition: file.c:246
CRITICAL_SECTION crit
Definition: file.c:247
Definition: fci.c:127
Definition: format.c:58
Definition: file.c:128
int exflag
Definition: file.c:132
char lookahead[3]
Definition: file.c:131
unsigned char wxflag
Definition: file.c:130
HANDLE handle
Definition: file.c:129
CRITICAL_SECTION crit
Definition: file.c:133
Definition: ps.c:97
Definition: parse.h:23
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
Character const *const prefix
Definition: tempnam.cpp:195
#define DWORD_PTR
Definition: treelist.c:76
PVOID HANDLE
Definition: typedefs.h:73
uint64_t ULONGLONG
Definition: typedefs.h:67
LONGLONG QuadPart
Definition: typedefs.h:114
struct _LARGE_INTEGER::@2493 u
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
char mbs[5]
_In_ size_t cnt
Definition: wcstombs.cpp:43
#define wprintf(...)
Definition: whoami.c:18
#define STD_OUTPUT_HANDLE
Definition: winbase.h:292
#define FILE_END
Definition: winbase.h:116
#define STD_INPUT_HANDLE
Definition: winbase.h:291
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
BOOL WINAPI TryEnterCriticalSection(LPCRITICAL_SECTION)
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
#define FILE_TYPE_UNKNOWN
Definition: winbase.h:282
DWORD WINAPI GetCurrentProcessId(void)
Definition: proc.c:1158
#define STD_ERROR_HANDLE
Definition: winbase.h:293
#define FILE_CURRENT
Definition: winbase.h:115
#define FILE_TYPE_CHAR
Definition: winbase.h:284
#define FILE_TYPE_PIPE
Definition: winbase.h:285
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
#define __ASM_CFI(str)
Definition: asm.h:39
#define ERROR_BROKEN_PIPE
Definition: winerror.h:305
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36
#define RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO
Definition: winnt_old.h:1146
#define DUPLICATE_SAME_ACCESS
static unsigned int block
Definition: xmlmemory.c:101
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned char BYTE
Definition: xxhash.c:193