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
58 #define MSVCRT_R_OK 0x04
60 /* values for wxflag in file descriptor */
63 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
64 #define WX_READCR 0x08 /* underlying file is at \r */
65 #define WX_DONTINHERIT 0x10
66 #define WX_APPEND 0x20
69 /* FIXME: this should be allocated dynamically */
70 #define MSVCRT_MAX_FILES 2048
75 DWORD unkn
[7]; /* critical section and init flag */
78 static ioinfo MSVCRT_fdesc
[MSVCRT_MAX_FILES
];
80 MSVCRT_FILE MSVCRT__iob
[3] = { { 0 } };
82 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
83 static int MSVCRT_fdend
= 3; /* highest allocated fd */
85 static MSVCRT_FILE
* MSVCRT_fstreams
[2048];
86 static int MSVCRT_stream_idx
;
88 /* INTERNAL: process umask */
89 static int MSVCRT_umask
= 0;
91 /* INTERNAL: Static buffer for temp file name */
92 static char MSVCRT_tmpname
[MAX_PATH
];
94 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
95 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
96 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
97 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
99 #define TOUL(x) (ULONGLONG)(x)
100 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
101 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
102 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
103 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
105 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
106 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
107 * and MSVCRT_stream_idx, from race conditions.
108 * It doesn't protect against race conditions manipulating the underlying files
109 * or flags; doing so would probably be better accomplished with per-file
110 * protection, rather than locking the whole table for every change.
112 static CRITICAL_SECTION MSVCRT_file_cs
;
113 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
114 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
116 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stat
*buf
)
118 buf
->st_dev
= buf64
->st_dev
;
119 buf
->st_ino
= buf64
->st_ino
;
120 buf
->st_mode
= buf64
->st_mode
;
121 buf
->st_nlink
= buf64
->st_nlink
;
122 buf
->st_uid
= buf64
->st_uid
;
123 buf
->st_gid
= buf64
->st_gid
;
124 buf
->st_rdev
= buf64
->st_rdev
;
125 buf
->st_size
= buf64
->st_size
;
126 buf
->st_atime
= buf64
->st_atime
;
127 buf
->st_mtime
= buf64
->st_mtime
;
128 buf
->st_ctime
= buf64
->st_ctime
;
131 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64
*buf64
, struct MSVCRT__stati64
*buf
)
133 buf
->st_dev
= buf64
->st_dev
;
134 buf
->st_ino
= buf64
->st_ino
;
135 buf
->st_mode
= buf64
->st_mode
;
136 buf
->st_nlink
= buf64
->st_nlink
;
137 buf
->st_uid
= buf64
->st_uid
;
138 buf
->st_gid
= buf64
->st_gid
;
139 buf
->st_rdev
= buf64
->st_rdev
;
140 buf
->st_size
= buf64
->st_size
;
141 buf
->st_atime
= buf64
->st_atime
;
142 buf
->st_mtime
= buf64
->st_mtime
;
143 buf
->st_ctime
= buf64
->st_ctime
;
146 static void time_to_filetime( MSVCRT___time64_t time
, FILETIME
*ft
)
148 /* 1601 to 1970 is 369 years plus 89 leap days */
149 static const __int64 secs_1601_to_1970
= ((369 * 365 + 89) * (__int64
)86400);
151 __int64 ticks
= (time
+ secs_1601_to_1970
) * 10000000;
152 ft
->dwHighDateTime
= ticks
>> 32;
153 ft
->dwLowDateTime
= ticks
;
156 static inline BOOL
msvcrt_is_valid_fd(int fd
)
158 return fd
>= 0 && fd
< MSVCRT_fdend
&& (MSVCRT_fdesc
[fd
].wxflag
& WX_OPEN
);
161 /* INTERNAL: Get the HANDLE for a fd
162 * This doesn't lock the table, because a failure will result in
163 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
164 * it returns a valid handle which is about to be closed, a subsequent call
165 * will fail, most likely in a sane way.
167 static HANDLE
msvcrt_fdtoh(int fd
)
169 if (!msvcrt_is_valid_fd(fd
))
171 WARN(":fd (%d) - no handle!\n",fd
);
172 *MSVCRT___doserrno() = 0;
173 *MSVCRT__errno() = MSVCRT_EBADF
;
174 return INVALID_HANDLE_VALUE
;
176 if (MSVCRT_fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
177 return MSVCRT_fdesc
[fd
].handle
;
180 /* INTERNAL: free a file entry fd */
181 static void msvcrt_free_fd(int fd
)
186 old_handle
= MSVCRT_fdesc
[fd
].handle
;
187 MSVCRT_fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
188 MSVCRT_fdesc
[fd
].wxflag
= 0;
189 TRACE(":fd (%d) freed\n",fd
);
190 if (fd
< 3) /* don't use 0,1,2 for user files */
195 if (GetStdHandle(STD_INPUT_HANDLE
) == old_handle
) SetStdHandle(STD_INPUT_HANDLE
, 0);
198 if (GetStdHandle(STD_OUTPUT_HANDLE
) == old_handle
) SetStdHandle(STD_OUTPUT_HANDLE
, 0);
201 if (GetStdHandle(STD_ERROR_HANDLE
) == old_handle
) SetStdHandle(STD_ERROR_HANDLE
, 0);
207 if (fd
== MSVCRT_fdend
- 1)
209 if (fd
< MSVCRT_fdstart
)
215 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
216 /* caller must hold the files lock */
217 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
219 if (fd
>= MSVCRT_MAX_FILES
)
221 WARN(":files exhausted!\n");
222 *MSVCRT__errno() = MSVCRT_ENFILE
;
225 MSVCRT_fdesc
[fd
].handle
= hand
;
226 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
228 /* locate next free slot */
229 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
230 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
232 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
233 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
235 /* update last fd in use */
236 if (fd
>= MSVCRT_fdend
)
237 MSVCRT_fdend
= fd
+ 1;
238 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
242 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
243 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
244 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
250 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
251 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
256 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
257 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
262 /* INTERNAL: Allocate a FILE* for an fd slot */
263 /* caller must hold the files lock */
264 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
268 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
270 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
272 if (!MSVCRT_fstreams
[i
])
274 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
276 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
278 return MSVCRT_fstreams
[i
];
284 /* INTERNAL: initialize a FILE* from an open fd */
285 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
287 TRACE(":fd (%d) allocating FILE*\n",fd
);
288 if (!msvcrt_is_valid_fd(fd
))
290 WARN(":invalid fd %d\n",fd
);
291 *MSVCRT___doserrno() = 0;
292 *MSVCRT__errno() = MSVCRT_EBADF
;
295 memset(file
, 0, sizeof(*file
));
297 file
->_flag
= stream_flags
;
299 TRACE(":got FILE* (%p)\n",file
);
303 /* INTERNAL: Create an inheritance data block (for spawned process)
304 * The inheritance block is made of:
305 * 00 int nb of file descriptor (NBFD)
306 * 04 char file flags (wxflag): repeated for each fd
307 * 4+NBFD HANDLE file handle: repeated for each fd
309 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
315 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
316 *block
= MSVCRT_calloc(*size
, 1);
322 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
323 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
325 *(unsigned*)*block
= MSVCRT_fdend
;
326 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
328 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
329 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
331 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
332 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
337 *handle_ptr
= INVALID_HANDLE_VALUE
;
339 wxflag_ptr
++; handle_ptr
++;
344 /* INTERNAL: Set up all file descriptors,
345 * as well as default streams (stdin, stderr and stdout)
347 void msvcrt_init_io(void)
352 InitializeCriticalSection(&MSVCRT_file_cs
);
353 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs");
354 GetStartupInfoA(&si
);
355 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
361 count
= *(unsigned*)si
.lpReserved2
;
362 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
363 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
365 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
366 count
= min(count
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
367 for (i
= 0; i
< count
; i
++)
369 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
371 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
372 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
376 MSVCRT_fdesc
[i
].wxflag
= 0;
377 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
379 wxflag_ptr
++; handle_ptr
++;
381 MSVCRT_fdend
= max( 3, count
);
382 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
383 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
386 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
388 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
389 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
390 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
,
391 0, TRUE
, DUPLICATE_SAME_ACCESS
))
392 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
394 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
396 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
397 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
398 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
,
399 0, TRUE
, DUPLICATE_SAME_ACCESS
))
400 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
402 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
404 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
405 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
406 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
,
407 0, TRUE
, DUPLICATE_SAME_ACCESS
))
408 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
411 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
412 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
414 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
415 for (i
= 0; i
< 3; i
++)
417 /* FILE structs for stdin/out/err are static and never deleted */
418 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
419 MSVCRT__iob
[i
]._file
= i
;
420 MSVCRT__iob
[i
]._tmpfname
= NULL
;
421 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
423 MSVCRT_stream_idx
= 3;
426 /* INTERNAL: Flush stdio file buffer */
427 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
430 int cnt
=file
->_ptr
-file
->_base
;
431 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
432 file
->_flag
|= MSVCRT__IOERR
;
435 file
->_ptr
=file
->_base
;
436 file
->_cnt
=file
->_bufsiz
;
441 /* INTERNAL: Allocate stdio file buffer */
442 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
444 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
446 file
->_bufsiz
= MSVCRT_BUFSIZ
;
447 file
->_flag
|= MSVCRT__IOMYBUF
;
449 file
->_base
= (char*)(&file
->_charbuf
);
451 file
->_bufsiz
= sizeof(file
->_charbuf
);
453 file
->_ptr
= file
->_base
;
457 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
458 static void msvcrt_int_to_base32(int num
, char *str
)
473 *p
= (num
& 31) + '0';
475 *p
+= ('a' - '0' - 10);
480 /*********************************************************************
481 * __iob_func(MSVCRT.@)
483 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
485 return &MSVCRT__iob
[0];
488 /*********************************************************************
491 int CDECL
MSVCRT__access(const char *filename
, int mode
)
493 DWORD attr
= GetFileAttributesA(filename
);
495 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
497 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
499 msvcrt_set_errno(GetLastError());
502 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
504 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
510 /*********************************************************************
511 * _access_s (MSVCRT.@)
513 int CDECL
_access_s(const char *filename
, int mode
)
515 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
516 !MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0))
518 *MSVCRT__errno() = MSVCRT_EINVAL
;
522 return MSVCRT__access(filename
, mode
);
525 /*********************************************************************
526 * _waccess (MSVCRT.@)
528 int CDECL
_waccess(const MSVCRT_wchar_t
*filename
, int mode
)
530 DWORD attr
= GetFileAttributesW(filename
);
532 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
534 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
536 msvcrt_set_errno(GetLastError());
539 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
541 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
547 /*********************************************************************
548 * _waccess_s (MSVCRT.@)
550 int CDECL
_waccess_s(const MSVCRT_wchar_t
*filename
, int mode
)
552 if (!MSVCRT_CHECK_PMT(filename
!= NULL
) ||
553 !MSVCRT_CHECK_PMT((mode
& ~(MSVCRT_R_OK
| MSVCRT_W_OK
)) == 0))
555 *MSVCRT__errno() = MSVCRT_EINVAL
;
559 return _waccess(filename
, mode
);
562 /*********************************************************************
565 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
567 DWORD oldFlags
= GetFileAttributesA(path
);
569 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
571 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
572 oldFlags
| FILE_ATTRIBUTE_READONLY
;
574 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
577 msvcrt_set_errno(GetLastError());
581 /*********************************************************************
584 int CDECL
_wchmod(const MSVCRT_wchar_t
*path
, int flags
)
586 DWORD oldFlags
= GetFileAttributesW(path
);
588 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
590 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
591 oldFlags
| FILE_ATTRIBUTE_READONLY
;
593 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
596 msvcrt_set_errno(GetLastError());
600 /*********************************************************************
603 int CDECL
MSVCRT__unlink(const char *path
)
605 TRACE("%s\n",debugstr_a(path
));
606 if(DeleteFileA(path
))
608 TRACE("failed (%d)\n",GetLastError());
609 msvcrt_set_errno(GetLastError());
613 /*********************************************************************
614 * _wunlink (MSVCRT.@)
616 int CDECL
_wunlink(const MSVCRT_wchar_t
*path
)
618 TRACE("(%s)\n",debugstr_w(path
));
619 if(DeleteFileW(path
))
621 TRACE("failed (%d)\n",GetLastError());
622 msvcrt_set_errno(GetLastError());
626 /* _flushall calls MSVCRT_fflush which calls _flushall */
627 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
629 /*********************************************************************
630 * _flushall (MSVCRT.@)
632 int CDECL
_flushall(void)
634 int i
, num_flushed
= 0;
637 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
638 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
641 /* FIXME: flush, do not commit */
642 if (_commit(i
) == -1)
643 if (MSVCRT_fstreams
[i
])
644 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
646 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
647 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
653 TRACE(":flushed (%d) handles\n",num_flushed
);
657 /*********************************************************************
660 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
664 } else if(file
->_flag
& MSVCRT__IOWRT
) {
665 int res
=msvcrt_flush_buffer(file
);
671 /*********************************************************************
674 int CDECL
MSVCRT__close(int fd
)
680 hand
= msvcrt_fdtoh(fd
);
681 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
682 if (hand
== INVALID_HANDLE_VALUE
)
684 else if (!CloseHandle(hand
))
686 WARN(":failed-last error (%d)\n",GetLastError());
687 msvcrt_set_errno(GetLastError());
700 /*********************************************************************
703 int CDECL
_commit(int fd
)
705 HANDLE hand
= msvcrt_fdtoh(fd
);
707 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
708 if (hand
== INVALID_HANDLE_VALUE
)
711 if (!FlushFileBuffers(hand
))
713 if (GetLastError() == ERROR_INVALID_HANDLE
)
715 /* FlushFileBuffers fails for console handles
716 * so we ignore this error.
720 TRACE(":failed-last error (%d)\n",GetLastError());
721 msvcrt_set_errno(GetLastError());
728 /*********************************************************************
731 * MSDN isn't clear on this point, but the remarks for _pipe
732 * indicate file descriptors duplicated with _dup and _dup2 are always
735 int CDECL
MSVCRT__dup2(int od
, int nd
)
739 TRACE("(od=%d, nd=%d)\n", od
, nd
);
741 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
745 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
746 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
748 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
750 if (msvcrt_is_valid_fd(nd
))
752 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
756 *MSVCRT__errno() = MSVCRT_EMFILE
;
760 /* _dup2 returns 0, not nd, on success */
767 msvcrt_set_errno(GetLastError());
772 *MSVCRT__errno() = MSVCRT_EBADF
;
779 /*********************************************************************
782 int CDECL
MSVCRT__dup(int od
)
788 if (MSVCRT__dup2(od
, fd
) == 0)
796 /*********************************************************************
799 int CDECL
_eof(int fd
)
802 LONG hcurpos
,hendpos
;
803 HANDLE hand
= msvcrt_fdtoh(fd
);
805 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
807 if (hand
== INVALID_HANDLE_VALUE
)
810 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
812 /* Otherwise we do it the hard way */
813 hcurpos
= hendpos
= 0;
814 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
815 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
817 if (curpos
== endpos
&& hcurpos
== hendpos
)
819 /* FIXME: shouldn't WX_ATEOF be set here? */
823 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
827 /*********************************************************************
828 * _fcloseall (MSVCRT.@)
830 int CDECL
MSVCRT__fcloseall(void)
832 int num_closed
= 0, i
;
835 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
836 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
837 !MSVCRT_fclose(MSVCRT_fstreams
[i
]))
841 TRACE(":closed (%d) handles\n",num_closed
);
845 /* free everything on process exit */
846 void msvcrt_free_io(void)
849 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
850 * stdout, and stderr (unlike GNU), so we need to fclose() them here
851 * or they won't get flushed.
853 MSVCRT_fclose(&MSVCRT__iob
[0]);
854 MSVCRT_fclose(&MSVCRT__iob
[1]);
855 MSVCRT_fclose(&MSVCRT__iob
[2]);
856 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = 0;
857 DeleteCriticalSection(&MSVCRT_file_cs
);
860 /*********************************************************************
861 * _lseeki64 (MSVCRT.@)
863 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
865 HANDLE hand
= msvcrt_fdtoh(fd
);
866 LARGE_INTEGER ofs
, ret
;
868 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
869 if (hand
== INVALID_HANDLE_VALUE
)
872 if (whence
< 0 || whence
> 2)
874 *MSVCRT__errno() = MSVCRT_EINVAL
;
878 TRACE(":fd (%d) to %s pos %s\n",
879 fd
,wine_dbgstr_longlong(offset
),
880 (whence
==SEEK_SET
)?"SEEK_SET":
881 (whence
==SEEK_CUR
)?"SEEK_CUR":
882 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
884 ofs
.QuadPart
= offset
;
885 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
887 MSVCRT_fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
888 /* FIXME: What if we seek _to_ EOF - is EOF set? */
892 TRACE(":error-last error (%d)\n",GetLastError());
893 msvcrt_set_errno(GetLastError());
897 /*********************************************************************
900 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
902 return MSVCRT__lseeki64(fd
, offset
, whence
);
905 /*********************************************************************
906 * _lock_file (MSVCRT.@)
908 void CDECL
MSVCRT__lock_file(MSVCRT_FILE
*file
)
910 FIXME("(%p) stub\n",file
);
913 /*********************************************************************
914 * _unlock_file (MSVCRT.@)
916 void CDECL
MSVCRT__unlock_file(MSVCRT_FILE
*file
)
918 FIXME("(%p) stub\n",file
);
921 /*********************************************************************
922 * _locking (MSVCRT.@)
924 * This is untested; the underlying LockFile doesn't work yet.
926 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
930 HANDLE hand
= msvcrt_fdtoh(fd
);
932 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
933 if (hand
== INVALID_HANDLE_VALUE
)
936 if (mode
< 0 || mode
> 4)
938 *MSVCRT__errno() = MSVCRT_EINVAL
;
942 TRACE(":fd (%d) by 0x%08x mode %s\n",
943 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
944 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
945 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
946 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
947 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
950 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
952 FIXME ("Seek failed\n");
953 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
956 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
959 ret
= 1; /* just to satisfy gcc */
962 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
967 else if (mode
== MSVCRT__LK_UNLCK
)
968 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
970 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
971 /* FIXME - what about error settings? */
975 /*********************************************************************
976 * _fseeki64 (MSVCRT.@)
978 int CDECL
MSVCRT__fseeki64(MSVCRT_FILE
* file
, __int64 offset
, int whence
)
980 /* Flush output if needed */
981 if(file
->_flag
& MSVCRT__IOWRT
)
982 msvcrt_flush_buffer(file
);
984 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
985 offset
-= file
->_cnt
;
986 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
987 /* Black magic correction for CR removal */
989 for (i
=0; i
<file
->_cnt
; i
++) {
990 if (file
->_ptr
[i
] == '\n')
993 /* Black magic when reading CR at buffer boundary*/
994 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
998 /* Discard buffered input */
1000 file
->_ptr
= file
->_base
;
1001 /* Reset direction of i/o */
1002 if(file
->_flag
& MSVCRT__IORW
) {
1003 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1005 /* Clear end of file flag */
1006 file
->_flag
&= ~MSVCRT__IOEOF
;
1007 return (MSVCRT__lseeki64(file
->_file
,offset
,whence
) == -1)?-1:0;
1010 /*********************************************************************
1013 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
1015 return MSVCRT__fseeki64( file
, offset
, whence
);
1018 /*********************************************************************
1019 * _chsize (MSVCRT.@)
1021 int CDECL
MSVCRT__chsize(int fd
, MSVCRT_long size
)
1027 TRACE("(fd=%d, size=%d)\n", fd
, size
);
1031 handle
= msvcrt_fdtoh(fd
);
1032 if (handle
!= INVALID_HANDLE_VALUE
)
1034 /* save the current file pointer */
1035 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1038 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
1041 ret
= SetEndOfFile(handle
);
1042 if (!ret
) msvcrt_set_errno(GetLastError());
1045 /* restore the file pointer */
1046 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
1051 return ret
? 0 : -1;
1054 /*********************************************************************
1055 * clearerr (MSVCRT.@)
1057 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1059 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1060 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1063 /*********************************************************************
1066 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1068 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1069 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1070 MSVCRT_clearerr(file
);
1073 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1075 int plus
= strchrW(mode
, '+') != NULL
;
1080 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1081 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1084 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1085 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1088 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1089 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1092 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1093 *MSVCRT__errno() = MSVCRT_EINVAL
;
1101 *open_flags
|= MSVCRT__O_BINARY
;
1102 *open_flags
&= ~MSVCRT__O_TEXT
;
1105 *open_flags
|= MSVCRT__O_TEXT
;
1106 *open_flags
&= ~MSVCRT__O_BINARY
;
1112 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1117 /*********************************************************************
1118 * _fdopen (MSVCRT.@)
1120 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1123 MSVCRT_wchar_t
*modeW
= NULL
;
1125 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1127 ret
= MSVCRT__wfdopen(fd
, modeW
);
1133 /*********************************************************************
1134 * _wfdopen (MSVCRT.@)
1136 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1138 int open_flags
, stream_flags
;
1141 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1144 if (!(file
= msvcrt_alloc_fp()))
1146 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1151 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1157 /*********************************************************************
1158 * _filelength (MSVCRT.@)
1160 LONG CDECL
MSVCRT__filelength(int fd
)
1162 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1165 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1168 if (endPos
!= curPos
)
1169 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1176 /*********************************************************************
1177 * _filelengthi64 (MSVCRT.@)
1179 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1181 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1184 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1187 if (endPos
!= curPos
)
1188 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1195 /*********************************************************************
1196 * _fileno (MSVCRT.@)
1198 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1200 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1204 /*********************************************************************
1205 * _fstat64 (MSVCRT.@)
1207 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1211 BY_HANDLE_FILE_INFORMATION hfi
;
1212 HANDLE hand
= msvcrt_fdtoh(fd
);
1214 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1215 if (hand
== INVALID_HANDLE_VALUE
)
1220 WARN(":failed-NULL buf\n");
1221 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1225 memset(&hfi
, 0, sizeof(hfi
));
1226 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1227 type
= GetFileType(hand
);
1228 if (type
== FILE_TYPE_PIPE
)
1230 buf
->st_dev
= buf
->st_rdev
= fd
;
1231 buf
->st_mode
= S_IFIFO
;
1234 else if (type
== FILE_TYPE_CHAR
)
1236 buf
->st_dev
= buf
->st_rdev
= fd
;
1237 buf
->st_mode
= S_IFCHR
;
1240 else /* FILE_TYPE_DISK etc. */
1242 if (!GetFileInformationByHandle(hand
, &hfi
))
1244 WARN(":failed-last error (%d)\n",GetLastError());
1245 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1248 buf
->st_mode
= S_IFREG
| 0444;
1249 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1250 buf
->st_mode
|= 0222;
1251 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1252 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1254 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1255 buf
->st_mtime
= buf
->st_ctime
= dw
;
1256 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1258 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1263 /*********************************************************************
1264 * _fstati64 (MSVCRT.@)
1266 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1269 struct MSVCRT__stat64 buf64
;
1271 ret
= MSVCRT__fstat64(fd
, &buf64
);
1273 msvcrt_stat64_to_stati64(&buf64
, buf
);
1277 /*********************************************************************
1280 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1282 struct MSVCRT__stat64 buf64
;
1284 ret
= MSVCRT__fstat64(fd
, &buf64
);
1286 msvcrt_stat64_to_stat(&buf64
, buf
);
1290 /*********************************************************************
1291 * _futime64 (MSVCRT.@)
1293 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1295 HANDLE hand
= msvcrt_fdtoh(fd
);
1300 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1305 time_to_filetime( t
->actime
, &at
);
1306 time_to_filetime( t
->modtime
, &wt
);
1309 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1311 msvcrt_set_errno(GetLastError());
1317 /*********************************************************************
1318 * _futime32 (MSVCRT.@)
1320 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1322 struct MSVCRT___utimbuf64 t64
;
1323 t64
.actime
= t
->actime
;
1324 t64
.modtime
= t
->modtime
;
1325 return _futime64( fd
, &t64
);
1328 /*********************************************************************
1329 * _futime (MSVCRT.@)
1332 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1334 return _futime64( fd
, t
);
1337 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1339 return _futime32( fd
, t
);
1343 /*********************************************************************
1344 * _get_osfhandle (MSVCRT.@)
1346 MSVCRT_intptr_t CDECL
_get_osfhandle(int fd
)
1348 HANDLE hand
= msvcrt_fdtoh(fd
);
1349 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1351 return (MSVCRT_intptr_t
)hand
;
1354 /*********************************************************************
1355 * _isatty (MSVCRT.@)
1357 int CDECL
_isatty(int fd
)
1359 HANDLE hand
= msvcrt_fdtoh(fd
);
1361 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1362 if (hand
== INVALID_HANDLE_VALUE
)
1365 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1368 /*********************************************************************
1369 * _mktemp (MSVCRT.@)
1371 char * CDECL
_mktemp(char *pattern
)
1374 char *retVal
= pattern
;
1379 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1383 id
= GetCurrentProcessId();
1387 int tempNum
= id
/ 10;
1388 *pattern
-- = id
- (tempNum
* 10) + '0';
1394 *pattern
= letter
++;
1395 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1396 GetLastError() == ERROR_FILE_NOT_FOUND
)
1398 } while(letter
<= 'z');
1402 /*********************************************************************
1403 * _wmktemp (MSVCRT.@)
1405 MSVCRT_wchar_t
* CDECL
_wmktemp(MSVCRT_wchar_t
*pattern
)
1408 MSVCRT_wchar_t
*retVal
= pattern
;
1410 MSVCRT_wchar_t letter
= 'a';
1413 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1417 id
= GetCurrentProcessId();
1421 int tempNum
= id
/ 10;
1422 *pattern
-- = id
- (tempNum
* 10) + '0';
1428 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1429 GetLastError() == ERROR_FILE_NOT_FOUND
)
1431 *pattern
= letter
++;
1432 } while(letter
!= '|');
1436 static unsigned split_oflags(unsigned oflags
)
1439 unsigned unsupp
; /* until we support everything */
1441 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1442 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1443 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1444 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1445 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1446 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1448 if ((unsupp
= oflags
& ~(
1449 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1450 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1451 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1452 MSVCRT__O_NOINHERIT
|
1453 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1455 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1460 /*********************************************************************
1463 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1466 SECURITY_ATTRIBUTES sa
;
1467 HANDLE readHandle
, writeHandle
;
1471 *MSVCRT__errno() = MSVCRT_EINVAL
;
1475 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1476 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1477 sa
.lpSecurityDescriptor
= NULL
;
1478 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1480 unsigned int wxflags
= split_oflags(textmode
);
1484 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1488 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1496 MSVCRT__close(pfds
[0]);
1497 CloseHandle(writeHandle
);
1498 *MSVCRT__errno() = MSVCRT_EMFILE
;
1503 CloseHandle(readHandle
);
1504 CloseHandle(writeHandle
);
1505 *MSVCRT__errno() = MSVCRT_EMFILE
;
1510 msvcrt_set_errno(GetLastError());
1515 /*********************************************************************
1516 * _sopen_s (MSVCRT.@)
1518 int CDECL
MSVCRT__sopen_s( int *fd
, const char *path
, int oflags
, int shflags
, int pmode
)
1520 DWORD access
= 0, creation
= 0, attrib
;
1524 SECURITY_ATTRIBUTES sa
;
1526 TRACE("fd*: %p file: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1527 fd
, path
, oflags
, shflags
, pmode
);
1531 MSVCRT_INVALID_PMT("null out fd pointer");
1532 *MSVCRT__errno() = MSVCRT_EINVAL
;
1533 return MSVCRT_EINVAL
;
1537 wxflag
= split_oflags(oflags
);
1538 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1540 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1541 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1542 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1545 if (oflags
& MSVCRT__O_CREAT
)
1547 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1548 FIXME(": pmode 0x%04x ignored\n", pmode
);
1550 WARN(": pmode 0x%04x ignored\n", pmode
);
1552 if (oflags
& MSVCRT__O_EXCL
)
1553 creation
= CREATE_NEW
;
1554 else if (oflags
& MSVCRT__O_TRUNC
)
1555 creation
= CREATE_ALWAYS
;
1557 creation
= OPEN_ALWAYS
;
1559 else /* no MSVCRT__O_CREAT */
1561 if (oflags
& MSVCRT__O_TRUNC
)
1562 creation
= TRUNCATE_EXISTING
;
1564 creation
= OPEN_EXISTING
;
1569 case MSVCRT__SH_DENYRW
:
1572 case MSVCRT__SH_DENYWR
:
1573 sharing
= FILE_SHARE_READ
;
1575 case MSVCRT__SH_DENYRD
:
1576 sharing
= FILE_SHARE_WRITE
;
1578 case MSVCRT__SH_DENYNO
:
1579 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1582 ERR( "Unhandled shflags 0x%x\n", shflags
);
1583 return MSVCRT_EINVAL
;
1585 attrib
= FILE_ATTRIBUTE_NORMAL
;
1587 if (oflags
& MSVCRT__O_TEMPORARY
)
1589 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1591 sharing
|= FILE_SHARE_DELETE
;
1594 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1595 sa
.lpSecurityDescriptor
= NULL
;
1596 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1598 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1599 if (hand
== INVALID_HANDLE_VALUE
) {
1600 WARN(":failed-last error (%d)\n", GetLastError());
1601 msvcrt_set_errno(GetLastError());
1602 return *MSVCRT__errno();
1605 *fd
= msvcrt_alloc_fd(hand
, wxflag
);
1607 TRACE(":fd (%d) handle (%p)\n", *fd
, hand
);
1611 /*********************************************************************
1614 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1619 if (oflags
& MSVCRT__O_CREAT
)
1623 __ms_va_start(ap
, shflags
);
1624 pmode
= va_arg(ap
, int);
1630 MSVCRT__sopen_s(&fd
, path
, oflags
, shflags
, pmode
);
1634 /*********************************************************************
1635 * _wsopen (MSVCRT.@)
1637 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1641 DWORD access
= 0, creation
= 0, attrib
;
1645 SECURITY_ATTRIBUTES sa
;
1648 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1649 debugstr_w(path
), oflags
, shflags
);
1651 wxflag
= split_oflags(oflags
);
1652 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1654 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1655 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1656 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1659 if (oflags
& MSVCRT__O_CREAT
)
1661 __ms_va_start(ap
, shflags
);
1662 pmode
= va_arg(ap
, int);
1665 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1666 FIXME(": pmode 0x%04x ignored\n", pmode
);
1668 WARN(": pmode 0x%04x ignored\n", pmode
);
1670 if (oflags
& MSVCRT__O_EXCL
)
1671 creation
= CREATE_NEW
;
1672 else if (oflags
& MSVCRT__O_TRUNC
)
1673 creation
= CREATE_ALWAYS
;
1675 creation
= OPEN_ALWAYS
;
1677 else /* no MSVCRT__O_CREAT */
1679 if (oflags
& MSVCRT__O_TRUNC
)
1680 creation
= TRUNCATE_EXISTING
;
1682 creation
= OPEN_EXISTING
;
1687 case MSVCRT__SH_DENYRW
:
1690 case MSVCRT__SH_DENYWR
:
1691 sharing
= FILE_SHARE_READ
;
1693 case MSVCRT__SH_DENYRD
:
1694 sharing
= FILE_SHARE_WRITE
;
1696 case MSVCRT__SH_DENYNO
:
1697 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1700 ERR( "Unhandled shflags 0x%x\n", shflags
);
1703 attrib
= FILE_ATTRIBUTE_NORMAL
;
1705 if (oflags
& MSVCRT__O_TEMPORARY
)
1707 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1709 sharing
|= FILE_SHARE_DELETE
;
1712 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1713 sa
.lpSecurityDescriptor
= NULL
;
1714 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1716 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1718 if (hand
== INVALID_HANDLE_VALUE
) {
1719 WARN(":failed-last error (%d)\n",GetLastError());
1720 msvcrt_set_errno(GetLastError());
1724 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1726 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1730 /*********************************************************************
1733 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1737 if (flags
& MSVCRT__O_CREAT
)
1740 __ms_va_start(ap
, flags
);
1741 pmode
= va_arg(ap
, int);
1743 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1746 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1749 /*********************************************************************
1752 int CDECL
_wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1756 if (flags
& MSVCRT__O_CREAT
)
1759 __ms_va_start(ap
, flags
);
1760 pmode
= va_arg(ap
, int);
1762 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1765 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1768 /*********************************************************************
1771 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1773 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1774 return MSVCRT__open(path
, usedFlags
);
1777 /*********************************************************************
1778 * _wcreat (MSVCRT.@)
1780 int CDECL
_wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1782 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1783 return _wopen(path
, usedFlags
);
1786 /*********************************************************************
1787 * _open_osfhandle (MSVCRT.@)
1789 int CDECL
_open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1793 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1794 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1795 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1796 * text - it never sets MSVCRT__O_BINARY.
1798 /* don't let split_oflags() decide the mode if no mode is passed */
1799 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1800 oflags
|= MSVCRT__O_BINARY
;
1802 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1803 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1807 /*********************************************************************
1810 int CDECL
_rmtmp(void)
1812 int num_removed
= 0, i
;
1815 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1816 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1818 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1824 TRACE(":removed (%d) temp files\n",num_removed
);
1828 /*********************************************************************
1831 * When reading \r as last character in text mode, read() positions
1832 * the file pointer on the \r character while getc() goes on to
1835 static int read_i(int fd
, void *buf
, unsigned int count
)
1838 char *bufstart
= buf
;
1839 HANDLE hand
= msvcrt_fdtoh(fd
);
1844 if (MSVCRT_fdesc
[fd
].wxflag
& WX_READEOF
) {
1845 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1846 TRACE("already at EOF, returning 0\n");
1849 /* Don't trace small reads, it gets *very* annoying */
1851 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1852 if (hand
== INVALID_HANDLE_VALUE
)
1855 /* Reading single bytes in O_TEXT mode makes things slow
1856 * So read big chunks
1858 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1860 if (count
!= 0 && num_read
== 0)
1862 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1863 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1865 else if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1868 if (bufstart
[num_read
-1] == '\r')
1872 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_READCR
;
1873 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1877 MSVCRT_fdesc
[fd
].wxflag
|= WX_READCR
;
1882 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_READCR
;
1883 for (i
=0, j
=0; i
<num_read
; i
++)
1885 /* in text mode, a ctrl-z signals EOF */
1886 if (bufstart
[i
] == 0x1a)
1888 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1889 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1892 /* in text mode, strip \r if followed by \n.
1893 * BUG: should save state across calls somehow, so CR LF that
1894 * straddles buffer boundary gets recognized properly?
1896 if ((bufstart
[i
] != '\r')
1897 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1898 bufstart
[j
++] = bufstart
[i
];
1905 if (GetLastError() == ERROR_BROKEN_PIPE
)
1907 TRACE(":end-of-pipe\n");
1908 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1913 TRACE(":failed-last error (%d)\n",GetLastError());
1919 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1923 /*********************************************************************
1926 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
1929 num_read
= read_i(fd
, buf
, count
);
1933 /*********************************************************************
1934 * _setmode (MSVCRT.@)
1936 int CDECL
_setmode(int fd
,int mode
)
1938 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1939 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1940 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1941 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1942 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1944 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1948 /*********************************************************************
1949 * _stat64 (MSVCRT.@)
1951 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
1954 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1955 unsigned short mode
= ALL_S_IREAD
;
1958 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1960 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1962 TRACE("failed (%d)\n",GetLastError());
1963 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1967 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1969 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1970 Bon 011120: This FIXME seems incorrect
1971 Also a letter as first char isn't enough to be classified
1974 if (isalpha(*path
)&& (*(path
+1)==':'))
1975 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1977 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1979 plen
= strlen(path
);
1981 /* Dir, or regular file? */
1982 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1983 (path
[plen
-1] == '\\'))
1984 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1987 mode
|= MSVCRT__S_IFREG
;
1989 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1991 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1992 (tolower(path
[plen
-3]) << 16);
1993 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1994 mode
|= ALL_S_IEXEC
;
1998 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1999 mode
|= ALL_S_IWRITE
;
2001 buf
->st_mode
= mode
;
2003 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2004 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2006 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2007 buf
->st_mtime
= buf
->st_ctime
= dw
;
2008 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2009 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2010 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2014 /*********************************************************************
2015 * _stati64 (MSVCRT.@)
2017 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
2020 struct MSVCRT__stat64 buf64
;
2022 ret
= MSVCRT_stat64(path
, &buf64
);
2024 msvcrt_stat64_to_stati64(&buf64
, buf
);
2028 /*********************************************************************
2031 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
2033 struct MSVCRT__stat64 buf64
;
2035 ret
= MSVCRT_stat64( path
, &buf64
);
2037 msvcrt_stat64_to_stat(&buf64
, buf
);
2041 /*********************************************************************
2042 * _wstat64 (MSVCRT.@)
2044 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
2047 WIN32_FILE_ATTRIBUTE_DATA hfi
;
2048 unsigned short mode
= ALL_S_IREAD
;
2051 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
2053 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
2055 TRACE("failed (%d)\n",GetLastError());
2056 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
2060 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
2062 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
2063 if (MSVCRT_iswalpha(*path
))
2064 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
2066 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
2068 plen
= strlenW(path
);
2070 /* Dir, or regular file? */
2071 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
2072 (path
[plen
-1] == '\\'))
2073 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
2076 mode
|= MSVCRT__S_IFREG
;
2078 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
2080 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2081 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2082 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2083 mode
|= ALL_S_IEXEC
;
2087 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2088 mode
|= ALL_S_IWRITE
;
2090 buf
->st_mode
= mode
;
2092 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2093 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2095 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2096 buf
->st_mtime
= buf
->st_ctime
= dw
;
2097 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2098 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2099 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2103 /*********************************************************************
2104 * _wstati64 (MSVCRT.@)
2106 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2109 struct MSVCRT__stat64 buf64
;
2111 ret
= MSVCRT__wstat64(path
, &buf64
);
2113 msvcrt_stat64_to_stati64(&buf64
, buf
);
2117 /*********************************************************************
2120 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2123 struct MSVCRT__stat64 buf64
;
2125 ret
= MSVCRT__wstat64( path
, &buf64
);
2126 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2130 /*********************************************************************
2133 MSVCRT_long CDECL
MSVCRT__tell(int fd
)
2135 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2138 /*********************************************************************
2139 * _telli64 (MSVCRT.@)
2141 __int64 CDECL
_telli64(int fd
)
2143 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2146 /*********************************************************************
2147 * _tempnam (MSVCRT.@)
2149 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2151 char tmpbuf
[MAX_PATH
];
2152 const char *tmp_dir
= MSVCRT_getenv("TMP");
2154 if (tmp_dir
) dir
= tmp_dir
;
2156 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2157 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2159 TRACE("got name (%s)\n",tmpbuf
);
2160 DeleteFileA(tmpbuf
);
2161 return _strdup(tmpbuf
);
2163 TRACE("failed (%d)\n",GetLastError());
2167 /*********************************************************************
2168 * _wtempnam (MSVCRT.@)
2170 MSVCRT_wchar_t
* CDECL
_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2172 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2174 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2175 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2177 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2178 DeleteFileW(tmpbuf
);
2179 return _wcsdup(tmpbuf
);
2181 TRACE("failed (%d)\n",GetLastError());
2185 /*********************************************************************
2188 int CDECL
MSVCRT__umask(int umask
)
2190 int old_umask
= MSVCRT_umask
;
2191 TRACE("(%d)\n",umask
);
2192 MSVCRT_umask
= umask
;
2196 /*********************************************************************
2197 * _utime64 (MSVCRT.@)
2199 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2201 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2205 int retVal
= _futime64(fd
, t
);
2212 /*********************************************************************
2213 * _utime32 (MSVCRT.@)
2215 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2217 struct MSVCRT___utimbuf64 t64
;
2218 t64
.actime
= t
->actime
;
2219 t64
.modtime
= t
->modtime
;
2220 return _utime64( path
, &t64
);
2223 /*********************************************************************
2227 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2229 return _utime64( path
, t
);
2232 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2234 return _utime32( path
, t
);
2238 /*********************************************************************
2239 * _wutime64 (MSVCRT.@)
2241 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2243 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2247 int retVal
= _futime64(fd
, t
);
2254 /*********************************************************************
2255 * _wutime32 (MSVCRT.@)
2257 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2259 struct MSVCRT___utimbuf64 t64
;
2260 t64
.actime
= t
->actime
;
2261 t64
.modtime
= t
->modtime
;
2262 return _wutime64( path
, &t64
);
2265 /*********************************************************************
2266 * _wutime (MSVCRT.@)
2269 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2271 return _wutime64( path
, t
);
2274 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2276 return _wutime32( path
, t
);
2280 /*********************************************************************
2283 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2286 HANDLE hand
= msvcrt_fdtoh(fd
);
2288 /* Don't trace small writes, it gets *very* annoying */
2291 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2293 if (hand
== INVALID_HANDLE_VALUE
)
2295 *MSVCRT__errno() = MSVCRT_EBADF
;
2299 /* If appending, go to EOF */
2300 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
2301 MSVCRT__lseek(fd
, 0, FILE_END
);
2303 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
2305 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2306 && (num_written
== count
))
2308 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2309 hand
, GetLastError());
2310 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2314 unsigned int i
, j
, nr_lf
;
2317 const char *s
= buf
, *buf_start
= buf
;
2318 /* find number of \n ( without preceding \r ) */
2319 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2324 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2329 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2331 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2336 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2343 FIXME("Malloc failed\n");
2351 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2353 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2354 fd
, hand
, GetLastError(), num_written
);
2355 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2358 return s
- buf_start
;
2370 /*********************************************************************
2373 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2376 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2377 if (len
== sizeof(val
)) return val
;
2378 file
->_flag
|= MSVCRT__IOERR
;
2382 /*********************************************************************
2385 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2390 MSVCRT_free(file
->_tmpfname
);
2391 file
->_tmpfname
= NULL
;
2392 /* flush stdio buffers */
2393 if(file
->_flag
& MSVCRT__IOWRT
)
2394 MSVCRT_fflush(file
);
2395 if(file
->_flag
& MSVCRT__IOMYBUF
)
2396 MSVCRT_free(file
->_base
);
2398 r
=MSVCRT__close(file
->_file
);
2402 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2405 /*********************************************************************
2408 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2410 return file
->_flag
& MSVCRT__IOEOF
;
2413 /*********************************************************************
2416 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2418 return file
->_flag
& MSVCRT__IOERR
;
2421 /*********************************************************************
2422 * _filbuf (MSVCRT.@)
2424 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2426 /* Allocate buffer if needed */
2427 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2428 msvcrt_alloc_buffer(file
);
2430 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2431 if(file
->_flag
& MSVCRT__IORW
) {
2432 file
->_flag
|= MSVCRT__IOREAD
;
2437 if(file
->_flag
& MSVCRT__IONBF
) {
2440 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2441 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2446 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2448 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2453 file
->_ptr
= file
->_base
+1;
2454 return *(unsigned char *)file
->_base
;
2458 /*********************************************************************
2461 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2467 i
= (unsigned char *)file
->_ptr
++;
2470 j
= MSVCRT__filbuf(file
);
2474 /*********************************************************************
2475 * _fgetchar (MSVCRT.@)
2477 int CDECL
_fgetchar(void)
2479 return MSVCRT_fgetc(MSVCRT_stdin
);
2482 /*********************************************************************
2485 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2487 int cc
= MSVCRT_EOF
;
2488 char * buf_start
= s
;
2490 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2491 file
,file
->_file
,s
,size
);
2493 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2498 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2500 TRACE(":nothing read\n");
2503 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2506 TRACE(":got %s\n", debugstr_a(buf_start
));
2510 /*********************************************************************
2513 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2514 * the CR from CR/LF combinations
2516 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2520 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2527 for(i
=0; i
<sizeof(wc
); i
++)
2537 j
= MSVCRT__filbuf(file
);
2540 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2550 c
= MSVCRT_fgetc(file
);
2551 if ((get_locale()->locinfo
->mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2553 FIXME("Treat Multibyte characters\n");
2555 if (c
== MSVCRT_EOF
)
2558 return (MSVCRT_wint_t
)c
;
2561 /*********************************************************************
2564 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2570 for (j
=0; j
<sizeof(int); j
++) {
2571 k
= MSVCRT_fgetc(file
);
2572 if (k
== MSVCRT_EOF
) {
2573 file
->_flag
|= MSVCRT__IOEOF
;
2581 /*********************************************************************
2584 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2586 return MSVCRT_fgetwc(file
);
2589 /*********************************************************************
2590 * _fgetwchar (MSVCRT.@)
2592 MSVCRT_wint_t CDECL
_fgetwchar(void)
2594 return MSVCRT_fgetwc(MSVCRT_stdin
);
2597 /*********************************************************************
2598 * getwchar (MSVCRT.@)
2600 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2602 return _fgetwchar();
2605 /*********************************************************************
2608 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2610 int cc
= MSVCRT_WEOF
;
2611 MSVCRT_wchar_t
* buf_start
= s
;
2613 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2614 file
,file
->_file
,s
,size
);
2616 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2621 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2623 TRACE(":nothing read\n");
2626 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2629 TRACE(":got %s\n", debugstr_w(buf_start
));
2633 /*********************************************************************
2636 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2638 MSVCRT_size_t wrcnt
=size
* nmemb
;
2643 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2644 memcpy(file
->_ptr
, ptr
, pcnt
);
2649 ptr
= (const char*)ptr
+ pcnt
;
2650 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2651 if(file
->_flag
& MSVCRT__IORW
) {
2652 file
->_flag
|= MSVCRT__IOWRT
;
2658 int res
=msvcrt_flush_buffer(file
);
2660 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2663 file
->_flag
|= MSVCRT__IOERR
;
2666 written
+= pwritten
;
2669 return written
/ size
;
2672 /*********************************************************************
2675 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2677 MSVCRT_wchar_t mwc
=wc
;
2678 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2683 /*********************************************************************
2684 * _fputwchar (MSVCRT.@)
2686 MSVCRT_wint_t CDECL
_fputwchar(MSVCRT_wint_t wc
)
2688 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2691 /*********************************************************************
2692 * _wfsopen (MSVCRT.@)
2694 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2697 int open_flags
, stream_flags
, fd
;
2699 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2701 /* map mode string to open() flags. "man fopen" for possibilities. */
2702 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2706 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2709 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2711 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2718 TRACE(":got (%p)\n",file
);
2719 if (fd
>= 0 && !file
)
2725 /*********************************************************************
2726 * _fsopen (MSVCRT.@)
2728 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2731 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2733 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) {
2734 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2735 *MSVCRT__errno() = MSVCRT_EINVAL
;
2738 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2741 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
2742 *MSVCRT__errno() = MSVCRT_EINVAL
;
2746 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
2753 /*********************************************************************
2756 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
2758 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2761 /*********************************************************************
2762 * fopen_s (MSVCRT.@)
2764 int CDECL
MSVCRT_fopen_s(MSVCRT_FILE
** pFile
,
2765 const char *filename
, const char *mode
)
2767 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2768 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2769 *MSVCRT__errno() = MSVCRT_EINVAL
;
2770 return MSVCRT_EINVAL
;
2773 *pFile
= MSVCRT_fopen(filename
, mode
);
2776 return *MSVCRT__errno();
2780 /*********************************************************************
2781 * _wfopen (MSVCRT.@)
2783 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2785 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2788 /*********************************************************************
2789 * _wfopen_s (MSVCRT.@)
2791 int CDECL
MSVCRT__wfopen_s(MSVCRT_FILE
** pFile
, const MSVCRT_wchar_t
*filename
,
2792 const MSVCRT_wchar_t
*mode
)
2794 if (!MSVCRT_CHECK_PMT(pFile
!= NULL
) || !MSVCRT_CHECK_PMT(filename
!= NULL
) ||
2795 !MSVCRT_CHECK_PMT(mode
!= NULL
)) {
2796 *MSVCRT__errno() = MSVCRT_EINVAL
;
2797 return MSVCRT_EINVAL
;
2800 *pFile
= MSVCRT__wfopen(filename
, mode
);
2803 return *MSVCRT__errno();
2807 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2808 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2810 /*********************************************************************
2813 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2820 int res
= msvcrt_flush_buffer(file
);
2821 return res
? res
: c
;
2826 return MSVCRT__flsbuf(c
, file
);
2830 /*********************************************************************
2831 * _flsbuf (MSVCRT.@)
2833 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2835 /* Flush output buffer */
2836 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2837 msvcrt_alloc_buffer(file
);
2839 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2840 if(file
->_flag
& MSVCRT__IORW
) {
2841 file
->_flag
|= MSVCRT__IOWRT
;
2847 int res
=msvcrt_flush_buffer(file
);
2848 return res
?res
: MSVCRT_fputc(c
, file
);
2852 /* set _cnt to 0 for unbuffered FILEs */
2854 len
= MSVCRT__write(file
->_file
, &cc
, 1);
2855 if (len
== 1) return c
& 0xff;
2856 file
->_flag
|= MSVCRT__IOERR
;
2861 /*********************************************************************
2862 * _fputchar (MSVCRT.@)
2864 int CDECL
_fputchar(int c
)
2866 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2869 /*********************************************************************
2872 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2873 { MSVCRT_size_t rcnt
=size
* nmemb
;
2874 MSVCRT_size_t read
=0;
2880 /* first buffered data */
2882 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2883 memcpy(ptr
, file
->_ptr
, pcnt
);
2888 ptr
= (char*)ptr
+ pcnt
;
2889 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2890 if(file
->_flag
& MSVCRT__IORW
) {
2891 file
->_flag
|= MSVCRT__IOREAD
;
2898 /* Fill the buffer on small reads.
2899 * TODO: Use a better buffering strategy.
2901 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
2902 if (file
->_bufsiz
== 0) {
2903 msvcrt_alloc_buffer(file
);
2905 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
2906 file
->_ptr
= file
->_base
;
2907 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2908 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2909 if (i
> 0 && i
< file
->_cnt
) {
2910 MSVCRT_fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2911 file
->_flag
&= ~MSVCRT__IOEOF
;
2914 memcpy(ptr
, file
->_ptr
, i
);
2919 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
2923 ptr
= (char *)ptr
+i
;
2924 /* expose feof condition in the flags
2925 * MFC tests file->_flag for feof, and doesn't call feof())
2927 if ( MSVCRT_fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2928 file
->_flag
|= MSVCRT__IOEOF
;
2931 file
->_flag
|= MSVCRT__IOERR
;
2941 /*********************************************************************
2942 * _wfreopen (MSVCRT.@)
2945 MSVCRT_FILE
* CDECL
MSVCRT__wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
2947 int open_flags
, stream_flags
, fd
;
2949 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2952 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2956 MSVCRT_fclose(file
);
2957 /* map mode string to open() flags. "man fopen" for possibilities. */
2958 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2962 fd
= _wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2965 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2968 WARN(":failed-last error (%d)\n",GetLastError());
2969 msvcrt_set_errno(GetLastError());
2978 /*********************************************************************
2979 * freopen (MSVCRT.@)
2982 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
2985 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2987 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2988 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2994 ret
= MSVCRT__wfreopen(pathW
, modeW
, file
);
3001 /*********************************************************************
3002 * fsetpos (MSVCRT.@)
3004 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3006 /* Note that all this has been lifted 'as is' from fseek */
3007 if(file
->_flag
& MSVCRT__IOWRT
)
3008 msvcrt_flush_buffer(file
);
3010 /* Discard buffered input */
3012 file
->_ptr
= file
->_base
;
3014 /* Reset direction of i/o */
3015 if(file
->_flag
& MSVCRT__IORW
) {
3016 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
3019 return (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
3022 /*********************************************************************
3023 * _ftelli64 (MSVCRT.@)
3025 __int64 CDECL
MSVCRT__ftelli64(MSVCRT_FILE
* file
)
3027 /* TODO: just call fgetpos and return lower half of result */
3030 pos
= _telli64(file
->_file
);
3031 if(pos
== -1) return -1;
3033 if( file
->_flag
& MSVCRT__IOWRT
) {
3034 off
= file
->_ptr
- file
->_base
;
3037 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
3038 /* Black magic correction for CR removal */
3040 for (i
=0; i
<file
->_cnt
; i
++) {
3041 if (file
->_ptr
[i
] == '\n')
3044 /* Black magic when reading CR at buffer boundary*/
3045 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
3054 /*********************************************************************
3057 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
3059 return MSVCRT__ftelli64(file
);
3062 /*********************************************************************
3063 * fgetpos (MSVCRT.@)
3065 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
3068 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
3069 if(*pos
== -1) return -1;
3071 if( file
->_flag
& MSVCRT__IOWRT
) {
3072 off
= file
->_ptr
- file
->_base
;
3075 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
3076 /* Black magic correction for CR removal */
3078 for (i
=0; i
<file
->_cnt
; i
++) {
3079 if (file
->_ptr
[i
] == '\n')
3082 /* Black magic when reading CR at buffer boundary*/
3083 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
3092 /*********************************************************************
3095 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
3097 MSVCRT_size_t i
, len
= strlen(s
);
3098 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
3099 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3100 for (i
=0; i
<len
; i
++)
3101 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
3106 /*********************************************************************
3109 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
3111 MSVCRT_size_t i
, len
= strlenW(s
);
3112 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
3113 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
3114 for (i
=0; i
<len
; i
++)
3116 if ((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
3118 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
3124 /*********************************************************************
3125 * getchar (MSVCRT.@)
3127 int CDECL
MSVCRT_getchar(void)
3129 return MSVCRT_fgetc(MSVCRT_stdin
);
3132 /*********************************************************************
3135 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
3137 return MSVCRT_fgetc(file
);
3140 /*********************************************************************
3143 char * CDECL
MSVCRT_gets(char *buf
)
3146 char * buf_start
= buf
;
3148 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
3149 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
3150 if(cc
!= '\r') *buf
++ = (char)cc
;
3154 TRACE("got '%s'\n", buf_start
);
3158 /*********************************************************************
3161 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
3164 MSVCRT_wchar_t
* ws
= buf
;
3166 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
3167 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3170 *buf
++ = (MSVCRT_wchar_t
)cc
;
3174 TRACE("got %s\n", debugstr_w(ws
));
3178 /*********************************************************************
3181 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3183 return MSVCRT_fputc(c
, file
);
3186 /*********************************************************************
3187 * putchar (MSVCRT.@)
3189 int CDECL
MSVCRT_putchar(int c
)
3191 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3194 /*********************************************************************
3195 * _putwch (MSVCRT.@)
3197 int CDECL
MSVCRT__putwch(int c
)
3199 return MSVCRT_fputwc(c
, MSVCRT_stdout
);
3202 /*********************************************************************
3205 int CDECL
MSVCRT_puts(const char *s
)
3207 MSVCRT_size_t len
= strlen(s
);
3208 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3209 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3212 /*********************************************************************
3215 int CDECL
_putws(const MSVCRT_wchar_t
*s
)
3217 static const MSVCRT_wchar_t nl
= '\n';
3218 MSVCRT_size_t len
= strlenW(s
);
3219 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3220 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3223 /*********************************************************************
3226 int CDECL
MSVCRT_remove(const char *path
)
3228 TRACE("(%s)\n",path
);
3229 if (DeleteFileA(path
))
3231 TRACE(":failed (%d)\n",GetLastError());
3232 msvcrt_set_errno(GetLastError());
3236 /*********************************************************************
3237 * _wremove (MSVCRT.@)
3239 int CDECL
_wremove(const MSVCRT_wchar_t
*path
)
3241 TRACE("(%s)\n",debugstr_w(path
));
3242 if (DeleteFileW(path
))
3244 TRACE(":failed (%d)\n",GetLastError());
3245 msvcrt_set_errno(GetLastError());
3249 /*********************************************************************
3252 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3254 TRACE(":from %s to %s\n",oldpath
,newpath
);
3255 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3257 TRACE(":failed (%d)\n",GetLastError());
3258 msvcrt_set_errno(GetLastError());
3262 /*********************************************************************
3263 * _wrename (MSVCRT.@)
3265 int CDECL
_wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3267 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3268 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3270 TRACE(":failed (%d)\n",GetLastError());
3271 msvcrt_set_errno(GetLastError());
3275 /*********************************************************************
3276 * setvbuf (MSVCRT.@)
3278 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3280 /* TODO: Check if file busy */
3282 MSVCRT_free(file
->_base
);
3286 if(mode
== MSVCRT__IOFBF
) {
3287 file
->_flag
&= ~MSVCRT__IONBF
;
3288 file
->_base
= file
->_ptr
= buf
;
3290 file
->_bufsiz
= size
;
3293 file
->_flag
|= MSVCRT__IONBF
;
3298 /*********************************************************************
3301 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3303 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3306 /*********************************************************************
3309 char * CDECL
MSVCRT_tmpnam(char *s
)
3317 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3318 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3319 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3321 msvcrt_int_to_base32(unique
++, tmpstr
);
3323 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3324 GetLastError() == ERROR_FILE_NOT_FOUND
)
3330 /*********************************************************************
3331 * tmpfile (MSVCRT.@)
3333 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3335 char *filename
= MSVCRT_tmpnam(NULL
);
3337 MSVCRT_FILE
* file
= NULL
;
3340 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3341 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3343 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3348 else file
->_tmpfname
= _strdup(filename
);
3354 /*********************************************************************
3355 * vfprintf (MSVCRT.@)
3357 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3360 LPWSTR formatW
= NULL
;
3363 int written
, retval
;
3365 out
.unicode
= FALSE
;
3366 out
.buf
.A
= out
.grow
.A
= buf
;
3368 out
.len
= sizeof(buf
);
3370 sz
= MultiByteToWideChar( CP_ACP
, 0, format
, -1, NULL
, 0 );
3371 formatW
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
3372 MultiByteToWideChar( CP_ACP
, 0, format
, -1, formatW
, sz
);
3374 if ((written
= pf_vsnprintf( &out
, formatW
, NULL
, FALSE
, valist
)) >= 0)
3376 retval
= MSVCRT_fwrite(out
.buf
.A
, sizeof(*out
.buf
.A
), written
, file
);
3380 HeapFree( GetProcessHeap(), 0, formatW
);
3382 if (out
.buf
.A
!= out
.grow
.A
)
3383 MSVCRT_free (out
.buf
.A
);
3387 /*********************************************************************
3388 * vfprintf_s (MSVCRT.@)
3390 int CDECL
MSVCRT_vfprintf_s(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3393 LPWSTR formatW
= NULL
;
3396 int written
, retval
;
3398 if( !MSVCRT_CHECK_PMT( file
!= NULL
) )
3400 *MSVCRT__errno() = MSVCRT_EINVAL
;
3404 out
.unicode
= FALSE
;
3405 out
.buf
.A
= out
.grow
.A
= buf
;
3407 out
.len
= sizeof(buf
);
3409 sz
= MultiByteToWideChar( CP_ACP
, 0, format
, -1, NULL
, 0 );
3410 formatW
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
3411 MultiByteToWideChar( CP_ACP
, 0, format
, -1, formatW
, sz
);
3413 if ((written
= pf_vsnprintf( &out
, formatW
, NULL
, TRUE
, valist
)) >= 0)
3415 retval
= MSVCRT_fwrite(out
.buf
.A
, sizeof(*out
.buf
.A
), written
, file
);
3419 HeapFree( GetProcessHeap(), 0, formatW
);
3421 if (out
.buf
.A
!= out
.grow
.A
)
3422 MSVCRT_free (out
.buf
.A
);
3426 /*********************************************************************
3427 * vfwprintf (MSVCRT.@)
3429 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3431 MSVCRT_wchar_t buf
[2048];
3433 int written
, retval
;
3436 out
.buf
.W
= out
.grow
.W
= buf
;
3438 out
.len
= sizeof(buf
) / sizeof(buf
[0]);
3440 if ((written
= pf_vsnprintf( &out
, format
, NULL
, FALSE
, valist
)) >= 0)
3442 retval
= MSVCRT_fwrite(out
.buf
.W
, sizeof(*out
.buf
.W
), written
, file
);
3445 if (out
.buf
.W
!= out
.grow
.W
)
3446 MSVCRT_free (out
.buf
.W
);
3450 /*********************************************************************
3451 * vfwprintf_s (MSVCRT.@)
3453 int CDECL
MSVCRT_vfwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3455 MSVCRT_wchar_t buf
[2048];
3457 int written
, retval
;
3459 if( !MSVCRT_CHECK_PMT( file
!= NULL
) )
3461 *MSVCRT__errno() = MSVCRT_EINVAL
;
3466 out
.buf
.W
= out
.grow
.W
= buf
;
3468 out
.len
= sizeof(buf
) / sizeof(buf
[0]);
3470 if ((written
= pf_vsnprintf( &out
, format
, NULL
, TRUE
, valist
)) >= 0)
3472 retval
= MSVCRT_fwrite(out
.buf
.W
, sizeof(*out
.buf
.W
), written
, file
);
3475 if (out
.buf
.W
!= out
.grow
.W
)
3476 MSVCRT_free (out
.buf
.W
);
3480 /*********************************************************************
3481 * vprintf (MSVCRT.@)
3483 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3485 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3488 /*********************************************************************
3489 * vprintf_s (MSVCRT.@)
3491 int CDECL
MSVCRT_vprintf_s(const char *format
, __ms_va_list valist
)
3493 return MSVCRT_vfprintf_s(MSVCRT_stdout
,format
,valist
);
3496 /*********************************************************************
3497 * vwprintf (MSVCRT.@)
3499 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3501 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3504 /*********************************************************************
3505 * vwprintf_s (MSVCRT.@)
3507 int CDECL
MSVCRT_vwprintf_s(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3509 return MSVCRT_vfwprintf_s(MSVCRT_stdout
,format
,valist
);
3512 /*********************************************************************
3513 * fprintf (MSVCRT.@)
3515 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3517 __ms_va_list valist
;
3519 __ms_va_start(valist
, format
);
3520 res
= MSVCRT_vfprintf(file
, format
, valist
);
3521 __ms_va_end(valist
);
3525 /*********************************************************************
3526 * fprintf_s (MSVCRT.@)
3528 int CDECL
MSVCRT_fprintf_s(MSVCRT_FILE
* file
, const char *format
, ...)
3530 __ms_va_list valist
;
3532 __ms_va_start(valist
, format
);
3533 res
= MSVCRT_vfprintf_s(file
, format
, valist
);
3534 __ms_va_end(valist
);
3538 /*********************************************************************
3539 * fwprintf (MSVCRT.@)
3541 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3543 __ms_va_list valist
;
3545 __ms_va_start(valist
, format
);
3546 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3547 __ms_va_end(valist
);
3551 /*********************************************************************
3552 * fwprintf_s (MSVCRT.@)
3554 int CDECL
MSVCRT_fwprintf_s(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3556 __ms_va_list valist
;
3558 __ms_va_start(valist
, format
);
3559 res
= MSVCRT_vfwprintf_s(file
, format
, valist
);
3560 __ms_va_end(valist
);
3564 /*********************************************************************
3567 int CDECL
MSVCRT_printf(const char *format
, ...)
3569 __ms_va_list valist
;
3571 __ms_va_start(valist
, format
);
3572 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3573 __ms_va_end(valist
);
3577 /*********************************************************************
3578 * printf_s (MSVCRT.@)
3580 int CDECL
MSVCRT_printf_s(const char *format
, ...)
3582 __ms_va_list valist
;
3584 __ms_va_start(valist
, format
);
3585 res
= MSVCRT_vprintf_s(format
, valist
);
3586 __ms_va_end(valist
);
3590 /*********************************************************************
3593 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3595 if (c
== MSVCRT_EOF
)
3597 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3598 msvcrt_alloc_buffer(file
);
3601 if(file
->_ptr
>file
->_base
) {
3605 MSVCRT_clearerr(file
);
3611 /*********************************************************************
3612 * ungetwc (MSVCRT.@)
3614 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3616 MSVCRT_wchar_t mwc
= wc
;
3617 char * pp
= (char *)&mwc
;
3619 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3620 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
3626 /*********************************************************************
3627 * wprintf (MSVCRT.@)
3629 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
3631 __ms_va_list valist
;
3633 __ms_va_start(valist
, format
);
3634 res
= MSVCRT_vwprintf(format
, valist
);
3635 __ms_va_end(valist
);
3639 /*********************************************************************
3640 * wprintf_s (MSVCRT.@)
3642 int CDECL
MSVCRT_wprintf_s(const MSVCRT_wchar_t
*format
, ...)
3644 __ms_va_list valist
;
3646 __ms_va_start(valist
, format
);
3647 res
= MSVCRT_vwprintf_s(format
, valist
);
3648 __ms_va_end(valist
);
3652 /*********************************************************************
3653 * _getmaxstdio (MSVCRT.@)
3655 int CDECL
_getmaxstdio(void)
3657 FIXME("stub, always returns 512\n");
3661 /*********************************************************************
3662 * _setmaxstdio_ (MSVCRT.@)
3664 int CDECL
_setmaxstdio(int newmax
)
3671 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3675 /*********************************************************************
3676 * __pioinfo (MSVCRT.@)
3677 * FIXME: see MSVCRT_MAX_FILES define.
3679 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3680 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
3681 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
3682 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
3683 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
3684 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
3685 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
3686 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
3687 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
3688 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
3689 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
3690 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
3693 /*********************************************************************
3694 * __badioinfo (MSVCRT.@)
3696 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};