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
!= 0 && si
.lpReserved2
!= NULL
)
357 MSVCRT_fdend
= *(unsigned*)si
.lpReserved2
;
359 wxflag_ptr
= (char*)(si
.lpReserved2
+ sizeof(unsigned));
360 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
362 MSVCRT_fdend
= min(MSVCRT_fdend
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
363 for (i
= 0; i
< MSVCRT_fdend
; i
++)
365 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
367 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
368 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
372 MSVCRT_fdesc
[i
].wxflag
= 0;
373 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
375 wxflag_ptr
++; handle_ptr
++;
377 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
378 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
381 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
383 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
384 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
385 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
,
386 0, TRUE
, DUPLICATE_SAME_ACCESS
))
387 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
389 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
391 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
392 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
393 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
,
394 0, TRUE
, DUPLICATE_SAME_ACCESS
))
395 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
397 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
399 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
400 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
401 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
,
402 0, TRUE
, DUPLICATE_SAME_ACCESS
))
403 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
406 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
407 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
409 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
410 for (i
= 0; i
< 3; i
++)
412 /* FILE structs for stdin/out/err are static and never deleted */
413 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
414 MSVCRT__iob
[i
]._file
= i
;
415 MSVCRT__iob
[i
]._tmpfname
= NULL
;
416 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
418 MSVCRT_stream_idx
= 3;
421 /* INTERNAL: Flush stdio file buffer */
422 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
425 int cnt
=file
->_ptr
-file
->_base
;
426 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
427 file
->_flag
|= MSVCRT__IOERR
;
430 file
->_ptr
=file
->_base
;
431 file
->_cnt
=file
->_bufsiz
;
436 /* INTERNAL: Allocate stdio file buffer */
437 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
439 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
441 file
->_bufsiz
= MSVCRT_BUFSIZ
;
442 file
->_flag
|= MSVCRT__IOMYBUF
;
444 file
->_base
= (char*)(&file
->_charbuf
);
446 file
->_bufsiz
= sizeof(file
->_charbuf
);
448 file
->_ptr
= file
->_base
;
452 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
453 static void msvcrt_int_to_base32(int num
, char *str
)
468 *p
= (num
& 31) + '0';
470 *p
+= ('a' - '0' - 10);
475 /*********************************************************************
476 * __iob_func(MSVCRT.@)
478 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
480 return &MSVCRT__iob
[0];
483 /*********************************************************************
486 int CDECL
MSVCRT__access(const char *filename
, int mode
)
488 DWORD attr
= GetFileAttributesA(filename
);
490 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
492 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
494 msvcrt_set_errno(GetLastError());
497 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
499 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
505 /*********************************************************************
506 * _waccess (MSVCRT.@)
508 int CDECL
_waccess(const MSVCRT_wchar_t
*filename
, int mode
)
510 DWORD attr
= GetFileAttributesW(filename
);
512 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
514 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
516 msvcrt_set_errno(GetLastError());
519 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
521 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
527 /*********************************************************************
530 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
532 DWORD oldFlags
= GetFileAttributesA(path
);
534 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
536 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
537 oldFlags
| FILE_ATTRIBUTE_READONLY
;
539 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
542 msvcrt_set_errno(GetLastError());
546 /*********************************************************************
549 int CDECL
_wchmod(const MSVCRT_wchar_t
*path
, int flags
)
551 DWORD oldFlags
= GetFileAttributesW(path
);
553 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
555 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
556 oldFlags
| FILE_ATTRIBUTE_READONLY
;
558 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
561 msvcrt_set_errno(GetLastError());
565 /*********************************************************************
568 int CDECL
MSVCRT__unlink(const char *path
)
570 TRACE("%s\n",debugstr_a(path
));
571 if(DeleteFileA(path
))
573 TRACE("failed (%d)\n",GetLastError());
574 msvcrt_set_errno(GetLastError());
578 /*********************************************************************
579 * _wunlink (MSVCRT.@)
581 int CDECL
_wunlink(const MSVCRT_wchar_t
*path
)
583 TRACE("(%s)\n",debugstr_w(path
));
584 if(DeleteFileW(path
))
586 TRACE("failed (%d)\n",GetLastError());
587 msvcrt_set_errno(GetLastError());
591 /* _flushall calls MSVCRT_fflush which calls _flushall */
592 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
594 /*********************************************************************
595 * _flushall (MSVCRT.@)
597 int CDECL
_flushall(void)
599 int i
, num_flushed
= 0;
602 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
603 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
606 /* FIXME: flush, do not commit */
607 if (_commit(i
) == -1)
608 if (MSVCRT_fstreams
[i
])
609 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
611 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
612 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
618 TRACE(":flushed (%d) handles\n",num_flushed
);
622 /*********************************************************************
625 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
629 } else if(file
->_flag
& MSVCRT__IOWRT
) {
630 int res
=msvcrt_flush_buffer(file
);
636 /*********************************************************************
639 int CDECL
MSVCRT__close(int fd
)
645 hand
= msvcrt_fdtoh(fd
);
646 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
647 if (hand
== INVALID_HANDLE_VALUE
)
649 else if (!CloseHandle(hand
))
651 WARN(":failed-last error (%d)\n",GetLastError());
652 msvcrt_set_errno(GetLastError());
665 /*********************************************************************
668 int CDECL
_commit(int fd
)
670 HANDLE hand
= msvcrt_fdtoh(fd
);
672 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
673 if (hand
== INVALID_HANDLE_VALUE
)
676 if (!FlushFileBuffers(hand
))
678 if (GetLastError() == ERROR_INVALID_HANDLE
)
680 /* FlushFileBuffers fails for console handles
681 * so we ignore this error.
685 TRACE(":failed-last error (%d)\n",GetLastError());
686 msvcrt_set_errno(GetLastError());
693 /*********************************************************************
696 * MSDN isn't clear on this point, but the remarks for _pipe
697 * indicate file descriptors duplicated with _dup and _dup2 are always
700 int CDECL
MSVCRT__dup2(int od
, int nd
)
704 TRACE("(od=%d, nd=%d)\n", od
, nd
);
706 if (nd
< MSVCRT_MAX_FILES
&& msvcrt_is_valid_fd(od
))
710 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
711 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
713 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
715 if (msvcrt_is_valid_fd(nd
))
717 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
721 *MSVCRT__errno() = MSVCRT_EMFILE
;
725 /* _dup2 returns 0, not nd, on success */
732 msvcrt_set_errno(GetLastError());
737 *MSVCRT__errno() = MSVCRT_EBADF
;
744 /*********************************************************************
747 int CDECL
MSVCRT__dup(int od
)
753 if (MSVCRT__dup2(od
, fd
) == 0)
761 /*********************************************************************
764 int CDECL
_eof(int fd
)
767 LONG hcurpos
,hendpos
;
768 HANDLE hand
= msvcrt_fdtoh(fd
);
770 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
772 if (hand
== INVALID_HANDLE_VALUE
)
775 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
777 /* Otherwise we do it the hard way */
778 hcurpos
= hendpos
= 0;
779 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
780 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
782 if (curpos
== endpos
&& hcurpos
== hendpos
)
784 /* FIXME: shouldn't WX_ATEOF be set here? */
788 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
792 /*********************************************************************
793 * _fcloseall (MSVCRT.@)
795 int CDECL
MSVCRT__fcloseall(void)
797 int num_closed
= 0, i
;
800 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
801 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
802 !MSVCRT_fclose(MSVCRT_fstreams
[i
]))
806 TRACE(":closed (%d) handles\n",num_closed
);
810 /* free everything on process exit */
811 void msvcrt_free_io(void)
814 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
815 * stdout, and stderr (unlike GNU), so we need to fclose() them here
816 * or they won't get flushed.
818 MSVCRT_fclose(&MSVCRT__iob
[0]);
819 MSVCRT_fclose(&MSVCRT__iob
[1]);
820 MSVCRT_fclose(&MSVCRT__iob
[2]);
821 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = 0;
822 DeleteCriticalSection(&MSVCRT_file_cs
);
825 /*********************************************************************
826 * _lseeki64 (MSVCRT.@)
828 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
830 HANDLE hand
= msvcrt_fdtoh(fd
);
831 LARGE_INTEGER ofs
, ret
;
833 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
834 if (hand
== INVALID_HANDLE_VALUE
)
837 if (whence
< 0 || whence
> 2)
839 *MSVCRT__errno() = MSVCRT_EINVAL
;
843 TRACE(":fd (%d) to %s pos %s\n",
844 fd
,wine_dbgstr_longlong(offset
),
845 (whence
==SEEK_SET
)?"SEEK_SET":
846 (whence
==SEEK_CUR
)?"SEEK_CUR":
847 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
849 ofs
.QuadPart
= offset
;
850 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
852 MSVCRT_fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
853 /* FIXME: What if we seek _to_ EOF - is EOF set? */
857 TRACE(":error-last error (%d)\n",GetLastError());
858 msvcrt_set_errno(GetLastError());
862 /*********************************************************************
865 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
867 return MSVCRT__lseeki64(fd
, offset
, whence
);
870 /*********************************************************************
871 * _locking (MSVCRT.@)
873 * This is untested; the underlying LockFile doesn't work yet.
875 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
879 HANDLE hand
= msvcrt_fdtoh(fd
);
881 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
882 if (hand
== INVALID_HANDLE_VALUE
)
885 if (mode
< 0 || mode
> 4)
887 *MSVCRT__errno() = MSVCRT_EINVAL
;
891 TRACE(":fd (%d) by 0x%08x mode %s\n",
892 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
893 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
894 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
895 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
896 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
899 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
901 FIXME ("Seek failed\n");
902 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
905 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
908 ret
= 1; /* just to satisfy gcc */
911 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
916 else if (mode
== MSVCRT__LK_UNLCK
)
917 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
919 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
920 /* FIXME - what about error settings? */
924 /*********************************************************************
927 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
929 /* Flush output if needed */
930 if(file
->_flag
& MSVCRT__IOWRT
)
931 msvcrt_flush_buffer(file
);
933 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
934 offset
-= file
->_cnt
;
935 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
936 /* Black magic correction for CR removal */
938 for (i
=0; i
<file
->_cnt
; i
++) {
939 if (file
->_ptr
[i
] == '\n')
944 /* Discard buffered input */
946 file
->_ptr
= file
->_base
;
947 /* Reset direction of i/o */
948 if(file
->_flag
& MSVCRT__IORW
) {
949 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
951 /* Clear end of file flag */
952 file
->_flag
&= ~MSVCRT__IOEOF
;
953 return (MSVCRT__lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
956 /*********************************************************************
959 int CDECL
_chsize(int fd
, MSVCRT_long size
)
965 TRACE("(fd=%d, size=%d)\n", fd
, size
);
969 handle
= msvcrt_fdtoh(fd
);
970 if (handle
!= INVALID_HANDLE_VALUE
)
972 /* save the current file pointer */
973 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
976 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
979 ret
= SetEndOfFile(handle
);
980 if (!ret
) msvcrt_set_errno(GetLastError());
983 /* restore the file pointer */
984 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
992 /*********************************************************************
993 * clearerr (MSVCRT.@)
995 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
997 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
998 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1001 /*********************************************************************
1004 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1006 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1007 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1008 MSVCRT_clearerr(file
);
1011 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1013 int plus
= strchrW(mode
, '+') != NULL
;
1018 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1019 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1022 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1023 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1026 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1027 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1037 *open_flags
|= MSVCRT__O_BINARY
;
1038 *open_flags
&= ~MSVCRT__O_TEXT
;
1041 *open_flags
|= MSVCRT__O_TEXT
;
1042 *open_flags
&= ~MSVCRT__O_BINARY
;
1047 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1052 /*********************************************************************
1053 * _fdopen (MSVCRT.@)
1055 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1058 MSVCRT_wchar_t
*modeW
= NULL
;
1060 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1062 ret
= MSVCRT__wfdopen(fd
, modeW
);
1068 /*********************************************************************
1069 * _wfdopen (MSVCRT.@)
1071 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1073 int open_flags
, stream_flags
;
1076 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1079 if (!(file
= msvcrt_alloc_fp()))
1081 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1086 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1092 /*********************************************************************
1093 * _filelength (MSVCRT.@)
1095 LONG CDECL
MSVCRT__filelength(int fd
)
1097 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1100 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1103 if (endPos
!= curPos
)
1104 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1111 /*********************************************************************
1112 * _filelengthi64 (MSVCRT.@)
1114 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1116 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1119 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1122 if (endPos
!= curPos
)
1123 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1130 /*********************************************************************
1131 * _fileno (MSVCRT.@)
1133 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1135 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1139 /*********************************************************************
1140 * _fstat64 (MSVCRT.@)
1142 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1146 BY_HANDLE_FILE_INFORMATION hfi
;
1147 HANDLE hand
= msvcrt_fdtoh(fd
);
1149 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1150 if (hand
== INVALID_HANDLE_VALUE
)
1155 WARN(":failed-NULL buf\n");
1156 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1160 memset(&hfi
, 0, sizeof(hfi
));
1161 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1162 type
= GetFileType(hand
);
1163 if (type
== FILE_TYPE_PIPE
)
1165 buf
->st_dev
= buf
->st_rdev
= fd
;
1166 buf
->st_mode
= S_IFIFO
;
1169 else if (type
== FILE_TYPE_CHAR
)
1171 buf
->st_dev
= buf
->st_rdev
= fd
;
1172 buf
->st_mode
= S_IFCHR
;
1175 else /* FILE_TYPE_DISK etc. */
1177 if (!GetFileInformationByHandle(hand
, &hfi
))
1179 WARN(":failed-last error (%d)\n",GetLastError());
1180 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1183 buf
->st_mode
= S_IFREG
| 0444;
1184 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1185 buf
->st_mode
|= 0222;
1186 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1187 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1189 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1190 buf
->st_mtime
= buf
->st_ctime
= dw
;
1191 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1193 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1198 /*********************************************************************
1199 * _fstati64 (MSVCRT.@)
1201 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1204 struct MSVCRT__stat64 buf64
;
1206 ret
= MSVCRT__fstat64(fd
, &buf64
);
1208 msvcrt_stat64_to_stati64(&buf64
, buf
);
1212 /*********************************************************************
1215 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1217 struct MSVCRT__stat64 buf64
;
1219 ret
= MSVCRT__fstat64(fd
, &buf64
);
1221 msvcrt_stat64_to_stat(&buf64
, buf
);
1225 /*********************************************************************
1226 * _futime64 (MSVCRT.@)
1228 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1230 HANDLE hand
= msvcrt_fdtoh(fd
);
1235 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1240 time_to_filetime( t
->actime
, &at
);
1241 time_to_filetime( t
->modtime
, &wt
);
1244 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1246 msvcrt_set_errno(GetLastError());
1252 /*********************************************************************
1253 * _futime32 (MSVCRT.@)
1255 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1257 struct MSVCRT___utimbuf64 t64
;
1258 t64
.actime
= t
->actime
;
1259 t64
.modtime
= t
->modtime
;
1260 return _futime64( fd
, &t64
);
1263 /*********************************************************************
1264 * _futime (MSVCRT.@)
1267 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1269 return _futime64( fd
, t
);
1272 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1274 return _futime32( fd
, t
);
1278 /*********************************************************************
1279 * _get_osfhandle (MSVCRT.@)
1281 MSVCRT_intptr_t CDECL
_get_osfhandle(int fd
)
1283 HANDLE hand
= msvcrt_fdtoh(fd
);
1284 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1286 return (MSVCRT_intptr_t
)hand
;
1289 /*********************************************************************
1290 * _isatty (MSVCRT.@)
1292 int CDECL
_isatty(int fd
)
1294 HANDLE hand
= msvcrt_fdtoh(fd
);
1296 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1297 if (hand
== INVALID_HANDLE_VALUE
)
1300 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1303 /*********************************************************************
1304 * _mktemp (MSVCRT.@)
1306 char * CDECL
_mktemp(char *pattern
)
1309 char *retVal
= pattern
;
1314 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1318 id
= GetCurrentProcessId();
1322 int tempNum
= id
/ 10;
1323 *pattern
-- = id
- (tempNum
* 10) + '0';
1329 *pattern
= letter
++;
1330 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1331 GetLastError() == ERROR_FILE_NOT_FOUND
)
1333 } while(letter
<= 'z');
1337 /*********************************************************************
1338 * _wmktemp (MSVCRT.@)
1340 MSVCRT_wchar_t
* CDECL
_wmktemp(MSVCRT_wchar_t
*pattern
)
1343 MSVCRT_wchar_t
*retVal
= pattern
;
1345 MSVCRT_wchar_t letter
= 'a';
1348 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1352 id
= GetCurrentProcessId();
1356 int tempNum
= id
/ 10;
1357 *pattern
-- = id
- (tempNum
* 10) + '0';
1363 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1364 GetLastError() == ERROR_FILE_NOT_FOUND
)
1366 *pattern
= letter
++;
1367 } while(letter
!= '|');
1371 static unsigned split_oflags(unsigned oflags
)
1374 unsigned unsupp
; /* until we support everything */
1376 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1377 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1378 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1379 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1380 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1381 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1383 if ((unsupp
= oflags
& ~(
1384 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1385 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1386 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1387 MSVCRT__O_NOINHERIT
|
1388 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1390 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1395 /*********************************************************************
1398 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1401 SECURITY_ATTRIBUTES sa
;
1402 HANDLE readHandle
, writeHandle
;
1406 *MSVCRT__errno() = MSVCRT_EINVAL
;
1410 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1411 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1412 sa
.lpSecurityDescriptor
= NULL
;
1413 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1415 unsigned int wxflags
= split_oflags(textmode
);
1419 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1423 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1431 MSVCRT__close(pfds
[0]);
1432 CloseHandle(writeHandle
);
1433 *MSVCRT__errno() = MSVCRT_EMFILE
;
1438 CloseHandle(readHandle
);
1439 CloseHandle(writeHandle
);
1440 *MSVCRT__errno() = MSVCRT_EMFILE
;
1445 msvcrt_set_errno(GetLastError());
1450 /*********************************************************************
1453 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1457 DWORD access
= 0, creation
= 0, attrib
;
1461 SECURITY_ATTRIBUTES sa
;
1464 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1465 path
, oflags
, shflags
);
1467 wxflag
= split_oflags(oflags
);
1468 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1470 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1471 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1472 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1475 if (oflags
& MSVCRT__O_CREAT
)
1477 __ms_va_start(ap
, shflags
);
1478 pmode
= va_arg(ap
, int);
1481 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1482 FIXME(": pmode 0x%04x ignored\n", pmode
);
1484 WARN(": pmode 0x%04x ignored\n", pmode
);
1486 if (oflags
& MSVCRT__O_EXCL
)
1487 creation
= CREATE_NEW
;
1488 else if (oflags
& MSVCRT__O_TRUNC
)
1489 creation
= CREATE_ALWAYS
;
1491 creation
= OPEN_ALWAYS
;
1493 else /* no MSVCRT__O_CREAT */
1495 if (oflags
& MSVCRT__O_TRUNC
)
1496 creation
= TRUNCATE_EXISTING
;
1498 creation
= OPEN_EXISTING
;
1503 case MSVCRT__SH_DENYRW
:
1506 case MSVCRT__SH_DENYWR
:
1507 sharing
= FILE_SHARE_READ
;
1509 case MSVCRT__SH_DENYRD
:
1510 sharing
= FILE_SHARE_WRITE
;
1512 case MSVCRT__SH_DENYNO
:
1513 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1516 ERR( "Unhandled shflags 0x%x\n", shflags
);
1519 attrib
= FILE_ATTRIBUTE_NORMAL
;
1521 if (oflags
& MSVCRT__O_TEMPORARY
)
1523 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1525 sharing
|= FILE_SHARE_DELETE
;
1528 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1529 sa
.lpSecurityDescriptor
= NULL
;
1530 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1532 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1534 if (hand
== INVALID_HANDLE_VALUE
) {
1535 WARN(":failed-last error (%d)\n",GetLastError());
1536 msvcrt_set_errno(GetLastError());
1540 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1542 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1546 /*********************************************************************
1547 * _wsopen (MSVCRT.@)
1549 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1553 DWORD access
= 0, creation
= 0, attrib
;
1557 SECURITY_ATTRIBUTES sa
;
1560 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1561 debugstr_w(path
), oflags
, shflags
);
1563 wxflag
= split_oflags(oflags
);
1564 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1566 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1567 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1568 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1571 if (oflags
& MSVCRT__O_CREAT
)
1573 __ms_va_start(ap
, shflags
);
1574 pmode
= va_arg(ap
, int);
1577 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1578 FIXME(": pmode 0x%04x ignored\n", pmode
);
1580 WARN(": pmode 0x%04x ignored\n", pmode
);
1582 if (oflags
& MSVCRT__O_EXCL
)
1583 creation
= CREATE_NEW
;
1584 else if (oflags
& MSVCRT__O_TRUNC
)
1585 creation
= CREATE_ALWAYS
;
1587 creation
= OPEN_ALWAYS
;
1589 else /* no MSVCRT__O_CREAT */
1591 if (oflags
& MSVCRT__O_TRUNC
)
1592 creation
= TRUNCATE_EXISTING
;
1594 creation
= OPEN_EXISTING
;
1599 case MSVCRT__SH_DENYRW
:
1602 case MSVCRT__SH_DENYWR
:
1603 sharing
= FILE_SHARE_READ
;
1605 case MSVCRT__SH_DENYRD
:
1606 sharing
= FILE_SHARE_WRITE
;
1608 case MSVCRT__SH_DENYNO
:
1609 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1612 ERR( "Unhandled shflags 0x%x\n", shflags
);
1615 attrib
= FILE_ATTRIBUTE_NORMAL
;
1617 if (oflags
& MSVCRT__O_TEMPORARY
)
1619 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1621 sharing
|= FILE_SHARE_DELETE
;
1624 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1625 sa
.lpSecurityDescriptor
= NULL
;
1626 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1628 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1630 if (hand
== INVALID_HANDLE_VALUE
) {
1631 WARN(":failed-last error (%d)\n",GetLastError());
1632 msvcrt_set_errno(GetLastError());
1636 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1638 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1642 /*********************************************************************
1645 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1649 if (flags
& MSVCRT__O_CREAT
)
1652 __ms_va_start(ap
, flags
);
1653 pmode
= va_arg(ap
, int);
1655 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1658 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1661 /*********************************************************************
1664 int CDECL
_wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1668 if (flags
& MSVCRT__O_CREAT
)
1671 __ms_va_start(ap
, flags
);
1672 pmode
= va_arg(ap
, int);
1674 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1677 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1680 /*********************************************************************
1683 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1685 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1686 return MSVCRT__open(path
, usedFlags
);
1689 /*********************************************************************
1690 * _wcreat (MSVCRT.@)
1692 int CDECL
_wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1694 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1695 return _wopen(path
, usedFlags
);
1698 /*********************************************************************
1699 * _open_osfhandle (MSVCRT.@)
1701 int CDECL
_open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1705 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1706 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1707 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1708 * text - it never sets MSVCRT__O_BINARY.
1710 /* don't let split_oflags() decide the mode if no mode is passed */
1711 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1712 oflags
|= MSVCRT__O_BINARY
;
1714 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1715 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1719 /*********************************************************************
1722 int CDECL
_rmtmp(void)
1724 int num_removed
= 0, i
;
1727 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1728 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1730 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1736 TRACE(":removed (%d) temp files\n",num_removed
);
1740 /*********************************************************************
1743 static int read_i(int fd
, void *buf
, unsigned int count
)
1746 char *bufstart
= buf
;
1747 HANDLE hand
= msvcrt_fdtoh(fd
);
1749 if (MSVCRT_fdesc
[fd
].wxflag
& WX_READEOF
) {
1750 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1751 TRACE("already at EOF, returning 0\n");
1754 /* Don't trace small reads, it gets *very* annoying */
1756 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1757 if (hand
== INVALID_HANDLE_VALUE
)
1760 /* Reading single bytes in O_TEXT mode makes things slow
1761 * So read big chunks
1763 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1765 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1768 for (i
=0, j
=0; i
<num_read
; i
++)
1770 /* in text mode, a ctrl-z signals EOF */
1771 if (bufstart
[i
] == 0x1a)
1773 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1774 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1777 /* in text mode, strip \r if followed by \n.
1778 * BUG: should save state across calls somehow, so CR LF that
1779 * straddles buffer boundary gets recognized properly?
1781 if ((bufstart
[i
] != '\r')
1782 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1783 bufstart
[j
++] = bufstart
[i
];
1787 if (count
!= 0 && num_read
== 0)
1789 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1790 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1795 if (GetLastError() == ERROR_BROKEN_PIPE
)
1797 TRACE(":end-of-pipe\n");
1798 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1803 TRACE(":failed-last error (%d)\n",GetLastError());
1809 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1813 /*********************************************************************
1816 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
1819 num_read
= read_i(fd
, buf
, count
);
1823 /*********************************************************************
1824 * _setmode (MSVCRT.@)
1826 int CDECL
_setmode(int fd
,int mode
)
1828 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1829 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1830 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1831 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1832 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1834 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1838 /*********************************************************************
1839 * _stat64 (MSVCRT.@)
1841 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
1844 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1845 unsigned short mode
= ALL_S_IREAD
;
1848 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1850 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1852 TRACE("failed (%d)\n",GetLastError());
1853 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1857 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1859 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1860 Bon 011120: This FIXME seems incorrect
1861 Also a letter as first char isn't enough to be classified
1864 if (isalpha(*path
)&& (*(path
+1)==':'))
1865 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1867 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1869 plen
= strlen(path
);
1871 /* Dir, or regular file? */
1872 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1873 (path
[plen
-1] == '\\'))
1874 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1877 mode
|= MSVCRT__S_IFREG
;
1879 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1881 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1882 (tolower(path
[plen
-3]) << 16);
1883 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1884 mode
|= ALL_S_IEXEC
;
1888 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1889 mode
|= ALL_S_IWRITE
;
1891 buf
->st_mode
= mode
;
1893 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1894 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1896 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1897 buf
->st_mtime
= buf
->st_ctime
= dw
;
1898 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
1899 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
1900 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
1904 /*********************************************************************
1905 * _stati64 (MSVCRT.@)
1907 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1910 struct MSVCRT__stat64 buf64
;
1912 ret
= MSVCRT_stat64(path
, &buf64
);
1914 msvcrt_stat64_to_stati64(&buf64
, buf
);
1918 /*********************************************************************
1921 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
1923 struct MSVCRT__stat64 buf64
;
1925 ret
= MSVCRT_stat64( path
, &buf64
);
1927 msvcrt_stat64_to_stat(&buf64
, buf
);
1931 /*********************************************************************
1932 * _wstat64 (MSVCRT.@)
1934 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
1937 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1938 unsigned short mode
= ALL_S_IREAD
;
1941 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1943 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1945 TRACE("failed (%d)\n",GetLastError());
1946 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1950 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1952 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1953 if (MSVCRT_iswalpha(*path
))
1954 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1956 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1958 plen
= strlenW(path
);
1960 /* Dir, or regular file? */
1961 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1962 (path
[plen
-1] == '\\'))
1963 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1966 mode
|= MSVCRT__S_IFREG
;
1968 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1970 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1971 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1972 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1973 mode
|= ALL_S_IEXEC
;
1977 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1978 mode
|= ALL_S_IWRITE
;
1980 buf
->st_mode
= mode
;
1982 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1983 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1985 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1986 buf
->st_mtime
= buf
->st_ctime
= dw
;
1987 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
1988 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
1989 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
1993 /*********************************************************************
1994 * _wstati64 (MSVCRT.@)
1996 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
1999 struct MSVCRT__stat64 buf64
;
2001 ret
= MSVCRT__wstat64(path
, &buf64
);
2003 msvcrt_stat64_to_stati64(&buf64
, buf
);
2007 /*********************************************************************
2010 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2013 struct MSVCRT__stat64 buf64
;
2015 ret
= MSVCRT__wstat64( path
, &buf64
);
2016 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2020 /*********************************************************************
2023 MSVCRT_long CDECL
_tell(int fd
)
2025 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2028 /*********************************************************************
2029 * _telli64 (MSVCRT.@)
2031 __int64 CDECL
_telli64(int fd
)
2033 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2036 /*********************************************************************
2037 * _tempnam (MSVCRT.@)
2039 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2041 char tmpbuf
[MAX_PATH
];
2042 const char *tmp_dir
= MSVCRT_getenv("TMP");
2044 if (tmp_dir
) dir
= tmp_dir
;
2046 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2047 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2049 TRACE("got name (%s)\n",tmpbuf
);
2050 DeleteFileA(tmpbuf
);
2051 return _strdup(tmpbuf
);
2053 TRACE("failed (%d)\n",GetLastError());
2057 /*********************************************************************
2058 * _wtempnam (MSVCRT.@)
2060 MSVCRT_wchar_t
* CDECL
_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2062 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2064 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2065 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2067 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2068 DeleteFileW(tmpbuf
);
2069 return _wcsdup(tmpbuf
);
2071 TRACE("failed (%d)\n",GetLastError());
2075 /*********************************************************************
2078 int CDECL
MSVCRT__umask(int umask
)
2080 int old_umask
= MSVCRT_umask
;
2081 TRACE("(%d)\n",umask
);
2082 MSVCRT_umask
= umask
;
2086 /*********************************************************************
2087 * _utime64 (MSVCRT.@)
2089 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2091 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2095 int retVal
= _futime64(fd
, t
);
2102 /*********************************************************************
2103 * _utime32 (MSVCRT.@)
2105 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2107 struct MSVCRT___utimbuf64 t64
;
2108 t64
.actime
= t
->actime
;
2109 t64
.modtime
= t
->modtime
;
2110 return _utime64( path
, &t64
);
2113 /*********************************************************************
2117 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2119 return _utime64( path
, t
);
2122 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2124 return _utime32( path
, t
);
2128 /*********************************************************************
2129 * _wutime64 (MSVCRT.@)
2131 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2133 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2137 int retVal
= _futime64(fd
, t
);
2144 /*********************************************************************
2145 * _wutime32 (MSVCRT.@)
2147 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2149 struct MSVCRT___utimbuf64 t64
;
2150 t64
.actime
= t
->actime
;
2151 t64
.modtime
= t
->modtime
;
2152 return _wutime64( path
, &t64
);
2155 /*********************************************************************
2156 * _wutime (MSVCRT.@)
2159 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2161 return _wutime64( path
, t
);
2164 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2166 return _wutime32( path
, t
);
2170 /*********************************************************************
2173 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2176 HANDLE hand
= msvcrt_fdtoh(fd
);
2178 /* Don't trace small writes, it gets *very* annoying */
2181 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2183 if (hand
== INVALID_HANDLE_VALUE
)
2185 *MSVCRT__errno() = MSVCRT_EBADF
;
2189 /* If appending, go to EOF */
2190 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
2191 MSVCRT__lseek(fd
, 0, FILE_END
);
2193 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
2195 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2196 && (num_written
== count
))
2198 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2199 hand
, GetLastError());
2200 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2204 unsigned int i
, j
, nr_lf
;
2207 const char *s
= buf
, *buf_start
= buf
;
2208 /* find number of \n ( without preceding \r ) */
2209 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2214 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2219 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2221 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2226 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2233 FIXME("Malloc failed\n");
2241 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2243 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2244 fd
, hand
, GetLastError(), num_written
);
2245 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2248 return s
- buf_start
;
2260 /*********************************************************************
2263 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2266 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2267 if (len
== sizeof(val
)) return val
;
2268 file
->_flag
|= MSVCRT__IOERR
;
2272 /*********************************************************************
2275 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2280 MSVCRT_free(file
->_tmpfname
);
2281 file
->_tmpfname
= NULL
;
2282 /* flush stdio buffers */
2283 if(file
->_flag
& MSVCRT__IOWRT
)
2284 MSVCRT_fflush(file
);
2285 if(file
->_flag
& MSVCRT__IOMYBUF
)
2286 MSVCRT_free(file
->_base
);
2288 r
=MSVCRT__close(file
->_file
);
2292 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2295 /*********************************************************************
2298 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2300 return file
->_flag
& MSVCRT__IOEOF
;
2303 /*********************************************************************
2306 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2308 return file
->_flag
& MSVCRT__IOERR
;
2311 /*********************************************************************
2312 * _filbuf (MSVCRT.@)
2314 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2316 /* Allocate buffer if needed */
2317 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2318 msvcrt_alloc_buffer(file
);
2320 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2321 if(file
->_flag
& MSVCRT__IORW
) {
2322 file
->_flag
|= MSVCRT__IOREAD
;
2327 if(file
->_flag
& MSVCRT__IONBF
) {
2330 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2331 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2336 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2338 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2343 file
->_ptr
= file
->_base
+1;
2344 return *(unsigned char *)file
->_base
;
2348 /*********************************************************************
2351 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2357 i
= (unsigned char *)file
->_ptr
++;
2360 j
= MSVCRT__filbuf(file
);
2364 /*********************************************************************
2365 * _fgetchar (MSVCRT.@)
2367 int CDECL
_fgetchar(void)
2369 return MSVCRT_fgetc(MSVCRT_stdin
);
2372 /*********************************************************************
2375 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2377 int cc
= MSVCRT_EOF
;
2378 char * buf_start
= s
;
2380 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2381 file
,file
->_file
,s
,size
);
2383 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2388 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2390 TRACE(":nothing read\n");
2393 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2396 TRACE(":got %s\n", debugstr_a(buf_start
));
2400 /*********************************************************************
2403 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2404 * the CR from CR/LF combinations
2406 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2410 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2417 for(i
=0; i
<sizeof(wc
); i
++)
2427 j
= MSVCRT__filbuf(file
);
2430 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2440 c
= MSVCRT_fgetc(file
);
2441 if ((MSVCRT___mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2443 FIXME("Treat Multibyte characters\n");
2445 if (c
== MSVCRT_EOF
)
2448 return (MSVCRT_wint_t
)c
;
2451 /*********************************************************************
2454 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2460 for (j
=0; j
<sizeof(int); j
++) {
2461 k
= MSVCRT_fgetc(file
);
2462 if (k
== MSVCRT_EOF
) {
2463 file
->_flag
|= MSVCRT__IOEOF
;
2471 /*********************************************************************
2474 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2476 return MSVCRT_fgetwc(file
);
2479 /*********************************************************************
2480 * _fgetwchar (MSVCRT.@)
2482 MSVCRT_wint_t CDECL
_fgetwchar(void)
2484 return MSVCRT_fgetwc(MSVCRT_stdin
);
2487 /*********************************************************************
2488 * getwchar (MSVCRT.@)
2490 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2492 return _fgetwchar();
2495 /*********************************************************************
2498 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2500 int cc
= MSVCRT_WEOF
;
2501 MSVCRT_wchar_t
* buf_start
= s
;
2503 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2504 file
,file
->_file
,s
,size
);
2506 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2511 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2513 TRACE(":nothing read\n");
2516 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2519 TRACE(":got %s\n", debugstr_w(buf_start
));
2523 /*********************************************************************
2526 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2528 MSVCRT_size_t wrcnt
=size
* nmemb
;
2533 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2534 memcpy(file
->_ptr
, ptr
, pcnt
);
2539 ptr
= (const char*)ptr
+ pcnt
;
2540 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2541 if(file
->_flag
& MSVCRT__IORW
) {
2542 file
->_flag
|= MSVCRT__IOWRT
;
2548 int res
=msvcrt_flush_buffer(file
);
2550 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2553 file
->_flag
|= MSVCRT__IOERR
;
2556 written
+= pwritten
;
2559 return written
/ size
;
2562 /*********************************************************************
2565 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2567 MSVCRT_wchar_t mwc
=wc
;
2568 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2573 /*********************************************************************
2574 * _fputwchar (MSVCRT.@)
2576 MSVCRT_wint_t CDECL
_fputwchar(MSVCRT_wint_t wc
)
2578 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2581 /*********************************************************************
2582 * _wfsopen (MSVCRT.@)
2584 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2587 int open_flags
, stream_flags
, fd
;
2589 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2591 /* map mode string to open() flags. "man fopen" for possibilities. */
2592 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2596 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2599 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2601 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2608 TRACE(":got (%p)\n",file
);
2609 if (fd
>= 0 && !file
)
2615 /*********************************************************************
2616 * _fsopen (MSVCRT.@)
2618 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2621 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2623 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2624 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2630 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
2637 /*********************************************************************
2640 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
2642 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2645 /*********************************************************************
2646 * _wfopen (MSVCRT.@)
2648 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2650 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2653 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2654 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2656 /*********************************************************************
2659 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2666 int res
= msvcrt_flush_buffer(file
);
2667 return res
? res
: c
;
2672 return MSVCRT__flsbuf(c
, file
);
2676 /*********************************************************************
2677 * _flsbuf (MSVCRT.@)
2679 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2681 /* Flush output buffer */
2682 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2683 msvcrt_alloc_buffer(file
);
2685 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2686 if(file
->_flag
& MSVCRT__IORW
) {
2687 file
->_flag
|= MSVCRT__IOWRT
;
2693 int res
=msvcrt_flush_buffer(file
);
2694 return res
?res
: MSVCRT_fputc(c
, file
);
2698 /* set _cnt to 0 for unbuffered FILEs */
2700 len
= MSVCRT__write(file
->_file
, &cc
, 1);
2701 if (len
== 1) return c
& 0xff;
2702 file
->_flag
|= MSVCRT__IOERR
;
2707 /*********************************************************************
2708 * _fputchar (MSVCRT.@)
2710 int CDECL
_fputchar(int c
)
2712 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2715 /*********************************************************************
2718 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2719 { MSVCRT_size_t rcnt
=size
* nmemb
;
2720 MSVCRT_size_t read
=0;
2726 /* first buffered data */
2728 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2729 memcpy(ptr
, file
->_ptr
, pcnt
);
2734 ptr
= (char*)ptr
+ pcnt
;
2735 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2736 if(file
->_flag
& MSVCRT__IORW
) {
2737 file
->_flag
|= MSVCRT__IOREAD
;
2744 /* Fill the buffer on small reads.
2745 * TODO: Use a better buffering strategy.
2747 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
2748 if (file
->_bufsiz
== 0) {
2749 msvcrt_alloc_buffer(file
);
2751 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
2752 file
->_ptr
= file
->_base
;
2753 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2754 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2755 if (i
> 0 && i
< file
->_cnt
) {
2756 MSVCRT_fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2757 file
->_flag
&= ~MSVCRT__IOEOF
;
2760 memcpy(ptr
, file
->_ptr
, i
);
2765 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
2769 ptr
= (char *)ptr
+i
;
2770 /* expose feof condition in the flags
2771 * MFC tests file->_flag for feof, and doesn't call feof())
2773 if ( MSVCRT_fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2774 file
->_flag
|= MSVCRT__IOEOF
;
2777 file
->_flag
|= MSVCRT__IOERR
;
2787 /*********************************************************************
2788 * _wfreopen (MSVCRT.@)
2791 MSVCRT_FILE
* CDECL
_wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
2793 int open_flags
, stream_flags
, fd
;
2795 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2798 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2802 MSVCRT_fclose(file
);
2803 /* map mode string to open() flags. "man fopen" for possibilities. */
2804 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2808 fd
= _wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2811 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2814 WARN(":failed-last error (%d)\n",GetLastError());
2815 msvcrt_set_errno(GetLastError());
2824 /*********************************************************************
2825 * freopen (MSVCRT.@)
2828 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
2831 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2833 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2834 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2840 ret
= _wfreopen(pathW
, modeW
, file
);
2847 /*********************************************************************
2848 * fsetpos (MSVCRT.@)
2850 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2852 /* Note that all this has been lifted 'as is' from fseek */
2853 if(file
->_flag
& MSVCRT__IOWRT
)
2854 msvcrt_flush_buffer(file
);
2856 /* Discard buffered input */
2858 file
->_ptr
= file
->_base
;
2860 /* Reset direction of i/o */
2861 if(file
->_flag
& MSVCRT__IORW
) {
2862 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2865 return (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2868 /*********************************************************************
2871 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
2873 /* TODO: just call fgetpos and return lower half of result */
2876 pos
= _tell(file
->_file
);
2877 if(pos
== -1) return -1;
2879 if( file
->_flag
& MSVCRT__IOWRT
) {
2880 off
= file
->_ptr
- file
->_base
;
2883 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2884 /* Black magic correction for CR removal */
2886 for (i
=0; i
<file
->_cnt
; i
++) {
2887 if (file
->_ptr
[i
] == '\n')
2896 /*********************************************************************
2897 * fgetpos (MSVCRT.@)
2899 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2902 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
2903 if(*pos
== -1) return -1;
2905 if( file
->_flag
& MSVCRT__IOWRT
) {
2906 off
= file
->_ptr
- file
->_base
;
2909 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2910 /* Black magic correction for CR removal */
2912 for (i
=0; i
<file
->_cnt
; i
++) {
2913 if (file
->_ptr
[i
] == '\n')
2923 /*********************************************************************
2926 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2928 MSVCRT_size_t i
, len
= strlen(s
);
2929 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2930 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2931 for (i
=0; i
<len
; i
++)
2932 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2937 /*********************************************************************
2940 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2942 MSVCRT_size_t i
, len
= strlenW(s
);
2943 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2944 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2945 for (i
=0; i
<len
; i
++)
2947 if ((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2949 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2955 /*********************************************************************
2956 * getchar (MSVCRT.@)
2958 int CDECL
MSVCRT_getchar(void)
2960 return MSVCRT_fgetc(MSVCRT_stdin
);
2963 /*********************************************************************
2966 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
2968 return MSVCRT_fgetc(file
);
2971 /*********************************************************************
2974 char * CDECL
MSVCRT_gets(char *buf
)
2977 char * buf_start
= buf
;
2979 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2980 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2981 if(cc
!= '\r') *buf
++ = (char)cc
;
2985 TRACE("got '%s'\n", buf_start
);
2989 /*********************************************************************
2992 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2995 MSVCRT_wchar_t
* ws
= buf
;
2997 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2998 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3001 *buf
++ = (MSVCRT_wchar_t
)cc
;
3005 TRACE("got %s\n", debugstr_w(ws
));
3009 /*********************************************************************
3012 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3014 return MSVCRT_fputc(c
, file
);
3017 /*********************************************************************
3018 * putchar (MSVCRT.@)
3020 int CDECL
MSVCRT_putchar(int c
)
3022 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3025 /*********************************************************************
3028 int CDECL
MSVCRT_puts(const char *s
)
3030 MSVCRT_size_t len
= strlen(s
);
3031 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3032 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3035 /*********************************************************************
3038 int CDECL
_putws(const MSVCRT_wchar_t
*s
)
3040 static const MSVCRT_wchar_t nl
= '\n';
3041 MSVCRT_size_t len
= strlenW(s
);
3042 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3043 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3046 /*********************************************************************
3049 int CDECL
MSVCRT_remove(const char *path
)
3051 TRACE("(%s)\n",path
);
3052 if (DeleteFileA(path
))
3054 TRACE(":failed (%d)\n",GetLastError());
3055 msvcrt_set_errno(GetLastError());
3059 /*********************************************************************
3060 * _wremove (MSVCRT.@)
3062 int CDECL
_wremove(const MSVCRT_wchar_t
*path
)
3064 TRACE("(%s)\n",debugstr_w(path
));
3065 if (DeleteFileW(path
))
3067 TRACE(":failed (%d)\n",GetLastError());
3068 msvcrt_set_errno(GetLastError());
3072 /*********************************************************************
3075 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3077 TRACE(":from %s to %s\n",oldpath
,newpath
);
3078 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3080 TRACE(":failed (%d)\n",GetLastError());
3081 msvcrt_set_errno(GetLastError());
3085 /*********************************************************************
3086 * _wrename (MSVCRT.@)
3088 int CDECL
_wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3090 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3091 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3093 TRACE(":failed (%d)\n",GetLastError());
3094 msvcrt_set_errno(GetLastError());
3098 /*********************************************************************
3099 * setvbuf (MSVCRT.@)
3101 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3103 /* TODO: Check if file busy */
3105 MSVCRT_free(file
->_base
);
3109 if(mode
== MSVCRT__IOFBF
) {
3110 file
->_flag
&= ~MSVCRT__IONBF
;
3111 file
->_base
= file
->_ptr
= buf
;
3113 file
->_bufsiz
= size
;
3116 file
->_flag
|= MSVCRT__IONBF
;
3121 /*********************************************************************
3124 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3126 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3129 /*********************************************************************
3132 char * CDECL
MSVCRT_tmpnam(char *s
)
3140 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3141 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3142 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3144 msvcrt_int_to_base32(unique
++, tmpstr
);
3146 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3147 GetLastError() == ERROR_FILE_NOT_FOUND
)
3153 /*********************************************************************
3154 * tmpfile (MSVCRT.@)
3156 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3158 char *filename
= MSVCRT_tmpnam(NULL
);
3160 MSVCRT_FILE
* file
= NULL
;
3163 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3164 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3166 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3171 else file
->_tmpfname
= _strdup(filename
);
3177 /*********************************************************************
3178 * vfprintf (MSVCRT.@)
3180 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3182 char buf
[2048], *mem
= buf
;
3183 int written
, resize
= sizeof(buf
), retval
;
3184 /* There are two conventions for vsnprintf failing:
3185 * Return -1 if we truncated, or
3186 * Return the number of bytes that would have been written
3187 * The code below handles both cases
3189 while ((written
= MSVCRT_vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3192 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3195 if (!(mem
= MSVCRT_malloc(resize
)))
3198 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3204 /*********************************************************************
3205 * vfwprintf (MSVCRT.@)
3207 * Is final char included in written (then resize is too big) or not
3208 * (then we must test for equality too)?
3210 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3212 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
3213 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
3214 /* See vfprintf comments */
3215 while ((written
= MSVCRT_vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3218 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
3221 if (!(mem
= MSVCRT_malloc(resize
*sizeof(*mem
))))
3224 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3230 /*********************************************************************
3231 * vprintf (MSVCRT.@)
3233 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3235 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3238 /*********************************************************************
3239 * vwprintf (MSVCRT.@)
3241 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3243 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3246 /*********************************************************************
3247 * fprintf (MSVCRT.@)
3249 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3251 __ms_va_list valist
;
3253 __ms_va_start(valist
, format
);
3254 res
= MSVCRT_vfprintf(file
, format
, valist
);
3255 __ms_va_end(valist
);
3259 /*********************************************************************
3260 * fwprintf (MSVCRT.@)
3262 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3264 __ms_va_list valist
;
3266 __ms_va_start(valist
, format
);
3267 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3268 __ms_va_end(valist
);
3272 /*********************************************************************
3275 int CDECL
MSVCRT_printf(const char *format
, ...)
3277 __ms_va_list valist
;
3279 __ms_va_start(valist
, format
);
3280 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3281 __ms_va_end(valist
);
3285 /*********************************************************************
3288 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3290 if (c
== MSVCRT_EOF
)
3292 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3293 msvcrt_alloc_buffer(file
);
3296 if(file
->_ptr
>file
->_base
) {
3300 MSVCRT_clearerr(file
);
3306 /*********************************************************************
3307 * ungetwc (MSVCRT.@)
3309 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3311 MSVCRT_wchar_t mwc
= wc
;
3312 char * pp
= (char *)&mwc
;
3314 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3315 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
3321 /*********************************************************************
3322 * wprintf (MSVCRT.@)
3324 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
3326 __ms_va_list valist
;
3328 __ms_va_start(valist
, format
);
3329 res
= MSVCRT_vwprintf(format
, valist
);
3330 __ms_va_end(valist
);
3334 /*********************************************************************
3335 * _getmaxstdio (MSVCRT.@)
3337 int CDECL
_getmaxstdio(void)
3339 FIXME("stub, always returns 512\n");
3343 /*********************************************************************
3344 * _setmaxstdio_ (MSVCRT.@)
3346 int CDECL
_setmaxstdio(int newmax
)
3353 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3357 /*********************************************************************
3358 * __pioinfo (MSVCRT.@)
3359 * FIXME: see MSVCRT_MAX_FILES define.
3361 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3362 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
3363 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
3364 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
3365 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
3366 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
3367 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
3368 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
3369 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
3370 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
3371 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
3372 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
3375 /*********************************************************************
3376 * __badioinfo (MSVCRT.@)
3378 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};