2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
9 * Copyright 2004 Juan Lang
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
30 #include "wine/port.h"
38 #include <sys/types.h>
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
51 /* for stat mode, permissions apply to all,owner and group */
52 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
53 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
54 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
56 /* _access() bit flags FIXME: incomplete */
57 #define MSVCRT_W_OK 0x02
59 /* values for wxflag in file descriptor */
62 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
63 #define WX_DONTINHERIT 0x10
64 #define WX_APPEND 0x20
67 /* FIXME: this should be allocated dynamically */
68 #define MSVCRT_MAX_FILES 2048
73 DWORD unkn
[7]; /* critical section and init flag */
76 static ioinfo MSVCRT_fdesc
[MSVCRT_MAX_FILES
];
78 MSVCRT_FILE MSVCRT__iob
[3] = { { 0 } };
80 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
81 static int MSVCRT_fdend
= 3; /* highest allocated fd */
83 static MSVCRT_FILE
* MSVCRT_fstreams
[2048];
84 static int MSVCRT_stream_idx
;
86 /* INTERNAL: process umask */
87 static int MSVCRT_umask
= 0;
89 /* INTERNAL: Static buffer for temp file name */
90 static char MSVCRT_tmpname
[MAX_PATH
];
92 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
93 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
94 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
95 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
97 #define TOUL(x) (ULONGLONG)(x)
98 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
99 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
100 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
101 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
103 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
104 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
105 * and MSVCRT_stream_idx, from race conditions.
106 * It doesn't protect against race conditions manipulating the underlying files
107 * or flags; doing so would probably be better accomplished with per-file
108 * protection, rather than locking the whole table for every change.
110 static CRITICAL_SECTION MSVCRT_file_cs
;
111 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
112 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
114 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
116 buf
->st_dev
= buf64
->st_dev
;
117 buf
->st_ino
= buf64
->st_ino
;
118 buf
->st_mode
= buf64
->st_mode
;
119 buf
->st_nlink
= buf64
->st_nlink
;
120 buf
->st_uid
= buf64
->st_uid
;
121 buf
->st_gid
= buf64
->st_gid
;
122 buf
->st_rdev
= buf64
->st_rdev
;
123 buf
->st_size
= buf64
->st_size
;
124 buf
->st_atime
= buf64
->st_atime
;
125 buf
->st_mtime
= buf64
->st_mtime
;
126 buf
->st_ctime
= buf64
->st_ctime
;
129 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
131 buf
->st_dev
= buf64
->st_dev
;
132 buf
->st_ino
= buf64
->st_ino
;
133 buf
->st_mode
= buf64
->st_mode
;
134 buf
->st_nlink
= buf64
->st_nlink
;
135 buf
->st_uid
= buf64
->st_uid
;
136 buf
->st_gid
= buf64
->st_gid
;
137 buf
->st_rdev
= buf64
->st_rdev
;
138 buf
->st_size
= buf64
->st_size
;
139 buf
->st_atime
= buf64
->st_atime
;
140 buf
->st_mtime
= buf64
->st_mtime
;
141 buf
->st_ctime
= buf64
->st_ctime
;
144 static inline BOOL
msvcrt_is_valid_fd(int fd
)
146 return fd
>= 0 && fd
< MSVCRT_fdend
&& (MSVCRT_fdesc
[fd
].wxflag
& WX_OPEN
);
149 /* INTERNAL: Get the HANDLE for a fd
150 * This doesn't lock the table, because a failure will result in
151 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
152 * it returns a valid handle which is about to be closed, a subsequent call
153 * will fail, most likely in a sane way.
155 static HANDLE
msvcrt_fdtoh(int fd
)
157 if (!msvcrt_is_valid_fd(fd
))
159 WARN(":fd (%d) - no handle!\n",fd
);
160 *MSVCRT___doserrno() = 0;
161 *MSVCRT__errno() = MSVCRT_EBADF
;
162 return INVALID_HANDLE_VALUE
;
164 if (MSVCRT_fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
165 return MSVCRT_fdesc
[fd
].handle
;
168 /* INTERNAL: free a file entry fd */
169 static void msvcrt_free_fd(int fd
)
174 old_handle
= MSVCRT_fdesc
[fd
].handle
;
175 MSVCRT_fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
176 MSVCRT_fdesc
[fd
].wxflag
= 0;
177 TRACE(":fd (%d) freed\n",fd
);
178 if (fd
< 3) /* don't use 0,1,2 for user files */
183 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
186 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
189 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
195 if (fd
== MSVCRT_fdend
- 1)
197 if (fd
< MSVCRT_fdstart
)
203 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
204 /* caller must hold the files lock */
205 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
207 if (fd
>= MSVCRT_MAX_FILES
)
209 WARN(":files exhausted!\n");
212 MSVCRT_fdesc
[fd
].handle
= hand
;
213 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
215 /* locate next free slot */
216 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
217 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
219 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
220 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
222 /* update last fd in use */
223 if (fd
>= MSVCRT_fdend
)
224 MSVCRT_fdend
= fd
+ 1;
225 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
229 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
230 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
231 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
237 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
238 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
243 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
244 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
249 /* INTERNAL: Allocate a FILE* for an fd slot */
250 /* caller must hold the files lock */
251 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
255 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
257 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
259 if (!MSVCRT_fstreams
[i
])
261 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
263 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
265 return MSVCRT_fstreams
[i
];
271 /* INTERNAL: initialize a FILE* from an open fd */
272 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
274 TRACE(":fd (%d) allocating FILE*\n",fd
);
275 if (!msvcrt_is_valid_fd(fd
))
277 WARN(":invalid fd %d\n",fd
);
278 *MSVCRT___doserrno() = 0;
279 *MSVCRT__errno() = MSVCRT_EBADF
;
282 memset(file
, 0, sizeof(*file
));
284 file
->_flag
= stream_flags
;
286 TRACE(":got FILE* (%p)\n",file
);
290 /* INTERNAL: Create an inheritance data block (for spawned process)
291 * The inheritance block is made of:
292 * 00 int nb of file descriptor (NBFD)
293 * 04 char file flags (wxflag): repeated for each fd
294 * 4+NBFD HANDLE file handle: repeated for each fd
296 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
302 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
303 *block
= MSVCRT_calloc(*size
, 1);
309 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
310 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
312 *(unsigned*)*block
= MSVCRT_fdend
;
313 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
315 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
316 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
318 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
319 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
324 *handle_ptr
= INVALID_HANDLE_VALUE
;
326 wxflag_ptr
++; handle_ptr
++;
331 /* INTERNAL: Set up all file descriptors,
332 * as well as default streams (stdin, stderr and stdout)
334 void msvcrt_init_io(void)
339 InitializeCriticalSection(&MSVCRT_file_cs
);
340 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs");
341 GetStartupInfoA(&si
);
342 if (si
.cbReserved2
!= 0 && si
.lpReserved2
!= NULL
)
347 MSVCRT_fdend
= *(unsigned*)si
.lpReserved2
;
349 wxflag_ptr
= (char*)(si
.lpReserved2
+ sizeof(unsigned));
350 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
352 MSVCRT_fdend
= min(MSVCRT_fdend
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
353 for (i
= 0; i
< MSVCRT_fdend
; i
++)
355 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
357 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
358 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
362 MSVCRT_fdesc
[i
].wxflag
= 0;
363 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
365 wxflag_ptr
++; handle_ptr
++;
367 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
368 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
371 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
373 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
374 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
375 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
,
376 0, TRUE
, DUPLICATE_SAME_ACCESS
))
377 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
379 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
381 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
382 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
383 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
,
384 0, TRUE
, DUPLICATE_SAME_ACCESS
))
385 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
387 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
389 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
390 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
391 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
,
392 0, TRUE
, DUPLICATE_SAME_ACCESS
))
393 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
396 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
397 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
399 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
400 for (i
= 0; i
< 3; i
++)
402 /* FILE structs for stdin/out/err are static and never deleted */
403 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
404 MSVCRT__iob
[i
]._file
= i
;
405 MSVCRT__iob
[i
]._tmpfname
= NULL
;
406 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
408 MSVCRT_stream_idx
= 3;
411 /* INTERNAL: Flush stdio file buffer */
412 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
415 int cnt
=file
->_ptr
-file
->_base
;
416 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
417 file
->_flag
|= MSVCRT__IOERR
;
420 file
->_ptr
=file
->_base
;
421 file
->_cnt
=file
->_bufsiz
;
426 /* INTERNAL: Allocate stdio file buffer */
427 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
429 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
431 file
->_bufsiz
= MSVCRT_BUFSIZ
;
432 file
->_flag
|= MSVCRT__IOMYBUF
;
434 file
->_base
= (char*)(&file
->_charbuf
);
436 file
->_bufsiz
= sizeof(file
->_charbuf
);
438 file
->_ptr
= file
->_base
;
442 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
443 static void msvcrt_int_to_base32(int num
, char *str
)
458 *p
= (num
& 31) + '0';
460 *p
+= ('a' - '0' - 10);
465 /*********************************************************************
466 * __iob_func(MSVCRT.@)
468 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
470 return &MSVCRT__iob
[0];
473 /*********************************************************************
476 int CDECL
MSVCRT__access(const char *filename
, int mode
)
478 DWORD attr
= GetFileAttributesA(filename
);
480 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
482 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
484 msvcrt_set_errno(GetLastError());
487 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
489 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
495 /*********************************************************************
496 * _waccess (MSVCRT.@)
498 int CDECL
_waccess(const MSVCRT_wchar_t
*filename
, int mode
)
500 DWORD attr
= GetFileAttributesW(filename
);
502 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
504 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
506 msvcrt_set_errno(GetLastError());
509 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
511 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
517 /*********************************************************************
520 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
522 DWORD oldFlags
= GetFileAttributesA(path
);
524 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
526 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
527 oldFlags
| FILE_ATTRIBUTE_READONLY
;
529 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
532 msvcrt_set_errno(GetLastError());
536 /*********************************************************************
539 int CDECL
_wchmod(const MSVCRT_wchar_t
*path
, int flags
)
541 DWORD oldFlags
= GetFileAttributesW(path
);
543 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
545 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
546 oldFlags
| FILE_ATTRIBUTE_READONLY
;
548 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
551 msvcrt_set_errno(GetLastError());
555 /*********************************************************************
558 int CDECL
MSVCRT__unlink(const char *path
)
560 TRACE("%s\n",debugstr_a(path
));
561 if(DeleteFileA(path
))
563 TRACE("failed (%d)\n",GetLastError());
564 msvcrt_set_errno(GetLastError());
568 /*********************************************************************
569 * _wunlink (MSVCRT.@)
571 int CDECL
_wunlink(const MSVCRT_wchar_t
*path
)
573 TRACE("(%s)\n",debugstr_w(path
));
574 if(DeleteFileW(path
))
576 TRACE("failed (%d)\n",GetLastError());
577 msvcrt_set_errno(GetLastError());
581 /* _flushall calls MSVCRT_fflush which calls _flushall */
582 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
584 /*********************************************************************
585 * _flushall (MSVCRT.@)
587 int CDECL
_flushall(void)
589 int i
, num_flushed
= 0;
592 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
593 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
596 /* FIXME: flush, do not commit */
597 if (_commit(i
) == -1)
598 if (MSVCRT_fstreams
[i
])
599 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
601 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
602 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
608 TRACE(":flushed (%d) handles\n",num_flushed
);
612 /*********************************************************************
615 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
619 } else if(file
->_flag
& MSVCRT__IOWRT
) {
620 int res
=msvcrt_flush_buffer(file
);
626 /*********************************************************************
629 int CDECL
MSVCRT__close(int fd
)
635 hand
= msvcrt_fdtoh(fd
);
636 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
637 if (hand
== INVALID_HANDLE_VALUE
)
639 else if (!CloseHandle(hand
))
641 WARN(":failed-last error (%d)\n",GetLastError());
642 msvcrt_set_errno(GetLastError());
655 /*********************************************************************
658 int CDECL
_commit(int fd
)
660 HANDLE hand
= msvcrt_fdtoh(fd
);
662 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
663 if (hand
== INVALID_HANDLE_VALUE
)
666 if (!FlushFileBuffers(hand
))
668 if (GetLastError() == ERROR_INVALID_HANDLE
)
670 /* FlushFileBuffers fails for console handles
671 * so we ignore this error.
675 TRACE(":failed-last error (%d)\n",GetLastError());
676 msvcrt_set_errno(GetLastError());
683 /*********************************************************************
686 * MSDN isn't clear on this point, but the remarks for _pipe
687 * indicate file descriptors duplicated with _dup and _dup2 are always
690 int CDECL
MSVCRT__dup2(int od
, int nd
)
694 TRACE("(od=%d, nd=%d)\n", od
, nd
);
696 if (nd
< MSVCRT_MAX_FILES
&& msvcrt_is_valid_fd(od
))
700 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
701 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
703 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
705 if (msvcrt_is_valid_fd(nd
))
707 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
711 *MSVCRT__errno() = MSVCRT_EMFILE
;
715 /* _dup2 returns 0, not nd, on success */
722 msvcrt_set_errno(GetLastError());
727 *MSVCRT__errno() = MSVCRT_EBADF
;
734 /*********************************************************************
737 int CDECL
MSVCRT__dup(int od
)
743 if (MSVCRT__dup2(od
, fd
) == 0)
751 /*********************************************************************
754 int CDECL
_eof(int fd
)
757 LONG hcurpos
,hendpos
;
758 HANDLE hand
= msvcrt_fdtoh(fd
);
760 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
762 if (hand
== INVALID_HANDLE_VALUE
)
765 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
767 /* Otherwise we do it the hard way */
768 hcurpos
= hendpos
= 0;
769 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
770 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
772 if (curpos
== endpos
&& hcurpos
== hendpos
)
774 /* FIXME: shouldn't WX_ATEOF be set here? */
778 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
782 /*********************************************************************
783 * _fcloseall (MSVCRT.@)
785 int CDECL
MSVCRT__fcloseall(void)
787 int num_closed
= 0, i
;
790 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
791 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
792 !MSVCRT_fclose(MSVCRT_fstreams
[i
]))
796 TRACE(":closed (%d) handles\n",num_closed
);
800 /* free everything on process exit */
801 void msvcrt_free_io(void)
804 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
805 * stdout, and stderr (unlike GNU), so we need to fclose() them here
806 * or they won't get flushed.
808 MSVCRT_fclose(&MSVCRT__iob
[0]);
809 MSVCRT_fclose(&MSVCRT__iob
[1]);
810 MSVCRT_fclose(&MSVCRT__iob
[2]);
811 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = 0;
812 DeleteCriticalSection(&MSVCRT_file_cs
);
815 /*********************************************************************
816 * _lseeki64 (MSVCRT.@)
818 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
820 HANDLE hand
= msvcrt_fdtoh(fd
);
821 LARGE_INTEGER ofs
, ret
;
823 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
824 if (hand
== INVALID_HANDLE_VALUE
)
827 if (whence
< 0 || whence
> 2)
829 *MSVCRT__errno() = MSVCRT_EINVAL
;
833 TRACE(":fd (%d) to %s pos %s\n",
834 fd
,wine_dbgstr_longlong(offset
),
835 (whence
==SEEK_SET
)?"SEEK_SET":
836 (whence
==SEEK_CUR
)?"SEEK_CUR":
837 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
839 ofs
.QuadPart
= offset
;
840 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
842 MSVCRT_fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
843 /* FIXME: What if we seek _to_ EOF - is EOF set? */
847 TRACE(":error-last error (%d)\n",GetLastError());
848 msvcrt_set_errno(GetLastError());
852 /*********************************************************************
855 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
857 return MSVCRT__lseeki64(fd
, offset
, whence
);
860 /*********************************************************************
861 * _locking (MSVCRT.@)
863 * This is untested; the underlying LockFile doesn't work yet.
865 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
869 HANDLE hand
= msvcrt_fdtoh(fd
);
871 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
872 if (hand
== INVALID_HANDLE_VALUE
)
875 if (mode
< 0 || mode
> 4)
877 *MSVCRT__errno() = MSVCRT_EINVAL
;
881 TRACE(":fd (%d) by 0x%08x mode %s\n",
882 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
883 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
884 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
885 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
886 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
889 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
891 FIXME ("Seek failed\n");
892 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
895 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
898 ret
= 1; /* just to satisfy gcc */
901 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
906 else if (mode
== MSVCRT__LK_UNLCK
)
907 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
909 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
910 /* FIXME - what about error settings? */
914 /*********************************************************************
917 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
919 /* Flush output if needed */
920 if(file
->_flag
& MSVCRT__IOWRT
)
921 msvcrt_flush_buffer(file
);
923 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
924 offset
-= file
->_cnt
;
925 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
926 /* Black magic correction for CR removal */
928 for (i
=0; i
<file
->_cnt
; i
++) {
929 if (file
->_ptr
[i
] == '\n')
934 /* Discard buffered input */
936 file
->_ptr
= file
->_base
;
937 /* Reset direction of i/o */
938 if(file
->_flag
& MSVCRT__IORW
) {
939 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
941 /* Clear end of file flag */
942 file
->_flag
&= ~MSVCRT__IOEOF
;
943 return (MSVCRT__lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
946 /*********************************************************************
949 int CDECL
_chsize(int fd
, long size
)
955 TRACE("(fd=%d, size=%ld)\n", fd
, size
);
959 handle
= msvcrt_fdtoh(fd
);
960 if (handle
!= INVALID_HANDLE_VALUE
)
962 /* save the current file pointer */
963 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
966 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
969 ret
= SetEndOfFile(handle
);
970 if (!ret
) msvcrt_set_errno(GetLastError());
973 /* restore the file pointer */
974 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
982 /*********************************************************************
983 * clearerr (MSVCRT.@)
985 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
987 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
988 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
991 /*********************************************************************
994 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
996 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
997 MSVCRT_fseek(file
, 0L, SEEK_SET
);
998 MSVCRT_clearerr(file
);
1001 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1003 int plus
= strchrW(mode
, '+') != NULL
;
1008 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1009 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1012 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1013 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1016 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1017 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1027 *open_flags
|= MSVCRT__O_BINARY
;
1028 *open_flags
&= ~MSVCRT__O_TEXT
;
1031 *open_flags
|= MSVCRT__O_TEXT
;
1032 *open_flags
&= ~MSVCRT__O_BINARY
;
1037 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1042 /*********************************************************************
1043 * _fdopen (MSVCRT.@)
1045 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1048 MSVCRT_wchar_t
*modeW
= NULL
;
1050 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1052 ret
= MSVCRT__wfdopen(fd
, modeW
);
1058 /*********************************************************************
1059 * _wfdopen (MSVCRT.@)
1061 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1063 int open_flags
, stream_flags
;
1066 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1069 if (!(file
= msvcrt_alloc_fp()))
1071 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1076 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1082 /*********************************************************************
1083 * _filelength (MSVCRT.@)
1085 LONG CDECL
MSVCRT__filelength(int fd
)
1087 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1090 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1093 if (endPos
!= curPos
)
1094 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1101 /*********************************************************************
1102 * _filelengthi64 (MSVCRT.@)
1104 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1106 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1109 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1112 if (endPos
!= curPos
)
1113 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1120 /*********************************************************************
1121 * _fileno (MSVCRT.@)
1123 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1125 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1129 /*********************************************************************
1130 * _fstat64 (MSVCRT.@)
1132 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1136 BY_HANDLE_FILE_INFORMATION hfi
;
1137 HANDLE hand
= msvcrt_fdtoh(fd
);
1139 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1140 if (hand
== INVALID_HANDLE_VALUE
)
1145 WARN(":failed-NULL buf\n");
1146 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1150 memset(&hfi
, 0, sizeof(hfi
));
1151 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1152 type
= GetFileType(hand
);
1153 if (type
== FILE_TYPE_PIPE
)
1155 buf
->st_dev
= buf
->st_rdev
= fd
;
1156 buf
->st_mode
= S_IFIFO
;
1159 else if (type
== FILE_TYPE_CHAR
)
1161 buf
->st_dev
= buf
->st_rdev
= fd
;
1162 buf
->st_mode
= S_IFCHR
;
1165 else /* FILE_TYPE_DISK etc. */
1167 if (!GetFileInformationByHandle(hand
, &hfi
))
1169 WARN(":failed-last error (%d)\n",GetLastError());
1170 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1173 buf
->st_mode
= S_IFREG
| 0444;
1174 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1175 buf
->st_mode
|= 0222;
1176 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1177 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1179 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1180 buf
->st_mtime
= buf
->st_ctime
= dw
;
1181 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1183 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1188 /*********************************************************************
1189 * _fstati64 (MSVCRT.@)
1191 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1194 struct MSVCRT__stat64 buf64
;
1196 ret
= MSVCRT__fstat64(fd
, &buf64
);
1198 msvcrt_stat64_to_stati64(&buf64
, buf
);
1202 /*********************************************************************
1205 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1207 struct MSVCRT__stat64 buf64
;
1209 ret
= MSVCRT__fstat64(fd
, &buf64
);
1211 msvcrt_stat64_to_stat(&buf64
, buf
);
1215 /*********************************************************************
1216 * _futime (MSVCRT.@)
1218 int CDECL
_futime(int fd
, struct MSVCRT__utimbuf
*t
)
1220 HANDLE hand
= msvcrt_fdtoh(fd
);
1225 MSVCRT_time_t currTime
;
1226 MSVCRT_time(&currTime
);
1227 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
1232 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
1233 if (t
->actime
== t
->modtime
)
1236 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
1239 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1241 msvcrt_set_errno(GetLastError());
1247 /*********************************************************************
1248 * _get_osfhandle (MSVCRT.@)
1250 MSVCRT_intptr_t CDECL
_get_osfhandle(int fd
)
1252 HANDLE hand
= msvcrt_fdtoh(fd
);
1253 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1255 return (MSVCRT_intptr_t
)hand
;
1258 /*********************************************************************
1259 * _isatty (MSVCRT.@)
1261 int CDECL
_isatty(int fd
)
1263 HANDLE hand
= msvcrt_fdtoh(fd
);
1265 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1266 if (hand
== INVALID_HANDLE_VALUE
)
1269 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1272 /*********************************************************************
1273 * _mktemp (MSVCRT.@)
1275 char * CDECL
_mktemp(char *pattern
)
1278 char *retVal
= pattern
;
1283 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1287 id
= GetCurrentProcessId();
1291 int tempNum
= id
/ 10;
1292 *pattern
-- = id
- (tempNum
* 10) + '0';
1298 *pattern
= letter
++;
1299 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1300 GetLastError() == ERROR_FILE_NOT_FOUND
)
1302 } while(letter
<= 'z');
1306 /*********************************************************************
1307 * _wmktemp (MSVCRT.@)
1309 MSVCRT_wchar_t
* CDECL
_wmktemp(MSVCRT_wchar_t
*pattern
)
1312 MSVCRT_wchar_t
*retVal
= pattern
;
1314 MSVCRT_wchar_t letter
= 'a';
1317 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1321 id
= GetCurrentProcessId();
1325 int tempNum
= id
/ 10;
1326 *pattern
-- = id
- (tempNum
* 10) + '0';
1332 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1333 GetLastError() == ERROR_FILE_NOT_FOUND
)
1335 *pattern
= letter
++;
1336 } while(letter
!= '|');
1340 static unsigned split_oflags(unsigned oflags
)
1343 unsigned unsupp
; /* until we support everything */
1345 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1346 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1347 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1348 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1349 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1350 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1352 if ((unsupp
= oflags
& ~(
1353 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1354 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1355 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1356 MSVCRT__O_NOINHERIT
|
1357 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1359 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1364 /*********************************************************************
1367 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1370 SECURITY_ATTRIBUTES sa
;
1371 HANDLE readHandle
, writeHandle
;
1375 *MSVCRT__errno() = MSVCRT_EINVAL
;
1379 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1380 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1381 sa
.lpSecurityDescriptor
= NULL
;
1382 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1384 unsigned int wxflags
= split_oflags(textmode
);
1388 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1392 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1400 MSVCRT__close(pfds
[0]);
1401 CloseHandle(writeHandle
);
1402 *MSVCRT__errno() = MSVCRT_EMFILE
;
1407 CloseHandle(readHandle
);
1408 CloseHandle(writeHandle
);
1409 *MSVCRT__errno() = MSVCRT_EMFILE
;
1414 msvcrt_set_errno(GetLastError());
1419 /*********************************************************************
1422 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1426 DWORD access
= 0, creation
= 0, attrib
;
1430 SECURITY_ATTRIBUTES sa
;
1433 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1434 path
, oflags
, shflags
);
1436 wxflag
= split_oflags(oflags
);
1437 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1439 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1440 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1441 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1444 if (oflags
& MSVCRT__O_CREAT
)
1446 __ms_va_start(ap
, shflags
);
1447 pmode
= va_arg(ap
, int);
1450 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1451 FIXME(": pmode 0x%04x ignored\n", pmode
);
1453 WARN(": pmode 0x%04x ignored\n", pmode
);
1455 if (oflags
& MSVCRT__O_EXCL
)
1456 creation
= CREATE_NEW
;
1457 else if (oflags
& MSVCRT__O_TRUNC
)
1458 creation
= CREATE_ALWAYS
;
1460 creation
= OPEN_ALWAYS
;
1462 else /* no MSVCRT__O_CREAT */
1464 if (oflags
& MSVCRT__O_TRUNC
)
1465 creation
= TRUNCATE_EXISTING
;
1467 creation
= OPEN_EXISTING
;
1472 case MSVCRT__SH_DENYRW
:
1475 case MSVCRT__SH_DENYWR
:
1476 sharing
= FILE_SHARE_READ
;
1478 case MSVCRT__SH_DENYRD
:
1479 sharing
= FILE_SHARE_WRITE
;
1481 case MSVCRT__SH_DENYNO
:
1482 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1485 ERR( "Unhandled shflags 0x%x\n", shflags
);
1488 attrib
= FILE_ATTRIBUTE_NORMAL
;
1490 if (oflags
& MSVCRT__O_TEMPORARY
)
1492 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1494 sharing
|= FILE_SHARE_DELETE
;
1497 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1498 sa
.lpSecurityDescriptor
= NULL
;
1499 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1501 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1503 if (hand
== INVALID_HANDLE_VALUE
) {
1504 WARN(":failed-last error (%d)\n",GetLastError());
1505 msvcrt_set_errno(GetLastError());
1509 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1511 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1515 /*********************************************************************
1516 * _wsopen (MSVCRT.@)
1518 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1522 DWORD access
= 0, creation
= 0, attrib
;
1526 SECURITY_ATTRIBUTES sa
;
1529 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1530 debugstr_w(path
), oflags
, shflags
);
1532 wxflag
= split_oflags(oflags
);
1533 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1535 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1536 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1537 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1540 if (oflags
& MSVCRT__O_CREAT
)
1542 __ms_va_start(ap
, shflags
);
1543 pmode
= va_arg(ap
, int);
1546 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1547 FIXME(": pmode 0x%04x ignored\n", pmode
);
1549 WARN(": pmode 0x%04x ignored\n", pmode
);
1551 if (oflags
& MSVCRT__O_EXCL
)
1552 creation
= CREATE_NEW
;
1553 else if (oflags
& MSVCRT__O_TRUNC
)
1554 creation
= CREATE_ALWAYS
;
1556 creation
= OPEN_ALWAYS
;
1558 else /* no MSVCRT__O_CREAT */
1560 if (oflags
& MSVCRT__O_TRUNC
)
1561 creation
= TRUNCATE_EXISTING
;
1563 creation
= OPEN_EXISTING
;
1568 case MSVCRT__SH_DENYRW
:
1571 case MSVCRT__SH_DENYWR
:
1572 sharing
= FILE_SHARE_READ
;
1574 case MSVCRT__SH_DENYRD
:
1575 sharing
= FILE_SHARE_WRITE
;
1577 case MSVCRT__SH_DENYNO
:
1578 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1581 ERR( "Unhandled shflags 0x%x\n", shflags
);
1584 attrib
= FILE_ATTRIBUTE_NORMAL
;
1586 if (oflags
& MSVCRT__O_TEMPORARY
)
1588 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1590 sharing
|= FILE_SHARE_DELETE
;
1593 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1594 sa
.lpSecurityDescriptor
= NULL
;
1595 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1597 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1599 if (hand
== INVALID_HANDLE_VALUE
) {
1600 WARN(":failed-last error (%d)\n",GetLastError());
1601 msvcrt_set_errno(GetLastError());
1605 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1607 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1611 /*********************************************************************
1614 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1618 if (flags
& MSVCRT__O_CREAT
)
1621 __ms_va_start(ap
, flags
);
1622 pmode
= va_arg(ap
, int);
1624 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1627 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1630 /*********************************************************************
1633 int CDECL
_wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1637 if (flags
& MSVCRT__O_CREAT
)
1640 __ms_va_start(ap
, flags
);
1641 pmode
= va_arg(ap
, int);
1643 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1646 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1649 /*********************************************************************
1652 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1654 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1655 return MSVCRT__open(path
, usedFlags
);
1658 /*********************************************************************
1659 * _wcreat (MSVCRT.@)
1661 int CDECL
_wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1663 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1664 return _wopen(path
, usedFlags
);
1667 /*********************************************************************
1668 * _open_osfhandle (MSVCRT.@)
1670 int CDECL
_open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1674 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1675 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1676 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1677 * text - it never sets MSVCRT__O_BINARY.
1679 /* don't let split_oflags() decide the mode if no mode is passed */
1680 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1681 oflags
|= MSVCRT__O_BINARY
;
1683 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1684 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1688 /*********************************************************************
1691 int CDECL
_rmtmp(void)
1693 int num_removed
= 0, i
;
1696 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1697 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1699 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1705 TRACE(":removed (%d) temp files\n",num_removed
);
1709 /*********************************************************************
1712 static int read_i(int fd
, void *buf
, unsigned int count
)
1715 char *bufstart
= buf
;
1716 HANDLE hand
= msvcrt_fdtoh(fd
);
1718 if (MSVCRT_fdesc
[fd
].wxflag
& WX_READEOF
) {
1719 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1720 TRACE("already at EOF, returning 0\n");
1723 /* Don't trace small reads, it gets *very* annoying */
1725 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1726 if (hand
== INVALID_HANDLE_VALUE
)
1729 /* Reading single bytes in O_TEXT mode makes things slow
1730 * So read big chunks
1732 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1734 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1737 for (i
=0, j
=0; i
<num_read
; i
++)
1739 /* in text mode, a ctrl-z signals EOF */
1740 if (bufstart
[i
] == 0x1a)
1742 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1743 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1746 /* in text mode, strip \r if followed by \n.
1747 * BUG: should save state across calls somehow, so CR LF that
1748 * straddles buffer boundary gets recognized properly?
1750 if ((bufstart
[i
] != '\r')
1751 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1752 bufstart
[j
++] = bufstart
[i
];
1756 if (count
!= 0 && num_read
== 0)
1758 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1759 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1764 if (GetLastError() == ERROR_BROKEN_PIPE
)
1766 TRACE(":end-of-pipe\n");
1767 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1772 TRACE(":failed-last error (%d)\n",GetLastError());
1778 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1782 /*********************************************************************
1785 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
1788 num_read
= read_i(fd
, buf
, count
);
1792 /*********************************************************************
1793 * _setmode (MSVCRT.@)
1795 int CDECL
_setmode(int fd
,int mode
)
1797 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1798 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1799 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1800 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1801 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1803 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1807 /*********************************************************************
1808 * _stat64 (MSVCRT.@)
1810 int CDECL
MSVCRT__stat64(const char* path
, struct MSVCRT__stat64
* buf
)
1813 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1814 unsigned short mode
= ALL_S_IREAD
;
1817 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1819 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1821 TRACE("failed (%d)\n",GetLastError());
1822 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1826 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1828 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1829 Bon 011120: This FIXME seems incorrect
1830 Also a letter as first char isn't enough to be classified
1833 if (isalpha(*path
)&& (*(path
+1)==':'))
1834 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1836 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1838 plen
= strlen(path
);
1840 /* Dir, or regular file? */
1841 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1842 (path
[plen
-1] == '\\'))
1843 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1846 mode
|= MSVCRT__S_IFREG
;
1848 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1850 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1851 (tolower(path
[plen
-3]) << 16);
1852 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1853 mode
|= ALL_S_IEXEC
;
1857 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1858 mode
|= ALL_S_IWRITE
;
1860 buf
->st_mode
= mode
;
1862 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1863 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1865 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1866 buf
->st_mtime
= buf
->st_ctime
= dw
;
1867 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1868 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1869 (long)buf
->st_atime
,(long)buf
->st_mtime
,(long)buf
->st_ctime
);
1873 /*********************************************************************
1874 * _stati64 (MSVCRT.@)
1876 int CDECL
MSVCRT__stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1879 struct MSVCRT__stat64 buf64
;
1881 ret
= MSVCRT__stat64(path
, &buf64
);
1883 msvcrt_stat64_to_stati64(&buf64
, buf
);
1887 /*********************************************************************
1890 int CDECL
MSVCRT__stat(const char* path
, struct MSVCRT__stat
* buf
)
1892 struct MSVCRT__stat64 buf64
;
1894 ret
= MSVCRT__stat64( path
, &buf64
);
1896 msvcrt_stat64_to_stat(&buf64
, buf
);
1900 /*********************************************************************
1901 * _wstat64 (MSVCRT.@)
1903 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
1906 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1907 unsigned short mode
= ALL_S_IREAD
;
1910 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1912 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1914 TRACE("failed (%d)\n",GetLastError());
1915 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1919 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1921 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1922 if (MSVCRT_iswalpha(*path
))
1923 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1925 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1927 plen
= strlenW(path
);
1929 /* Dir, or regular file? */
1930 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1931 (path
[plen
-1] == '\\'))
1932 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1935 mode
|= MSVCRT__S_IFREG
;
1937 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1939 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1940 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1941 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1942 mode
|= ALL_S_IEXEC
;
1946 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1947 mode
|= ALL_S_IWRITE
;
1949 buf
->st_mode
= mode
;
1951 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1952 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1954 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1955 buf
->st_mtime
= buf
->st_ctime
= dw
;
1956 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1957 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1958 (long)buf
->st_atime
,(long)buf
->st_mtime
,(long)buf
->st_ctime
);
1962 /*********************************************************************
1963 * _wstati64 (MSVCRT.@)
1965 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
1968 struct MSVCRT__stat64 buf64
;
1970 ret
= MSVCRT__wstat64(path
, &buf64
);
1972 msvcrt_stat64_to_stati64(&buf64
, buf
);
1976 /*********************************************************************
1979 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
1982 struct MSVCRT__stat64 buf64
;
1984 ret
= MSVCRT__wstat64( path
, &buf64
);
1985 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
1989 /*********************************************************************
1992 long CDECL
_tell(int fd
)
1994 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1997 /*********************************************************************
1998 * _telli64 (MSVCRT.@)
2000 __int64 CDECL
_telli64(int fd
)
2002 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2005 /*********************************************************************
2006 * _tempnam (MSVCRT.@)
2008 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2010 char tmpbuf
[MAX_PATH
];
2011 const char *tmp_dir
= MSVCRT_getenv("TMP");
2013 if (tmp_dir
) dir
= tmp_dir
;
2015 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2016 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2018 TRACE("got name (%s)\n",tmpbuf
);
2019 DeleteFileA(tmpbuf
);
2020 return _strdup(tmpbuf
);
2022 TRACE("failed (%d)\n",GetLastError());
2026 /*********************************************************************
2027 * _wtempnam (MSVCRT.@)
2029 MSVCRT_wchar_t
* CDECL
_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2031 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2033 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2034 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2036 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2037 DeleteFileW(tmpbuf
);
2038 return _wcsdup(tmpbuf
);
2040 TRACE("failed (%d)\n",GetLastError());
2044 /*********************************************************************
2047 int CDECL
MSVCRT__umask(int umask
)
2049 int old_umask
= MSVCRT_umask
;
2050 TRACE("(%d)\n",umask
);
2051 MSVCRT_umask
= umask
;
2055 /*********************************************************************
2058 int CDECL
_utime(const char* path
, struct MSVCRT__utimbuf
*t
)
2060 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2064 int retVal
= _futime(fd
, t
);
2071 /*********************************************************************
2072 * _wutime (MSVCRT.@)
2074 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT__utimbuf
*t
)
2076 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2080 int retVal
= _futime(fd
, t
);
2087 /*********************************************************************
2090 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2093 HANDLE hand
= msvcrt_fdtoh(fd
);
2095 /* Don't trace small writes, it gets *very* annoying */
2098 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2100 if (hand
== INVALID_HANDLE_VALUE
)
2102 *MSVCRT__errno() = MSVCRT_EBADF
;
2106 /* If appending, go to EOF */
2107 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
2108 MSVCRT__lseek(fd
, 0, FILE_END
);
2110 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
2112 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2113 && (num_written
== count
))
2115 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2116 hand
, GetLastError());
2117 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2121 unsigned int i
, j
, nr_lf
;
2124 const char *s
= buf
, *buf_start
= buf
;
2125 /* find number of \n ( without preceding \r ) */
2126 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2131 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2136 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2138 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2143 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2150 FIXME("Malloc failed\n");
2158 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2160 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2161 fd
, hand
, GetLastError(), num_written
);
2162 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2165 return s
- buf_start
;
2177 /*********************************************************************
2180 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2183 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2184 if (len
== sizeof(val
)) return val
;
2185 file
->_flag
|= MSVCRT__IOERR
;
2189 /*********************************************************************
2192 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2197 MSVCRT_free(file
->_tmpfname
);
2198 file
->_tmpfname
= NULL
;
2199 /* flush stdio buffers */
2200 if(file
->_flag
& MSVCRT__IOWRT
)
2201 MSVCRT_fflush(file
);
2202 if(file
->_flag
& MSVCRT__IOMYBUF
)
2203 MSVCRT_free(file
->_base
);
2205 r
=MSVCRT__close(file
->_file
);
2209 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2212 /*********************************************************************
2215 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2217 return file
->_flag
& MSVCRT__IOEOF
;
2220 /*********************************************************************
2223 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2225 return file
->_flag
& MSVCRT__IOERR
;
2228 /*********************************************************************
2229 * _filbuf (MSVCRT.@)
2231 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2233 /* Allocate buffer if needed */
2234 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2235 msvcrt_alloc_buffer(file
);
2237 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2238 if(file
->_flag
& MSVCRT__IORW
) {
2239 file
->_flag
|= MSVCRT__IOREAD
;
2244 if(file
->_flag
& MSVCRT__IONBF
) {
2247 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2248 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2253 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2255 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2260 file
->_ptr
= file
->_base
+1;
2261 return *(unsigned char *)file
->_base
;
2265 /*********************************************************************
2268 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2274 i
= (unsigned char *)file
->_ptr
++;
2277 j
= MSVCRT__filbuf(file
);
2281 /*********************************************************************
2282 * _fgetchar (MSVCRT.@)
2284 int CDECL
_fgetchar(void)
2286 return MSVCRT_fgetc(MSVCRT_stdin
);
2289 /*********************************************************************
2292 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2294 int cc
= MSVCRT_EOF
;
2295 char * buf_start
= s
;
2297 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2298 file
,file
->_file
,s
,size
);
2300 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2305 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2307 TRACE(":nothing read\n");
2310 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2313 TRACE(":got %s\n", debugstr_a(buf_start
));
2317 /*********************************************************************
2320 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2321 * the CR from CR/LF combinations
2323 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2327 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2334 for(i
=0; i
<sizeof(wc
); i
++)
2344 j
= MSVCRT__filbuf(file
);
2347 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2357 c
= MSVCRT_fgetc(file
);
2358 if ((MSVCRT___mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2360 FIXME("Treat Multibyte characters\n");
2362 if (c
== MSVCRT_EOF
)
2365 return (MSVCRT_wint_t
)c
;
2368 /*********************************************************************
2371 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2377 for (j
=0; j
<sizeof(int); j
++) {
2378 k
= MSVCRT_fgetc(file
);
2379 if (k
== MSVCRT_EOF
) {
2380 file
->_flag
|= MSVCRT__IOEOF
;
2388 /*********************************************************************
2391 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2393 return MSVCRT_fgetwc(file
);
2396 /*********************************************************************
2397 * _fgetwchar (MSVCRT.@)
2399 MSVCRT_wint_t CDECL
_fgetwchar(void)
2401 return MSVCRT_fgetwc(MSVCRT_stdin
);
2404 /*********************************************************************
2405 * getwchar (MSVCRT.@)
2407 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2409 return _fgetwchar();
2412 /*********************************************************************
2415 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2417 int cc
= MSVCRT_WEOF
;
2418 MSVCRT_wchar_t
* buf_start
= s
;
2420 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2421 file
,file
->_file
,s
,size
);
2423 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2428 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2430 TRACE(":nothing read\n");
2433 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2436 TRACE(":got %s\n", debugstr_w(buf_start
));
2440 /*********************************************************************
2443 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2445 MSVCRT_size_t wrcnt
=size
* nmemb
;
2450 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2451 memcpy(file
->_ptr
, ptr
, pcnt
);
2456 ptr
= (const char*)ptr
+ pcnt
;
2457 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2458 if(file
->_flag
& MSVCRT__IORW
) {
2459 file
->_flag
|= MSVCRT__IOWRT
;
2465 int res
=msvcrt_flush_buffer(file
);
2467 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2470 file
->_flag
|= MSVCRT__IOERR
;
2473 written
+= pwritten
;
2476 return written
/ size
;
2479 /*********************************************************************
2482 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2484 MSVCRT_wchar_t mwc
=wc
;
2485 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2490 /*********************************************************************
2491 * _fputwchar (MSVCRT.@)
2493 MSVCRT_wint_t CDECL
_fputwchar(MSVCRT_wint_t wc
)
2495 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2498 /*********************************************************************
2499 * _wfsopen (MSVCRT.@)
2501 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2504 int open_flags
, stream_flags
, fd
;
2506 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2508 /* map mode string to open() flags. "man fopen" for possibilities. */
2509 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2513 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2516 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2518 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2525 TRACE(":got (%p)\n",file
);
2526 if (fd
>= 0 && !file
)
2532 /*********************************************************************
2533 * _fsopen (MSVCRT.@)
2535 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2538 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2540 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2541 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2547 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
2554 /*********************************************************************
2557 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
2559 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2562 /*********************************************************************
2563 * _wfopen (MSVCRT.@)
2565 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2567 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2570 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2571 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2573 /*********************************************************************
2576 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2583 int res
= msvcrt_flush_buffer(file
);
2584 return res
? res
: c
;
2589 return MSVCRT__flsbuf(c
, file
);
2593 /*********************************************************************
2594 * _flsbuf (MSVCRT.@)
2596 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2598 /* Flush output buffer */
2599 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2600 msvcrt_alloc_buffer(file
);
2602 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2603 if(file
->_flag
& MSVCRT__IORW
) {
2604 file
->_flag
|= MSVCRT__IOWRT
;
2610 int res
=msvcrt_flush_buffer(file
);
2611 return res
?res
: MSVCRT_fputc(c
, file
);
2615 /* set _cnt to 0 for unbuffered FILEs */
2617 len
= MSVCRT__write(file
->_file
, &cc
, 1);
2618 if (len
== 1) return c
& 0xff;
2619 file
->_flag
|= MSVCRT__IOERR
;
2624 /*********************************************************************
2625 * _fputchar (MSVCRT.@)
2627 int CDECL
_fputchar(int c
)
2629 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2632 /*********************************************************************
2635 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2636 { MSVCRT_size_t rcnt
=size
* nmemb
;
2637 MSVCRT_size_t read
=0;
2643 /* first buffered data */
2645 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2646 memcpy(ptr
, file
->_ptr
, pcnt
);
2651 ptr
= (char*)ptr
+ pcnt
;
2652 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2653 if(file
->_flag
& MSVCRT__IORW
) {
2654 file
->_flag
|= MSVCRT__IOREAD
;
2661 /* Fill the buffer on small reads.
2662 * TODO: Use a better buffering strategy.
2664 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
2665 if (file
->_bufsiz
== 0) {
2666 msvcrt_alloc_buffer(file
);
2668 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
2669 file
->_ptr
= file
->_base
;
2670 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2671 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2672 if (i
> 0 && i
< file
->_cnt
) {
2673 MSVCRT_fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2674 file
->_flag
&= ~MSVCRT__IOEOF
;
2677 memcpy(ptr
, file
->_ptr
, i
);
2682 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
2686 ptr
= (char *)ptr
+i
;
2687 /* expose feof condition in the flags
2688 * MFC tests file->_flag for feof, and doesn't call feof())
2690 if ( MSVCRT_fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2691 file
->_flag
|= MSVCRT__IOEOF
;
2694 file
->_flag
|= MSVCRT__IOERR
;
2704 /*********************************************************************
2705 * _wfreopen (MSVCRT.@)
2708 MSVCRT_FILE
* CDECL
_wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
2710 int open_flags
, stream_flags
, fd
;
2712 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2715 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2719 MSVCRT_fclose(file
);
2720 /* map mode string to open() flags. "man fopen" for possibilities. */
2721 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2725 fd
= _wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2728 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2731 WARN(":failed-last error (%d)\n",GetLastError());
2732 msvcrt_set_errno(GetLastError());
2741 /*********************************************************************
2742 * freopen (MSVCRT.@)
2745 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
2748 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2750 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2751 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2757 ret
= _wfreopen(pathW
, modeW
, file
);
2764 /*********************************************************************
2765 * fsetpos (MSVCRT.@)
2767 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2769 /* Note that all this has been lifted 'as is' from fseek */
2770 if(file
->_flag
& MSVCRT__IOWRT
)
2771 msvcrt_flush_buffer(file
);
2773 /* Discard buffered input */
2775 file
->_ptr
= file
->_base
;
2777 /* Reset direction of i/o */
2778 if(file
->_flag
& MSVCRT__IORW
) {
2779 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2782 return (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2785 /*********************************************************************
2788 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
2790 /* TODO: just call fgetpos and return lower half of result */
2793 pos
= _tell(file
->_file
);
2794 if(pos
== -1) return -1;
2796 if( file
->_flag
& MSVCRT__IOWRT
) {
2797 off
= file
->_ptr
- file
->_base
;
2800 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2801 /* Black magic correction for CR removal */
2803 for (i
=0; i
<file
->_cnt
; i
++) {
2804 if (file
->_ptr
[i
] == '\n')
2813 /*********************************************************************
2814 * fgetpos (MSVCRT.@)
2816 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2819 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
2820 if(*pos
== -1) return -1;
2822 if( file
->_flag
& MSVCRT__IOWRT
) {
2823 off
= file
->_ptr
- file
->_base
;
2826 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2827 /* Black magic correction for CR removal */
2829 for (i
=0; i
<file
->_cnt
; i
++) {
2830 if (file
->_ptr
[i
] == '\n')
2840 /*********************************************************************
2843 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2845 size_t i
, len
= strlen(s
);
2846 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2847 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2848 for (i
=0; i
<len
; i
++)
2849 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2854 /*********************************************************************
2857 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2859 size_t i
, len
= strlenW(s
);
2860 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2861 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2862 for (i
=0; i
<len
; i
++)
2864 if ((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2866 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2872 /*********************************************************************
2873 * getchar (MSVCRT.@)
2875 int CDECL
MSVCRT_getchar(void)
2877 return MSVCRT_fgetc(MSVCRT_stdin
);
2880 /*********************************************************************
2883 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
2885 return MSVCRT_fgetc(file
);
2888 /*********************************************************************
2891 char * CDECL
MSVCRT_gets(char *buf
)
2894 char * buf_start
= buf
;
2896 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2897 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2898 if(cc
!= '\r') *buf
++ = (char)cc
;
2902 TRACE("got '%s'\n", buf_start
);
2906 /*********************************************************************
2909 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2912 MSVCRT_wchar_t
* ws
= buf
;
2914 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2915 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2918 *buf
++ = (MSVCRT_wchar_t
)cc
;
2922 TRACE("got %s\n", debugstr_w(ws
));
2926 /*********************************************************************
2929 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2931 return MSVCRT_fputc(c
, file
);
2934 /*********************************************************************
2935 * putchar (MSVCRT.@)
2937 int CDECL
MSVCRT_putchar(int c
)
2939 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2942 /*********************************************************************
2945 int CDECL
MSVCRT_puts(const char *s
)
2947 size_t len
= strlen(s
);
2948 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2949 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2952 /*********************************************************************
2955 int CDECL
_putws(const MSVCRT_wchar_t
*s
)
2957 static const MSVCRT_wchar_t nl
= '\n';
2958 size_t len
= strlenW(s
);
2959 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2960 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2963 /*********************************************************************
2966 int CDECL
MSVCRT_remove(const char *path
)
2968 TRACE("(%s)\n",path
);
2969 if (DeleteFileA(path
))
2971 TRACE(":failed (%d)\n",GetLastError());
2972 msvcrt_set_errno(GetLastError());
2976 /*********************************************************************
2977 * _wremove (MSVCRT.@)
2979 int CDECL
_wremove(const MSVCRT_wchar_t
*path
)
2981 TRACE("(%s)\n",debugstr_w(path
));
2982 if (DeleteFileW(path
))
2984 TRACE(":failed (%d)\n",GetLastError());
2985 msvcrt_set_errno(GetLastError());
2989 /*********************************************************************
2992 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
2994 TRACE(":from %s to %s\n",oldpath
,newpath
);
2995 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2997 TRACE(":failed (%d)\n",GetLastError());
2998 msvcrt_set_errno(GetLastError());
3002 /*********************************************************************
3003 * _wrename (MSVCRT.@)
3005 int CDECL
_wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3007 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3008 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3010 TRACE(":failed (%d)\n",GetLastError());
3011 msvcrt_set_errno(GetLastError());
3015 /*********************************************************************
3016 * setvbuf (MSVCRT.@)
3018 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3020 /* TODO: Check if file busy */
3022 MSVCRT_free(file
->_base
);
3026 if(mode
== MSVCRT__IOFBF
) {
3027 file
->_flag
&= ~MSVCRT__IONBF
;
3028 file
->_base
= file
->_ptr
= buf
;
3030 file
->_bufsiz
= size
;
3033 file
->_flag
|= MSVCRT__IONBF
;
3038 /*********************************************************************
3041 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3043 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3046 /*********************************************************************
3049 char * CDECL
MSVCRT_tmpnam(char *s
)
3057 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3058 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3059 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3061 msvcrt_int_to_base32(unique
++, tmpstr
);
3063 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3064 GetLastError() == ERROR_FILE_NOT_FOUND
)
3070 /*********************************************************************
3071 * tmpfile (MSVCRT.@)
3073 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3075 char *filename
= MSVCRT_tmpnam(NULL
);
3077 MSVCRT_FILE
* file
= NULL
;
3080 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3081 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3083 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3088 else file
->_tmpfname
= _strdup(filename
);
3094 /*********************************************************************
3095 * vfprintf (MSVCRT.@)
3097 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3099 char buf
[2048], *mem
= buf
;
3100 int written
, resize
= sizeof(buf
), retval
;
3101 /* There are two conventions for vsnprintf failing:
3102 * Return -1 if we truncated, or
3103 * Return the number of bytes that would have been written
3104 * The code below handles both cases
3106 while ((written
= MSVCRT_vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3109 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3112 if (!(mem
= MSVCRT_malloc(resize
)))
3115 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3121 /*********************************************************************
3122 * vfwprintf (MSVCRT.@)
3124 * Is final char included in written (then resize is too big) or not
3125 * (then we must test for equality too)?
3127 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3129 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
3130 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
3131 /* See vfprintf comments */
3132 while ((written
= MSVCRT_vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3135 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
3138 if (!(mem
= MSVCRT_malloc(resize
*sizeof(*mem
))))
3141 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3147 /*********************************************************************
3148 * vprintf (MSVCRT.@)
3150 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3152 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3155 /*********************************************************************
3156 * vwprintf (MSVCRT.@)
3158 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3160 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3163 /*********************************************************************
3164 * fprintf (MSVCRT.@)
3166 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3168 __ms_va_list valist
;
3170 __ms_va_start(valist
, format
);
3171 res
= MSVCRT_vfprintf(file
, format
, valist
);
3172 __ms_va_end(valist
);
3176 /*********************************************************************
3177 * fwprintf (MSVCRT.@)
3179 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3181 __ms_va_list valist
;
3183 __ms_va_start(valist
, format
);
3184 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3185 __ms_va_end(valist
);
3189 /*********************************************************************
3192 int CDECL
MSVCRT_printf(const char *format
, ...)
3194 __ms_va_list valist
;
3196 __ms_va_start(valist
, format
);
3197 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3198 __ms_va_end(valist
);
3202 /*********************************************************************
3205 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3207 if (c
== MSVCRT_EOF
)
3209 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3210 msvcrt_alloc_buffer(file
);
3213 if(file
->_ptr
>file
->_base
) {
3217 MSVCRT_clearerr(file
);
3223 /*********************************************************************
3224 * ungetwc (MSVCRT.@)
3226 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3228 MSVCRT_wchar_t mwc
= wc
;
3229 char * pp
= (char *)&mwc
;
3231 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3232 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
3238 /*********************************************************************
3239 * wprintf (MSVCRT.@)
3241 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
3243 __ms_va_list valist
;
3245 __ms_va_start(valist
, format
);
3246 res
= MSVCRT_vwprintf(format
, valist
);
3247 __ms_va_end(valist
);
3251 /*********************************************************************
3252 * _getmaxstdio (MSVCRT.@)
3254 int CDECL
_getmaxstdio(void)
3256 FIXME("stub, always returns 512\n");
3260 /*********************************************************************
3261 * _setmaxstdio_ (MSVCRT.@)
3263 int CDECL
_setmaxstdio(int newmax
)
3270 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3274 /*********************************************************************
3275 * __pioinfo (MSVCRT.@)
3276 * FIXME: see MSVCRT_MAX_FILES define.
3278 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3279 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
3280 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
3281 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
3282 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
3283 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
3284 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
3285 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
3286 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
3287 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
3288 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
3289 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
3292 /*********************************************************************
3293 * __badioinfo (MSVCRT.@)
3295 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};