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_READCR 0x08 /* underlying file is at \r */
64 #define WX_DONTINHERIT 0x10
65 #define WX_APPEND 0x20
68 /* FIXME: this should be allocated dynamically */
69 #define MSVCRT_MAX_FILES 2048
74 DWORD unkn
[7]; /* critical section and init flag */
77 static ioinfo MSVCRT_fdesc
[MSVCRT_MAX_FILES
];
79 MSVCRT_FILE MSVCRT__iob
[3] = { { 0 } };
81 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
82 static int MSVCRT_fdend
= 3; /* highest allocated fd */
84 static MSVCRT_FILE
* MSVCRT_fstreams
[2048];
85 static int MSVCRT_stream_idx
;
87 /* INTERNAL: process umask */
88 static int MSVCRT_umask
= 0;
90 /* INTERNAL: Static buffer for temp file name */
91 static char MSVCRT_tmpname
[MAX_PATH
];
93 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
94 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
95 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
96 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
98 #define TOUL(x) (ULONGLONG)(x)
99 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
100 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
101 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
102 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
104 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
105 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
106 * and MSVCRT_stream_idx, from race conditions.
107 * It doesn't protect against race conditions manipulating the underlying files
108 * or flags; doing so would probably be better accomplished with per-file
109 * protection, rather than locking the whole table for every change.
111 static CRITICAL_SECTION MSVCRT_file_cs
;
112 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
113 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
115 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
117 buf
->st_dev
= buf64
->st_dev
;
118 buf
->st_ino
= buf64
->st_ino
;
119 buf
->st_mode
= buf64
->st_mode
;
120 buf
->st_nlink
= buf64
->st_nlink
;
121 buf
->st_uid
= buf64
->st_uid
;
122 buf
->st_gid
= buf64
->st_gid
;
123 buf
->st_rdev
= buf64
->st_rdev
;
124 buf
->st_size
= buf64
->st_size
;
125 buf
->st_atime
= buf64
->st_atime
;
126 buf
->st_mtime
= buf64
->st_mtime
;
127 buf
->st_ctime
= buf64
->st_ctime
;
130 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
132 buf
->st_dev
= buf64
->st_dev
;
133 buf
->st_ino
= buf64
->st_ino
;
134 buf
->st_mode
= buf64
->st_mode
;
135 buf
->st_nlink
= buf64
->st_nlink
;
136 buf
->st_uid
= buf64
->st_uid
;
137 buf
->st_gid
= buf64
->st_gid
;
138 buf
->st_rdev
= buf64
->st_rdev
;
139 buf
->st_size
= buf64
->st_size
;
140 buf
->st_atime
= buf64
->st_atime
;
141 buf
->st_mtime
= buf64
->st_mtime
;
142 buf
->st_ctime
= buf64
->st_ctime
;
145 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
147 /* 1601 to 1970 is 369 years plus 89 leap days */
148 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
150 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
151 ft
->dwHighDateTime
= ticks
>> 32;
152 ft
->dwLowDateTime
= ticks
;
155 static inline BOOL
msvcrt_is_valid_fd(int fd
)
157 return fd
>= 0 && fd
< MSVCRT_fdend
&& (MSVCRT_fdesc
[fd
].wxflag
& WX_OPEN
);
160 /* INTERNAL: Get the HANDLE for a fd
161 * This doesn't lock the table, because a failure will result in
162 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
163 * it returns a valid handle which is about to be closed, a subsequent call
164 * will fail, most likely in a sane way.
166 static HANDLE
msvcrt_fdtoh(int fd
)
168 if (!msvcrt_is_valid_fd(fd
))
170 WARN(":fd (%d) - no handle!\n",fd
);
171 *MSVCRT___doserrno() = 0;
172 *MSVCRT__errno() = MSVCRT_EBADF
;
173 return INVALID_HANDLE_VALUE
;
175 if (MSVCRT_fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
176 return MSVCRT_fdesc
[fd
].handle
;
179 /* INTERNAL: free a file entry fd */
180 static void msvcrt_free_fd(int fd
)
185 old_handle
= MSVCRT_fdesc
[fd
].handle
;
186 MSVCRT_fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
187 MSVCRT_fdesc
[fd
].wxflag
= 0;
188 TRACE(":fd (%d) freed\n",fd
);
189 if (fd
< 3) /* don't use 0,1,2 for user files */
194 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
197 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
200 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
206 if (fd
== MSVCRT_fdend
- 1)
208 if (fd
< MSVCRT_fdstart
)
214 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
215 /* caller must hold the files lock */
216 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
218 if (fd
>= MSVCRT_MAX_FILES
)
220 WARN(":files exhausted!\n");
221 *MSVCRT__errno() = MSVCRT_ENFILE
;
224 MSVCRT_fdesc
[fd
].handle
= hand
;
225 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
227 /* locate next free slot */
228 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
229 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
231 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
232 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
234 /* update last fd in use */
235 if (fd
>= MSVCRT_fdend
)
236 MSVCRT_fdend
= fd
+ 1;
237 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
241 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
242 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
243 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
249 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
250 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
255 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
256 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
261 /* INTERNAL: Allocate a FILE* for an fd slot */
262 /* caller must hold the files lock */
263 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
267 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
269 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
271 if (!MSVCRT_fstreams
[i
])
273 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
275 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
277 return MSVCRT_fstreams
[i
];
283 /* INTERNAL: initialize a FILE* from an open fd */
284 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
286 TRACE(":fd (%d) allocating FILE*\n",fd
);
287 if (!msvcrt_is_valid_fd(fd
))
289 WARN(":invalid fd %d\n",fd
);
290 *MSVCRT___doserrno() = 0;
291 *MSVCRT__errno() = MSVCRT_EBADF
;
294 memset(file
, 0, sizeof(*file
));
296 file
->_flag
= stream_flags
;
298 TRACE(":got FILE* (%p)\n",file
);
302 /* INTERNAL: Create an inheritance data block (for spawned process)
303 * The inheritance block is made of:
304 * 00 int nb of file descriptor (NBFD)
305 * 04 char file flags (wxflag): repeated for each fd
306 * 4+NBFD HANDLE file handle: repeated for each fd
308 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
314 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
315 *block
= MSVCRT_calloc(*size
, 1);
321 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
322 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
324 *(unsigned*)*block
= MSVCRT_fdend
;
325 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
327 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
328 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
330 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
331 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
336 *handle_ptr
= INVALID_HANDLE_VALUE
;
338 wxflag_ptr
++; handle_ptr
++;
343 /* INTERNAL: Set up all file descriptors,
344 * as well as default streams (stdin, stderr and stdout)
346 void msvcrt_init_io(void)
351 InitializeCriticalSection(&MSVCRT_file_cs
);
352 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs");
353 GetStartupInfoA(&si
);
354 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
360 count
= *(unsigned*)si
.lpReserved2
;
361 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
362 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
364 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
365 count
= min(count
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
366 for (i
= 0; i
< count
; i
++)
368 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
370 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
371 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
375 MSVCRT_fdesc
[i
].wxflag
= 0;
376 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
378 wxflag_ptr
++; handle_ptr
++;
380 MSVCRT_fdend
= max( 3, count
);
381 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
382 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
385 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
387 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
388 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
389 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
,
390 0, TRUE
, DUPLICATE_SAME_ACCESS
))
391 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
393 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
395 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
396 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
397 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
,
398 0, TRUE
, DUPLICATE_SAME_ACCESS
))
399 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
401 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
403 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
404 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
405 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
,
406 0, TRUE
, DUPLICATE_SAME_ACCESS
))
407 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
410 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
411 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
413 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
414 for (i
= 0; i
< 3; i
++)
416 /* FILE structs for stdin/out/err are static and never deleted */
417 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
418 MSVCRT__iob
[i
]._file
= i
;
419 MSVCRT__iob
[i
]._tmpfname
= NULL
;
420 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
422 MSVCRT_stream_idx
= 3;
425 /* INTERNAL: Flush stdio file buffer */
426 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
429 int cnt
=file
->_ptr
-file
->_base
;
430 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
431 file
->_flag
|= MSVCRT__IOERR
;
434 file
->_ptr
=file
->_base
;
435 file
->_cnt
=file
->_bufsiz
;
440 /* INTERNAL: Allocate stdio file buffer */
441 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
443 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
445 file
->_bufsiz
= MSVCRT_BUFSIZ
;
446 file
->_flag
|= MSVCRT__IOMYBUF
;
448 file
->_base
= (char*)(&file
->_charbuf
);
450 file
->_bufsiz
= sizeof(file
->_charbuf
);
452 file
->_ptr
= file
->_base
;
456 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
457 static void msvcrt_int_to_base32(int num
, char *str
)
472 *p
= (num
& 31) + '0';
474 *p
+= ('a' - '0' - 10);
479 /*********************************************************************
480 * __iob_func(MSVCRT.@)
482 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
484 return &MSVCRT__iob
[0];
487 /*********************************************************************
490 int CDECL
MSVCRT__access(const char *filename
, int mode
)
492 DWORD attr
= GetFileAttributesA(filename
);
494 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
496 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
498 msvcrt_set_errno(GetLastError());
501 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
503 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
509 /*********************************************************************
510 * _waccess (MSVCRT.@)
512 int CDECL
_waccess(const MSVCRT_wchar_t
*filename
, int mode
)
514 DWORD attr
= GetFileAttributesW(filename
);
516 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
518 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
520 msvcrt_set_errno(GetLastError());
523 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
525 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
531 /*********************************************************************
534 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
536 DWORD oldFlags
= GetFileAttributesA(path
);
538 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
540 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
541 oldFlags
| FILE_ATTRIBUTE_READONLY
;
543 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
546 msvcrt_set_errno(GetLastError());
550 /*********************************************************************
553 int CDECL
_wchmod(const MSVCRT_wchar_t
*path
, int flags
)
555 DWORD oldFlags
= GetFileAttributesW(path
);
557 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
559 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
560 oldFlags
| FILE_ATTRIBUTE_READONLY
;
562 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
565 msvcrt_set_errno(GetLastError());
569 /*********************************************************************
572 int CDECL
MSVCRT__unlink(const char *path
)
574 TRACE("%s\n",debugstr_a(path
));
575 if(DeleteFileA(path
))
577 TRACE("failed (%d)\n",GetLastError());
578 msvcrt_set_errno(GetLastError());
582 /*********************************************************************
583 * _wunlink (MSVCRT.@)
585 int CDECL
_wunlink(const MSVCRT_wchar_t
*path
)
587 TRACE("(%s)\n",debugstr_w(path
));
588 if(DeleteFileW(path
))
590 TRACE("failed (%d)\n",GetLastError());
591 msvcrt_set_errno(GetLastError());
595 /* _flushall calls MSVCRT_fflush which calls _flushall */
596 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
598 /*********************************************************************
599 * _flushall (MSVCRT.@)
601 int CDECL
_flushall(void)
603 int i
, num_flushed
= 0;
606 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
607 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
610 /* FIXME: flush, do not commit */
611 if (_commit(i
) == -1)
612 if (MSVCRT_fstreams
[i
])
613 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
615 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
616 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
622 TRACE(":flushed (%d) handles\n",num_flushed
);
626 /*********************************************************************
629 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
633 } else if(file
->_flag
& MSVCRT__IOWRT
) {
634 int res
=msvcrt_flush_buffer(file
);
640 /*********************************************************************
643 int CDECL
MSVCRT__close(int fd
)
649 hand
= msvcrt_fdtoh(fd
);
650 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
651 if (hand
== INVALID_HANDLE_VALUE
)
653 else if (!CloseHandle(hand
))
655 WARN(":failed-last error (%d)\n",GetLastError());
656 msvcrt_set_errno(GetLastError());
669 /*********************************************************************
672 int CDECL
_commit(int fd
)
674 HANDLE hand
= msvcrt_fdtoh(fd
);
676 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
677 if (hand
== INVALID_HANDLE_VALUE
)
680 if (!FlushFileBuffers(hand
))
682 if (GetLastError() == ERROR_INVALID_HANDLE
)
684 /* FlushFileBuffers fails for console handles
685 * so we ignore this error.
689 TRACE(":failed-last error (%d)\n",GetLastError());
690 msvcrt_set_errno(GetLastError());
697 /*********************************************************************
700 * MSDN isn't clear on this point, but the remarks for _pipe
701 * indicate file descriptors duplicated with _dup and _dup2 are always
704 int CDECL
MSVCRT__dup2(int od
, int nd
)
708 TRACE("(od=%d, nd=%d)\n", od
, nd
);
710 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
714 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
715 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
717 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
719 if (msvcrt_is_valid_fd(nd
))
721 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
725 *MSVCRT__errno() = MSVCRT_EMFILE
;
729 /* _dup2 returns 0, not nd, on success */
736 msvcrt_set_errno(GetLastError());
741 *MSVCRT__errno() = MSVCRT_EBADF
;
748 /*********************************************************************
751 int CDECL
MSVCRT__dup(int od
)
757 if (MSVCRT__dup2(od
, fd
) == 0)
765 /*********************************************************************
768 int CDECL
_eof(int fd
)
771 LONG hcurpos
,hendpos
;
772 HANDLE hand
= msvcrt_fdtoh(fd
);
774 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
776 if (hand
== INVALID_HANDLE_VALUE
)
779 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
781 /* Otherwise we do it the hard way */
782 hcurpos
= hendpos
= 0;
783 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
784 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
786 if (curpos
== endpos
&& hcurpos
== hendpos
)
788 /* FIXME: shouldn't WX_ATEOF be set here? */
792 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
796 /*********************************************************************
797 * _fcloseall (MSVCRT.@)
799 int CDECL
MSVCRT__fcloseall(void)
801 int num_closed
= 0, i
;
804 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
805 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
806 !MSVCRT_fclose(MSVCRT_fstreams
[i
]))
810 TRACE(":closed (%d) handles\n",num_closed
);
814 /* free everything on process exit */
815 void msvcrt_free_io(void)
818 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
819 * stdout, and stderr (unlike GNU), so we need to fclose() them here
820 * or they won't get flushed.
822 MSVCRT_fclose(&MSVCRT__iob
[0]);
823 MSVCRT_fclose(&MSVCRT__iob
[1]);
824 MSVCRT_fclose(&MSVCRT__iob
[2]);
825 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = 0;
826 DeleteCriticalSection(&MSVCRT_file_cs
);
829 /*********************************************************************
830 * _lseeki64 (MSVCRT.@)
832 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
834 HANDLE hand
= msvcrt_fdtoh(fd
);
835 LARGE_INTEGER ofs
, ret
;
837 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
838 if (hand
== INVALID_HANDLE_VALUE
)
841 if (whence
< 0 || whence
> 2)
843 *MSVCRT__errno() = MSVCRT_EINVAL
;
847 TRACE(":fd (%d) to %s pos %s\n",
848 fd
,wine_dbgstr_longlong(offset
),
849 (whence
==SEEK_SET
)?"SEEK_SET":
850 (whence
==SEEK_CUR
)?"SEEK_CUR":
851 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
853 ofs
.QuadPart
= offset
;
854 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
856 MSVCRT_fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
857 /* FIXME: What if we seek _to_ EOF - is EOF set? */
861 TRACE(":error-last error (%d)\n",GetLastError());
862 msvcrt_set_errno(GetLastError());
866 /*********************************************************************
869 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
871 return MSVCRT__lseeki64(fd
, offset
, whence
);
874 /*********************************************************************
875 * _lock_file (MSVCRT.@)
877 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
879 FIXME("(%p) stub\n",file
);
882 /*********************************************************************
883 * _unlock_file (MSVCRT.@)
885 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
887 FIXME("(%p) stub\n",file
);
890 /*********************************************************************
891 * _locking (MSVCRT.@)
893 * This is untested; the underlying LockFile doesn't work yet.
895 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
899 HANDLE hand
= msvcrt_fdtoh(fd
);
901 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
902 if (hand
== INVALID_HANDLE_VALUE
)
905 if (mode
< 0 || mode
> 4)
907 *MSVCRT__errno() = MSVCRT_EINVAL
;
911 TRACE(":fd (%d) by 0x%08x mode %s\n",
912 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
913 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
914 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
915 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
916 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
919 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
921 FIXME ("Seek failed\n");
922 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
925 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
928 ret
= 1; /* just to satisfy gcc */
931 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
936 else if (mode
== MSVCRT__LK_UNLCK
)
937 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
939 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
940 /* FIXME - what about error settings? */
944 /*********************************************************************
945 * _fseeki64 (MSVCRT.@)
947 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
949 /* Flush output if needed */
950 if(file
->_flag
& MSVCRT__IOWRT
)
951 msvcrt_flush_buffer(file
);
953 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
954 offset
-= file
->_cnt
;
955 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
956 /* Black magic correction for CR removal */
958 for (i
=0; i
<file
->_cnt
; i
++) {
959 if (file
->_ptr
[i
] == '\n')
962 /* Black magic when reading CR at buffer boundary*/
963 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
967 /* Discard buffered input */
969 file
->_ptr
= file
->_base
;
970 /* Reset direction of i/o */
971 if(file
->_flag
& MSVCRT__IORW
) {
972 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
974 /* Clear end of file flag */
975 file
->_flag
&= ~MSVCRT__IOEOF
;
976 return (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
979 /*********************************************************************
982 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
984 return MSVCRT__fseeki64( file
, offset
, whence
);
987 /*********************************************************************
990 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
996 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1000 handle
= msvcrt_fdtoh(fd
);
1001 if (handle
!= INVALID_HANDLE_VALUE
)
1003 /* save the current file pointer */
1004 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1007 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
1010 ret
= SetEndOfFile(handle
);
1011 if (!ret
) msvcrt_set_errno(GetLastError());
1014 /* restore the file pointer */
1015 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
1020 return ret
? 0 : -1;
1023 /*********************************************************************
1024 * clearerr (MSVCRT.@)
1026 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1028 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1029 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1032 /*********************************************************************
1035 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1037 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1038 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1039 MSVCRT_clearerr(file
);
1042 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1044 int plus
= strchrW(mode
, '+') != NULL
;
1049 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1050 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1053 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1054 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1057 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1058 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1061 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1062 *MSVCRT__errno() = MSVCRT_EINVAL
;
1070 *open_flags
|= MSVCRT__O_BINARY
;
1071 *open_flags
&= ~MSVCRT__O_TEXT
;
1074 *open_flags
|= MSVCRT__O_TEXT
;
1075 *open_flags
&= ~MSVCRT__O_BINARY
;
1081 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1086 /*********************************************************************
1087 * _fdopen (MSVCRT.@)
1089 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1092 MSVCRT_wchar_t
*modeW
= NULL
;
1094 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1096 ret
= MSVCRT__wfdopen(fd
, modeW
);
1102 /*********************************************************************
1103 * _wfdopen (MSVCRT.@)
1105 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1107 int open_flags
, stream_flags
;
1110 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1113 if (!(file
= msvcrt_alloc_fp()))
1115 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1120 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1126 /*********************************************************************
1127 * _filelength (MSVCRT.@)
1129 LONG CDECL
MSVCRT__filelength(int fd
)
1131 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1134 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1137 if (endPos
!= curPos
)
1138 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1145 /*********************************************************************
1146 * _filelengthi64 (MSVCRT.@)
1148 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1150 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1153 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1156 if (endPos
!= curPos
)
1157 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1164 /*********************************************************************
1165 * _fileno (MSVCRT.@)
1167 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1169 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1173 /*********************************************************************
1174 * _fstat64 (MSVCRT.@)
1176 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1180 BY_HANDLE_FILE_INFORMATION hfi
;
1181 HANDLE hand
= msvcrt_fdtoh(fd
);
1183 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1184 if (hand
== INVALID_HANDLE_VALUE
)
1189 WARN(":failed-NULL buf\n");
1190 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1194 memset(&hfi
, 0, sizeof(hfi
));
1195 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1196 type
= GetFileType(hand
);
1197 if (type
== FILE_TYPE_PIPE
)
1199 buf
->st_dev
= buf
->st_rdev
= fd
;
1200 buf
->st_mode
= S_IFIFO
;
1203 else if (type
== FILE_TYPE_CHAR
)
1205 buf
->st_dev
= buf
->st_rdev
= fd
;
1206 buf
->st_mode
= S_IFCHR
;
1209 else /* FILE_TYPE_DISK etc. */
1211 if (!GetFileInformationByHandle(hand
, &hfi
))
1213 WARN(":failed-last error (%d)\n",GetLastError());
1214 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1217 buf
->st_mode
= S_IFREG
| 0444;
1218 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1219 buf
->st_mode
|= 0222;
1220 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1221 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1223 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1224 buf
->st_mtime
= buf
->st_ctime
= dw
;
1225 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1227 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1232 /*********************************************************************
1233 * _fstati64 (MSVCRT.@)
1235 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1238 struct MSVCRT__stat64 buf64
;
1240 ret
= MSVCRT__fstat64(fd
, &buf64
);
1242 msvcrt_stat64_to_stati64(&buf64
, buf
);
1246 /*********************************************************************
1249 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1251 struct MSVCRT__stat64 buf64
;
1253 ret
= MSVCRT__fstat64(fd
, &buf64
);
1255 msvcrt_stat64_to_stat(&buf64
, buf
);
1259 /*********************************************************************
1260 * _futime64 (MSVCRT.@)
1262 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1264 HANDLE hand
= msvcrt_fdtoh(fd
);
1269 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1274 time_to_filetime( t
->actime
, &at
);
1275 time_to_filetime( t
->modtime
, &wt
);
1278 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1280 msvcrt_set_errno(GetLastError());
1286 /*********************************************************************
1287 * _futime32 (MSVCRT.@)
1289 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1291 struct MSVCRT___utimbuf64 t64
;
1292 t64
.actime
= t
->actime
;
1293 t64
.modtime
= t
->modtime
;
1294 return _futime64( fd
, &t64
);
1297 /*********************************************************************
1298 * _futime (MSVCRT.@)
1301 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1303 return _futime64( fd
, t
);
1306 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1308 return _futime32( fd
, t
);
1312 /*********************************************************************
1313 * _get_osfhandle (MSVCRT.@)
1315 MSVCRT_intptr_t CDECL
_get_osfhandle(int fd
)
1317 HANDLE hand
= msvcrt_fdtoh(fd
);
1318 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1320 return (MSVCRT_intptr_t
)hand
;
1323 /*********************************************************************
1324 * _isatty (MSVCRT.@)
1326 int CDECL
_isatty(int fd
)
1328 HANDLE hand
= msvcrt_fdtoh(fd
);
1330 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1331 if (hand
== INVALID_HANDLE_VALUE
)
1334 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1337 /*********************************************************************
1338 * _mktemp (MSVCRT.@)
1340 char * CDECL
_mktemp(char *pattern
)
1343 char *retVal
= pattern
;
1348 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1352 id
= GetCurrentProcessId();
1356 int tempNum
= id
/ 10;
1357 *pattern
-- = id
- (tempNum
* 10) + '0';
1363 *pattern
= letter
++;
1364 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1365 GetLastError() == ERROR_FILE_NOT_FOUND
)
1367 } while(letter
<= 'z');
1371 /*********************************************************************
1372 * _wmktemp (MSVCRT.@)
1374 MSVCRT_wchar_t
* CDECL
_wmktemp(MSVCRT_wchar_t
*pattern
)
1377 MSVCRT_wchar_t
*retVal
= pattern
;
1379 MSVCRT_wchar_t letter
= 'a';
1382 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1386 id
= GetCurrentProcessId();
1390 int tempNum
= id
/ 10;
1391 *pattern
-- = id
- (tempNum
* 10) + '0';
1397 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1398 GetLastError() == ERROR_FILE_NOT_FOUND
)
1400 *pattern
= letter
++;
1401 } while(letter
!= '|');
1405 static unsigned split_oflags(unsigned oflags
)
1408 unsigned unsupp
; /* until we support everything */
1410 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1411 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1412 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1413 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1414 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1415 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1417 if ((unsupp
= oflags
& ~(
1418 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1419 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1420 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1421 MSVCRT__O_NOINHERIT
|
1422 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1424 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1429 /*********************************************************************
1432 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1435 SECURITY_ATTRIBUTES sa
;
1436 HANDLE readHandle
, writeHandle
;
1440 *MSVCRT__errno() = MSVCRT_EINVAL
;
1444 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1445 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1446 sa
.lpSecurityDescriptor
= NULL
;
1447 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1449 unsigned int wxflags
= split_oflags(textmode
);
1453 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1457 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1465 MSVCRT__close(pfds
[0]);
1466 CloseHandle(writeHandle
);
1467 *MSVCRT__errno() = MSVCRT_EMFILE
;
1472 CloseHandle(readHandle
);
1473 CloseHandle(writeHandle
);
1474 *MSVCRT__errno() = MSVCRT_EMFILE
;
1479 msvcrt_set_errno(GetLastError());
1484 /*********************************************************************
1487 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1491 DWORD access
= 0, creation
= 0, attrib
;
1495 SECURITY_ATTRIBUTES sa
;
1498 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1499 path
, oflags
, shflags
);
1501 wxflag
= split_oflags(oflags
);
1502 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1504 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1505 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1506 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1509 if (oflags
& MSVCRT__O_CREAT
)
1511 __ms_va_start(ap
, shflags
);
1512 pmode
= va_arg(ap
, int);
1515 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1516 FIXME(": pmode 0x%04x ignored\n", pmode
);
1518 WARN(": pmode 0x%04x ignored\n", pmode
);
1520 if (oflags
& MSVCRT__O_EXCL
)
1521 creation
= CREATE_NEW
;
1522 else if (oflags
& MSVCRT__O_TRUNC
)
1523 creation
= CREATE_ALWAYS
;
1525 creation
= OPEN_ALWAYS
;
1527 else /* no MSVCRT__O_CREAT */
1529 if (oflags
& MSVCRT__O_TRUNC
)
1530 creation
= TRUNCATE_EXISTING
;
1532 creation
= OPEN_EXISTING
;
1537 case MSVCRT__SH_DENYRW
:
1540 case MSVCRT__SH_DENYWR
:
1541 sharing
= FILE_SHARE_READ
;
1543 case MSVCRT__SH_DENYRD
:
1544 sharing
= FILE_SHARE_WRITE
;
1546 case MSVCRT__SH_DENYNO
:
1547 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1550 ERR( "Unhandled shflags 0x%x\n", shflags
);
1553 attrib
= FILE_ATTRIBUTE_NORMAL
;
1555 if (oflags
& MSVCRT__O_TEMPORARY
)
1557 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1559 sharing
|= FILE_SHARE_DELETE
;
1562 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1563 sa
.lpSecurityDescriptor
= NULL
;
1564 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1566 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1568 if (hand
== INVALID_HANDLE_VALUE
) {
1569 WARN(":failed-last error (%d)\n",GetLastError());
1570 msvcrt_set_errno(GetLastError());
1574 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1576 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1580 /*********************************************************************
1581 * _wsopen (MSVCRT.@)
1583 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1587 DWORD access
= 0, creation
= 0, attrib
;
1591 SECURITY_ATTRIBUTES sa
;
1594 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1595 debugstr_w(path
), oflags
, shflags
);
1597 wxflag
= split_oflags(oflags
);
1598 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1600 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1601 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1602 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1605 if (oflags
& MSVCRT__O_CREAT
)
1607 __ms_va_start(ap
, shflags
);
1608 pmode
= va_arg(ap
, int);
1611 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1612 FIXME(": pmode 0x%04x ignored\n", pmode
);
1614 WARN(": pmode 0x%04x ignored\n", pmode
);
1616 if (oflags
& MSVCRT__O_EXCL
)
1617 creation
= CREATE_NEW
;
1618 else if (oflags
& MSVCRT__O_TRUNC
)
1619 creation
= CREATE_ALWAYS
;
1621 creation
= OPEN_ALWAYS
;
1623 else /* no MSVCRT__O_CREAT */
1625 if (oflags
& MSVCRT__O_TRUNC
)
1626 creation
= TRUNCATE_EXISTING
;
1628 creation
= OPEN_EXISTING
;
1633 case MSVCRT__SH_DENYRW
:
1636 case MSVCRT__SH_DENYWR
:
1637 sharing
= FILE_SHARE_READ
;
1639 case MSVCRT__SH_DENYRD
:
1640 sharing
= FILE_SHARE_WRITE
;
1642 case MSVCRT__SH_DENYNO
:
1643 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1646 ERR( "Unhandled shflags 0x%x\n", shflags
);
1649 attrib
= FILE_ATTRIBUTE_NORMAL
;
1651 if (oflags
& MSVCRT__O_TEMPORARY
)
1653 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1655 sharing
|= FILE_SHARE_DELETE
;
1658 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1659 sa
.lpSecurityDescriptor
= NULL
;
1660 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1662 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1664 if (hand
== INVALID_HANDLE_VALUE
) {
1665 WARN(":failed-last error (%d)\n",GetLastError());
1666 msvcrt_set_errno(GetLastError());
1670 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1672 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1676 /*********************************************************************
1679 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1683 if (flags
& MSVCRT__O_CREAT
)
1686 __ms_va_start(ap
, flags
);
1687 pmode
= va_arg(ap
, int);
1689 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1692 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1695 /*********************************************************************
1698 int CDECL
_wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1702 if (flags
& MSVCRT__O_CREAT
)
1705 __ms_va_start(ap
, flags
);
1706 pmode
= va_arg(ap
, int);
1708 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1711 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1714 /*********************************************************************
1717 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1719 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1720 return MSVCRT__open(path
, usedFlags
);
1723 /*********************************************************************
1724 * _wcreat (MSVCRT.@)
1726 int CDECL
_wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1728 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1729 return _wopen(path
, usedFlags
);
1732 /*********************************************************************
1733 * _open_osfhandle (MSVCRT.@)
1735 int CDECL
_open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1739 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1740 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1741 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1742 * text - it never sets MSVCRT__O_BINARY.
1744 /* don't let split_oflags() decide the mode if no mode is passed */
1745 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1746 oflags
|= MSVCRT__O_BINARY
;
1748 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1749 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1753 /*********************************************************************
1756 int CDECL
_rmtmp(void)
1758 int num_removed
= 0, i
;
1761 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1762 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1764 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1770 TRACE(":removed (%d) temp files\n",num_removed
);
1774 /*********************************************************************
1777 * When reading \r as last character in text mode, read() positions
1778 * the file pointer on the \r character while getc() goes on to
1781 static int read_i(int fd
, void *buf
, unsigned int count
)
1784 char *bufstart
= buf
;
1785 HANDLE hand
= msvcrt_fdtoh(fd
);
1790 if (MSVCRT_fdesc
[fd
].wxflag
& WX_READEOF
) {
1791 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1792 TRACE("already at EOF, returning 0\n");
1795 /* Don't trace small reads, it gets *very* annoying */
1797 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1798 if (hand
== INVALID_HANDLE_VALUE
)
1801 /* Reading single bytes in O_TEXT mode makes things slow
1802 * So read big chunks
1804 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1806 if (count
!= 0 && num_read
== 0)
1808 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1809 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1811 else if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1814 if (bufstart
[num_read
-1] == '\r')
1818 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_READCR
;
1819 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1823 MSVCRT_fdesc
[fd
].wxflag
|= WX_READCR
;
1828 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_READCR
;
1829 for (i
=0, j
=0; i
<num_read
; i
++)
1831 /* in text mode, a ctrl-z signals EOF */
1832 if (bufstart
[i
] == 0x1a)
1834 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1835 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1838 /* in text mode, strip \r if followed by \n.
1839 * BUG: should save state across calls somehow, so CR LF that
1840 * straddles buffer boundary gets recognized properly?
1842 if ((bufstart
[i
] != '\r')
1843 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1844 bufstart
[j
++] = bufstart
[i
];
1851 if (GetLastError() == ERROR_BROKEN_PIPE
)
1853 TRACE(":end-of-pipe\n");
1854 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1859 TRACE(":failed-last error (%d)\n",GetLastError());
1865 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1869 /*********************************************************************
1872 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
1875 num_read
= read_i(fd
, buf
, count
);
1879 /*********************************************************************
1880 * _setmode (MSVCRT.@)
1882 int CDECL
_setmode(int fd
,int mode
)
1884 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1885 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1886 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1887 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1888 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1890 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1894 /*********************************************************************
1895 * _stat64 (MSVCRT.@)
1897 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
1900 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1901 unsigned short mode
= ALL_S_IREAD
;
1904 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1906 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1908 TRACE("failed (%d)\n",GetLastError());
1909 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1913 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1915 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1916 Bon 011120: This FIXME seems incorrect
1917 Also a letter as first char isn't enough to be classified
1920 if (isalpha(*path
)&& (*(path
+1)==':'))
1921 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1923 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1925 plen
= strlen(path
);
1927 /* Dir, or regular file? */
1928 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1929 (path
[plen
-1] == '\\'))
1930 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1933 mode
|= MSVCRT__S_IFREG
;
1935 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1937 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1938 (tolower(path
[plen
-3]) << 16);
1939 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1940 mode
|= ALL_S_IEXEC
;
1944 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1945 mode
|= ALL_S_IWRITE
;
1947 buf
->st_mode
= mode
;
1949 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1950 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1952 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1953 buf
->st_mtime
= buf
->st_ctime
= dw
;
1954 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
1955 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
1956 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
1960 /*********************************************************************
1961 * _stati64 (MSVCRT.@)
1963 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1966 struct MSVCRT__stat64 buf64
;
1968 ret
= MSVCRT_stat64(path
, &buf64
);
1970 msvcrt_stat64_to_stati64(&buf64
, buf
);
1974 /*********************************************************************
1977 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
1979 struct MSVCRT__stat64 buf64
;
1981 ret
= MSVCRT_stat64( path
, &buf64
);
1983 msvcrt_stat64_to_stat(&buf64
, buf
);
1987 /*********************************************************************
1988 * _wstat64 (MSVCRT.@)
1990 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
1993 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1994 unsigned short mode
= ALL_S_IREAD
;
1997 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1999 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2001 TRACE("failed (%d)\n",GetLastError());
2002 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
2006 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2008 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2009 if (MSVCRT_iswalpha(*path
))
2010 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2012 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
2014 plen
= strlenW(path
);
2016 /* Dir, or regular file? */
2017 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
2018 (path
[plen
-1] == '\\'))
2019 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2022 mode
|= MSVCRT__S_IFREG
;
2024 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2026 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2027 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2028 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2029 mode
|= ALL_S_IEXEC
;
2033 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2034 mode
|= ALL_S_IWRITE
;
2036 buf
->st_mode
= mode
;
2038 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2039 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2041 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2042 buf
->st_mtime
= buf
->st_ctime
= dw
;
2043 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2044 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2045 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2049 /*********************************************************************
2050 * _wstati64 (MSVCRT.@)
2052 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2055 struct MSVCRT__stat64 buf64
;
2057 ret
= MSVCRT__wstat64(path
, &buf64
);
2059 msvcrt_stat64_to_stati64(&buf64
, buf
);
2063 /*********************************************************************
2066 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2069 struct MSVCRT__stat64 buf64
;
2071 ret
= MSVCRT__wstat64( path
, &buf64
);
2072 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2076 /*********************************************************************
2079 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
2081 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2084 /*********************************************************************
2085 * _telli64 (MSVCRT.@)
2087 __int64 CDECL
_telli64(int fd
)
2089 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2092 /*********************************************************************
2093 * _tempnam (MSVCRT.@)
2095 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2097 char tmpbuf
[MAX_PATH
];
2098 const char *tmp_dir
= MSVCRT_getenv("TMP");
2100 if (tmp_dir
) dir
= tmp_dir
;
2102 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2103 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2105 TRACE("got name (%s)\n",tmpbuf
);
2106 DeleteFileA(tmpbuf
);
2107 return _strdup(tmpbuf
);
2109 TRACE("failed (%d)\n",GetLastError());
2113 /*********************************************************************
2114 * _wtempnam (MSVCRT.@)
2116 MSVCRT_wchar_t
* CDECL
_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2118 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2120 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2121 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2123 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2124 DeleteFileW(tmpbuf
);
2125 return _wcsdup(tmpbuf
);
2127 TRACE("failed (%d)\n",GetLastError());
2131 /*********************************************************************
2134 int CDECL
MSVCRT__umask(int umask
)
2136 int old_umask
= MSVCRT_umask
;
2137 TRACE("(%d)\n",umask
);
2138 MSVCRT_umask
= umask
;
2142 /*********************************************************************
2143 * _utime64 (MSVCRT.@)
2145 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2147 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2151 int retVal
= _futime64(fd
, t
);
2158 /*********************************************************************
2159 * _utime32 (MSVCRT.@)
2161 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2163 struct MSVCRT___utimbuf64 t64
;
2164 t64
.actime
= t
->actime
;
2165 t64
.modtime
= t
->modtime
;
2166 return _utime64( path
, &t64
);
2169 /*********************************************************************
2173 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2175 return _utime64( path
, t
);
2178 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2180 return _utime32( path
, t
);
2184 /*********************************************************************
2185 * _wutime64 (MSVCRT.@)
2187 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2189 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2193 int retVal
= _futime64(fd
, t
);
2200 /*********************************************************************
2201 * _wutime32 (MSVCRT.@)
2203 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2205 struct MSVCRT___utimbuf64 t64
;
2206 t64
.actime
= t
->actime
;
2207 t64
.modtime
= t
->modtime
;
2208 return _wutime64( path
, &t64
);
2211 /*********************************************************************
2212 * _wutime (MSVCRT.@)
2215 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2217 return _wutime64( path
, t
);
2220 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2222 return _wutime32( path
, t
);
2226 /*********************************************************************
2229 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2232 HANDLE hand
= msvcrt_fdtoh(fd
);
2234 /* Don't trace small writes, it gets *very* annoying */
2237 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2239 if (hand
== INVALID_HANDLE_VALUE
)
2241 *MSVCRT__errno() = MSVCRT_EBADF
;
2245 /* If appending, go to EOF */
2246 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
2247 MSVCRT__lseek(fd
, 0, FILE_END
);
2249 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
2251 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2252 && (num_written
== count
))
2254 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2255 hand
, GetLastError());
2256 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2260 unsigned int i
, j
, nr_lf
;
2263 const char *s
= buf
, *buf_start
= buf
;
2264 /* find number of \n ( without preceding \r ) */
2265 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2270 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2275 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2277 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2282 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2289 FIXME("Malloc failed\n");
2297 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2299 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2300 fd
, hand
, GetLastError(), num_written
);
2301 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2304 return s
- buf_start
;
2316 /*********************************************************************
2319 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2322 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2323 if (len
== sizeof(val
)) return val
;
2324 file
->_flag
|= MSVCRT__IOERR
;
2328 /*********************************************************************
2331 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2336 MSVCRT_free(file
->_tmpfname
);
2337 file
->_tmpfname
= NULL
;
2338 /* flush stdio buffers */
2339 if(file
->_flag
& MSVCRT__IOWRT
)
2340 MSVCRT_fflush(file
);
2341 if(file
->_flag
& MSVCRT__IOMYBUF
)
2342 MSVCRT_free(file
->_base
);
2344 r
=MSVCRT__close(file
->_file
);
2348 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2351 /*********************************************************************
2354 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2356 return file
->_flag
& MSVCRT__IOEOF
;
2359 /*********************************************************************
2362 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2364 return file
->_flag
& MSVCRT__IOERR
;
2367 /*********************************************************************
2368 * _filbuf (MSVCRT.@)
2370 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2372 /* Allocate buffer if needed */
2373 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2374 msvcrt_alloc_buffer(file
);
2376 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2377 if(file
->_flag
& MSVCRT__IORW
) {
2378 file
->_flag
|= MSVCRT__IOREAD
;
2383 if(file
->_flag
& MSVCRT__IONBF
) {
2386 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2387 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2392 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2394 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2399 file
->_ptr
= file
->_base
+1;
2400 return *(unsigned char *)file
->_base
;
2404 /*********************************************************************
2407 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2413 i
= (unsigned char *)file
->_ptr
++;
2416 j
= MSVCRT__filbuf(file
);
2420 /*********************************************************************
2421 * _fgetchar (MSVCRT.@)
2423 int CDECL
_fgetchar(void)
2425 return MSVCRT_fgetc(MSVCRT_stdin
);
2428 /*********************************************************************
2431 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2433 int cc
= MSVCRT_EOF
;
2434 char * buf_start
= s
;
2436 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2437 file
,file
->_file
,s
,size
);
2439 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2444 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2446 TRACE(":nothing read\n");
2449 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2452 TRACE(":got %s\n", debugstr_a(buf_start
));
2456 /*********************************************************************
2459 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2460 * the CR from CR/LF combinations
2462 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2466 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2473 for(i
=0; i
<sizeof(wc
); i
++)
2483 j
= MSVCRT__filbuf(file
);
2486 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2496 c
= MSVCRT_fgetc(file
);
2497 if ((get_locale()->locinfo
->mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2499 FIXME("Treat Multibyte characters\n");
2501 if (c
== MSVCRT_EOF
)
2504 return (MSVCRT_wint_t
)c
;
2507 /*********************************************************************
2510 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2516 for (j
=0; j
<sizeof(int); j
++) {
2517 k
= MSVCRT_fgetc(file
);
2518 if (k
== MSVCRT_EOF
) {
2519 file
->_flag
|= MSVCRT__IOEOF
;
2527 /*********************************************************************
2530 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2532 return MSVCRT_fgetwc(file
);
2535 /*********************************************************************
2536 * _fgetwchar (MSVCRT.@)
2538 MSVCRT_wint_t CDECL
_fgetwchar(void)
2540 return MSVCRT_fgetwc(MSVCRT_stdin
);
2543 /*********************************************************************
2544 * getwchar (MSVCRT.@)
2546 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2548 return _fgetwchar();
2551 /*********************************************************************
2554 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2556 int cc
= MSVCRT_WEOF
;
2557 MSVCRT_wchar_t
* buf_start
= s
;
2559 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2560 file
,file
->_file
,s
,size
);
2562 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2567 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2569 TRACE(":nothing read\n");
2572 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2575 TRACE(":got %s\n", debugstr_w(buf_start
));
2579 /*********************************************************************
2582 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2584 MSVCRT_size_t wrcnt
=size
* nmemb
;
2589 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2590 memcpy(file
->_ptr
, ptr
, pcnt
);
2595 ptr
= (const char*)ptr
+ pcnt
;
2596 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2597 if(file
->_flag
& MSVCRT__IORW
) {
2598 file
->_flag
|= MSVCRT__IOWRT
;
2604 int res
=msvcrt_flush_buffer(file
);
2606 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2609 file
->_flag
|= MSVCRT__IOERR
;
2612 written
+= pwritten
;
2615 return written
/ size
;
2618 /*********************************************************************
2621 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2623 MSVCRT_wchar_t mwc
=wc
;
2624 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2629 /*********************************************************************
2630 * _fputwchar (MSVCRT.@)
2632 MSVCRT_wint_t CDECL
_fputwchar(MSVCRT_wint_t wc
)
2634 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2637 /*********************************************************************
2638 * _wfsopen (MSVCRT.@)
2640 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2643 int open_flags
, stream_flags
, fd
;
2645 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2647 /* map mode string to open() flags. "man fopen" for possibilities. */
2648 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2652 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2655 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2657 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2664 TRACE(":got (%p)\n",file
);
2665 if (fd
>= 0 && !file
)
2671 /*********************************************************************
2672 * _fsopen (MSVCRT.@)
2674 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2677 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2679 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2680 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2681 *MSVCRT__errno() = MSVCRT_EINVAL
;
2684 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2687 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2688 *MSVCRT__errno() = MSVCRT_EINVAL
;
2692 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
2699 /*********************************************************************
2702 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
2704 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2707 /*********************************************************************
2708 * fopen_s (MSVCRT.@)
2710 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
2711 const char *filename
, const char *mode
)
2713 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2714 MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2715 *MSVCRT__errno() = MSVCRT_EINVAL
;
2716 return MSVCRT_EINVAL
;
2719 *pFile
= MSVCRT_fopen(filename
, mode
);
2722 return *MSVCRT__errno();
2726 /*********************************************************************
2727 * _wfopen (MSVCRT.@)
2729 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2731 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2734 /*********************************************************************
2735 * _wfopen_s (MSVCRT.@)
2737 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
2738 const MSVCRT_wchar_t
*mode
)
2740 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2741 MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2742 *MSVCRT__errno() = MSVCRT_EINVAL
;
2743 return MSVCRT_EINVAL
;
2746 *pFile
= MSVCRT__wfopen(filename
, mode
);
2749 return *MSVCRT__errno();
2753 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2754 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2756 /*********************************************************************
2759 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2766 int res
= msvcrt_flush_buffer(file
);
2767 return res
? res
: c
;
2772 return MSVCRT__flsbuf(c
, file
);
2776 /*********************************************************************
2777 * _flsbuf (MSVCRT.@)
2779 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2781 /* Flush output buffer */
2782 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2783 msvcrt_alloc_buffer(file
);
2785 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2786 if(file
->_flag
& MSVCRT__IORW
) {
2787 file
->_flag
|= MSVCRT__IOWRT
;
2793 int res
=msvcrt_flush_buffer(file
);
2794 return res
?res
: MSVCRT_fputc(c
, file
);
2798 /* set _cnt to 0 for unbuffered FILEs */
2800 len
= MSVCRT__write(file
->_file
, &cc
, 1);
2801 if (len
== 1) return c
& 0xff;
2802 file
->_flag
|= MSVCRT__IOERR
;
2807 /*********************************************************************
2808 * _fputchar (MSVCRT.@)
2810 int CDECL
_fputchar(int c
)
2812 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2815 /*********************************************************************
2818 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2819 { MSVCRT_size_t rcnt
=size
* nmemb
;
2820 MSVCRT_size_t read
=0;
2826 /* first buffered data */
2828 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2829 memcpy(ptr
, file
->_ptr
, pcnt
);
2834 ptr
= (char*)ptr
+ pcnt
;
2835 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2836 if(file
->_flag
& MSVCRT__IORW
) {
2837 file
->_flag
|= MSVCRT__IOREAD
;
2844 /* Fill the buffer on small reads.
2845 * TODO: Use a better buffering strategy.
2847 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
2848 if (file
->_bufsiz
== 0) {
2849 msvcrt_alloc_buffer(file
);
2851 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
2852 file
->_ptr
= file
->_base
;
2853 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2854 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2855 if (i
> 0 && i
< file
->_cnt
) {
2856 MSVCRT_fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2857 file
->_flag
&= ~MSVCRT__IOEOF
;
2860 memcpy(ptr
, file
->_ptr
, i
);
2865 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
2869 ptr
= (char *)ptr
+i
;
2870 /* expose feof condition in the flags
2871 * MFC tests file->_flag for feof, and doesn't call feof())
2873 if ( MSVCRT_fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2874 file
->_flag
|= MSVCRT__IOEOF
;
2877 file
->_flag
|= MSVCRT__IOERR
;
2887 /*********************************************************************
2888 * _wfreopen (MSVCRT.@)
2891 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
2893 int open_flags
, stream_flags
, fd
;
2895 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2898 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2902 MSVCRT_fclose(file
);
2903 /* map mode string to open() flags. "man fopen" for possibilities. */
2904 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2908 fd
= _wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2911 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2914 WARN(":failed-last error (%d)\n",GetLastError());
2915 msvcrt_set_errno(GetLastError());
2924 /*********************************************************************
2925 * freopen (MSVCRT.@)
2928 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
2931 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2933 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2934 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2940 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
2947 /*********************************************************************
2948 * fsetpos (MSVCRT.@)
2950 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2952 /* Note that all this has been lifted 'as is' from fseek */
2953 if(file
->_flag
& MSVCRT__IOWRT
)
2954 msvcrt_flush_buffer(file
);
2956 /* Discard buffered input */
2958 file
->_ptr
= file
->_base
;
2960 /* Reset direction of i/o */
2961 if(file
->_flag
& MSVCRT__IORW
) {
2962 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2965 return (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2968 /*********************************************************************
2971 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
2973 /* TODO: just call fgetpos and return lower half of result */
2976 pos
= MSVCRT__tell(file
->_file
);
2977 if(pos
== -1) return -1;
2979 if( file
->_flag
& MSVCRT__IOWRT
) {
2980 off
= file
->_ptr
- file
->_base
;
2983 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2984 /* Black magic correction for CR removal */
2986 for (i
=0; i
<file
->_cnt
; i
++) {
2987 if (file
->_ptr
[i
] == '\n')
2990 /* Black magic when reading CR at buffer boundary*/
2991 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
3000 /*********************************************************************
3001 * fgetpos (MSVCRT.@)
3003 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3006 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
3007 if(*pos
== -1) return -1;
3009 if( file
->_flag
& MSVCRT__IOWRT
) {
3010 off
= file
->_ptr
- file
->_base
;
3013 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
3014 /* Black magic correction for CR removal */
3016 for (i
=0; i
<file
->_cnt
; i
++) {
3017 if (file
->_ptr
[i
] == '\n')
3020 /* Black magic when reading CR at buffer boundary*/
3021 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
3030 /*********************************************************************
3033 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
3035 MSVCRT_size_t i
, len
= strlen(s
);
3036 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
3037 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3038 for (i
=0; i
<len
; i
++)
3039 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
3044 /*********************************************************************
3047 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
3049 MSVCRT_size_t i
, len
= strlenW(s
);
3050 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
3051 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3052 for (i
=0; i
<len
; i
++)
3054 if ((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
3056 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
3062 /*********************************************************************
3063 * getchar (MSVCRT.@)
3065 int CDECL
MSVCRT_getchar(void)
3067 return MSVCRT_fgetc(MSVCRT_stdin
);
3070 /*********************************************************************
3073 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
3075 return MSVCRT_fgetc(file
);
3078 /*********************************************************************
3081 char * CDECL
MSVCRT_gets(char *buf
)
3084 char * buf_start
= buf
;
3086 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
3087 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
3088 if(cc
!= '\r') *buf
++ = (char)cc
;
3092 TRACE("got '%s'\n", buf_start
);
3096 /*********************************************************************
3099 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
3102 MSVCRT_wchar_t
* ws
= buf
;
3104 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
3105 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3108 *buf
++ = (MSVCRT_wchar_t
)cc
;
3112 TRACE("got %s\n", debugstr_w(ws
));
3116 /*********************************************************************
3119 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3121 return MSVCRT_fputc(c
, file
);
3124 /*********************************************************************
3125 * putchar (MSVCRT.@)
3127 int CDECL
MSVCRT_putchar(int c
)
3129 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3132 /*********************************************************************
3133 * _putwch (MSVCRT.@)
3135 int CDECL
MSVCRT__putwch(int c
)
3137 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
3140 /*********************************************************************
3143 int CDECL
MSVCRT_puts(const char *s
)
3145 MSVCRT_size_t len
= strlen(s
);
3146 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3147 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3150 /*********************************************************************
3153 int CDECL
_putws(const MSVCRT_wchar_t
*s
)
3155 static const MSVCRT_wchar_t nl
= '\n';
3156 MSVCRT_size_t len
= strlenW(s
);
3157 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3158 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3161 /*********************************************************************
3164 int CDECL
MSVCRT_remove(const char *path
)
3166 TRACE("(%s)\n",path
);
3167 if (DeleteFileA(path
))
3169 TRACE(":failed (%d)\n",GetLastError());
3170 msvcrt_set_errno(GetLastError());
3174 /*********************************************************************
3175 * _wremove (MSVCRT.@)
3177 int CDECL
_wremove(const MSVCRT_wchar_t
*path
)
3179 TRACE("(%s)\n",debugstr_w(path
));
3180 if (DeleteFileW(path
))
3182 TRACE(":failed (%d)\n",GetLastError());
3183 msvcrt_set_errno(GetLastError());
3187 /*********************************************************************
3190 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3192 TRACE(":from %s to %s\n",oldpath
,newpath
);
3193 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3195 TRACE(":failed (%d)\n",GetLastError());
3196 msvcrt_set_errno(GetLastError());
3200 /*********************************************************************
3201 * _wrename (MSVCRT.@)
3203 int CDECL
_wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3205 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3206 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3208 TRACE(":failed (%d)\n",GetLastError());
3209 msvcrt_set_errno(GetLastError());
3213 /*********************************************************************
3214 * setvbuf (MSVCRT.@)
3216 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3218 /* TODO: Check if file busy */
3220 MSVCRT_free(file
->_base
);
3224 if(mode
== MSVCRT__IOFBF
) {
3225 file
->_flag
&= ~MSVCRT__IONBF
;
3226 file
->_base
= file
->_ptr
= buf
;
3228 file
->_bufsiz
= size
;
3231 file
->_flag
|= MSVCRT__IONBF
;
3236 /*********************************************************************
3239 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3241 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3244 /*********************************************************************
3247 char * CDECL
MSVCRT_tmpnam(char *s
)
3255 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3256 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3257 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3259 msvcrt_int_to_base32(unique
++, tmpstr
);
3261 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3262 GetLastError() == ERROR_FILE_NOT_FOUND
)
3268 /*********************************************************************
3269 * tmpfile (MSVCRT.@)
3271 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3273 char *filename
= MSVCRT_tmpnam(NULL
);
3275 MSVCRT_FILE
* file
= NULL
;
3278 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3279 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3281 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3286 else file
->_tmpfname
= _strdup(filename
);
3292 /*********************************************************************
3293 * vfprintf (MSVCRT.@)
3295 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3297 char buf
[2048], *mem
= buf
;
3298 int written
, resize
= sizeof(buf
), retval
;
3299 /* There are two conventions for vsnprintf failing:
3300 * Return -1 if we truncated, or
3301 * Return the number of bytes that would have been written
3302 * The code below handles both cases
3304 while ((written
= MSVCRT_vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3307 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3310 if (!(mem
= MSVCRT_malloc(resize
)))
3313 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3319 /*********************************************************************
3320 * vfwprintf (MSVCRT.@)
3322 * Is final char included in written (then resize is too big) or not
3323 * (then we must test for equality too)?
3325 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3327 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
3328 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
3329 /* See vfprintf comments */
3330 while ((written
= MSVCRT_vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3333 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
3336 if (!(mem
= MSVCRT_malloc(resize
*sizeof(*mem
))))
3339 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3345 /*********************************************************************
3346 * vprintf (MSVCRT.@)
3348 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3350 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3353 /*********************************************************************
3354 * vwprintf (MSVCRT.@)
3356 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3358 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3361 /*********************************************************************
3362 * fprintf (MSVCRT.@)
3364 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3366 __ms_va_list valist
;
3368 __ms_va_start(valist
, format
);
3369 res
= MSVCRT_vfprintf(file
, format
, valist
);
3370 __ms_va_end(valist
);
3374 /*********************************************************************
3375 * fwprintf (MSVCRT.@)
3377 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3379 __ms_va_list valist
;
3381 __ms_va_start(valist
, format
);
3382 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3383 __ms_va_end(valist
);
3387 /*********************************************************************
3390 int CDECL
MSVCRT_printf(const char *format
, ...)
3392 __ms_va_list valist
;
3394 __ms_va_start(valist
, format
);
3395 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3396 __ms_va_end(valist
);
3400 /*********************************************************************
3403 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3405 if (c
== MSVCRT_EOF
)
3407 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3408 msvcrt_alloc_buffer(file
);
3411 if(file
->_ptr
>file
->_base
) {
3415 MSVCRT_clearerr(file
);
3421 /*********************************************************************
3422 * ungetwc (MSVCRT.@)
3424 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3426 MSVCRT_wchar_t mwc
= wc
;
3427 char * pp
= (char *)&mwc
;
3429 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3430 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
3436 /*********************************************************************
3437 * wprintf (MSVCRT.@)
3439 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
3441 __ms_va_list valist
;
3443 __ms_va_start(valist
, format
);
3444 res
= MSVCRT_vwprintf(format
, valist
);
3445 __ms_va_end(valist
);
3449 /*********************************************************************
3450 * _getmaxstdio (MSVCRT.@)
3452 int CDECL
_getmaxstdio(void)
3454 FIXME("stub, always returns 512\n");
3458 /*********************************************************************
3459 * _setmaxstdio_ (MSVCRT.@)
3461 int CDECL
_setmaxstdio(int newmax
)
3468 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3472 /*********************************************************************
3473 * __pioinfo (MSVCRT.@)
3474 * FIXME: see MSVCRT_MAX_FILES define.
3476 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3477 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
3478 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
3479 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
3480 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
3481 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
3482 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
3483 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
3484 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
3485 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
3486 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
3487 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
3490 /*********************************************************************
3491 * __badioinfo (MSVCRT.@)
3493 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};