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 void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
146 /* 1601 to 1970 is 369 years plus 89 leap days */
147 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
149 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
150 ft
->dwHighDateTime
= ticks
>> 32;
151 ft
->dwLowDateTime
= ticks
;
154 static inline BOOL
msvcrt_is_valid_fd(int fd
)
156 return fd
>= 0 && fd
< MSVCRT_fdend
&& (MSVCRT_fdesc
[fd
].wxflag
& WX_OPEN
);
159 /* INTERNAL: Get the HANDLE for a fd
160 * This doesn't lock the table, because a failure will result in
161 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
162 * it returns a valid handle which is about to be closed, a subsequent call
163 * will fail, most likely in a sane way.
165 static HANDLE
msvcrt_fdtoh(int fd
)
167 if (!msvcrt_is_valid_fd(fd
))
169 WARN(":fd (%d) - no handle!\n",fd
);
170 *MSVCRT___doserrno() = 0;
171 *MSVCRT__errno() = MSVCRT_EBADF
;
172 return INVALID_HANDLE_VALUE
;
174 if (MSVCRT_fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
175 return MSVCRT_fdesc
[fd
].handle
;
178 /* INTERNAL: free a file entry fd */
179 static void msvcrt_free_fd(int fd
)
184 old_handle
= MSVCRT_fdesc
[fd
].handle
;
185 MSVCRT_fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
186 MSVCRT_fdesc
[fd
].wxflag
= 0;
187 TRACE(":fd (%d) freed\n",fd
);
188 if (fd
< 3) /* don't use 0,1,2 for user files */
193 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
196 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
199 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
205 if (fd
== MSVCRT_fdend
- 1)
207 if (fd
< MSVCRT_fdstart
)
213 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
214 /* caller must hold the files lock */
215 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
217 if (fd
>= MSVCRT_MAX_FILES
)
219 WARN(":files exhausted!\n");
222 MSVCRT_fdesc
[fd
].handle
= hand
;
223 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
225 /* locate next free slot */
226 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
227 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
229 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
230 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
232 /* update last fd in use */
233 if (fd
>= MSVCRT_fdend
)
234 MSVCRT_fdend
= fd
+ 1;
235 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
239 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
240 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
241 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
247 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
248 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
253 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
254 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
259 /* INTERNAL: Allocate a FILE* for an fd slot */
260 /* caller must hold the files lock */
261 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
265 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
267 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
269 if (!MSVCRT_fstreams
[i
])
271 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
273 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
275 return MSVCRT_fstreams
[i
];
281 /* INTERNAL: initialize a FILE* from an open fd */
282 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
284 TRACE(":fd (%d) allocating FILE*\n",fd
);
285 if (!msvcrt_is_valid_fd(fd
))
287 WARN(":invalid fd %d\n",fd
);
288 *MSVCRT___doserrno() = 0;
289 *MSVCRT__errno() = MSVCRT_EBADF
;
292 memset(file
, 0, sizeof(*file
));
294 file
->_flag
= stream_flags
;
296 TRACE(":got FILE* (%p)\n",file
);
300 /* INTERNAL: Create an inheritance data block (for spawned process)
301 * The inheritance block is made of:
302 * 00 int nb of file descriptor (NBFD)
303 * 04 char file flags (wxflag): repeated for each fd
304 * 4+NBFD HANDLE file handle: repeated for each fd
306 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
312 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
313 *block
= MSVCRT_calloc(*size
, 1);
319 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
320 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
322 *(unsigned*)*block
= MSVCRT_fdend
;
323 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
325 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
326 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
328 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
329 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
334 *handle_ptr
= INVALID_HANDLE_VALUE
;
336 wxflag_ptr
++; handle_ptr
++;
341 /* INTERNAL: Set up all file descriptors,
342 * as well as default streams (stdin, stderr and stdout)
344 void msvcrt_init_io(void)
349 InitializeCriticalSection(&MSVCRT_file_cs
);
350 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs");
351 GetStartupInfoA(&si
);
352 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
358 count
= *(unsigned*)si
.lpReserved2
;
359 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
360 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
362 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
363 count
= min(count
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
364 for (i
= 0; i
< count
; i
++)
366 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
368 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
369 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
373 MSVCRT_fdesc
[i
].wxflag
= 0;
374 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
376 wxflag_ptr
++; handle_ptr
++;
378 MSVCRT_fdend
= max( 3, count
);
379 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
380 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
383 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
385 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
386 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
387 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
,
388 0, TRUE
, DUPLICATE_SAME_ACCESS
))
389 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
391 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
393 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
394 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
395 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
,
396 0, TRUE
, DUPLICATE_SAME_ACCESS
))
397 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
399 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
401 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
402 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
403 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
,
404 0, TRUE
, DUPLICATE_SAME_ACCESS
))
405 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
408 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
409 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
411 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
412 for (i
= 0; i
< 3; i
++)
414 /* FILE structs for stdin/out/err are static and never deleted */
415 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
416 MSVCRT__iob
[i
]._file
= i
;
417 MSVCRT__iob
[i
]._tmpfname
= NULL
;
418 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
420 MSVCRT_stream_idx
= 3;
423 /* INTERNAL: Flush stdio file buffer */
424 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
427 int cnt
=file
->_ptr
-file
->_base
;
428 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
429 file
->_flag
|= MSVCRT__IOERR
;
432 file
->_ptr
=file
->_base
;
433 file
->_cnt
=file
->_bufsiz
;
438 /* INTERNAL: Allocate stdio file buffer */
439 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
441 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
443 file
->_bufsiz
= MSVCRT_BUFSIZ
;
444 file
->_flag
|= MSVCRT__IOMYBUF
;
446 file
->_base
= (char*)(&file
->_charbuf
);
448 file
->_bufsiz
= sizeof(file
->_charbuf
);
450 file
->_ptr
= file
->_base
;
454 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
455 static void msvcrt_int_to_base32(int num
, char *str
)
470 *p
= (num
& 31) + '0';
472 *p
+= ('a' - '0' - 10);
477 /*********************************************************************
478 * __iob_func(MSVCRT.@)
480 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
482 return &MSVCRT__iob
[0];
485 /*********************************************************************
488 int CDECL
MSVCRT__access(const char *filename
, int mode
)
490 DWORD attr
= GetFileAttributesA(filename
);
492 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
494 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
496 msvcrt_set_errno(GetLastError());
499 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
501 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
507 /*********************************************************************
508 * _waccess (MSVCRT.@)
510 int CDECL
_waccess(const MSVCRT_wchar_t
*filename
, int mode
)
512 DWORD attr
= GetFileAttributesW(filename
);
514 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
516 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
518 msvcrt_set_errno(GetLastError());
521 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
523 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
529 /*********************************************************************
532 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
534 DWORD oldFlags
= GetFileAttributesA(path
);
536 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
538 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
539 oldFlags
| FILE_ATTRIBUTE_READONLY
;
541 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
544 msvcrt_set_errno(GetLastError());
548 /*********************************************************************
551 int CDECL
_wchmod(const MSVCRT_wchar_t
*path
, int flags
)
553 DWORD oldFlags
= GetFileAttributesW(path
);
555 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
557 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
558 oldFlags
| FILE_ATTRIBUTE_READONLY
;
560 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
563 msvcrt_set_errno(GetLastError());
567 /*********************************************************************
570 int CDECL
MSVCRT__unlink(const char *path
)
572 TRACE("%s\n",debugstr_a(path
));
573 if(DeleteFileA(path
))
575 TRACE("failed (%d)\n",GetLastError());
576 msvcrt_set_errno(GetLastError());
580 /*********************************************************************
581 * _wunlink (MSVCRT.@)
583 int CDECL
_wunlink(const MSVCRT_wchar_t
*path
)
585 TRACE("(%s)\n",debugstr_w(path
));
586 if(DeleteFileW(path
))
588 TRACE("failed (%d)\n",GetLastError());
589 msvcrt_set_errno(GetLastError());
593 /* _flushall calls MSVCRT_fflush which calls _flushall */
594 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
596 /*********************************************************************
597 * _flushall (MSVCRT.@)
599 int CDECL
_flushall(void)
601 int i
, num_flushed
= 0;
604 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
605 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
608 /* FIXME: flush, do not commit */
609 if (_commit(i
) == -1)
610 if (MSVCRT_fstreams
[i
])
611 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
613 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
614 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
620 TRACE(":flushed (%d) handles\n",num_flushed
);
624 /*********************************************************************
627 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
631 } else if(file
->_flag
& MSVCRT__IOWRT
) {
632 int res
=msvcrt_flush_buffer(file
);
638 /*********************************************************************
641 int CDECL
MSVCRT__close(int fd
)
647 hand
= msvcrt_fdtoh(fd
);
648 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
649 if (hand
== INVALID_HANDLE_VALUE
)
651 else if (!CloseHandle(hand
))
653 WARN(":failed-last error (%d)\n",GetLastError());
654 msvcrt_set_errno(GetLastError());
667 /*********************************************************************
670 int CDECL
_commit(int fd
)
672 HANDLE hand
= msvcrt_fdtoh(fd
);
674 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
675 if (hand
== INVALID_HANDLE_VALUE
)
678 if (!FlushFileBuffers(hand
))
680 if (GetLastError() == ERROR_INVALID_HANDLE
)
682 /* FlushFileBuffers fails for console handles
683 * so we ignore this error.
687 TRACE(":failed-last error (%d)\n",GetLastError());
688 msvcrt_set_errno(GetLastError());
695 /*********************************************************************
698 * MSDN isn't clear on this point, but the remarks for _pipe
699 * indicate file descriptors duplicated with _dup and _dup2 are always
702 int CDECL
MSVCRT__dup2(int od
, int nd
)
706 TRACE("(od=%d, nd=%d)\n", od
, nd
);
708 if (nd
< MSVCRT_MAX_FILES
&& msvcrt_is_valid_fd(od
))
712 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
713 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
715 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
717 if (msvcrt_is_valid_fd(nd
))
719 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
723 *MSVCRT__errno() = MSVCRT_EMFILE
;
727 /* _dup2 returns 0, not nd, on success */
734 msvcrt_set_errno(GetLastError());
739 *MSVCRT__errno() = MSVCRT_EBADF
;
746 /*********************************************************************
749 int CDECL
MSVCRT__dup(int od
)
755 if (MSVCRT__dup2(od
, fd
) == 0)
763 /*********************************************************************
766 int CDECL
_eof(int fd
)
769 LONG hcurpos
,hendpos
;
770 HANDLE hand
= msvcrt_fdtoh(fd
);
772 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
774 if (hand
== INVALID_HANDLE_VALUE
)
777 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
779 /* Otherwise we do it the hard way */
780 hcurpos
= hendpos
= 0;
781 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
782 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
784 if (curpos
== endpos
&& hcurpos
== hendpos
)
786 /* FIXME: shouldn't WX_ATEOF be set here? */
790 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
794 /*********************************************************************
795 * _fcloseall (MSVCRT.@)
797 int CDECL
MSVCRT__fcloseall(void)
799 int num_closed
= 0, i
;
802 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
803 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
804 !MSVCRT_fclose(MSVCRT_fstreams
[i
]))
808 TRACE(":closed (%d) handles\n",num_closed
);
812 /* free everything on process exit */
813 void msvcrt_free_io(void)
816 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
817 * stdout, and stderr (unlike GNU), so we need to fclose() them here
818 * or they won't get flushed.
820 MSVCRT_fclose(&MSVCRT__iob
[0]);
821 MSVCRT_fclose(&MSVCRT__iob
[1]);
822 MSVCRT_fclose(&MSVCRT__iob
[2]);
823 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = 0;
824 DeleteCriticalSection(&MSVCRT_file_cs
);
827 /*********************************************************************
828 * _lseeki64 (MSVCRT.@)
830 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
832 HANDLE hand
= msvcrt_fdtoh(fd
);
833 LARGE_INTEGER ofs
, ret
;
835 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
836 if (hand
== INVALID_HANDLE_VALUE
)
839 if (whence
< 0 || whence
> 2)
841 *MSVCRT__errno() = MSVCRT_EINVAL
;
845 TRACE(":fd (%d) to %s pos %s\n",
846 fd
,wine_dbgstr_longlong(offset
),
847 (whence
==SEEK_SET
)?"SEEK_SET":
848 (whence
==SEEK_CUR
)?"SEEK_CUR":
849 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
851 ofs
.QuadPart
= offset
;
852 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
854 MSVCRT_fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
855 /* FIXME: What if we seek _to_ EOF - is EOF set? */
859 TRACE(":error-last error (%d)\n",GetLastError());
860 msvcrt_set_errno(GetLastError());
864 /*********************************************************************
867 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
869 return MSVCRT__lseeki64(fd
, offset
, whence
);
872 /*********************************************************************
873 * _locking (MSVCRT.@)
875 * This is untested; the underlying LockFile doesn't work yet.
877 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
881 HANDLE hand
= msvcrt_fdtoh(fd
);
883 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
884 if (hand
== INVALID_HANDLE_VALUE
)
887 if (mode
< 0 || mode
> 4)
889 *MSVCRT__errno() = MSVCRT_EINVAL
;
893 TRACE(":fd (%d) by 0x%08x mode %s\n",
894 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
895 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
896 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
897 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
898 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
901 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
903 FIXME ("Seek failed\n");
904 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
907 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
910 ret
= 1; /* just to satisfy gcc */
913 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
918 else if (mode
== MSVCRT__LK_UNLCK
)
919 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
921 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
922 /* FIXME - what about error settings? */
926 /*********************************************************************
929 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
931 /* Flush output if needed */
932 if(file
->_flag
& MSVCRT__IOWRT
)
933 msvcrt_flush_buffer(file
);
935 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
936 offset
-= file
->_cnt
;
937 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
938 /* Black magic correction for CR removal */
940 for (i
=0; i
<file
->_cnt
; i
++) {
941 if (file
->_ptr
[i
] == '\n')
946 /* Discard buffered input */
948 file
->_ptr
= file
->_base
;
949 /* Reset direction of i/o */
950 if(file
->_flag
& MSVCRT__IORW
) {
951 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
953 /* Clear end of file flag */
954 file
->_flag
&= ~MSVCRT__IOEOF
;
955 return (MSVCRT__lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
958 /*********************************************************************
961 int CDECL
_chsize(int fd
, MSVCRT_long size
)
967 TRACE("(fd=%d, size=%d)\n", fd
, size
);
971 handle
= msvcrt_fdtoh(fd
);
972 if (handle
!= INVALID_HANDLE_VALUE
)
974 /* save the current file pointer */
975 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
978 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
981 ret
= SetEndOfFile(handle
);
982 if (!ret
) msvcrt_set_errno(GetLastError());
985 /* restore the file pointer */
986 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
994 /*********************************************************************
995 * clearerr (MSVCRT.@)
997 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
999 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1000 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1003 /*********************************************************************
1006 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1008 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1009 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1010 MSVCRT_clearerr(file
);
1013 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1015 int plus
= strchrW(mode
, '+') != NULL
;
1020 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1021 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1024 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1025 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1028 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1029 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1039 *open_flags
|= MSVCRT__O_BINARY
;
1040 *open_flags
&= ~MSVCRT__O_TEXT
;
1043 *open_flags
|= MSVCRT__O_TEXT
;
1044 *open_flags
&= ~MSVCRT__O_BINARY
;
1049 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1054 /*********************************************************************
1055 * _fdopen (MSVCRT.@)
1057 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1060 MSVCRT_wchar_t
*modeW
= NULL
;
1062 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1064 ret
= MSVCRT__wfdopen(fd
, modeW
);
1070 /*********************************************************************
1071 * _wfdopen (MSVCRT.@)
1073 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1075 int open_flags
, stream_flags
;
1078 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1081 if (!(file
= msvcrt_alloc_fp()))
1083 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1088 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1094 /*********************************************************************
1095 * _filelength (MSVCRT.@)
1097 LONG CDECL
MSVCRT__filelength(int fd
)
1099 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1102 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1105 if (endPos
!= curPos
)
1106 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1113 /*********************************************************************
1114 * _filelengthi64 (MSVCRT.@)
1116 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1118 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1121 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1124 if (endPos
!= curPos
)
1125 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1132 /*********************************************************************
1133 * _fileno (MSVCRT.@)
1135 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1137 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1141 /*********************************************************************
1142 * _fstat64 (MSVCRT.@)
1144 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1148 BY_HANDLE_FILE_INFORMATION hfi
;
1149 HANDLE hand
= msvcrt_fdtoh(fd
);
1151 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1152 if (hand
== INVALID_HANDLE_VALUE
)
1157 WARN(":failed-NULL buf\n");
1158 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1162 memset(&hfi
, 0, sizeof(hfi
));
1163 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1164 type
= GetFileType(hand
);
1165 if (type
== FILE_TYPE_PIPE
)
1167 buf
->st_dev
= buf
->st_rdev
= fd
;
1168 buf
->st_mode
= S_IFIFO
;
1171 else if (type
== FILE_TYPE_CHAR
)
1173 buf
->st_dev
= buf
->st_rdev
= fd
;
1174 buf
->st_mode
= S_IFCHR
;
1177 else /* FILE_TYPE_DISK etc. */
1179 if (!GetFileInformationByHandle(hand
, &hfi
))
1181 WARN(":failed-last error (%d)\n",GetLastError());
1182 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1185 buf
->st_mode
= S_IFREG
| 0444;
1186 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1187 buf
->st_mode
|= 0222;
1188 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1189 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1191 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1192 buf
->st_mtime
= buf
->st_ctime
= dw
;
1193 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1195 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1200 /*********************************************************************
1201 * _fstati64 (MSVCRT.@)
1203 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1206 struct MSVCRT__stat64 buf64
;
1208 ret
= MSVCRT__fstat64(fd
, &buf64
);
1210 msvcrt_stat64_to_stati64(&buf64
, buf
);
1214 /*********************************************************************
1217 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1219 struct MSVCRT__stat64 buf64
;
1221 ret
= MSVCRT__fstat64(fd
, &buf64
);
1223 msvcrt_stat64_to_stat(&buf64
, buf
);
1227 /*********************************************************************
1228 * _futime64 (MSVCRT.@)
1230 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1232 HANDLE hand
= msvcrt_fdtoh(fd
);
1237 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1242 time_to_filetime( t
->actime
, &at
);
1243 time_to_filetime( t
->modtime
, &wt
);
1246 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1248 msvcrt_set_errno(GetLastError());
1254 /*********************************************************************
1255 * _futime32 (MSVCRT.@)
1257 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1259 struct MSVCRT___utimbuf64 t64
;
1260 t64
.actime
= t
->actime
;
1261 t64
.modtime
= t
->modtime
;
1262 return _futime64( fd
, &t64
);
1265 /*********************************************************************
1266 * _futime (MSVCRT.@)
1269 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1271 return _futime64( fd
, t
);
1274 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1276 return _futime32( fd
, t
);
1280 /*********************************************************************
1281 * _get_osfhandle (MSVCRT.@)
1283 MSVCRT_intptr_t CDECL
_get_osfhandle(int fd
)
1285 HANDLE hand
= msvcrt_fdtoh(fd
);
1286 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1288 return (MSVCRT_intptr_t
)hand
;
1291 /*********************************************************************
1292 * _isatty (MSVCRT.@)
1294 int CDECL
_isatty(int fd
)
1296 HANDLE hand
= msvcrt_fdtoh(fd
);
1298 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1299 if (hand
== INVALID_HANDLE_VALUE
)
1302 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1305 /*********************************************************************
1306 * _mktemp (MSVCRT.@)
1308 char * CDECL
_mktemp(char *pattern
)
1311 char *retVal
= pattern
;
1316 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1320 id
= GetCurrentProcessId();
1324 int tempNum
= id
/ 10;
1325 *pattern
-- = id
- (tempNum
* 10) + '0';
1331 *pattern
= letter
++;
1332 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1333 GetLastError() == ERROR_FILE_NOT_FOUND
)
1335 } while(letter
<= 'z');
1339 /*********************************************************************
1340 * _wmktemp (MSVCRT.@)
1342 MSVCRT_wchar_t
* CDECL
_wmktemp(MSVCRT_wchar_t
*pattern
)
1345 MSVCRT_wchar_t
*retVal
= pattern
;
1347 MSVCRT_wchar_t letter
= 'a';
1350 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1354 id
= GetCurrentProcessId();
1358 int tempNum
= id
/ 10;
1359 *pattern
-- = id
- (tempNum
* 10) + '0';
1365 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1366 GetLastError() == ERROR_FILE_NOT_FOUND
)
1368 *pattern
= letter
++;
1369 } while(letter
!= '|');
1373 static unsigned split_oflags(unsigned oflags
)
1376 unsigned unsupp
; /* until we support everything */
1378 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1379 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1380 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1381 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1382 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1383 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1385 if ((unsupp
= oflags
& ~(
1386 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1387 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1388 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1389 MSVCRT__O_NOINHERIT
|
1390 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1392 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1397 /*********************************************************************
1400 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1403 SECURITY_ATTRIBUTES sa
;
1404 HANDLE readHandle
, writeHandle
;
1408 *MSVCRT__errno() = MSVCRT_EINVAL
;
1412 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1413 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1414 sa
.lpSecurityDescriptor
= NULL
;
1415 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1417 unsigned int wxflags
= split_oflags(textmode
);
1421 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1425 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1433 MSVCRT__close(pfds
[0]);
1434 CloseHandle(writeHandle
);
1435 *MSVCRT__errno() = MSVCRT_EMFILE
;
1440 CloseHandle(readHandle
);
1441 CloseHandle(writeHandle
);
1442 *MSVCRT__errno() = MSVCRT_EMFILE
;
1447 msvcrt_set_errno(GetLastError());
1452 /*********************************************************************
1455 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1459 DWORD access
= 0, creation
= 0, attrib
;
1463 SECURITY_ATTRIBUTES sa
;
1466 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1467 path
, oflags
, shflags
);
1469 wxflag
= split_oflags(oflags
);
1470 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1472 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1473 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1474 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1477 if (oflags
& MSVCRT__O_CREAT
)
1479 __ms_va_start(ap
, shflags
);
1480 pmode
= va_arg(ap
, int);
1483 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1484 FIXME(": pmode 0x%04x ignored\n", pmode
);
1486 WARN(": pmode 0x%04x ignored\n", pmode
);
1488 if (oflags
& MSVCRT__O_EXCL
)
1489 creation
= CREATE_NEW
;
1490 else if (oflags
& MSVCRT__O_TRUNC
)
1491 creation
= CREATE_ALWAYS
;
1493 creation
= OPEN_ALWAYS
;
1495 else /* no MSVCRT__O_CREAT */
1497 if (oflags
& MSVCRT__O_TRUNC
)
1498 creation
= TRUNCATE_EXISTING
;
1500 creation
= OPEN_EXISTING
;
1505 case MSVCRT__SH_DENYRW
:
1508 case MSVCRT__SH_DENYWR
:
1509 sharing
= FILE_SHARE_READ
;
1511 case MSVCRT__SH_DENYRD
:
1512 sharing
= FILE_SHARE_WRITE
;
1514 case MSVCRT__SH_DENYNO
:
1515 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1518 ERR( "Unhandled shflags 0x%x\n", shflags
);
1521 attrib
= FILE_ATTRIBUTE_NORMAL
;
1523 if (oflags
& MSVCRT__O_TEMPORARY
)
1525 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1527 sharing
|= FILE_SHARE_DELETE
;
1530 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1531 sa
.lpSecurityDescriptor
= NULL
;
1532 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1534 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1536 if (hand
== INVALID_HANDLE_VALUE
) {
1537 WARN(":failed-last error (%d)\n",GetLastError());
1538 msvcrt_set_errno(GetLastError());
1542 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1544 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1548 /*********************************************************************
1549 * _wsopen (MSVCRT.@)
1551 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1555 DWORD access
= 0, creation
= 0, attrib
;
1559 SECURITY_ATTRIBUTES sa
;
1562 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1563 debugstr_w(path
), oflags
, shflags
);
1565 wxflag
= split_oflags(oflags
);
1566 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1568 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1569 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1570 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1573 if (oflags
& MSVCRT__O_CREAT
)
1575 __ms_va_start(ap
, shflags
);
1576 pmode
= va_arg(ap
, int);
1579 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1580 FIXME(": pmode 0x%04x ignored\n", pmode
);
1582 WARN(": pmode 0x%04x ignored\n", pmode
);
1584 if (oflags
& MSVCRT__O_EXCL
)
1585 creation
= CREATE_NEW
;
1586 else if (oflags
& MSVCRT__O_TRUNC
)
1587 creation
= CREATE_ALWAYS
;
1589 creation
= OPEN_ALWAYS
;
1591 else /* no MSVCRT__O_CREAT */
1593 if (oflags
& MSVCRT__O_TRUNC
)
1594 creation
= TRUNCATE_EXISTING
;
1596 creation
= OPEN_EXISTING
;
1601 case MSVCRT__SH_DENYRW
:
1604 case MSVCRT__SH_DENYWR
:
1605 sharing
= FILE_SHARE_READ
;
1607 case MSVCRT__SH_DENYRD
:
1608 sharing
= FILE_SHARE_WRITE
;
1610 case MSVCRT__SH_DENYNO
:
1611 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1614 ERR( "Unhandled shflags 0x%x\n", shflags
);
1617 attrib
= FILE_ATTRIBUTE_NORMAL
;
1619 if (oflags
& MSVCRT__O_TEMPORARY
)
1621 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1623 sharing
|= FILE_SHARE_DELETE
;
1626 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1627 sa
.lpSecurityDescriptor
= NULL
;
1628 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1630 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1632 if (hand
== INVALID_HANDLE_VALUE
) {
1633 WARN(":failed-last error (%d)\n",GetLastError());
1634 msvcrt_set_errno(GetLastError());
1638 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1640 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1644 /*********************************************************************
1647 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1651 if (flags
& MSVCRT__O_CREAT
)
1654 __ms_va_start(ap
, flags
);
1655 pmode
= va_arg(ap
, int);
1657 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1660 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1663 /*********************************************************************
1666 int CDECL
_wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1670 if (flags
& MSVCRT__O_CREAT
)
1673 __ms_va_start(ap
, flags
);
1674 pmode
= va_arg(ap
, int);
1676 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1679 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1682 /*********************************************************************
1685 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1687 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1688 return MSVCRT__open(path
, usedFlags
);
1691 /*********************************************************************
1692 * _wcreat (MSVCRT.@)
1694 int CDECL
_wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1696 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1697 return _wopen(path
, usedFlags
);
1700 /*********************************************************************
1701 * _open_osfhandle (MSVCRT.@)
1703 int CDECL
_open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1707 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1708 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1709 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1710 * text - it never sets MSVCRT__O_BINARY.
1712 /* don't let split_oflags() decide the mode if no mode is passed */
1713 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1714 oflags
|= MSVCRT__O_BINARY
;
1716 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1717 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1721 /*********************************************************************
1724 int CDECL
_rmtmp(void)
1726 int num_removed
= 0, i
;
1729 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1730 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1732 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1738 TRACE(":removed (%d) temp files\n",num_removed
);
1742 /*********************************************************************
1745 static int read_i(int fd
, void *buf
, unsigned int count
)
1748 char *bufstart
= buf
;
1749 HANDLE hand
= msvcrt_fdtoh(fd
);
1751 if (MSVCRT_fdesc
[fd
].wxflag
& WX_READEOF
) {
1752 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1753 TRACE("already at EOF, returning 0\n");
1756 /* Don't trace small reads, it gets *very* annoying */
1758 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1759 if (hand
== INVALID_HANDLE_VALUE
)
1762 /* Reading single bytes in O_TEXT mode makes things slow
1763 * So read big chunks
1765 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1767 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1770 for (i
=0, j
=0; i
<num_read
; i
++)
1772 /* in text mode, a ctrl-z signals EOF */
1773 if (bufstart
[i
] == 0x1a)
1775 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1776 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1779 /* in text mode, strip \r if followed by \n.
1780 * BUG: should save state across calls somehow, so CR LF that
1781 * straddles buffer boundary gets recognized properly?
1783 if ((bufstart
[i
] != '\r')
1784 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1785 bufstart
[j
++] = bufstart
[i
];
1789 if (count
!= 0 && num_read
== 0)
1791 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1792 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1797 if (GetLastError() == ERROR_BROKEN_PIPE
)
1799 TRACE(":end-of-pipe\n");
1800 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1805 TRACE(":failed-last error (%d)\n",GetLastError());
1811 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1815 /*********************************************************************
1818 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
1821 num_read
= read_i(fd
, buf
, count
);
1825 /*********************************************************************
1826 * _setmode (MSVCRT.@)
1828 int CDECL
_setmode(int fd
,int mode
)
1830 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1831 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1832 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1833 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1834 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1836 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1840 /*********************************************************************
1841 * _stat64 (MSVCRT.@)
1843 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
1846 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1847 unsigned short mode
= ALL_S_IREAD
;
1850 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1852 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1854 TRACE("failed (%d)\n",GetLastError());
1855 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1859 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1861 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1862 Bon 011120: This FIXME seems incorrect
1863 Also a letter as first char isn't enough to be classified
1866 if (isalpha(*path
)&& (*(path
+1)==':'))
1867 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1869 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1871 plen
= strlen(path
);
1873 /* Dir, or regular file? */
1874 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1875 (path
[plen
-1] == '\\'))
1876 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1879 mode
|= MSVCRT__S_IFREG
;
1881 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1883 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1884 (tolower(path
[plen
-3]) << 16);
1885 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1886 mode
|= ALL_S_IEXEC
;
1890 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1891 mode
|= ALL_S_IWRITE
;
1893 buf
->st_mode
= mode
;
1895 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1896 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1898 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1899 buf
->st_mtime
= buf
->st_ctime
= dw
;
1900 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
1901 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
1902 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
1906 /*********************************************************************
1907 * _stati64 (MSVCRT.@)
1909 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1912 struct MSVCRT__stat64 buf64
;
1914 ret
= MSVCRT_stat64(path
, &buf64
);
1916 msvcrt_stat64_to_stati64(&buf64
, buf
);
1920 /*********************************************************************
1923 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
1925 struct MSVCRT__stat64 buf64
;
1927 ret
= MSVCRT_stat64( path
, &buf64
);
1929 msvcrt_stat64_to_stat(&buf64
, buf
);
1933 /*********************************************************************
1934 * _wstat64 (MSVCRT.@)
1936 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
1939 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1940 unsigned short mode
= ALL_S_IREAD
;
1943 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1945 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1947 TRACE("failed (%d)\n",GetLastError());
1948 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1952 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1954 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1955 if (MSVCRT_iswalpha(*path
))
1956 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1958 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1960 plen
= strlenW(path
);
1962 /* Dir, or regular file? */
1963 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1964 (path
[plen
-1] == '\\'))
1965 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1968 mode
|= MSVCRT__S_IFREG
;
1970 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1972 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1973 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1974 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1975 mode
|= ALL_S_IEXEC
;
1979 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1980 mode
|= ALL_S_IWRITE
;
1982 buf
->st_mode
= mode
;
1984 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1985 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1987 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1988 buf
->st_mtime
= buf
->st_ctime
= dw
;
1989 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
1990 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
1991 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
1995 /*********************************************************************
1996 * _wstati64 (MSVCRT.@)
1998 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2001 struct MSVCRT__stat64 buf64
;
2003 ret
= MSVCRT__wstat64(path
, &buf64
);
2005 msvcrt_stat64_to_stati64(&buf64
, buf
);
2009 /*********************************************************************
2012 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2015 struct MSVCRT__stat64 buf64
;
2017 ret
= MSVCRT__wstat64( path
, &buf64
);
2018 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2022 /*********************************************************************
2025 MSVCRT_long CDECL
_tell(int fd
)
2027 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2030 /*********************************************************************
2031 * _telli64 (MSVCRT.@)
2033 __int64 CDECL
_telli64(int fd
)
2035 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2038 /*********************************************************************
2039 * _tempnam (MSVCRT.@)
2041 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2043 char tmpbuf
[MAX_PATH
];
2044 const char *tmp_dir
= MSVCRT_getenv("TMP");
2046 if (tmp_dir
) dir
= tmp_dir
;
2048 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2049 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2051 TRACE("got name (%s)\n",tmpbuf
);
2052 DeleteFileA(tmpbuf
);
2053 return _strdup(tmpbuf
);
2055 TRACE("failed (%d)\n",GetLastError());
2059 /*********************************************************************
2060 * _wtempnam (MSVCRT.@)
2062 MSVCRT_wchar_t
* CDECL
_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2064 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2066 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2067 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2069 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2070 DeleteFileW(tmpbuf
);
2071 return _wcsdup(tmpbuf
);
2073 TRACE("failed (%d)\n",GetLastError());
2077 /*********************************************************************
2080 int CDECL
MSVCRT__umask(int umask
)
2082 int old_umask
= MSVCRT_umask
;
2083 TRACE("(%d)\n",umask
);
2084 MSVCRT_umask
= umask
;
2088 /*********************************************************************
2089 * _utime64 (MSVCRT.@)
2091 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2093 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2097 int retVal
= _futime64(fd
, t
);
2104 /*********************************************************************
2105 * _utime32 (MSVCRT.@)
2107 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2109 struct MSVCRT___utimbuf64 t64
;
2110 t64
.actime
= t
->actime
;
2111 t64
.modtime
= t
->modtime
;
2112 return _utime64( path
, &t64
);
2115 /*********************************************************************
2119 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2121 return _utime64( path
, t
);
2124 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2126 return _utime32( path
, t
);
2130 /*********************************************************************
2131 * _wutime64 (MSVCRT.@)
2133 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2135 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2139 int retVal
= _futime64(fd
, t
);
2146 /*********************************************************************
2147 * _wutime32 (MSVCRT.@)
2149 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2151 struct MSVCRT___utimbuf64 t64
;
2152 t64
.actime
= t
->actime
;
2153 t64
.modtime
= t
->modtime
;
2154 return _wutime64( path
, &t64
);
2157 /*********************************************************************
2158 * _wutime (MSVCRT.@)
2161 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2163 return _wutime64( path
, t
);
2166 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2168 return _wutime32( path
, t
);
2172 /*********************************************************************
2175 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2178 HANDLE hand
= msvcrt_fdtoh(fd
);
2180 /* Don't trace small writes, it gets *very* annoying */
2183 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2185 if (hand
== INVALID_HANDLE_VALUE
)
2187 *MSVCRT__errno() = MSVCRT_EBADF
;
2191 /* If appending, go to EOF */
2192 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
2193 MSVCRT__lseek(fd
, 0, FILE_END
);
2195 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
2197 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2198 && (num_written
== count
))
2200 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2201 hand
, GetLastError());
2202 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2206 unsigned int i
, j
, nr_lf
;
2209 const char *s
= buf
, *buf_start
= buf
;
2210 /* find number of \n ( without preceding \r ) */
2211 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2216 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2221 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2223 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2228 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2235 FIXME("Malloc failed\n");
2243 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2245 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2246 fd
, hand
, GetLastError(), num_written
);
2247 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2250 return s
- buf_start
;
2262 /*********************************************************************
2265 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2268 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2269 if (len
== sizeof(val
)) return val
;
2270 file
->_flag
|= MSVCRT__IOERR
;
2274 /*********************************************************************
2277 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2282 MSVCRT_free(file
->_tmpfname
);
2283 file
->_tmpfname
= NULL
;
2284 /* flush stdio buffers */
2285 if(file
->_flag
& MSVCRT__IOWRT
)
2286 MSVCRT_fflush(file
);
2287 if(file
->_flag
& MSVCRT__IOMYBUF
)
2288 MSVCRT_free(file
->_base
);
2290 r
=MSVCRT__close(file
->_file
);
2294 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2297 /*********************************************************************
2300 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2302 return file
->_flag
& MSVCRT__IOEOF
;
2305 /*********************************************************************
2308 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2310 return file
->_flag
& MSVCRT__IOERR
;
2313 /*********************************************************************
2314 * _filbuf (MSVCRT.@)
2316 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2318 /* Allocate buffer if needed */
2319 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2320 msvcrt_alloc_buffer(file
);
2322 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2323 if(file
->_flag
& MSVCRT__IORW
) {
2324 file
->_flag
|= MSVCRT__IOREAD
;
2329 if(file
->_flag
& MSVCRT__IONBF
) {
2332 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2333 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2338 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2340 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2345 file
->_ptr
= file
->_base
+1;
2346 return *(unsigned char *)file
->_base
;
2350 /*********************************************************************
2353 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2359 i
= (unsigned char *)file
->_ptr
++;
2362 j
= MSVCRT__filbuf(file
);
2366 /*********************************************************************
2367 * _fgetchar (MSVCRT.@)
2369 int CDECL
_fgetchar(void)
2371 return MSVCRT_fgetc(MSVCRT_stdin
);
2374 /*********************************************************************
2377 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2379 int cc
= MSVCRT_EOF
;
2380 char * buf_start
= s
;
2382 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2383 file
,file
->_file
,s
,size
);
2385 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2390 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2392 TRACE(":nothing read\n");
2395 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2398 TRACE(":got %s\n", debugstr_a(buf_start
));
2402 /*********************************************************************
2405 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2406 * the CR from CR/LF combinations
2408 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2412 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2419 for(i
=0; i
<sizeof(wc
); i
++)
2429 j
= MSVCRT__filbuf(file
);
2432 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2442 c
= MSVCRT_fgetc(file
);
2443 if ((MSVCRT___mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2445 FIXME("Treat Multibyte characters\n");
2447 if (c
== MSVCRT_EOF
)
2450 return (MSVCRT_wint_t
)c
;
2453 /*********************************************************************
2456 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2462 for (j
=0; j
<sizeof(int); j
++) {
2463 k
= MSVCRT_fgetc(file
);
2464 if (k
== MSVCRT_EOF
) {
2465 file
->_flag
|= MSVCRT__IOEOF
;
2473 /*********************************************************************
2476 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2478 return MSVCRT_fgetwc(file
);
2481 /*********************************************************************
2482 * _fgetwchar (MSVCRT.@)
2484 MSVCRT_wint_t CDECL
_fgetwchar(void)
2486 return MSVCRT_fgetwc(MSVCRT_stdin
);
2489 /*********************************************************************
2490 * getwchar (MSVCRT.@)
2492 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2494 return _fgetwchar();
2497 /*********************************************************************
2500 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2502 int cc
= MSVCRT_WEOF
;
2503 MSVCRT_wchar_t
* buf_start
= s
;
2505 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2506 file
,file
->_file
,s
,size
);
2508 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2513 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2515 TRACE(":nothing read\n");
2518 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2521 TRACE(":got %s\n", debugstr_w(buf_start
));
2525 /*********************************************************************
2528 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2530 MSVCRT_size_t wrcnt
=size
* nmemb
;
2535 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2536 memcpy(file
->_ptr
, ptr
, pcnt
);
2541 ptr
= (const char*)ptr
+ pcnt
;
2542 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2543 if(file
->_flag
& MSVCRT__IORW
) {
2544 file
->_flag
|= MSVCRT__IOWRT
;
2550 int res
=msvcrt_flush_buffer(file
);
2552 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2555 file
->_flag
|= MSVCRT__IOERR
;
2558 written
+= pwritten
;
2561 return written
/ size
;
2564 /*********************************************************************
2567 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2569 MSVCRT_wchar_t mwc
=wc
;
2570 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2575 /*********************************************************************
2576 * _fputwchar (MSVCRT.@)
2578 MSVCRT_wint_t CDECL
_fputwchar(MSVCRT_wint_t wc
)
2580 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2583 /*********************************************************************
2584 * _wfsopen (MSVCRT.@)
2586 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2589 int open_flags
, stream_flags
, fd
;
2591 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2593 /* map mode string to open() flags. "man fopen" for possibilities. */
2594 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2598 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2601 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2603 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2610 TRACE(":got (%p)\n",file
);
2611 if (fd
>= 0 && !file
)
2617 /*********************************************************************
2618 * _fsopen (MSVCRT.@)
2620 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2623 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2625 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2626 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2632 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
2639 /*********************************************************************
2642 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
2644 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2647 /*********************************************************************
2648 * _wfopen (MSVCRT.@)
2650 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2652 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2655 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2656 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2658 /*********************************************************************
2661 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2668 int res
= msvcrt_flush_buffer(file
);
2669 return res
? res
: c
;
2674 return MSVCRT__flsbuf(c
, file
);
2678 /*********************************************************************
2679 * _flsbuf (MSVCRT.@)
2681 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2683 /* Flush output buffer */
2684 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2685 msvcrt_alloc_buffer(file
);
2687 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2688 if(file
->_flag
& MSVCRT__IORW
) {
2689 file
->_flag
|= MSVCRT__IOWRT
;
2695 int res
=msvcrt_flush_buffer(file
);
2696 return res
?res
: MSVCRT_fputc(c
, file
);
2700 /* set _cnt to 0 for unbuffered FILEs */
2702 len
= MSVCRT__write(file
->_file
, &cc
, 1);
2703 if (len
== 1) return c
& 0xff;
2704 file
->_flag
|= MSVCRT__IOERR
;
2709 /*********************************************************************
2710 * _fputchar (MSVCRT.@)
2712 int CDECL
_fputchar(int c
)
2714 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2717 /*********************************************************************
2720 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2721 { MSVCRT_size_t rcnt
=size
* nmemb
;
2722 MSVCRT_size_t read
=0;
2728 /* first buffered data */
2730 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2731 memcpy(ptr
, file
->_ptr
, pcnt
);
2736 ptr
= (char*)ptr
+ pcnt
;
2737 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2738 if(file
->_flag
& MSVCRT__IORW
) {
2739 file
->_flag
|= MSVCRT__IOREAD
;
2746 /* Fill the buffer on small reads.
2747 * TODO: Use a better buffering strategy.
2749 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
2750 if (file
->_bufsiz
== 0) {
2751 msvcrt_alloc_buffer(file
);
2753 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
2754 file
->_ptr
= file
->_base
;
2755 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2756 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2757 if (i
> 0 && i
< file
->_cnt
) {
2758 MSVCRT_fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2759 file
->_flag
&= ~MSVCRT__IOEOF
;
2762 memcpy(ptr
, file
->_ptr
, i
);
2767 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
2771 ptr
= (char *)ptr
+i
;
2772 /* expose feof condition in the flags
2773 * MFC tests file->_flag for feof, and doesn't call feof())
2775 if ( MSVCRT_fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2776 file
->_flag
|= MSVCRT__IOEOF
;
2779 file
->_flag
|= MSVCRT__IOERR
;
2789 /*********************************************************************
2790 * _wfreopen (MSVCRT.@)
2793 MSVCRT_FILE
* CDECL
_wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
2795 int open_flags
, stream_flags
, fd
;
2797 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2800 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2804 MSVCRT_fclose(file
);
2805 /* map mode string to open() flags. "man fopen" for possibilities. */
2806 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2810 fd
= _wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2813 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2816 WARN(":failed-last error (%d)\n",GetLastError());
2817 msvcrt_set_errno(GetLastError());
2826 /*********************************************************************
2827 * freopen (MSVCRT.@)
2830 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
2833 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2835 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2836 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2842 ret
= _wfreopen(pathW
, modeW
, file
);
2849 /*********************************************************************
2850 * fsetpos (MSVCRT.@)
2852 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2854 /* Note that all this has been lifted 'as is' from fseek */
2855 if(file
->_flag
& MSVCRT__IOWRT
)
2856 msvcrt_flush_buffer(file
);
2858 /* Discard buffered input */
2860 file
->_ptr
= file
->_base
;
2862 /* Reset direction of i/o */
2863 if(file
->_flag
& MSVCRT__IORW
) {
2864 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2867 return (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2870 /*********************************************************************
2873 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
2875 /* TODO: just call fgetpos and return lower half of result */
2878 pos
= _tell(file
->_file
);
2879 if(pos
== -1) return -1;
2881 if( file
->_flag
& MSVCRT__IOWRT
) {
2882 off
= file
->_ptr
- file
->_base
;
2885 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2886 /* Black magic correction for CR removal */
2888 for (i
=0; i
<file
->_cnt
; i
++) {
2889 if (file
->_ptr
[i
] == '\n')
2898 /*********************************************************************
2899 * fgetpos (MSVCRT.@)
2901 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2904 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
2905 if(*pos
== -1) return -1;
2907 if( file
->_flag
& MSVCRT__IOWRT
) {
2908 off
= file
->_ptr
- file
->_base
;
2911 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2912 /* Black magic correction for CR removal */
2914 for (i
=0; i
<file
->_cnt
; i
++) {
2915 if (file
->_ptr
[i
] == '\n')
2925 /*********************************************************************
2928 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2930 MSVCRT_size_t i
, len
= strlen(s
);
2931 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2932 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2933 for (i
=0; i
<len
; i
++)
2934 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2939 /*********************************************************************
2942 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2944 MSVCRT_size_t i
, len
= strlenW(s
);
2945 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2946 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2947 for (i
=0; i
<len
; i
++)
2949 if ((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2951 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2957 /*********************************************************************
2958 * getchar (MSVCRT.@)
2960 int CDECL
MSVCRT_getchar(void)
2962 return MSVCRT_fgetc(MSVCRT_stdin
);
2965 /*********************************************************************
2968 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
2970 return MSVCRT_fgetc(file
);
2973 /*********************************************************************
2976 char * CDECL
MSVCRT_gets(char *buf
)
2979 char * buf_start
= buf
;
2981 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2982 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2983 if(cc
!= '\r') *buf
++ = (char)cc
;
2987 TRACE("got '%s'\n", buf_start
);
2991 /*********************************************************************
2994 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2997 MSVCRT_wchar_t
* ws
= buf
;
2999 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
3000 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3003 *buf
++ = (MSVCRT_wchar_t
)cc
;
3007 TRACE("got %s\n", debugstr_w(ws
));
3011 /*********************************************************************
3014 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3016 return MSVCRT_fputc(c
, file
);
3019 /*********************************************************************
3020 * putchar (MSVCRT.@)
3022 int CDECL
MSVCRT_putchar(int c
)
3024 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3027 /*********************************************************************
3030 int CDECL
MSVCRT_puts(const char *s
)
3032 MSVCRT_size_t len
= strlen(s
);
3033 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3034 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3037 /*********************************************************************
3040 int CDECL
_putws(const MSVCRT_wchar_t
*s
)
3042 static const MSVCRT_wchar_t nl
= '\n';
3043 MSVCRT_size_t len
= strlenW(s
);
3044 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3045 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3048 /*********************************************************************
3051 int CDECL
MSVCRT_remove(const char *path
)
3053 TRACE("(%s)\n",path
);
3054 if (DeleteFileA(path
))
3056 TRACE(":failed (%d)\n",GetLastError());
3057 msvcrt_set_errno(GetLastError());
3061 /*********************************************************************
3062 * _wremove (MSVCRT.@)
3064 int CDECL
_wremove(const MSVCRT_wchar_t
*path
)
3066 TRACE("(%s)\n",debugstr_w(path
));
3067 if (DeleteFileW(path
))
3069 TRACE(":failed (%d)\n",GetLastError());
3070 msvcrt_set_errno(GetLastError());
3074 /*********************************************************************
3077 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3079 TRACE(":from %s to %s\n",oldpath
,newpath
);
3080 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3082 TRACE(":failed (%d)\n",GetLastError());
3083 msvcrt_set_errno(GetLastError());
3087 /*********************************************************************
3088 * _wrename (MSVCRT.@)
3090 int CDECL
_wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3092 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3093 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3095 TRACE(":failed (%d)\n",GetLastError());
3096 msvcrt_set_errno(GetLastError());
3100 /*********************************************************************
3101 * setvbuf (MSVCRT.@)
3103 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3105 /* TODO: Check if file busy */
3107 MSVCRT_free(file
->_base
);
3111 if(mode
== MSVCRT__IOFBF
) {
3112 file
->_flag
&= ~MSVCRT__IONBF
;
3113 file
->_base
= file
->_ptr
= buf
;
3115 file
->_bufsiz
= size
;
3118 file
->_flag
|= MSVCRT__IONBF
;
3123 /*********************************************************************
3126 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3128 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3131 /*********************************************************************
3134 char * CDECL
MSVCRT_tmpnam(char *s
)
3142 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3143 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3144 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3146 msvcrt_int_to_base32(unique
++, tmpstr
);
3148 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3149 GetLastError() == ERROR_FILE_NOT_FOUND
)
3155 /*********************************************************************
3156 * tmpfile (MSVCRT.@)
3158 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3160 char *filename
= MSVCRT_tmpnam(NULL
);
3162 MSVCRT_FILE
* file
= NULL
;
3165 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3166 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3168 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3173 else file
->_tmpfname
= _strdup(filename
);
3179 /*********************************************************************
3180 * vfprintf (MSVCRT.@)
3182 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3184 char buf
[2048], *mem
= buf
;
3185 int written
, resize
= sizeof(buf
), retval
;
3186 /* There are two conventions for vsnprintf failing:
3187 * Return -1 if we truncated, or
3188 * Return the number of bytes that would have been written
3189 * The code below handles both cases
3191 while ((written
= MSVCRT_vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3194 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3197 if (!(mem
= MSVCRT_malloc(resize
)))
3200 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3206 /*********************************************************************
3207 * vfwprintf (MSVCRT.@)
3209 * Is final char included in written (then resize is too big) or not
3210 * (then we must test for equality too)?
3212 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3214 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
3215 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
3216 /* See vfprintf comments */
3217 while ((written
= MSVCRT_vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3220 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
3223 if (!(mem
= MSVCRT_malloc(resize
*sizeof(*mem
))))
3226 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3232 /*********************************************************************
3233 * vprintf (MSVCRT.@)
3235 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3237 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3240 /*********************************************************************
3241 * vwprintf (MSVCRT.@)
3243 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3245 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3248 /*********************************************************************
3249 * fprintf (MSVCRT.@)
3251 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3253 __ms_va_list valist
;
3255 __ms_va_start(valist
, format
);
3256 res
= MSVCRT_vfprintf(file
, format
, valist
);
3257 __ms_va_end(valist
);
3261 /*********************************************************************
3262 * fwprintf (MSVCRT.@)
3264 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3266 __ms_va_list valist
;
3268 __ms_va_start(valist
, format
);
3269 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3270 __ms_va_end(valist
);
3274 /*********************************************************************
3277 int CDECL
MSVCRT_printf(const char *format
, ...)
3279 __ms_va_list valist
;
3281 __ms_va_start(valist
, format
);
3282 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3283 __ms_va_end(valist
);
3287 /*********************************************************************
3290 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3292 if (c
== MSVCRT_EOF
)
3294 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3295 msvcrt_alloc_buffer(file
);
3298 if(file
->_ptr
>file
->_base
) {
3302 MSVCRT_clearerr(file
);
3308 /*********************************************************************
3309 * ungetwc (MSVCRT.@)
3311 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3313 MSVCRT_wchar_t mwc
= wc
;
3314 char * pp
= (char *)&mwc
;
3316 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3317 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
3323 /*********************************************************************
3324 * wprintf (MSVCRT.@)
3326 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
3328 __ms_va_list valist
;
3330 __ms_va_start(valist
, format
);
3331 res
= MSVCRT_vwprintf(format
, valist
);
3332 __ms_va_end(valist
);
3336 /*********************************************************************
3337 * _getmaxstdio (MSVCRT.@)
3339 int CDECL
_getmaxstdio(void)
3341 FIXME("stub, always returns 512\n");
3345 /*********************************************************************
3346 * _setmaxstdio_ (MSVCRT.@)
3348 int CDECL
_setmaxstdio(int newmax
)
3355 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3359 /*********************************************************************
3360 * __pioinfo (MSVCRT.@)
3361 * FIXME: see MSVCRT_MAX_FILES define.
3363 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3364 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
3365 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
3366 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
3367 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
3368 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
3369 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
3370 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
3371 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
3372 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
3373 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
3374 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
3377 /*********************************************************************
3378 * __badioinfo (MSVCRT.@)
3380 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};