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");
223 MSVCRT_fdesc
[fd
].handle
= hand
;
224 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
226 /* locate next free slot */
227 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
228 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
230 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
231 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
233 /* update last fd in use */
234 if (fd
>= MSVCRT_fdend
)
235 MSVCRT_fdend
= fd
+ 1;
236 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
240 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
241 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
242 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
248 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
249 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
254 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
255 ret
= msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
260 /* INTERNAL: Allocate a FILE* for an fd slot */
261 /* caller must hold the files lock */
262 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
266 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
268 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
270 if (!MSVCRT_fstreams
[i
])
272 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
274 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
276 return MSVCRT_fstreams
[i
];
282 /* INTERNAL: initialize a FILE* from an open fd */
283 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
285 TRACE(":fd (%d) allocating FILE*\n",fd
);
286 if (!msvcrt_is_valid_fd(fd
))
288 WARN(":invalid fd %d\n",fd
);
289 *MSVCRT___doserrno() = 0;
290 *MSVCRT__errno() = MSVCRT_EBADF
;
293 memset(file
, 0, sizeof(*file
));
295 file
->_flag
= stream_flags
;
297 TRACE(":got FILE* (%p)\n",file
);
301 /* INTERNAL: Create an inheritance data block (for spawned process)
302 * The inheritance block is made of:
303 * 00 int nb of file descriptor (NBFD)
304 * 04 char file flags (wxflag): repeated for each fd
305 * 4+NBFD HANDLE file handle: repeated for each fd
307 unsigned msvcrt_create_io_inherit_block(WORD
*size
, BYTE
**block
)
313 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
314 *block
= MSVCRT_calloc(*size
, 1);
320 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
321 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
323 *(unsigned*)*block
= MSVCRT_fdend
;
324 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
326 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
327 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
329 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
330 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
335 *handle_ptr
= INVALID_HANDLE_VALUE
;
337 wxflag_ptr
++; handle_ptr
++;
342 /* INTERNAL: Set up all file descriptors,
343 * as well as default streams (stdin, stderr and stdout)
345 void msvcrt_init_io(void)
350 InitializeCriticalSection(&MSVCRT_file_cs
);
351 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MSVCRT_file_cs");
352 GetStartupInfoA(&si
);
353 if (si
.cbReserved2
>= sizeof(unsigned int) && si
.lpReserved2
!= NULL
)
359 count
= *(unsigned*)si
.lpReserved2
;
360 wxflag_ptr
= si
.lpReserved2
+ sizeof(unsigned);
361 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ count
);
363 count
= min(count
, (si
.cbReserved2
- sizeof(unsigned)) / (sizeof(HANDLE
) + 1));
364 count
= min(count
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
365 for (i
= 0; i
< count
; i
++)
367 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
369 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
370 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
374 MSVCRT_fdesc
[i
].wxflag
= 0;
375 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
377 wxflag_ptr
++; handle_ptr
++;
379 MSVCRT_fdend
= max( 3, count
);
380 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
381 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
384 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
386 HANDLE std
= GetStdHandle(STD_INPUT_HANDLE
);
387 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
388 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
,
389 0, TRUE
, DUPLICATE_SAME_ACCESS
))
390 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
392 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
394 HANDLE std
= GetStdHandle(STD_OUTPUT_HANDLE
);
395 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
396 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
,
397 0, TRUE
, DUPLICATE_SAME_ACCESS
))
398 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
400 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
402 HANDLE std
= GetStdHandle(STD_ERROR_HANDLE
);
403 if (std
!= INVALID_HANDLE_VALUE
&& DuplicateHandle(GetCurrentProcess(), std
,
404 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
,
405 0, TRUE
, DUPLICATE_SAME_ACCESS
))
406 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
409 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
410 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
412 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
413 for (i
= 0; i
< 3; i
++)
415 /* FILE structs for stdin/out/err are static and never deleted */
416 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
417 MSVCRT__iob
[i
]._file
= i
;
418 MSVCRT__iob
[i
]._tmpfname
= NULL
;
419 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
421 MSVCRT_stream_idx
= 3;
424 /* INTERNAL: Flush stdio file buffer */
425 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
428 int cnt
=file
->_ptr
-file
->_base
;
429 if(cnt
>0 && MSVCRT__write(file
->_file
, file
->_base
, cnt
) != cnt
) {
430 file
->_flag
|= MSVCRT__IOERR
;
433 file
->_ptr
=file
->_base
;
434 file
->_cnt
=file
->_bufsiz
;
439 /* INTERNAL: Allocate stdio file buffer */
440 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
442 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
444 file
->_bufsiz
= MSVCRT_BUFSIZ
;
445 file
->_flag
|= MSVCRT__IOMYBUF
;
447 file
->_base
= (char*)(&file
->_charbuf
);
449 file
->_bufsiz
= sizeof(file
->_charbuf
);
451 file
->_ptr
= file
->_base
;
455 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
456 static void msvcrt_int_to_base32(int num
, char *str
)
471 *p
= (num
& 31) + '0';
473 *p
+= ('a' - '0' - 10);
478 /*********************************************************************
479 * __iob_func(MSVCRT.@)
481 MSVCRT_FILE
* CDECL
MSVCRT___iob_func(void)
483 return &MSVCRT__iob
[0];
486 /*********************************************************************
489 int CDECL
MSVCRT__access(const char *filename
, int mode
)
491 DWORD attr
= GetFileAttributesA(filename
);
493 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
495 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
497 msvcrt_set_errno(GetLastError());
500 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
502 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
508 /*********************************************************************
509 * _waccess (MSVCRT.@)
511 int CDECL
_waccess(const MSVCRT_wchar_t
*filename
, int mode
)
513 DWORD attr
= GetFileAttributesW(filename
);
515 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
517 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
519 msvcrt_set_errno(GetLastError());
522 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
524 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
530 /*********************************************************************
533 int CDECL
MSVCRT__chmod(const char *path
, int flags
)
535 DWORD oldFlags
= GetFileAttributesA(path
);
537 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
539 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
540 oldFlags
| FILE_ATTRIBUTE_READONLY
;
542 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
545 msvcrt_set_errno(GetLastError());
549 /*********************************************************************
552 int CDECL
_wchmod(const MSVCRT_wchar_t
*path
, int flags
)
554 DWORD oldFlags
= GetFileAttributesW(path
);
556 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
558 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
559 oldFlags
| FILE_ATTRIBUTE_READONLY
;
561 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
564 msvcrt_set_errno(GetLastError());
568 /*********************************************************************
571 int CDECL
MSVCRT__unlink(const char *path
)
573 TRACE("%s\n",debugstr_a(path
));
574 if(DeleteFileA(path
))
576 TRACE("failed (%d)\n",GetLastError());
577 msvcrt_set_errno(GetLastError());
581 /*********************************************************************
582 * _wunlink (MSVCRT.@)
584 int CDECL
_wunlink(const MSVCRT_wchar_t
*path
)
586 TRACE("(%s)\n",debugstr_w(path
));
587 if(DeleteFileW(path
))
589 TRACE("failed (%d)\n",GetLastError());
590 msvcrt_set_errno(GetLastError());
594 /* _flushall calls MSVCRT_fflush which calls _flushall */
595 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
);
597 /*********************************************************************
598 * _flushall (MSVCRT.@)
600 int CDECL
_flushall(void)
602 int i
, num_flushed
= 0;
605 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
606 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
609 /* FIXME: flush, do not commit */
610 if (_commit(i
) == -1)
611 if (MSVCRT_fstreams
[i
])
612 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
614 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
615 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
621 TRACE(":flushed (%d) handles\n",num_flushed
);
625 /*********************************************************************
628 int CDECL
MSVCRT_fflush(MSVCRT_FILE
* file
)
632 } else if(file
->_flag
& MSVCRT__IOWRT
) {
633 int res
=msvcrt_flush_buffer(file
);
639 /*********************************************************************
642 int CDECL
MSVCRT__close(int fd
)
648 hand
= msvcrt_fdtoh(fd
);
649 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
650 if (hand
== INVALID_HANDLE_VALUE
)
652 else if (!CloseHandle(hand
))
654 WARN(":failed-last error (%d)\n",GetLastError());
655 msvcrt_set_errno(GetLastError());
668 /*********************************************************************
671 int CDECL
_commit(int fd
)
673 HANDLE hand
= msvcrt_fdtoh(fd
);
675 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
676 if (hand
== INVALID_HANDLE_VALUE
)
679 if (!FlushFileBuffers(hand
))
681 if (GetLastError() == ERROR_INVALID_HANDLE
)
683 /* FlushFileBuffers fails for console handles
684 * so we ignore this error.
688 TRACE(":failed-last error (%d)\n",GetLastError());
689 msvcrt_set_errno(GetLastError());
696 /*********************************************************************
699 * MSDN isn't clear on this point, but the remarks for _pipe
700 * indicate file descriptors duplicated with _dup and _dup2 are always
703 int CDECL
MSVCRT__dup2(int od
, int nd
)
707 TRACE("(od=%d, nd=%d)\n", od
, nd
);
709 if (nd
< MSVCRT_MAX_FILES
&& nd
>= 0 && msvcrt_is_valid_fd(od
))
713 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
714 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
716 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
718 if (msvcrt_is_valid_fd(nd
))
720 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
724 *MSVCRT__errno() = MSVCRT_EMFILE
;
728 /* _dup2 returns 0, not nd, on success */
735 msvcrt_set_errno(GetLastError());
740 *MSVCRT__errno() = MSVCRT_EBADF
;
747 /*********************************************************************
750 int CDECL
MSVCRT__dup(int od
)
756 if (MSVCRT__dup2(od
, fd
) == 0)
764 /*********************************************************************
767 int CDECL
_eof(int fd
)
770 LONG hcurpos
,hendpos
;
771 HANDLE hand
= msvcrt_fdtoh(fd
);
773 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
775 if (hand
== INVALID_HANDLE_VALUE
)
778 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
780 /* Otherwise we do it the hard way */
781 hcurpos
= hendpos
= 0;
782 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
783 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
785 if (curpos
== endpos
&& hcurpos
== hendpos
)
787 /* FIXME: shouldn't WX_ATEOF be set here? */
791 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
795 /*********************************************************************
796 * _fcloseall (MSVCRT.@)
798 int CDECL
MSVCRT__fcloseall(void)
800 int num_closed
= 0, i
;
803 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
804 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
805 !MSVCRT_fclose(MSVCRT_fstreams
[i
]))
809 TRACE(":closed (%d) handles\n",num_closed
);
813 /* free everything on process exit */
814 void msvcrt_free_io(void)
817 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
818 * stdout, and stderr (unlike GNU), so we need to fclose() them here
819 * or they won't get flushed.
821 MSVCRT_fclose(&MSVCRT__iob
[0]);
822 MSVCRT_fclose(&MSVCRT__iob
[1]);
823 MSVCRT_fclose(&MSVCRT__iob
[2]);
824 MSVCRT_file_cs
.DebugInfo
->Spare
[0] = 0;
825 DeleteCriticalSection(&MSVCRT_file_cs
);
828 /*********************************************************************
829 * _lseeki64 (MSVCRT.@)
831 __int64 CDECL
MSVCRT__lseeki64(int fd
, __int64 offset
, int whence
)
833 HANDLE hand
= msvcrt_fdtoh(fd
);
834 LARGE_INTEGER ofs
, ret
;
836 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
837 if (hand
== INVALID_HANDLE_VALUE
)
840 if (whence
< 0 || whence
> 2)
842 *MSVCRT__errno() = MSVCRT_EINVAL
;
846 TRACE(":fd (%d) to %s pos %s\n",
847 fd
,wine_dbgstr_longlong(offset
),
848 (whence
==SEEK_SET
)?"SEEK_SET":
849 (whence
==SEEK_CUR
)?"SEEK_CUR":
850 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
852 ofs
.QuadPart
= offset
;
853 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
855 MSVCRT_fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
856 /* FIXME: What if we seek _to_ EOF - is EOF set? */
860 TRACE(":error-last error (%d)\n",GetLastError());
861 msvcrt_set_errno(GetLastError());
865 /*********************************************************************
868 LONG CDECL
MSVCRT__lseek(int fd
, LONG offset
, int whence
)
870 return MSVCRT__lseeki64(fd
, offset
, whence
);
873 /*********************************************************************
874 * _locking (MSVCRT.@)
876 * This is untested; the underlying LockFile doesn't work yet.
878 int CDECL
MSVCRT__locking(int fd
, int mode
, LONG nbytes
)
882 HANDLE hand
= msvcrt_fdtoh(fd
);
884 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
885 if (hand
== INVALID_HANDLE_VALUE
)
888 if (mode
< 0 || mode
> 4)
890 *MSVCRT__errno() = MSVCRT_EINVAL
;
894 TRACE(":fd (%d) by 0x%08x mode %s\n",
895 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
896 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
897 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
898 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
899 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
902 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
904 FIXME ("Seek failed\n");
905 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
908 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
911 ret
= 1; /* just to satisfy gcc */
914 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
919 else if (mode
== MSVCRT__LK_UNLCK
)
920 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
922 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
923 /* FIXME - what about error settings? */
927 /*********************************************************************
930 int CDECL
MSVCRT_fseek(MSVCRT_FILE
* file
, MSVCRT_long offset
, int whence
)
932 /* Flush output if needed */
933 if(file
->_flag
& MSVCRT__IOWRT
)
934 msvcrt_flush_buffer(file
);
936 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
937 offset
-= file
->_cnt
;
938 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
939 /* Black magic correction for CR removal */
941 for (i
=0; i
<file
->_cnt
; i
++) {
942 if (file
->_ptr
[i
] == '\n')
945 /* Black magic when reading CR at buffer boundary*/
946 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
950 /* Discard buffered input */
952 file
->_ptr
= file
->_base
;
953 /* Reset direction of i/o */
954 if(file
->_flag
& MSVCRT__IORW
) {
955 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
957 /* Clear end of file flag */
958 file
->_flag
&= ~MSVCRT__IOEOF
;
959 return (MSVCRT__lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
962 /*********************************************************************
965 int CDECL
_chsize(int fd
, MSVCRT_long size
)
971 TRACE("(fd=%d, size=%d)\n", fd
, size
);
975 handle
= msvcrt_fdtoh(fd
);
976 if (handle
!= INVALID_HANDLE_VALUE
)
978 /* save the current file pointer */
979 cur
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
982 pos
= MSVCRT__lseek(fd
, size
, SEEK_SET
);
985 ret
= SetEndOfFile(handle
);
986 if (!ret
) msvcrt_set_errno(GetLastError());
989 /* restore the file pointer */
990 MSVCRT__lseek(fd
, cur
, SEEK_SET
);
998 /*********************************************************************
999 * clearerr (MSVCRT.@)
1001 void CDECL
MSVCRT_clearerr(MSVCRT_FILE
* file
)
1003 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1004 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1007 /*********************************************************************
1010 void CDECL
MSVCRT_rewind(MSVCRT_FILE
* file
)
1012 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1013 MSVCRT_fseek(file
, 0L, SEEK_SET
);
1014 MSVCRT_clearerr(file
);
1017 static int msvcrt_get_flags(const MSVCRT_wchar_t
* mode
, int *open_flags
, int* stream_flags
)
1019 int plus
= strchrW(mode
, '+') != NULL
;
1024 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
1025 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
1028 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1029 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1032 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
1033 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
1043 *open_flags
|= MSVCRT__O_BINARY
;
1044 *open_flags
&= ~MSVCRT__O_TEXT
;
1047 *open_flags
|= MSVCRT__O_TEXT
;
1048 *open_flags
&= ~MSVCRT__O_BINARY
;
1054 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1059 /*********************************************************************
1060 * _fdopen (MSVCRT.@)
1062 MSVCRT_FILE
* CDECL
MSVCRT__fdopen(int fd
, const char *mode
)
1065 MSVCRT_wchar_t
*modeW
= NULL
;
1067 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
))) return NULL
;
1069 ret
= MSVCRT__wfdopen(fd
, modeW
);
1075 /*********************************************************************
1076 * _wfdopen (MSVCRT.@)
1078 MSVCRT_FILE
* CDECL
MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
1080 int open_flags
, stream_flags
;
1083 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1086 if (!(file
= msvcrt_alloc_fp()))
1088 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
1093 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
1099 /*********************************************************************
1100 * _filelength (MSVCRT.@)
1102 LONG CDECL
MSVCRT__filelength(int fd
)
1104 LONG curPos
= MSVCRT__lseek(fd
, 0, SEEK_CUR
);
1107 LONG endPos
= MSVCRT__lseek(fd
, 0, SEEK_END
);
1110 if (endPos
!= curPos
)
1111 MSVCRT__lseek(fd
, curPos
, SEEK_SET
);
1118 /*********************************************************************
1119 * _filelengthi64 (MSVCRT.@)
1121 __int64 CDECL
MSVCRT__filelengthi64(int fd
)
1123 __int64 curPos
= MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
1126 __int64 endPos
= MSVCRT__lseeki64(fd
, 0, SEEK_END
);
1129 if (endPos
!= curPos
)
1130 MSVCRT__lseeki64(fd
, curPos
, SEEK_SET
);
1137 /*********************************************************************
1138 * _fileno (MSVCRT.@)
1140 int CDECL
MSVCRT__fileno(MSVCRT_FILE
* file
)
1142 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1146 /*********************************************************************
1147 * _fstat64 (MSVCRT.@)
1149 int CDECL
MSVCRT__fstat64(int fd
, struct MSVCRT__stat64
* buf
)
1153 BY_HANDLE_FILE_INFORMATION hfi
;
1154 HANDLE hand
= msvcrt_fdtoh(fd
);
1156 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1157 if (hand
== INVALID_HANDLE_VALUE
)
1162 WARN(":failed-NULL buf\n");
1163 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1167 memset(&hfi
, 0, sizeof(hfi
));
1168 memset(buf
, 0, sizeof(struct MSVCRT__stat64
));
1169 type
= GetFileType(hand
);
1170 if (type
== FILE_TYPE_PIPE
)
1172 buf
->st_dev
= buf
->st_rdev
= fd
;
1173 buf
->st_mode
= S_IFIFO
;
1176 else if (type
== FILE_TYPE_CHAR
)
1178 buf
->st_dev
= buf
->st_rdev
= fd
;
1179 buf
->st_mode
= S_IFCHR
;
1182 else /* FILE_TYPE_DISK etc. */
1184 if (!GetFileInformationByHandle(hand
, &hfi
))
1186 WARN(":failed-last error (%d)\n",GetLastError());
1187 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1190 buf
->st_mode
= S_IFREG
| 0444;
1191 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1192 buf
->st_mode
|= 0222;
1193 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1194 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1196 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1197 buf
->st_mtime
= buf
->st_ctime
= dw
;
1198 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1200 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1205 /*********************************************************************
1206 * _fstati64 (MSVCRT.@)
1208 int CDECL
MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1211 struct MSVCRT__stat64 buf64
;
1213 ret
= MSVCRT__fstat64(fd
, &buf64
);
1215 msvcrt_stat64_to_stati64(&buf64
, buf
);
1219 /*********************************************************************
1222 int CDECL
MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1224 struct MSVCRT__stat64 buf64
;
1226 ret
= MSVCRT__fstat64(fd
, &buf64
);
1228 msvcrt_stat64_to_stat(&buf64
, buf
);
1232 /*********************************************************************
1233 * _futime64 (MSVCRT.@)
1235 int CDECL
_futime64(int fd
, struct MSVCRT___utimbuf64
*t
)
1237 HANDLE hand
= msvcrt_fdtoh(fd
);
1242 time_to_filetime( MSVCRT__time64(NULL
), &at
);
1247 time_to_filetime( t
->actime
, &at
);
1248 time_to_filetime( t
->modtime
, &wt
);
1251 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1253 msvcrt_set_errno(GetLastError());
1259 /*********************************************************************
1260 * _futime32 (MSVCRT.@)
1262 int CDECL
_futime32(int fd
, struct MSVCRT___utimbuf32
*t
)
1264 struct MSVCRT___utimbuf64 t64
;
1265 t64
.actime
= t
->actime
;
1266 t64
.modtime
= t
->modtime
;
1267 return _futime64( fd
, &t64
);
1270 /*********************************************************************
1271 * _futime (MSVCRT.@)
1274 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf64
*t
)
1276 return _futime64( fd
, t
);
1279 int CDECL
_futime(int fd
, struct MSVCRT___utimbuf32
*t
)
1281 return _futime32( fd
, t
);
1285 /*********************************************************************
1286 * _get_osfhandle (MSVCRT.@)
1288 MSVCRT_intptr_t CDECL
_get_osfhandle(int fd
)
1290 HANDLE hand
= msvcrt_fdtoh(fd
);
1291 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1293 return (MSVCRT_intptr_t
)hand
;
1296 /*********************************************************************
1297 * _isatty (MSVCRT.@)
1299 int CDECL
_isatty(int fd
)
1301 HANDLE hand
= msvcrt_fdtoh(fd
);
1303 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1304 if (hand
== INVALID_HANDLE_VALUE
)
1307 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1310 /*********************************************************************
1311 * _mktemp (MSVCRT.@)
1313 char * CDECL
_mktemp(char *pattern
)
1316 char *retVal
= pattern
;
1321 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1325 id
= GetCurrentProcessId();
1329 int tempNum
= id
/ 10;
1330 *pattern
-- = id
- (tempNum
* 10) + '0';
1336 *pattern
= letter
++;
1337 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1338 GetLastError() == ERROR_FILE_NOT_FOUND
)
1340 } while(letter
<= 'z');
1344 /*********************************************************************
1345 * _wmktemp (MSVCRT.@)
1347 MSVCRT_wchar_t
* CDECL
_wmktemp(MSVCRT_wchar_t
*pattern
)
1350 MSVCRT_wchar_t
*retVal
= pattern
;
1352 MSVCRT_wchar_t letter
= 'a';
1355 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1359 id
= GetCurrentProcessId();
1363 int tempNum
= id
/ 10;
1364 *pattern
-- = id
- (tempNum
* 10) + '0';
1370 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1371 GetLastError() == ERROR_FILE_NOT_FOUND
)
1373 *pattern
= letter
++;
1374 } while(letter
!= '|');
1378 static unsigned split_oflags(unsigned oflags
)
1381 unsigned unsupp
; /* until we support everything */
1383 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1384 if (oflags
& MSVCRT__O_BINARY
) {/* Nothing to do */}
1385 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1386 else if (*__p__fmode() & MSVCRT__O_BINARY
) {/* Nothing to do */}
1387 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1388 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1390 if ((unsupp
= oflags
& ~(
1391 MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|
1392 MSVCRT__O_TRUNC
|MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|
1393 MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|MSVCRT__O_TEMPORARY
|
1394 MSVCRT__O_NOINHERIT
|
1395 MSVCRT__O_SEQUENTIAL
|MSVCRT__O_RANDOM
|MSVCRT__O_SHORT_LIVED
1397 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1402 /*********************************************************************
1405 int CDECL
MSVCRT__pipe(int *pfds
, unsigned int psize
, int textmode
)
1408 SECURITY_ATTRIBUTES sa
;
1409 HANDLE readHandle
, writeHandle
;
1413 *MSVCRT__errno() = MSVCRT_EINVAL
;
1417 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1418 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1419 sa
.lpSecurityDescriptor
= NULL
;
1420 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1422 unsigned int wxflags
= split_oflags(textmode
);
1426 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1430 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1438 MSVCRT__close(pfds
[0]);
1439 CloseHandle(writeHandle
);
1440 *MSVCRT__errno() = MSVCRT_EMFILE
;
1445 CloseHandle(readHandle
);
1446 CloseHandle(writeHandle
);
1447 *MSVCRT__errno() = MSVCRT_EMFILE
;
1452 msvcrt_set_errno(GetLastError());
1457 /*********************************************************************
1460 int CDECL
MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1464 DWORD access
= 0, creation
= 0, attrib
;
1468 SECURITY_ATTRIBUTES sa
;
1471 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1472 path
, oflags
, shflags
);
1474 wxflag
= split_oflags(oflags
);
1475 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1477 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1478 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1479 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1482 if (oflags
& MSVCRT__O_CREAT
)
1484 __ms_va_start(ap
, shflags
);
1485 pmode
= va_arg(ap
, int);
1488 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1489 FIXME(": pmode 0x%04x ignored\n", pmode
);
1491 WARN(": pmode 0x%04x ignored\n", pmode
);
1493 if (oflags
& MSVCRT__O_EXCL
)
1494 creation
= CREATE_NEW
;
1495 else if (oflags
& MSVCRT__O_TRUNC
)
1496 creation
= CREATE_ALWAYS
;
1498 creation
= OPEN_ALWAYS
;
1500 else /* no MSVCRT__O_CREAT */
1502 if (oflags
& MSVCRT__O_TRUNC
)
1503 creation
= TRUNCATE_EXISTING
;
1505 creation
= OPEN_EXISTING
;
1510 case MSVCRT__SH_DENYRW
:
1513 case MSVCRT__SH_DENYWR
:
1514 sharing
= FILE_SHARE_READ
;
1516 case MSVCRT__SH_DENYRD
:
1517 sharing
= FILE_SHARE_WRITE
;
1519 case MSVCRT__SH_DENYNO
:
1520 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1523 ERR( "Unhandled shflags 0x%x\n", shflags
);
1526 attrib
= FILE_ATTRIBUTE_NORMAL
;
1528 if (oflags
& MSVCRT__O_TEMPORARY
)
1530 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1532 sharing
|= FILE_SHARE_DELETE
;
1535 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1536 sa
.lpSecurityDescriptor
= NULL
;
1537 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1539 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1541 if (hand
== INVALID_HANDLE_VALUE
) {
1542 WARN(":failed-last error (%d)\n",GetLastError());
1543 msvcrt_set_errno(GetLastError());
1547 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1549 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1553 /*********************************************************************
1554 * _wsopen (MSVCRT.@)
1556 int CDECL
MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1560 DWORD access
= 0, creation
= 0, attrib
;
1564 SECURITY_ATTRIBUTES sa
;
1567 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1568 debugstr_w(path
), oflags
, shflags
);
1570 wxflag
= split_oflags(oflags
);
1571 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1573 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1574 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1575 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1578 if (oflags
& MSVCRT__O_CREAT
)
1580 __ms_va_start(ap
, shflags
);
1581 pmode
= va_arg(ap
, int);
1584 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1585 FIXME(": pmode 0x%04x ignored\n", pmode
);
1587 WARN(": pmode 0x%04x ignored\n", pmode
);
1589 if (oflags
& MSVCRT__O_EXCL
)
1590 creation
= CREATE_NEW
;
1591 else if (oflags
& MSVCRT__O_TRUNC
)
1592 creation
= CREATE_ALWAYS
;
1594 creation
= OPEN_ALWAYS
;
1596 else /* no MSVCRT__O_CREAT */
1598 if (oflags
& MSVCRT__O_TRUNC
)
1599 creation
= TRUNCATE_EXISTING
;
1601 creation
= OPEN_EXISTING
;
1606 case MSVCRT__SH_DENYRW
:
1609 case MSVCRT__SH_DENYWR
:
1610 sharing
= FILE_SHARE_READ
;
1612 case MSVCRT__SH_DENYRD
:
1613 sharing
= FILE_SHARE_WRITE
;
1615 case MSVCRT__SH_DENYNO
:
1616 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1619 ERR( "Unhandled shflags 0x%x\n", shflags
);
1622 attrib
= FILE_ATTRIBUTE_NORMAL
;
1624 if (oflags
& MSVCRT__O_TEMPORARY
)
1626 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1628 sharing
|= FILE_SHARE_DELETE
;
1631 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1632 sa
.lpSecurityDescriptor
= NULL
;
1633 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1635 hand
= CreateFileW(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1637 if (hand
== INVALID_HANDLE_VALUE
) {
1638 WARN(":failed-last error (%d)\n",GetLastError());
1639 msvcrt_set_errno(GetLastError());
1643 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1645 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1649 /*********************************************************************
1652 int CDECL
MSVCRT__open( const char *path
, int flags
, ... )
1656 if (flags
& MSVCRT__O_CREAT
)
1659 __ms_va_start(ap
, flags
);
1660 pmode
= va_arg(ap
, int);
1662 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1665 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1668 /*********************************************************************
1671 int CDECL
_wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1675 if (flags
& MSVCRT__O_CREAT
)
1678 __ms_va_start(ap
, flags
);
1679 pmode
= va_arg(ap
, int);
1681 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1684 return MSVCRT__wsopen( path
, flags
, MSVCRT__SH_DENYNO
);
1687 /*********************************************************************
1690 int CDECL
MSVCRT__creat(const char *path
, int flags
)
1692 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1693 return MSVCRT__open(path
, usedFlags
);
1696 /*********************************************************************
1697 * _wcreat (MSVCRT.@)
1699 int CDECL
_wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1701 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1702 return _wopen(path
, usedFlags
);
1705 /*********************************************************************
1706 * _open_osfhandle (MSVCRT.@)
1708 int CDECL
_open_osfhandle(MSVCRT_intptr_t handle
, int oflags
)
1712 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1713 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1714 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1715 * text - it never sets MSVCRT__O_BINARY.
1717 /* don't let split_oflags() decide the mode if no mode is passed */
1718 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)))
1719 oflags
|= MSVCRT__O_BINARY
;
1721 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1722 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1726 /*********************************************************************
1729 int CDECL
_rmtmp(void)
1731 int num_removed
= 0, i
;
1734 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1735 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1737 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1743 TRACE(":removed (%d) temp files\n",num_removed
);
1747 /*********************************************************************
1750 * When reading \r as last character in text mode, read() positions
1751 * the file pointer on the \r character while getc() goes on to
1754 static int read_i(int fd
, void *buf
, unsigned int count
)
1757 char *bufstart
= buf
;
1758 HANDLE hand
= msvcrt_fdtoh(fd
);
1763 if (MSVCRT_fdesc
[fd
].wxflag
& WX_READEOF
) {
1764 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1765 TRACE("already at EOF, returning 0\n");
1768 /* Don't trace small reads, it gets *very* annoying */
1770 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1771 if (hand
== INVALID_HANDLE_VALUE
)
1774 /* Reading single bytes in O_TEXT mode makes things slow
1775 * So read big chunks
1777 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1779 if (count
!= 0 && num_read
== 0)
1781 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1782 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1784 else if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1787 if (bufstart
[num_read
-1] == '\r')
1791 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_READCR
;
1792 ReadFile(hand
, bufstart
, 1, &num_read
, NULL
);
1796 MSVCRT_fdesc
[fd
].wxflag
|= WX_READCR
;
1801 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_READCR
;
1802 for (i
=0, j
=0; i
<num_read
; i
++)
1804 /* in text mode, a ctrl-z signals EOF */
1805 if (bufstart
[i
] == 0x1a)
1807 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1808 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1811 /* in text mode, strip \r if followed by \n.
1812 * BUG: should save state across calls somehow, so CR LF that
1813 * straddles buffer boundary gets recognized properly?
1815 if ((bufstart
[i
] != '\r')
1816 || ((i
+1) < num_read
&& bufstart
[i
+1] != '\n'))
1817 bufstart
[j
++] = bufstart
[i
];
1824 if (GetLastError() == ERROR_BROKEN_PIPE
)
1826 TRACE(":end-of-pipe\n");
1827 MSVCRT_fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1832 TRACE(":failed-last error (%d)\n",GetLastError());
1838 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1842 /*********************************************************************
1845 int CDECL
MSVCRT__read(int fd
, void *buf
, unsigned int count
)
1848 num_read
= read_i(fd
, buf
, count
);
1852 /*********************************************************************
1853 * _setmode (MSVCRT.@)
1855 int CDECL
_setmode(int fd
,int mode
)
1857 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1858 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1859 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1860 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1861 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1863 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1867 /*********************************************************************
1868 * _stat64 (MSVCRT.@)
1870 int CDECL
MSVCRT_stat64(const char* path
, struct MSVCRT__stat64
* buf
)
1873 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1874 unsigned short mode
= ALL_S_IREAD
;
1877 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1879 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1881 TRACE("failed (%d)\n",GetLastError());
1882 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1886 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1888 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1889 Bon 011120: This FIXME seems incorrect
1890 Also a letter as first char isn't enough to be classified
1893 if (isalpha(*path
)&& (*(path
+1)==':'))
1894 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1896 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1898 plen
= strlen(path
);
1900 /* Dir, or regular file? */
1901 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1902 (path
[plen
-1] == '\\'))
1903 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1906 mode
|= MSVCRT__S_IFREG
;
1908 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1910 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1911 (tolower(path
[plen
-3]) << 16);
1912 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1913 mode
|= ALL_S_IEXEC
;
1917 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1918 mode
|= ALL_S_IWRITE
;
1920 buf
->st_mode
= mode
;
1922 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1923 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1925 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1926 buf
->st_mtime
= buf
->st_ctime
= dw
;
1927 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
1928 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
1929 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
1933 /*********************************************************************
1934 * _stati64 (MSVCRT.@)
1936 int CDECL
MSVCRT_stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1939 struct MSVCRT__stat64 buf64
;
1941 ret
= MSVCRT_stat64(path
, &buf64
);
1943 msvcrt_stat64_to_stati64(&buf64
, buf
);
1947 /*********************************************************************
1950 int CDECL
MSVCRT_stat(const char* path
, struct MSVCRT__stat
* buf
)
1952 struct MSVCRT__stat64 buf64
;
1954 ret
= MSVCRT_stat64( path
, &buf64
);
1956 msvcrt_stat64_to_stat(&buf64
, buf
);
1960 /*********************************************************************
1961 * _wstat64 (MSVCRT.@)
1963 int CDECL
MSVCRT__wstat64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat64
* buf
)
1966 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1967 unsigned short mode
= ALL_S_IREAD
;
1970 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1972 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1974 TRACE("failed (%d)\n",GetLastError());
1975 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1979 memset(buf
,0,sizeof(struct MSVCRT__stat64
));
1981 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1982 if (MSVCRT_iswalpha(*path
))
1983 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1985 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1987 plen
= strlenW(path
);
1989 /* Dir, or regular file? */
1990 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1991 (path
[plen
-1] == '\\'))
1992 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1995 mode
|= MSVCRT__S_IFREG
;
1997 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1999 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
2000 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
2001 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
2002 mode
|= ALL_S_IEXEC
;
2006 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
2007 mode
|= ALL_S_IWRITE
;
2009 buf
->st_mode
= mode
;
2011 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
2012 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
2014 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
2015 buf
->st_mtime
= buf
->st_ctime
= dw
;
2016 TRACE("%d %d 0x%08x%08x %d %d %d\n", buf
->st_mode
,buf
->st_nlink
,
2017 (int)(buf
->st_size
>> 32),(int)buf
->st_size
,
2018 (int)buf
->st_atime
,(int)buf
->st_mtime
,(int)buf
->st_ctime
);
2022 /*********************************************************************
2023 * _wstati64 (MSVCRT.@)
2025 int CDECL
MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
2028 struct MSVCRT__stat64 buf64
;
2030 ret
= MSVCRT__wstat64(path
, &buf64
);
2032 msvcrt_stat64_to_stati64(&buf64
, buf
);
2036 /*********************************************************************
2039 int CDECL
MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
2042 struct MSVCRT__stat64 buf64
;
2044 ret
= MSVCRT__wstat64( path
, &buf64
);
2045 if (!ret
) msvcrt_stat64_to_stat(&buf64
, buf
);
2049 /*********************************************************************
2052 MSVCRT_long CDECL
_tell(int fd
)
2054 return MSVCRT__lseek(fd
, 0, SEEK_CUR
);
2057 /*********************************************************************
2058 * _telli64 (MSVCRT.@)
2060 __int64 CDECL
_telli64(int fd
)
2062 return MSVCRT__lseeki64(fd
, 0, SEEK_CUR
);
2065 /*********************************************************************
2066 * _tempnam (MSVCRT.@)
2068 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2070 char tmpbuf
[MAX_PATH
];
2071 const char *tmp_dir
= MSVCRT_getenv("TMP");
2073 if (tmp_dir
) dir
= tmp_dir
;
2075 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2076 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2078 TRACE("got name (%s)\n",tmpbuf
);
2079 DeleteFileA(tmpbuf
);
2080 return _strdup(tmpbuf
);
2082 TRACE("failed (%d)\n",GetLastError());
2086 /*********************************************************************
2087 * _wtempnam (MSVCRT.@)
2089 MSVCRT_wchar_t
* CDECL
_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
2091 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
2093 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2094 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2096 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2097 DeleteFileW(tmpbuf
);
2098 return _wcsdup(tmpbuf
);
2100 TRACE("failed (%d)\n",GetLastError());
2104 /*********************************************************************
2107 int CDECL
MSVCRT__umask(int umask
)
2109 int old_umask
= MSVCRT_umask
;
2110 TRACE("(%d)\n",umask
);
2111 MSVCRT_umask
= umask
;
2115 /*********************************************************************
2116 * _utime64 (MSVCRT.@)
2118 int CDECL
_utime64(const char* path
, struct MSVCRT___utimbuf64
*t
)
2120 int fd
= MSVCRT__open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2124 int retVal
= _futime64(fd
, t
);
2131 /*********************************************************************
2132 * _utime32 (MSVCRT.@)
2134 int CDECL
_utime32(const char* path
, struct MSVCRT___utimbuf32
*t
)
2136 struct MSVCRT___utimbuf64 t64
;
2137 t64
.actime
= t
->actime
;
2138 t64
.modtime
= t
->modtime
;
2139 return _utime64( path
, &t64
);
2142 /*********************************************************************
2146 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf64
*t
)
2148 return _utime64( path
, t
);
2151 int CDECL
_utime(const char* path
, struct MSVCRT___utimbuf32
*t
)
2153 return _utime32( path
, t
);
2157 /*********************************************************************
2158 * _wutime64 (MSVCRT.@)
2160 int CDECL
_wutime64(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2162 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
2166 int retVal
= _futime64(fd
, t
);
2173 /*********************************************************************
2174 * _wutime32 (MSVCRT.@)
2176 int CDECL
_wutime32(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2178 struct MSVCRT___utimbuf64 t64
;
2179 t64
.actime
= t
->actime
;
2180 t64
.modtime
= t
->modtime
;
2181 return _wutime64( path
, &t64
);
2184 /*********************************************************************
2185 * _wutime (MSVCRT.@)
2188 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf64
*t
)
2190 return _wutime64( path
, t
);
2193 int CDECL
_wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT___utimbuf32
*t
)
2195 return _wutime32( path
, t
);
2199 /*********************************************************************
2202 int CDECL
MSVCRT__write(int fd
, const void* buf
, unsigned int count
)
2205 HANDLE hand
= msvcrt_fdtoh(fd
);
2207 /* Don't trace small writes, it gets *very* annoying */
2210 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2212 if (hand
== INVALID_HANDLE_VALUE
)
2214 *MSVCRT__errno() = MSVCRT_EBADF
;
2218 /* If appending, go to EOF */
2219 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
2220 MSVCRT__lseek(fd
, 0, FILE_END
);
2222 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
2224 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2225 && (num_written
== count
))
2227 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2228 hand
, GetLastError());
2229 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2233 unsigned int i
, j
, nr_lf
;
2236 const char *s
= buf
, *buf_start
= buf
;
2237 /* find number of \n ( without preceding \r ) */
2238 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2243 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2248 if ((q
= p
= MSVCRT_malloc(count
+ nr_lf
)))
2250 for (s
= buf
, i
= 0, j
= 0; i
< count
; i
++)
2255 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2262 FIXME("Malloc failed\n");
2270 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2272 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2273 fd
, hand
, GetLastError(), num_written
);
2274 *MSVCRT__errno() = MSVCRT_ENOSPC
;
2277 return s
- buf_start
;
2289 /*********************************************************************
2292 int CDECL
MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
2295 len
= MSVCRT__write(file
->_file
, &val
, sizeof(val
));
2296 if (len
== sizeof(val
)) return val
;
2297 file
->_flag
|= MSVCRT__IOERR
;
2301 /*********************************************************************
2304 int CDECL
MSVCRT_fclose(MSVCRT_FILE
* file
)
2309 MSVCRT_free(file
->_tmpfname
);
2310 file
->_tmpfname
= NULL
;
2311 /* flush stdio buffers */
2312 if(file
->_flag
& MSVCRT__IOWRT
)
2313 MSVCRT_fflush(file
);
2314 if(file
->_flag
& MSVCRT__IOMYBUF
)
2315 MSVCRT_free(file
->_base
);
2317 r
=MSVCRT__close(file
->_file
);
2321 return ((r
== -1) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2324 /*********************************************************************
2327 int CDECL
MSVCRT_feof(MSVCRT_FILE
* file
)
2329 return file
->_flag
& MSVCRT__IOEOF
;
2332 /*********************************************************************
2335 int CDECL
MSVCRT_ferror(MSVCRT_FILE
* file
)
2337 return file
->_flag
& MSVCRT__IOERR
;
2340 /*********************************************************************
2341 * _filbuf (MSVCRT.@)
2343 int CDECL
MSVCRT__filbuf(MSVCRT_FILE
* file
)
2345 /* Allocate buffer if needed */
2346 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2347 msvcrt_alloc_buffer(file
);
2349 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2350 if(file
->_flag
& MSVCRT__IORW
) {
2351 file
->_flag
|= MSVCRT__IOREAD
;
2356 if(file
->_flag
& MSVCRT__IONBF
) {
2359 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2360 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2365 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2367 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2372 file
->_ptr
= file
->_base
+1;
2373 return *(unsigned char *)file
->_base
;
2377 /*********************************************************************
2380 int CDECL
MSVCRT_fgetc(MSVCRT_FILE
* file
)
2386 i
= (unsigned char *)file
->_ptr
++;
2389 j
= MSVCRT__filbuf(file
);
2393 /*********************************************************************
2394 * _fgetchar (MSVCRT.@)
2396 int CDECL
_fgetchar(void)
2398 return MSVCRT_fgetc(MSVCRT_stdin
);
2401 /*********************************************************************
2404 char * CDECL
MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2406 int cc
= MSVCRT_EOF
;
2407 char * buf_start
= s
;
2409 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2410 file
,file
->_file
,s
,size
);
2412 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2417 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2419 TRACE(":nothing read\n");
2422 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2425 TRACE(":got %s\n", debugstr_a(buf_start
));
2429 /*********************************************************************
2432 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2433 * the CR from CR/LF combinations
2435 MSVCRT_wint_t CDECL
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2439 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2446 for(i
=0; i
<sizeof(wc
); i
++)
2456 j
= MSVCRT__filbuf(file
);
2459 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2469 c
= MSVCRT_fgetc(file
);
2470 if ((MSVCRT___mb_cur_max
> 1) && MSVCRT_isleadbyte(c
))
2472 FIXME("Treat Multibyte characters\n");
2474 if (c
== MSVCRT_EOF
)
2477 return (MSVCRT_wint_t
)c
;
2480 /*********************************************************************
2483 int CDECL
MSVCRT__getw(MSVCRT_FILE
* file
)
2489 for (j
=0; j
<sizeof(int); j
++) {
2490 k
= MSVCRT_fgetc(file
);
2491 if (k
== MSVCRT_EOF
) {
2492 file
->_flag
|= MSVCRT__IOEOF
;
2500 /*********************************************************************
2503 MSVCRT_wint_t CDECL
MSVCRT_getwc(MSVCRT_FILE
* file
)
2505 return MSVCRT_fgetwc(file
);
2508 /*********************************************************************
2509 * _fgetwchar (MSVCRT.@)
2511 MSVCRT_wint_t CDECL
_fgetwchar(void)
2513 return MSVCRT_fgetwc(MSVCRT_stdin
);
2516 /*********************************************************************
2517 * getwchar (MSVCRT.@)
2519 MSVCRT_wint_t CDECL
MSVCRT_getwchar(void)
2521 return _fgetwchar();
2524 /*********************************************************************
2527 MSVCRT_wchar_t
* CDECL
MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2529 int cc
= MSVCRT_WEOF
;
2530 MSVCRT_wchar_t
* buf_start
= s
;
2532 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2533 file
,file
->_file
,s
,size
);
2535 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2540 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2542 TRACE(":nothing read\n");
2545 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2548 TRACE(":got %s\n", debugstr_w(buf_start
));
2552 /*********************************************************************
2555 MSVCRT_size_t CDECL
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2557 MSVCRT_size_t wrcnt
=size
* nmemb
;
2562 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2563 memcpy(file
->_ptr
, ptr
, pcnt
);
2568 ptr
= (const char*)ptr
+ pcnt
;
2569 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2570 if(file
->_flag
& MSVCRT__IORW
) {
2571 file
->_flag
|= MSVCRT__IOWRT
;
2577 int res
=msvcrt_flush_buffer(file
);
2579 int pwritten
= MSVCRT__write(file
->_file
, ptr
, wrcnt
);
2582 file
->_flag
|= MSVCRT__IOERR
;
2585 written
+= pwritten
;
2588 return written
/ size
;
2591 /*********************************************************************
2594 MSVCRT_wint_t CDECL
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2596 MSVCRT_wchar_t mwc
=wc
;
2597 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2602 /*********************************************************************
2603 * _fputwchar (MSVCRT.@)
2605 MSVCRT_wint_t CDECL
_fputwchar(MSVCRT_wint_t wc
)
2607 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2610 /*********************************************************************
2611 * _wfsopen (MSVCRT.@)
2613 MSVCRT_FILE
* CDECL
MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2616 int open_flags
, stream_flags
, fd
;
2618 TRACE("(%s,%s)\n", debugstr_w(path
), debugstr_w(mode
));
2620 /* map mode string to open() flags. "man fopen" for possibilities. */
2621 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2625 fd
= MSVCRT__wsopen(path
, open_flags
, share
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2628 else if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
)
2630 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd
, debugstr_w(mode
), file
);
2637 TRACE(":got (%p)\n",file
);
2638 if (fd
>= 0 && !file
)
2644 /*********************************************************************
2645 * _fsopen (MSVCRT.@)
2647 MSVCRT_FILE
* CDECL
MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2650 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2652 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2653 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2659 ret
= MSVCRT__wfsopen(pathW
, modeW
, share
);
2666 /*********************************************************************
2669 MSVCRT_FILE
* CDECL
MSVCRT_fopen(const char *path
, const char *mode
)
2671 return MSVCRT__fsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2674 /*********************************************************************
2675 * _wfopen (MSVCRT.@)
2677 MSVCRT_FILE
* CDECL
MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2679 return MSVCRT__wfsopen( path
, mode
, MSVCRT__SH_DENYNO
);
2682 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2683 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2685 /*********************************************************************
2688 int CDECL
MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2695 int res
= msvcrt_flush_buffer(file
);
2696 return res
? res
: c
;
2701 return MSVCRT__flsbuf(c
, file
);
2705 /*********************************************************************
2706 * _flsbuf (MSVCRT.@)
2708 int CDECL
MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2710 /* Flush output buffer */
2711 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2712 msvcrt_alloc_buffer(file
);
2714 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2715 if(file
->_flag
& MSVCRT__IORW
) {
2716 file
->_flag
|= MSVCRT__IOWRT
;
2722 int res
=msvcrt_flush_buffer(file
);
2723 return res
?res
: MSVCRT_fputc(c
, file
);
2727 /* set _cnt to 0 for unbuffered FILEs */
2729 len
= MSVCRT__write(file
->_file
, &cc
, 1);
2730 if (len
== 1) return c
& 0xff;
2731 file
->_flag
|= MSVCRT__IOERR
;
2736 /*********************************************************************
2737 * _fputchar (MSVCRT.@)
2739 int CDECL
_fputchar(int c
)
2741 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2744 /*********************************************************************
2747 MSVCRT_size_t CDECL
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2748 { MSVCRT_size_t rcnt
=size
* nmemb
;
2749 MSVCRT_size_t read
=0;
2755 /* first buffered data */
2757 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2758 memcpy(ptr
, file
->_ptr
, pcnt
);
2763 ptr
= (char*)ptr
+ pcnt
;
2764 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2765 if(file
->_flag
& MSVCRT__IORW
) {
2766 file
->_flag
|= MSVCRT__IOREAD
;
2773 /* Fill the buffer on small reads.
2774 * TODO: Use a better buffering strategy.
2776 if (!file
->_cnt
&& size
*nmemb
<= MSVCRT_BUFSIZ
/2 && !(file
->_flag
& MSVCRT__IONBF
)) {
2777 if (file
->_bufsiz
== 0) {
2778 msvcrt_alloc_buffer(file
);
2780 file
->_cnt
= MSVCRT__read(file
->_file
, file
->_base
, file
->_bufsiz
);
2781 file
->_ptr
= file
->_base
;
2782 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2783 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2784 if (i
> 0 && i
< file
->_cnt
) {
2785 MSVCRT_fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2786 file
->_flag
&= ~MSVCRT__IOEOF
;
2789 memcpy(ptr
, file
->_ptr
, i
);
2794 i
= MSVCRT__read(file
->_file
,ptr
, rcnt
);
2798 ptr
= (char *)ptr
+i
;
2799 /* expose feof condition in the flags
2800 * MFC tests file->_flag for feof, and doesn't call feof())
2802 if ( MSVCRT_fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2803 file
->_flag
|= MSVCRT__IOEOF
;
2806 file
->_flag
|= MSVCRT__IOERR
;
2816 /*********************************************************************
2817 * _wfreopen (MSVCRT.@)
2820 MSVCRT_FILE
* CDECL
_wfreopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, MSVCRT_FILE
* file
)
2822 int open_flags
, stream_flags
, fd
;
2824 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path
), debugstr_w(mode
), file
, file
->_file
);
2827 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2831 MSVCRT_fclose(file
);
2832 /* map mode string to open() flags. "man fopen" for possibilities. */
2833 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2837 fd
= _wopen(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2840 else if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2843 WARN(":failed-last error (%d)\n",GetLastError());
2844 msvcrt_set_errno(GetLastError());
2853 /*********************************************************************
2854 * freopen (MSVCRT.@)
2857 MSVCRT_FILE
* CDECL
MSVCRT_freopen(const char *path
, const char *mode
, MSVCRT_FILE
* file
)
2860 MSVCRT_wchar_t
*pathW
= NULL
, *modeW
= NULL
;
2862 if (path
&& !(pathW
= msvcrt_wstrdupa(path
))) return NULL
;
2863 if (mode
&& !(modeW
= msvcrt_wstrdupa(mode
)))
2869 ret
= _wfreopen(pathW
, modeW
, file
);
2876 /*********************************************************************
2877 * fsetpos (MSVCRT.@)
2879 int CDECL
MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2881 /* Note that all this has been lifted 'as is' from fseek */
2882 if(file
->_flag
& MSVCRT__IOWRT
)
2883 msvcrt_flush_buffer(file
);
2885 /* Discard buffered input */
2887 file
->_ptr
= file
->_base
;
2889 /* Reset direction of i/o */
2890 if(file
->_flag
& MSVCRT__IORW
) {
2891 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2894 return (MSVCRT__lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2897 /*********************************************************************
2900 LONG CDECL
MSVCRT_ftell(MSVCRT_FILE
* file
)
2902 /* TODO: just call fgetpos and return lower half of result */
2905 pos
= _tell(file
->_file
);
2906 if(pos
== -1) return -1;
2908 if( file
->_flag
& MSVCRT__IOWRT
) {
2909 off
= file
->_ptr
- file
->_base
;
2912 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2913 /* Black magic correction for CR removal */
2915 for (i
=0; i
<file
->_cnt
; i
++) {
2916 if (file
->_ptr
[i
] == '\n')
2919 /* Black magic when reading CR at buffer boundary*/
2920 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
2929 /*********************************************************************
2930 * fgetpos (MSVCRT.@)
2932 int CDECL
MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2935 *pos
= MSVCRT__lseeki64(file
->_file
,0,SEEK_CUR
);
2936 if(*pos
== -1) return -1;
2938 if( file
->_flag
& MSVCRT__IOWRT
) {
2939 off
= file
->_ptr
- file
->_base
;
2942 if (MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
) {
2943 /* Black magic correction for CR removal */
2945 for (i
=0; i
<file
->_cnt
; i
++) {
2946 if (file
->_ptr
[i
] == '\n')
2949 /* Black magic when reading CR at buffer boundary*/
2950 if(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_READCR
)
2959 /*********************************************************************
2962 int CDECL
MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2964 MSVCRT_size_t i
, len
= strlen(s
);
2965 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2966 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2967 for (i
=0; i
<len
; i
++)
2968 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2973 /*********************************************************************
2976 int CDECL
MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2978 MSVCRT_size_t i
, len
= strlenW(s
);
2979 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2980 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2981 for (i
=0; i
<len
; i
++)
2983 if ((s
[i
] == '\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2985 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2991 /*********************************************************************
2992 * getchar (MSVCRT.@)
2994 int CDECL
MSVCRT_getchar(void)
2996 return MSVCRT_fgetc(MSVCRT_stdin
);
2999 /*********************************************************************
3002 int CDECL
MSVCRT_getc(MSVCRT_FILE
* file
)
3004 return MSVCRT_fgetc(file
);
3007 /*********************************************************************
3010 char * CDECL
MSVCRT_gets(char *buf
)
3013 char * buf_start
= buf
;
3015 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
3016 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
3017 if(cc
!= '\r') *buf
++ = (char)cc
;
3021 TRACE("got '%s'\n", buf_start
);
3025 /*********************************************************************
3028 MSVCRT_wchar_t
* CDECL
MSVCRT__getws(MSVCRT_wchar_t
* buf
)
3031 MSVCRT_wchar_t
* ws
= buf
;
3033 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
3034 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
3037 *buf
++ = (MSVCRT_wchar_t
)cc
;
3041 TRACE("got %s\n", debugstr_w(ws
));
3045 /*********************************************************************
3048 int CDECL
MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
3050 return MSVCRT_fputc(c
, file
);
3053 /*********************************************************************
3054 * putchar (MSVCRT.@)
3056 int CDECL
MSVCRT_putchar(int c
)
3058 return MSVCRT_fputc(c
, MSVCRT_stdout
);
3061 /*********************************************************************
3064 int CDECL
MSVCRT_puts(const char *s
)
3066 MSVCRT_size_t len
= strlen(s
);
3067 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3068 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3071 /*********************************************************************
3074 int CDECL
_putws(const MSVCRT_wchar_t
*s
)
3076 static const MSVCRT_wchar_t nl
= '\n';
3077 MSVCRT_size_t len
= strlenW(s
);
3078 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
3079 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
3082 /*********************************************************************
3085 int CDECL
MSVCRT_remove(const char *path
)
3087 TRACE("(%s)\n",path
);
3088 if (DeleteFileA(path
))
3090 TRACE(":failed (%d)\n",GetLastError());
3091 msvcrt_set_errno(GetLastError());
3095 /*********************************************************************
3096 * _wremove (MSVCRT.@)
3098 int CDECL
_wremove(const MSVCRT_wchar_t
*path
)
3100 TRACE("(%s)\n",debugstr_w(path
));
3101 if (DeleteFileW(path
))
3103 TRACE(":failed (%d)\n",GetLastError());
3104 msvcrt_set_errno(GetLastError());
3108 /*********************************************************************
3111 int CDECL
MSVCRT_rename(const char *oldpath
,const char *newpath
)
3113 TRACE(":from %s to %s\n",oldpath
,newpath
);
3114 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3116 TRACE(":failed (%d)\n",GetLastError());
3117 msvcrt_set_errno(GetLastError());
3121 /*********************************************************************
3122 * _wrename (MSVCRT.@)
3124 int CDECL
_wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
3126 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
3127 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3129 TRACE(":failed (%d)\n",GetLastError());
3130 msvcrt_set_errno(GetLastError());
3134 /*********************************************************************
3135 * setvbuf (MSVCRT.@)
3137 int CDECL
MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
3139 /* TODO: Check if file busy */
3141 MSVCRT_free(file
->_base
);
3145 if(mode
== MSVCRT__IOFBF
) {
3146 file
->_flag
&= ~MSVCRT__IONBF
;
3147 file
->_base
= file
->_ptr
= buf
;
3149 file
->_bufsiz
= size
;
3152 file
->_flag
|= MSVCRT__IONBF
;
3157 /*********************************************************************
3160 void CDECL
MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
3162 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
3165 /*********************************************************************
3168 char * CDECL
MSVCRT_tmpnam(char *s
)
3176 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
3177 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3178 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
3180 msvcrt_int_to_base32(unique
++, tmpstr
);
3182 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3183 GetLastError() == ERROR_FILE_NOT_FOUND
)
3189 /*********************************************************************
3190 * tmpfile (MSVCRT.@)
3192 MSVCRT_FILE
* CDECL
MSVCRT_tmpfile(void)
3194 char *filename
= MSVCRT_tmpnam(NULL
);
3196 MSVCRT_FILE
* file
= NULL
;
3199 fd
= MSVCRT__open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
3200 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
3202 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
3207 else file
->_tmpfname
= _strdup(filename
);
3213 /*********************************************************************
3214 * vfprintf (MSVCRT.@)
3216 int CDECL
MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, __ms_va_list valist
)
3218 char buf
[2048], *mem
= buf
;
3219 int written
, resize
= sizeof(buf
), retval
;
3220 /* There are two conventions for vsnprintf failing:
3221 * Return -1 if we truncated, or
3222 * Return the number of bytes that would have been written
3223 * The code below handles both cases
3225 while ((written
= MSVCRT_vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3228 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3231 if (!(mem
= MSVCRT_malloc(resize
)))
3234 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3240 /*********************************************************************
3241 * vfwprintf (MSVCRT.@)
3243 * Is final char included in written (then resize is too big) or not
3244 * (then we must test for equality too)?
3246 int CDECL
MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3248 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
3249 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
3250 /* See vfprintf comments */
3251 while ((written
= MSVCRT_vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3254 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
3257 if (!(mem
= MSVCRT_malloc(resize
*sizeof(*mem
))))
3260 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
3266 /*********************************************************************
3267 * vprintf (MSVCRT.@)
3269 int CDECL
MSVCRT_vprintf(const char *format
, __ms_va_list valist
)
3271 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
3274 /*********************************************************************
3275 * vwprintf (MSVCRT.@)
3277 int CDECL
MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
3279 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
3282 /*********************************************************************
3283 * fprintf (MSVCRT.@)
3285 int CDECL
MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
3287 __ms_va_list valist
;
3289 __ms_va_start(valist
, format
);
3290 res
= MSVCRT_vfprintf(file
, format
, valist
);
3291 __ms_va_end(valist
);
3295 /*********************************************************************
3296 * fwprintf (MSVCRT.@)
3298 int CDECL
MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
3300 __ms_va_list valist
;
3302 __ms_va_start(valist
, format
);
3303 res
= MSVCRT_vfwprintf(file
, format
, valist
);
3304 __ms_va_end(valist
);
3308 /*********************************************************************
3311 int CDECL
MSVCRT_printf(const char *format
, ...)
3313 __ms_va_list valist
;
3315 __ms_va_start(valist
, format
);
3316 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
3317 __ms_va_end(valist
);
3321 /*********************************************************************
3324 int CDECL
MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
3326 if (c
== MSVCRT_EOF
)
3328 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
3329 msvcrt_alloc_buffer(file
);
3332 if(file
->_ptr
>file
->_base
) {
3336 MSVCRT_clearerr(file
);
3342 /*********************************************************************
3343 * ungetwc (MSVCRT.@)
3345 MSVCRT_wint_t CDECL
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
3347 MSVCRT_wchar_t mwc
= wc
;
3348 char * pp
= (char *)&mwc
;
3350 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
3351 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
3357 /*********************************************************************
3358 * wprintf (MSVCRT.@)
3360 int CDECL
MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
3362 __ms_va_list valist
;
3364 __ms_va_start(valist
, format
);
3365 res
= MSVCRT_vwprintf(format
, valist
);
3366 __ms_va_end(valist
);
3370 /*********************************************************************
3371 * _getmaxstdio (MSVCRT.@)
3373 int CDECL
_getmaxstdio(void)
3375 FIXME("stub, always returns 512\n");
3379 /*********************************************************************
3380 * _setmaxstdio_ (MSVCRT.@)
3382 int CDECL
_setmaxstdio(int newmax
)
3389 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3393 /*********************************************************************
3394 * __pioinfo (MSVCRT.@)
3395 * FIXME: see MSVCRT_MAX_FILES define.
3397 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3398 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
3399 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
3400 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
3401 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
3402 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
3403 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
3404 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
3405 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
3406 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
3407 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
3408 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
3411 /*********************************************************************
3412 * __badioinfo (MSVCRT.@)
3414 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};