msvcrt: Implemented stat64, wstat64 and fstat64.
[wine/hacks.git] / dlls / msvcrt / file.c
blobb55d600a2a73d2a02df4e8c7ff4cafa6cd991a77
1 /*
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
25 * TODO
26 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
29 #include "config.h"
30 #include "wine/port.h"
32 #include <time.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <sys/types.h>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winreg.h"
43 #include "winternl.h"
44 #include "msvcrt.h"
46 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
52 /* for stat mode, permissions apply to all,owner and group */
53 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
54 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
55 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
57 /* _access() bit flags FIXME: incomplete */
58 #define MSVCRT_W_OK 0x02
60 /* values for wxflag in file descriptor */
61 #define WX_OPEN 0x01
62 #define WX_ATEOF 0x02
63 #define WX_DONTINHERIT 0x10
64 #define WX_APPEND 0x20
65 #define WX_TEXT 0x80
67 /* FIXME: this should be allocated dynamically */
68 #define MSVCRT_MAX_FILES 2048
70 typedef struct {
71 HANDLE handle;
72 unsigned char wxflag;
73 DWORD unkn[7]; /* critical section and init flag */
74 } ioinfo;
76 static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
78 MSVCRT_FILE MSVCRT__iob[3];
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 static MSVCRT_FILE* MSVCRT_fstreams[2048];
84 static int MSVCRT_stream_idx;
86 /* INTERNAL: process umask */
87 static int MSVCRT_umask = 0;
89 /* INTERNAL: Static buffer for temp file name */
90 static char MSVCRT_tmpname[MAX_PATH];
92 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
93 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
94 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
95 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
97 #define TOUL(x) (ULONGLONG)(x)
98 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
99 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
100 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
101 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
103 /* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
104 * and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
105 * and MSVCRT_stream_idx, from race conditions.
106 * It doesn't protect against race conditions manipulating the underlying files
107 * or flags; doing so would probably be better accomplished with per-file
108 * protection, rather than locking the whole table for every change.
110 static CRITICAL_SECTION MSVCRT_file_cs;
111 #define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
112 #define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
114 static void msvcrt_stat64_to_stat(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stat *buf)
116 buf->st_dev = buf64->st_dev;
117 buf->st_ino = buf64->st_ino;
118 buf->st_mode = buf64->st_mode;
119 buf->st_nlink = buf64->st_nlink;
120 buf->st_uid = buf64->st_uid;
121 buf->st_gid = buf64->st_gid;
122 buf->st_rdev = buf64->st_rdev;
123 buf->st_size = buf64->st_size;
124 buf->st_atime = buf64->st_atime;
125 buf->st_mtime = buf64->st_mtime;
126 buf->st_ctime = buf64->st_ctime;
129 static void msvcrt_stat64_to_stati64(const struct MSVCRT__stat64 *buf64, struct MSVCRT__stati64 *buf)
131 buf->st_dev = buf64->st_dev;
132 buf->st_ino = buf64->st_ino;
133 buf->st_mode = buf64->st_mode;
134 buf->st_nlink = buf64->st_nlink;
135 buf->st_uid = buf64->st_uid;
136 buf->st_gid = buf64->st_gid;
137 buf->st_rdev = buf64->st_rdev;
138 buf->st_size = buf64->st_size;
139 buf->st_atime = buf64->st_atime;
140 buf->st_mtime = buf64->st_mtime;
141 buf->st_ctime = buf64->st_ctime;
144 static inline BOOL msvcrt_is_valid_fd(int fd)
146 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
149 /* INTERNAL: Get the HANDLE for a fd
150 * This doesn't lock the table, because a failure will result in
151 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
152 * it returns a valid handle which is about to be closed, a subsequent call
153 * will fail, most likely in a sane way.
155 static HANDLE msvcrt_fdtoh(int fd)
157 if (!msvcrt_is_valid_fd(fd))
159 WARN(":fd (%d) - no handle!\n",fd);
160 *MSVCRT___doserrno() = 0;
161 *MSVCRT__errno() = MSVCRT_EBADF;
162 return INVALID_HANDLE_VALUE;
164 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
165 return MSVCRT_fdesc[fd].handle;
168 /* INTERNAL: free a file entry fd */
169 static void msvcrt_free_fd(int fd)
171 LOCK_FILES();
172 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
173 MSVCRT_fdesc[fd].wxflag = 0;
174 TRACE(":fd (%d) freed\n",fd);
175 if (fd < 3) /* don't use 0,1,2 for user files */
177 switch (fd)
179 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
180 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
181 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
184 else
186 if (fd == MSVCRT_fdend - 1)
187 MSVCRT_fdend--;
188 if (fd < MSVCRT_fdstart)
189 MSVCRT_fdstart = fd;
191 UNLOCK_FILES();
194 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
195 /* caller must hold the files lock */
196 static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
198 if (fd >= MSVCRT_MAX_FILES)
200 WARN(":files exhausted!\n");
201 return -1;
203 MSVCRT_fdesc[fd].handle = hand;
204 MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
206 /* locate next free slot */
207 if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
208 MSVCRT_fdstart = MSVCRT_fdend + 1;
209 else
210 while (MSVCRT_fdstart < MSVCRT_fdend &&
211 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
212 MSVCRT_fdstart++;
213 /* update last fd in use */
214 if (fd >= MSVCRT_fdend)
215 MSVCRT_fdend = fd + 1;
216 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
218 switch (fd)
220 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
221 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
222 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
225 return fd;
228 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
229 static int msvcrt_alloc_fd(HANDLE hand, int flag)
231 int ret;
233 LOCK_FILES();
234 TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
235 ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
236 UNLOCK_FILES();
237 return ret;
240 /* INTERNAL: Allocate a FILE* for an fd slot */
241 /* caller must hold the files lock */
242 static MSVCRT_FILE* msvcrt_alloc_fp(void)
244 unsigned int i;
246 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
248 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
250 if (!MSVCRT_fstreams[i])
252 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
253 return NULL;
254 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
256 return MSVCRT_fstreams[i];
259 return NULL;
262 /* INTERNAL: initialize a FILE* from an open fd */
263 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
265 TRACE(":fd (%d) allocating FILE*\n",fd);
266 if (!msvcrt_is_valid_fd(fd))
268 WARN(":invalid fd %d\n",fd);
269 *MSVCRT___doserrno() = 0;
270 *MSVCRT__errno() = MSVCRT_EBADF;
271 return -1;
273 memset(file, 0, sizeof(*file));
274 file->_file = fd;
275 file->_flag = stream_flags;
277 TRACE(":got FILE* (%p)\n",file);
278 return 0;
281 /* INTERNAL: Create an inheritance data block (for spawned process)
282 * The inheritance block is made of:
283 * 00 int nb of file descriptor (NBFD)
284 * 04 char file flags (wxflag): repeated for each fd
285 * 4+NBFD HANDLE file handle: repeated for each fd
287 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
289 int fd;
290 char* wxflag_ptr;
291 HANDLE* handle_ptr;
293 si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
294 si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
295 if (!si->lpReserved2)
297 si->cbReserved2 = 0;
298 return FALSE;
300 wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
301 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
303 *(unsigned*)si->lpReserved2 = MSVCRT_fdend;
304 for (fd = 0; fd < MSVCRT_fdend; fd++)
306 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
307 if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
309 *wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
310 *handle_ptr = MSVCRT_fdesc[fd].handle;
312 else
314 *wxflag_ptr = 0;
315 *handle_ptr = INVALID_HANDLE_VALUE;
317 wxflag_ptr++; handle_ptr++;
319 return TRUE;
322 /* INTERNAL: Set up all file descriptors,
323 * as well as default streams (stdin, stderr and stdout)
325 void msvcrt_init_io(void)
327 STARTUPINFOA si;
328 int i;
330 InitializeCriticalSection(&MSVCRT_file_cs);
331 GetStartupInfoA(&si);
332 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
334 char* wxflag_ptr;
335 HANDLE* handle_ptr;
337 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
339 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
340 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
342 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
343 for (i = 0; i < MSVCRT_fdend; i++)
345 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
347 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
348 MSVCRT_fdesc[i].handle = *handle_ptr;
350 else
352 MSVCRT_fdesc[i].wxflag = 0;
353 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
355 wxflag_ptr++; handle_ptr++;
357 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
358 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
361 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
363 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
364 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, TRUE,
365 DUPLICATE_SAME_ACCESS);
366 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
368 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
370 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
371 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, TRUE,
372 DUPLICATE_SAME_ACCESS);
373 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
375 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
377 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
378 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, TRUE,
379 DUPLICATE_SAME_ACCESS);
380 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
383 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
384 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
386 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
387 for (i = 0; i < 3; i++)
389 /* FILE structs for stdin/out/err are static and never deleted */
390 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
391 MSVCRT__iob[i]._file = i;
392 MSVCRT__iob[i]._tmpfname = NULL;
393 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
395 MSVCRT_stream_idx = 3;
398 /* INTERNAL: Flush stdio file buffer */
399 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
401 if(file->_bufsiz) {
402 int cnt=file->_ptr-file->_base;
403 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
404 file->_flag |= MSVCRT__IOERR;
405 return MSVCRT_EOF;
407 file->_ptr=file->_base;
408 file->_cnt=file->_bufsiz;
410 return 0;
413 /* INTERNAL: Allocate stdio file buffer */
414 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
416 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
417 if(file->_base) {
418 file->_bufsiz = MSVCRT_BUFSIZ;
419 file->_flag |= MSVCRT__IOMYBUF;
420 } else {
421 file->_base = (char*)(&file->_charbuf);
422 /* put here 2 ??? */
423 file->_bufsiz = sizeof(file->_charbuf);
425 file->_ptr = file->_base;
426 file->_cnt = 0;
429 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
430 static void msvcrt_int_to_base32(int num, char *str)
432 char *p;
433 int n = num;
434 int digits = 0;
436 while (n != 0)
438 n >>= 5;
439 digits++;
441 p = str + digits;
442 *p = 0;
443 while (--p >= str)
445 *p = (num & 31) + '0';
446 if (*p > '9')
447 *p += ('a' - '0' - 10);
448 num >>= 5;
452 /*********************************************************************
453 * __p__iob(MSVCRT.@)
455 MSVCRT_FILE * CDECL __p__iob(void)
457 return &MSVCRT__iob[0];
460 /*********************************************************************
461 * _access (MSVCRT.@)
463 int CDECL _access(const char *filename, int mode)
465 DWORD attr = GetFileAttributesA(filename);
467 TRACE("(%s,%d) %d\n",filename,mode,attr);
469 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
471 msvcrt_set_errno(GetLastError());
472 return -1;
474 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
476 msvcrt_set_errno(ERROR_ACCESS_DENIED);
477 return -1;
479 return 0;
482 /*********************************************************************
483 * _waccess (MSVCRT.@)
485 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
487 DWORD attr = GetFileAttributesW(filename);
489 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
491 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
493 msvcrt_set_errno(GetLastError());
494 return -1;
496 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
498 msvcrt_set_errno(ERROR_ACCESS_DENIED);
499 return -1;
501 return 0;
504 /*********************************************************************
505 * _chmod (MSVCRT.@)
507 int CDECL _chmod(const char *path, int flags)
509 DWORD oldFlags = GetFileAttributesA(path);
511 if (oldFlags != INVALID_FILE_ATTRIBUTES)
513 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
514 oldFlags | FILE_ATTRIBUTE_READONLY;
516 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
517 return 0;
519 msvcrt_set_errno(GetLastError());
520 return -1;
523 /*********************************************************************
524 * _wchmod (MSVCRT.@)
526 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
528 DWORD oldFlags = GetFileAttributesW(path);
530 if (oldFlags != INVALID_FILE_ATTRIBUTES)
532 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
533 oldFlags | FILE_ATTRIBUTE_READONLY;
535 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
536 return 0;
538 msvcrt_set_errno(GetLastError());
539 return -1;
542 /*********************************************************************
543 * _unlink (MSVCRT.@)
545 int CDECL _unlink(const char *path)
547 TRACE("%s\n",debugstr_a(path));
548 if(DeleteFileA(path))
549 return 0;
550 TRACE("failed (%d)\n",GetLastError());
551 msvcrt_set_errno(GetLastError());
552 return -1;
555 /*********************************************************************
556 * _wunlink (MSVCRT.@)
558 int CDECL _wunlink(const MSVCRT_wchar_t *path)
560 TRACE("(%s)\n",debugstr_w(path));
561 if(DeleteFileW(path))
562 return 0;
563 TRACE("failed (%d)\n",GetLastError());
564 msvcrt_set_errno(GetLastError());
565 return -1;
568 /* _flushall calls MSVCRT_fflush which calls _flushall */
569 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
571 /*********************************************************************
572 * _flushall (MSVCRT.@)
574 int CDECL _flushall(void)
576 int i, num_flushed = 0;
578 LOCK_FILES();
579 for (i = 3; i < MSVCRT_stream_idx; i++)
580 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
582 #if 0
583 /* FIXME: flush, do not commit */
584 if (_commit(i) == -1)
585 if (MSVCRT_fstreams[i])
586 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
587 #endif
588 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
589 MSVCRT_fflush(MSVCRT_fstreams[i]);
590 num_flushed++;
593 UNLOCK_FILES();
595 TRACE(":flushed (%d) handles\n",num_flushed);
596 return num_flushed;
599 /*********************************************************************
600 * fflush (MSVCRT.@)
602 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
604 if(!file) {
605 _flushall();
606 } else if(file->_flag & MSVCRT__IOWRT) {
607 int res=msvcrt_flush_buffer(file);
608 return res;
610 return 0;
613 /*********************************************************************
614 * _close (MSVCRT.@)
616 int CDECL _close(int fd)
618 HANDLE hand;
619 int ret;
621 LOCK_FILES();
622 hand = msvcrt_fdtoh(fd);
623 TRACE(":fd (%d) handle (%p)\n",fd,hand);
624 if (hand == INVALID_HANDLE_VALUE)
625 ret = -1;
626 else if (!CloseHandle(hand))
628 WARN(":failed-last error (%d)\n",GetLastError());
629 msvcrt_set_errno(GetLastError());
630 ret = -1;
632 else
634 msvcrt_free_fd(fd);
635 ret = 0;
637 UNLOCK_FILES();
638 TRACE(":ok\n");
639 return ret;
642 /*********************************************************************
643 * _commit (MSVCRT.@)
645 int CDECL _commit(int fd)
647 HANDLE hand = msvcrt_fdtoh(fd);
649 TRACE(":fd (%d) handle (%p)\n",fd,hand);
650 if (hand == INVALID_HANDLE_VALUE)
651 return -1;
653 if (!FlushFileBuffers(hand))
655 if (GetLastError() == ERROR_INVALID_HANDLE)
657 /* FlushFileBuffers fails for console handles
658 * so we ignore this error.
660 return 0;
662 TRACE(":failed-last error (%d)\n",GetLastError());
663 msvcrt_set_errno(GetLastError());
664 return -1;
666 TRACE(":ok\n");
667 return 0;
670 /*********************************************************************
671 * _dup2 (MSVCRT.@)
672 * NOTES
673 * MSDN isn't clear on this point, but the remarks for _pipe,
674 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
675 * indicate file descriptors duplicated with _dup and _dup2 are always
676 * inheritable.
678 int CDECL _dup2(int od, int nd)
680 int ret;
682 TRACE("(od=%d, nd=%d)\n", od, nd);
683 LOCK_FILES();
684 if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
686 HANDLE handle;
688 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
689 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
691 int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
693 if (msvcrt_is_valid_fd(nd))
694 _close(nd);
695 ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
696 if (ret == -1)
698 CloseHandle(handle);
699 *MSVCRT__errno() = MSVCRT_EMFILE;
701 else
703 /* _dup2 returns 0, not nd, on success */
704 ret = 0;
707 else
709 ret = -1;
710 msvcrt_set_errno(GetLastError());
713 else
715 *MSVCRT__errno() = MSVCRT_EBADF;
716 ret = -1;
718 UNLOCK_FILES();
719 return ret;
722 /*********************************************************************
723 * _dup (MSVCRT.@)
725 int CDECL _dup(int od)
727 int fd, ret;
729 LOCK_FILES();
730 fd = MSVCRT_fdstart;
731 if (_dup2(od, fd) == 0)
732 ret = fd;
733 else
734 ret = -1;
735 UNLOCK_FILES();
736 return ret;
739 /*********************************************************************
740 * _eof (MSVCRT.@)
742 int CDECL _eof(int fd)
744 DWORD curpos,endpos;
745 LONG hcurpos,hendpos;
746 HANDLE hand = msvcrt_fdtoh(fd);
748 TRACE(":fd (%d) handle (%p)\n",fd,hand);
750 if (hand == INVALID_HANDLE_VALUE)
751 return -1;
753 if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
755 /* Otherwise we do it the hard way */
756 hcurpos = hendpos = 0;
757 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
758 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
760 if (curpos == endpos && hcurpos == hendpos)
762 /* FIXME: shouldn't WX_ATEOF be set here? */
763 return TRUE;
766 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
767 return FALSE;
770 /*********************************************************************
771 * _fcloseall (MSVCRT.@)
773 int CDECL MSVCRT__fcloseall(void)
775 int num_closed = 0, i;
777 LOCK_FILES();
778 for (i = 3; i < MSVCRT_stream_idx; i++)
779 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
780 !MSVCRT_fclose(MSVCRT_fstreams[i]))
781 num_closed++;
782 UNLOCK_FILES();
784 TRACE(":closed (%d) handles\n",num_closed);
785 return num_closed;
788 /* free everything on process exit */
789 void msvcrt_free_io(void)
791 MSVCRT__fcloseall();
792 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
793 * stdout, and stderr (unlike GNU), so we need to fclose() them here
794 * or they won't get flushed.
796 MSVCRT_fclose(&MSVCRT__iob[0]);
797 MSVCRT_fclose(&MSVCRT__iob[1]);
798 MSVCRT_fclose(&MSVCRT__iob[2]);
799 DeleteCriticalSection(&MSVCRT_file_cs);
802 /*********************************************************************
803 * _lseeki64 (MSVCRT.@)
805 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
807 HANDLE hand = msvcrt_fdtoh(fd);
808 LARGE_INTEGER ofs, ret;
810 TRACE(":fd (%d) handle (%p)\n",fd,hand);
811 if (hand == INVALID_HANDLE_VALUE)
812 return -1;
814 if (whence < 0 || whence > 2)
816 *MSVCRT__errno() = MSVCRT_EINVAL;
817 return -1;
820 TRACE(":fd (%d) to %s pos %s\n",
821 fd,wine_dbgstr_longlong(offset),
822 (whence==SEEK_SET)?"SEEK_SET":
823 (whence==SEEK_CUR)?"SEEK_CUR":
824 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
826 ofs.QuadPart = offset;
827 if (SetFilePointerEx(hand, ofs, &ret, whence))
829 MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
830 /* FIXME: What if we seek _to_ EOF - is EOF set? */
832 return ret.QuadPart;
834 TRACE(":error-last error (%d)\n",GetLastError());
835 msvcrt_set_errno(GetLastError());
836 return -1;
839 /*********************************************************************
840 * _lseek (MSVCRT.@)
842 LONG CDECL _lseek(int fd, LONG offset, int whence)
844 return _lseeki64(fd, offset, whence);
847 /*********************************************************************
848 * _locking (MSVCRT.@)
850 * This is untested; the underlying LockFile doesn't work yet.
852 int CDECL _locking(int fd, int mode, LONG nbytes)
854 BOOL ret;
855 DWORD cur_locn;
856 HANDLE hand = msvcrt_fdtoh(fd);
858 TRACE(":fd (%d) handle (%p)\n",fd,hand);
859 if (hand == INVALID_HANDLE_VALUE)
860 return -1;
862 if (mode < 0 || mode > 4)
864 *MSVCRT__errno() = MSVCRT_EINVAL;
865 return -1;
868 TRACE(":fd (%d) by 0x%08x mode %s\n",
869 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
870 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
871 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
872 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
873 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
874 "UNKNOWN");
876 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
878 FIXME ("Seek failed\n");
879 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
880 return -1;
882 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
884 int nretry = 10;
885 ret = 1; /* just to satisfy gcc */
886 while (nretry--)
888 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
889 if (ret) break;
890 Sleep(1);
893 else if (mode == MSVCRT__LK_UNLCK)
894 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
895 else
896 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
897 /* FIXME - what about error settings? */
898 return ret ? 0 : -1;
901 /*********************************************************************
902 * fseek (MSVCRT.@)
904 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
906 /* Flush output if needed */
907 if(file->_flag & MSVCRT__IOWRT)
908 msvcrt_flush_buffer(file);
910 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
911 offset -= file->_cnt;
913 /* Discard buffered input */
914 file->_cnt = 0;
915 file->_ptr = file->_base;
916 /* Reset direction of i/o */
917 if(file->_flag & MSVCRT__IORW) {
918 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
920 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
923 /*********************************************************************
924 * _chsize (MSVCRT.@)
926 int CDECL _chsize(int fd, long size)
928 LONG cur, pos;
929 HANDLE handle;
930 BOOL ret = FALSE;
932 TRACE("(fd=%d, size=%ld)\n", fd, size);
934 LOCK_FILES();
936 handle = msvcrt_fdtoh(fd);
937 if (handle != INVALID_HANDLE_VALUE)
939 /* save the current file pointer */
940 cur = _lseek(fd, 0, SEEK_CUR);
941 if (cur >= 0)
943 pos = _lseek(fd, size, SEEK_SET);
944 if (pos >= 0)
946 ret = SetEndOfFile(handle);
947 if (!ret) msvcrt_set_errno(GetLastError());
950 /* restore the file pointer */
951 _lseek(fd, cur, SEEK_SET);
955 UNLOCK_FILES();
956 return ret ? 0 : -1;
959 /*********************************************************************
960 * clearerr (MSVCRT.@)
962 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
964 TRACE(":file (%p) fd (%d)\n",file,file->_file);
965 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
968 /*********************************************************************
969 * rewind (MSVCRT.@)
971 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
973 TRACE(":file (%p) fd (%d)\n",file,file->_file);
974 MSVCRT_fseek(file, 0L, SEEK_SET);
975 MSVCRT_clearerr(file);
978 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
980 int plus = strchr(mode, '+') != NULL;
982 switch(*mode++)
984 case 'R': case 'r':
985 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
986 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
987 break;
988 case 'W': case 'w':
989 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
990 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
991 break;
992 case 'A': case 'a':
993 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
994 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
995 break;
996 default:
997 return -1;
1000 while (*mode)
1001 switch (*mode++)
1003 case 'B': case 'b':
1004 *open_flags |= MSVCRT__O_BINARY;
1005 *open_flags &= ~MSVCRT__O_TEXT;
1006 break;
1007 case 'T': case 't':
1008 *open_flags |= MSVCRT__O_TEXT;
1009 *open_flags &= ~MSVCRT__O_BINARY;
1010 break;
1011 case '+':
1012 break;
1013 default:
1014 FIXME(":unknown flag %c not supported\n",mode[-1]);
1016 return 0;
1019 /*********************************************************************
1020 * _fdopen (MSVCRT.@)
1022 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1024 int open_flags, stream_flags;
1025 MSVCRT_FILE* file;
1027 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1029 LOCK_FILES();
1030 if (!(file = msvcrt_alloc_fp()))
1031 file = NULL;
1032 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1034 file->_flag = 0;
1035 file = NULL;
1037 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1038 UNLOCK_FILES();
1040 return file;
1043 /*********************************************************************
1044 * _wfdopen (MSVCRT.@)
1046 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1048 unsigned mlen = strlenW(mode);
1049 char *modea = MSVCRT_calloc(mlen + 1, 1);
1050 MSVCRT_FILE* file = NULL;
1051 int open_flags, stream_flags;
1053 if (modea &&
1054 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1056 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1057 LOCK_FILES();
1058 if (!(file = msvcrt_alloc_fp()))
1059 file = NULL;
1060 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1062 file->_flag = 0;
1063 file = NULL;
1065 else
1067 if (file)
1068 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1069 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1071 UNLOCK_FILES();
1073 return file;
1076 /*********************************************************************
1077 * _filelength (MSVCRT.@)
1079 LONG CDECL _filelength(int fd)
1081 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1082 if (curPos != -1)
1084 LONG endPos = _lseek(fd, 0, SEEK_END);
1085 if (endPos != -1)
1087 if (endPos != curPos)
1088 _lseek(fd, curPos, SEEK_SET);
1089 return endPos;
1092 return -1;
1095 /*********************************************************************
1096 * _filelengthi64 (MSVCRT.@)
1098 __int64 CDECL _filelengthi64(int fd)
1100 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1101 if (curPos != -1)
1103 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1104 if (endPos != -1)
1106 if (endPos != curPos)
1107 _lseeki64(fd, curPos, SEEK_SET);
1108 return endPos;
1111 return -1;
1114 /*********************************************************************
1115 * _fileno (MSVCRT.@)
1117 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1119 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1120 return file->_file;
1123 /*********************************************************************
1124 * _fstat64 (MSVCRT.@)
1126 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1128 DWORD dw;
1129 BY_HANDLE_FILE_INFORMATION hfi;
1130 HANDLE hand = msvcrt_fdtoh(fd);
1132 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1133 if (hand == INVALID_HANDLE_VALUE)
1134 return -1;
1136 if (!buf)
1138 WARN(":failed-NULL buf\n");
1139 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1140 return -1;
1143 memset(&hfi, 0, sizeof(hfi));
1144 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1145 if (!GetFileInformationByHandle(hand, &hfi))
1147 WARN(":failed-last error (%d)\n",GetLastError());
1148 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1149 return -1;
1151 dw = GetFileType(hand);
1152 buf->st_mode = S_IREAD;
1153 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1154 buf->st_mode |= S_IWRITE;
1155 /* interestingly, Windows never seems to set S_IFDIR */
1156 if (dw == FILE_TYPE_CHAR)
1157 buf->st_mode |= S_IFCHR;
1158 else if (dw == FILE_TYPE_PIPE)
1159 buf->st_mode |= S_IFIFO;
1160 else
1161 buf->st_mode |= S_IFREG;
1162 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1163 buf->st_mode);
1164 buf->st_nlink = hfi.nNumberOfLinks;
1165 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1166 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1167 buf->st_atime = dw;
1168 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1169 buf->st_mtime = buf->st_ctime = dw;
1170 return 0;
1173 /*********************************************************************
1174 * _fstati64 (MSVCRT.@)
1176 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1178 int ret;
1179 struct MSVCRT__stat64 buf64;
1181 ret = MSVCRT__fstat64(fd, &buf64);
1182 if (!ret)
1183 msvcrt_stat64_to_stati64(&buf64, buf);
1184 return ret;
1187 /*********************************************************************
1188 * _fstat (MSVCRT.@)
1190 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1191 { int ret;
1192 struct MSVCRT__stat64 buf64;
1194 ret = MSVCRT__fstat64(fd, &buf64);
1195 if (!ret)
1196 msvcrt_stat64_to_stat(&buf64, buf);
1197 return ret;
1200 /*********************************************************************
1201 * _futime (MSVCRT.@)
1203 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1205 HANDLE hand = msvcrt_fdtoh(fd);
1206 FILETIME at, wt;
1208 if (!t)
1210 MSVCRT_time_t currTime;
1211 MSVCRT_time(&currTime);
1212 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1213 memcpy(&wt, &at, sizeof(wt));
1215 else
1217 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1218 if (t->actime == t->modtime)
1219 memcpy(&wt, &at, sizeof(wt));
1220 else
1221 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1224 if (!SetFileTime(hand, NULL, &at, &wt))
1226 msvcrt_set_errno(GetLastError());
1227 return -1 ;
1229 return 0;
1232 /*********************************************************************
1233 * _get_osfhandle (MSVCRT.@)
1235 long CDECL _get_osfhandle(int fd)
1237 HANDLE hand = msvcrt_fdtoh(fd);
1238 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1240 return (long)hand;
1243 /*********************************************************************
1244 * _isatty (MSVCRT.@)
1246 int CDECL _isatty(int fd)
1248 HANDLE hand = msvcrt_fdtoh(fd);
1250 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1251 if (hand == INVALID_HANDLE_VALUE)
1252 return 0;
1254 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1257 /*********************************************************************
1258 * _mktemp (MSVCRT.@)
1260 char * CDECL _mktemp(char *pattern)
1262 int numX = 0;
1263 char *retVal = pattern;
1264 int id;
1265 char letter = 'a';
1267 while(*pattern)
1268 numX = (*pattern++ == 'X')? numX + 1 : 0;
1269 if (numX < 5)
1270 return NULL;
1271 pattern--;
1272 id = GetCurrentProcessId();
1273 numX = 6;
1274 while(numX--)
1276 int tempNum = id / 10;
1277 *pattern-- = id - (tempNum * 10) + '0';
1278 id = tempNum;
1280 pattern++;
1283 *pattern = letter++;
1284 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1285 GetLastError() == ERROR_FILE_NOT_FOUND)
1286 return retVal;
1287 } while(letter <= 'z');
1288 return NULL;
1291 /*********************************************************************
1292 * _wmktemp (MSVCRT.@)
1294 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1296 int numX = 0;
1297 MSVCRT_wchar_t *retVal = pattern;
1298 int id;
1299 MSVCRT_wchar_t letter = 'a';
1301 while(*pattern)
1302 numX = (*pattern++ == 'X')? numX + 1 : 0;
1303 if (numX < 5)
1304 return NULL;
1305 pattern--;
1306 id = GetCurrentProcessId();
1307 numX = 6;
1308 while(numX--)
1310 int tempNum = id / 10;
1311 *pattern-- = id - (tempNum * 10) + '0';
1312 id = tempNum;
1314 pattern++;
1317 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1318 GetLastError() == ERROR_FILE_NOT_FOUND)
1319 return retVal;
1320 *pattern = letter++;
1321 } while(letter != '|');
1322 return NULL;
1325 static unsigned split_oflags(unsigned oflags)
1327 int wxflags = 0;
1328 unsigned unsupp; /* until we support everything */
1330 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1331 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1332 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1333 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1334 else wxflags |= WX_TEXT; /* default to TEXT*/
1335 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1337 if ((unsupp = oflags & ~(
1338 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1339 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1340 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1341 MSVCRT__O_NOINHERIT|
1342 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1344 ERR(":unsupported oflags 0x%04x\n",unsupp);
1346 return wxflags;
1349 /*********************************************************************
1350 * _pipe (MSVCRT.@)
1352 int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
1354 int ret = -1;
1355 SECURITY_ATTRIBUTES sa;
1356 HANDLE readHandle, writeHandle;
1358 if (!pfds)
1360 *MSVCRT__errno() = MSVCRT_EINVAL;
1361 return -1;
1364 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1365 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1366 sa.lpSecurityDescriptor = NULL;
1367 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1369 unsigned int wxflags = split_oflags(textmode);
1370 int fd;
1372 LOCK_FILES();
1373 fd = msvcrt_alloc_fd(readHandle, wxflags);
1374 if (fd != -1)
1376 pfds[0] = fd;
1377 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1378 if (fd != -1)
1380 pfds[1] = fd;
1381 ret = 0;
1383 else
1385 _close(pfds[0]);
1386 CloseHandle(writeHandle);
1387 *MSVCRT__errno() = MSVCRT_EMFILE;
1390 else
1392 CloseHandle(readHandle);
1393 CloseHandle(writeHandle);
1394 *MSVCRT__errno() = MSVCRT_EMFILE;
1396 UNLOCK_FILES();
1398 else
1399 msvcrt_set_errno(GetLastError());
1401 return ret;
1404 /*********************************************************************
1405 * _sopen (MSVCRT.@)
1407 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1409 va_list ap;
1410 int pmode;
1411 DWORD access = 0, creation = 0, attrib;
1412 DWORD sharing;
1413 int wxflag = 0, fd;
1414 HANDLE hand;
1415 SECURITY_ATTRIBUTES sa;
1418 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1419 path, oflags, shflags);
1421 wxflag = split_oflags(oflags);
1422 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1424 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1425 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1426 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1429 if (oflags & MSVCRT__O_CREAT)
1431 va_start(ap, shflags);
1432 pmode = va_arg(ap, int);
1433 va_end(ap);
1435 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1436 FIXME(": pmode 0x%04x ignored\n", pmode);
1437 else
1438 WARN(": pmode 0x%04x ignored\n", pmode);
1440 if (oflags & MSVCRT__O_EXCL)
1441 creation = CREATE_NEW;
1442 else if (oflags & MSVCRT__O_TRUNC)
1443 creation = CREATE_ALWAYS;
1444 else
1445 creation = OPEN_ALWAYS;
1447 else /* no MSVCRT__O_CREAT */
1449 if (oflags & MSVCRT__O_TRUNC)
1450 creation = TRUNCATE_EXISTING;
1451 else
1452 creation = OPEN_EXISTING;
1455 switch( shflags )
1457 case MSVCRT__SH_DENYRW:
1458 sharing = 0L;
1459 break;
1460 case MSVCRT__SH_DENYWR:
1461 sharing = FILE_SHARE_READ;
1462 break;
1463 case MSVCRT__SH_DENYRD:
1464 sharing = FILE_SHARE_WRITE;
1465 break;
1466 case MSVCRT__SH_DENYNO:
1467 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1468 break;
1469 default:
1470 ERR( "Unhandled shflags 0x%x\n", shflags );
1471 return -1;
1473 attrib = FILE_ATTRIBUTE_NORMAL;
1475 if (oflags & MSVCRT__O_TEMPORARY)
1477 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1478 access |= DELETE;
1479 sharing |= FILE_SHARE_DELETE;
1482 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1483 sa.lpSecurityDescriptor = NULL;
1484 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1486 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1488 if (hand == INVALID_HANDLE_VALUE) {
1489 WARN(":failed-last error (%d)\n",GetLastError());
1490 msvcrt_set_errno(GetLastError());
1491 return -1;
1494 fd = msvcrt_alloc_fd(hand, wxflag);
1496 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1497 return fd;
1500 /*********************************************************************
1501 * _wsopen (MSVCRT.@)
1503 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1505 const unsigned int len = strlenW(path);
1506 char *patha = MSVCRT_calloc(len + 1,1);
1507 va_list ap;
1508 int pmode;
1510 va_start(ap, shflags);
1511 pmode = va_arg(ap, int);
1512 va_end(ap);
1514 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1516 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1517 MSVCRT_free(patha);
1518 return retval;
1521 msvcrt_set_errno(GetLastError());
1522 return -1;
1525 /*********************************************************************
1526 * _open (MSVCRT.@)
1528 int CDECL _open( const char *path, int flags, ... )
1530 va_list ap;
1532 if (flags & MSVCRT__O_CREAT)
1534 int pmode;
1535 va_start(ap, flags);
1536 pmode = va_arg(ap, int);
1537 va_end(ap);
1538 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1540 else
1541 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1544 /*********************************************************************
1545 * _wopen (MSVCRT.@)
1547 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1549 const unsigned int len = strlenW(path);
1550 char *patha = MSVCRT_calloc(len + 1,1);
1551 va_list ap;
1552 int pmode;
1554 va_start(ap, flags);
1555 pmode = va_arg(ap, int);
1556 va_end(ap);
1558 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1560 int retval = _open(patha,flags,pmode);
1561 MSVCRT_free(patha);
1562 return retval;
1565 msvcrt_set_errno(GetLastError());
1566 return -1;
1569 /*********************************************************************
1570 * _creat (MSVCRT.@)
1572 int CDECL _creat(const char *path, int flags)
1574 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1575 return _open(path, usedFlags);
1578 /*********************************************************************
1579 * _wcreat (MSVCRT.@)
1581 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1583 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1584 return _wopen(path, usedFlags);
1587 /*********************************************************************
1588 * _open_osfhandle (MSVCRT.@)
1590 int CDECL _open_osfhandle(long handle, int oflags)
1592 int fd;
1594 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1595 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1596 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1597 * text - it never sets MSVCRT__O_BINARY.
1599 /* don't let split_oflags() decide the mode if no mode is passed */
1600 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1601 oflags |= MSVCRT__O_BINARY;
1603 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1604 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1605 return fd;
1608 /*********************************************************************
1609 * _rmtmp (MSVCRT.@)
1611 int CDECL _rmtmp(void)
1613 int num_removed = 0, i;
1615 LOCK_FILES();
1616 for (i = 3; i < MSVCRT_stream_idx; i++)
1617 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1619 MSVCRT_fclose(MSVCRT_fstreams[i]);
1620 num_removed++;
1622 UNLOCK_FILES();
1624 if (num_removed)
1625 TRACE(":removed (%d) temp files\n",num_removed);
1626 return num_removed;
1629 /*********************************************************************
1630 * (internal) remove_cr
1632 * Remove all \r inplace.
1633 * return the number of \r removed
1635 static unsigned int remove_cr(char *buf, unsigned int count)
1637 unsigned int i, j;
1639 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1640 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1641 return count - i;
1644 /*********************************************************************
1645 * (internal) read_i
1647 static int read_i(int fd, void *buf, unsigned int count)
1649 DWORD num_read;
1650 char *bufstart = buf;
1651 HANDLE hand = msvcrt_fdtoh(fd);
1653 /* Don't trace small reads, it gets *very* annoying */
1654 if (count > 4)
1655 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1656 if (hand == INVALID_HANDLE_VALUE)
1657 return -1;
1659 /* Reading single bytes in O_TEXT mode makes things slow
1660 * So read big chunks
1662 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1664 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1666 int i;
1667 /* in text mode, a ctrl-z signals EOF */
1668 for (i=0; i<num_read; i++)
1670 if (bufstart[i] == 0x1a)
1672 num_read = i;
1673 break;
1677 if (num_read != count)
1679 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1680 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1683 else
1685 TRACE(":failed-last error (%d)\n",GetLastError());
1686 return -1;
1689 if (count > 4)
1690 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1691 return num_read;
1694 /*********************************************************************
1695 * _read (MSVCRT.@)
1697 int CDECL _read(int fd, void *buf, unsigned int count)
1699 int num_read;
1700 num_read = read_i(fd, buf, count);
1701 if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1703 num_read -= remove_cr(buf,num_read);
1705 return num_read;
1708 /*********************************************************************
1709 * _setmode (MSVCRT.@)
1711 int CDECL _setmode(int fd,int mode)
1713 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1714 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1715 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1716 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1717 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1718 else
1719 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1720 return ret;
1723 /*********************************************************************
1724 * _stat64 (MSVCRT.@)
1726 int CDECL MSVCRT__stat64(const char* path, struct MSVCRT__stat64 * buf)
1728 DWORD dw;
1729 WIN32_FILE_ATTRIBUTE_DATA hfi;
1730 unsigned short mode = ALL_S_IREAD;
1731 int plen;
1733 TRACE(":file (%s) buf(%p)\n",path,buf);
1735 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1737 TRACE("failed (%d)\n",GetLastError());
1738 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1739 return -1;
1742 memset(buf,0,sizeof(struct MSVCRT__stat64));
1744 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1745 Bon 011120: This FIXME seems incorrect
1746 Also a letter as first char isn't enough to be classified
1747 as a drive letter
1749 if (isalpha(*path)&& (*(path+1)==':'))
1750 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1751 else
1752 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1754 plen = strlen(path);
1756 /* Dir, or regular file? */
1757 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1758 (path[plen-1] == '\\'))
1759 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1760 else
1762 mode |= MSVCRT__S_IFREG;
1763 /* executable? */
1764 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1766 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1767 (tolower(path[plen-3]) << 16);
1768 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1769 mode |= ALL_S_IEXEC;
1773 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1774 mode |= ALL_S_IWRITE;
1776 buf->st_mode = mode;
1777 buf->st_nlink = 1;
1778 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1779 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1780 buf->st_atime = dw;
1781 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1782 buf->st_mtime = buf->st_ctime = dw;
1783 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1784 (long)(buf->st_size >> 32),(long)buf->st_size,
1785 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1786 return 0;
1789 /*********************************************************************
1790 * _stati64 (MSVCRT.@)
1792 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1794 int ret;
1795 struct MSVCRT__stat64 buf64;
1797 ret = MSVCRT__stat64(path, &buf64);
1798 if (!ret)
1799 msvcrt_stat64_to_stati64(&buf64, buf);
1800 return ret;
1803 /*********************************************************************
1804 * _stat (MSVCRT.@)
1806 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1807 { int ret;
1808 struct MSVCRT__stat64 buf64;
1810 ret = MSVCRT__stat64( path, &buf64);
1811 if (!ret)
1812 msvcrt_stat64_to_stat(&buf64, buf);
1813 return ret;
1816 /*********************************************************************
1817 * _wstat64 (MSVCRT.@)
1819 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1821 DWORD dw;
1822 WIN32_FILE_ATTRIBUTE_DATA hfi;
1823 unsigned short mode = ALL_S_IREAD;
1824 int plen;
1826 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1828 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1830 TRACE("failed (%d)\n",GetLastError());
1831 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1832 return -1;
1835 memset(buf,0,sizeof(struct MSVCRT__stat64));
1837 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1838 if (MSVCRT_iswalpha(*path))
1839 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1840 else
1841 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1843 plen = strlenW(path);
1845 /* Dir, or regular file? */
1846 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1847 (path[plen-1] == '\\'))
1848 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1849 else
1851 mode |= MSVCRT__S_IFREG;
1852 /* executable? */
1853 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1855 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1856 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1857 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1858 mode |= ALL_S_IEXEC;
1862 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1863 mode |= ALL_S_IWRITE;
1865 buf->st_mode = mode;
1866 buf->st_nlink = 1;
1867 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1868 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1869 buf->st_atime = dw;
1870 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1871 buf->st_mtime = buf->st_ctime = dw;
1872 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1873 (long)(buf->st_size >> 32),(long)buf->st_size,
1874 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1875 return 0;
1878 /*********************************************************************
1879 * _wstati64 (MSVCRT.@)
1881 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1883 int ret;
1884 struct MSVCRT__stat64 buf64;
1886 ret = MSVCRT__wstat64(path, &buf64);
1887 if (!ret)
1888 msvcrt_stat64_to_stati64(&buf64, buf);
1889 return ret;
1892 /*********************************************************************
1893 * _wstat (MSVCRT.@)
1895 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1897 int ret;
1898 struct MSVCRT__stat64 buf64;
1900 ret = MSVCRT__wstat64( path, &buf64 );
1901 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
1902 return ret;
1905 /*********************************************************************
1906 * _tell (MSVCRT.@)
1908 long CDECL _tell(int fd)
1910 return _lseek(fd, 0, SEEK_CUR);
1913 /*********************************************************************
1914 * _telli64 (MSVCRT.@)
1916 __int64 CDECL _telli64(int fd)
1918 return _lseeki64(fd, 0, SEEK_CUR);
1921 /*********************************************************************
1922 * _tempnam (MSVCRT.@)
1924 char * CDECL _tempnam(const char *dir, const char *prefix)
1926 char tmpbuf[MAX_PATH];
1927 const char *tmp_dir = MSVCRT_getenv("TMP");
1929 if (tmp_dir) dir = tmp_dir;
1931 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1932 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1934 TRACE("got name (%s)\n",tmpbuf);
1935 DeleteFileA(tmpbuf);
1936 return _strdup(tmpbuf);
1938 TRACE("failed (%d)\n",GetLastError());
1939 return NULL;
1942 /*********************************************************************
1943 * _wtempnam (MSVCRT.@)
1945 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1947 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1949 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1950 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1952 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1953 DeleteFileW(tmpbuf);
1954 return _wcsdup(tmpbuf);
1956 TRACE("failed (%d)\n",GetLastError());
1957 return NULL;
1960 /*********************************************************************
1961 * _umask (MSVCRT.@)
1963 int CDECL _umask(int umask)
1965 int old_umask = MSVCRT_umask;
1966 TRACE("(%d)\n",umask);
1967 MSVCRT_umask = umask;
1968 return old_umask;
1971 /*********************************************************************
1972 * _utime (MSVCRT.@)
1974 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
1976 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1978 if (fd > 0)
1980 int retVal = _futime(fd, t);
1981 _close(fd);
1982 return retVal;
1984 return -1;
1987 /*********************************************************************
1988 * _wutime (MSVCRT.@)
1990 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1992 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1994 if (fd > 0)
1996 int retVal = _futime(fd, t);
1997 _close(fd);
1998 return retVal;
2000 return -1;
2003 /*********************************************************************
2004 * _write (MSVCRT.@)
2006 int CDECL _write(int fd, const void* buf, unsigned int count)
2008 DWORD num_written;
2009 HANDLE hand = msvcrt_fdtoh(fd);
2011 /* Don't trace small writes, it gets *very* annoying */
2012 #if 0
2013 if (count > 32)
2014 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2015 #endif
2016 if (hand == INVALID_HANDLE_VALUE)
2018 *MSVCRT__errno() = MSVCRT_EBADF;
2019 return -1;
2022 /* If appending, go to EOF */
2023 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2024 _lseek(fd, 0, FILE_END);
2026 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2028 if (WriteFile(hand, buf, count, &num_written, NULL)
2029 && (num_written == count))
2030 return num_written;
2031 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2032 hand, GetLastError());
2033 *MSVCRT__errno() = MSVCRT_ENOSPC;
2035 else
2037 unsigned int i, j, nr_lf;
2038 char *p = NULL;
2039 const char *q;
2040 const char *s = (const char *)buf, *buf_start = (const char *)buf;
2041 /* find number of \n ( without preceding \r ) */
2042 for ( nr_lf=0,i = 0; i <count; i++)
2044 if (s[i]== '\n')
2046 nr_lf++;
2047 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2050 if (nr_lf)
2052 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2054 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
2056 if (s[i]== '\n')
2058 p[j++] = '\r';
2059 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2061 p[j++] = s[i];
2064 else
2066 FIXME("Malloc failed\n");
2067 nr_lf =0;
2068 q = buf;
2071 else
2072 q = buf;
2074 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2076 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2077 fd, hand, GetLastError(), num_written);
2078 *MSVCRT__errno() = MSVCRT_ENOSPC;
2079 if(nr_lf)
2080 MSVCRT_free(p);
2081 return s - buf_start;
2083 else
2085 if(nr_lf)
2086 MSVCRT_free(p);
2087 return count;
2090 return -1;
2093 /*********************************************************************
2094 * _putw (MSVCRT.@)
2096 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2098 int len;
2099 len = _write(file->_file, &val, sizeof(val));
2100 if (len == sizeof(val)) return val;
2101 file->_flag |= MSVCRT__IOERR;
2102 return MSVCRT_EOF;
2105 /*********************************************************************
2106 * fclose (MSVCRT.@)
2108 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2110 int r, flag;
2112 flag = file->_flag;
2113 MSVCRT_free(file->_tmpfname);
2114 file->_tmpfname = NULL;
2115 /* flush stdio buffers */
2116 if(file->_flag & MSVCRT__IOWRT)
2117 MSVCRT_fflush(file);
2118 if(file->_flag & MSVCRT__IOMYBUF)
2119 MSVCRT_free(file->_base);
2121 r=_close(file->_file);
2123 file->_flag = 0;
2125 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2128 /*********************************************************************
2129 * feof (MSVCRT.@)
2131 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2133 return file->_flag & MSVCRT__IOEOF;
2136 /*********************************************************************
2137 * ferror (MSVCRT.@)
2139 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2141 return file->_flag & MSVCRT__IOERR;
2144 /*********************************************************************
2145 * _filbuf (MSVCRT.@)
2147 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2149 /* Allocate buffer if needed */
2150 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2151 msvcrt_alloc_buffer(file);
2153 if(!(file->_flag & MSVCRT__IOREAD)) {
2154 if(file->_flag & MSVCRT__IORW) {
2155 file->_flag |= MSVCRT__IOREAD;
2156 } else {
2157 return MSVCRT_EOF;
2160 if(file->_flag & MSVCRT__IONBF) {
2161 unsigned char c;
2162 int r;
2163 if ((r = read_i(file->_file,&c,1)) != 1) {
2164 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2165 return MSVCRT_EOF;
2167 return c;
2168 } else {
2169 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2170 if(file->_cnt<=0) {
2171 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2172 file->_cnt = 0;
2173 return MSVCRT_EOF;
2175 file->_cnt--;
2176 file->_ptr = file->_base+1;
2177 return *(unsigned char *)file->_base;
2181 /*********************************************************************
2182 * fgetc (MSVCRT.@)
2184 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2186 unsigned char *i;
2187 unsigned int j;
2188 do {
2189 if (file->_cnt>0) {
2190 file->_cnt--;
2191 i = (unsigned char *)file->_ptr++;
2192 j = *i;
2193 } else
2194 j = MSVCRT__filbuf(file);
2195 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) || (j != '\r'))
2196 return j;
2197 } while(1);
2200 /*********************************************************************
2201 * _fgetchar (MSVCRT.@)
2203 int CDECL _fgetchar(void)
2205 return MSVCRT_fgetc(MSVCRT_stdin);
2208 /*********************************************************************
2209 * fgets (MSVCRT.@)
2211 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2213 int cc = MSVCRT_EOF;
2214 char * buf_start = s;
2216 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2217 file,file->_file,s,size);
2219 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2221 *s++ = (char)cc;
2222 size --;
2224 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2226 TRACE(":nothing read\n");
2227 return NULL;
2229 if ((cc != MSVCRT_EOF) && (size > 1))
2230 *s++ = cc;
2231 *s = '\0';
2232 TRACE(":got %s\n", debugstr_a(buf_start));
2233 return buf_start;
2236 /*********************************************************************
2237 * fgetwc (MSVCRT.@)
2239 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2240 * the CR from CR/LF combinations
2242 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2244 char c;
2246 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2248 MSVCRT_wchar_t wc;
2249 int i,j;
2250 char *chp, *wcp;
2251 wcp = (char *)&wc;
2252 for(i=0; i<sizeof(wc); i++)
2254 if (file->_cnt>0)
2256 file->_cnt--;
2257 chp = file->_ptr++;
2258 wcp[i] = *chp;
2260 else
2262 j = MSVCRT__filbuf(file);
2263 if(file->_cnt<=0)
2265 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2266 file->_cnt = 0;
2267 return MSVCRT_WEOF;
2269 wcp[i] = j;
2272 return wc;
2275 c = MSVCRT_fgetc(file);
2276 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2278 FIXME("Treat Multibyte characters\n");
2280 if (c == MSVCRT_EOF)
2281 return MSVCRT_WEOF;
2282 else
2283 return (MSVCRT_wint_t)c;
2286 /*********************************************************************
2287 * _getw (MSVCRT.@)
2289 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2291 char *ch;
2292 int i, j, k;
2293 ch = (char *)&i;
2294 for (j=0; j<sizeof(int); j++) {
2295 k = MSVCRT_fgetc(file);
2296 if (k == MSVCRT_EOF) {
2297 file->_flag |= MSVCRT__IOEOF;
2298 return EOF;
2300 ch[j] = k;
2302 return i;
2305 /*********************************************************************
2306 * getwc (MSVCRT.@)
2308 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2310 return MSVCRT_fgetwc(file);
2313 /*********************************************************************
2314 * _fgetwchar (MSVCRT.@)
2316 MSVCRT_wint_t CDECL _fgetwchar(void)
2318 return MSVCRT_fgetwc(MSVCRT_stdin);
2321 /*********************************************************************
2322 * getwchar (MSVCRT.@)
2324 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2326 return _fgetwchar();
2329 /*********************************************************************
2330 * fgetws (MSVCRT.@)
2332 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2334 int cc = MSVCRT_WEOF;
2335 MSVCRT_wchar_t * buf_start = s;
2337 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2338 file,file->_file,s,size);
2340 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2342 *s++ = (char)cc;
2343 size --;
2345 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2347 TRACE(":nothing read\n");
2348 return NULL;
2350 if ((cc != MSVCRT_WEOF) && (size > 1))
2351 *s++ = cc;
2352 *s = 0;
2353 TRACE(":got %s\n", debugstr_w(buf_start));
2354 return buf_start;
2357 /*********************************************************************
2358 * fwrite (MSVCRT.@)
2360 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2362 MSVCRT_size_t wrcnt=size * nmemb;
2363 int written = 0;
2364 if (size == 0)
2365 return 0;
2366 if(file->_cnt) {
2367 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2368 memcpy(file->_ptr, ptr, pcnt);
2369 file->_cnt -= pcnt;
2370 file->_ptr += pcnt;
2371 written = pcnt;
2372 wrcnt -= pcnt;
2373 ptr = (const char*)ptr + pcnt;
2374 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2375 if(file->_flag & MSVCRT__IORW) {
2376 file->_flag |= MSVCRT__IOWRT;
2377 } else
2378 return 0;
2380 if(wrcnt) {
2381 /* Flush buffer */
2382 int res=msvcrt_flush_buffer(file);
2383 if(!res) {
2384 int pwritten = _write(file->_file, ptr, wrcnt);
2385 if (pwritten <= 0)
2387 file->_flag |= MSVCRT__IOERR;
2388 pwritten=0;
2390 written += pwritten;
2393 return written / size;
2396 /*********************************************************************
2397 * fputwc (MSVCRT.@)
2399 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2401 MSVCRT_wchar_t mwc=wc;
2402 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2403 return MSVCRT_WEOF;
2404 return wc;
2407 /*********************************************************************
2408 * _fputwchar (MSVCRT.@)
2410 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2412 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2415 /*********************************************************************
2416 * _fsopen (MSVCRT.@)
2418 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2420 MSVCRT_FILE* file;
2421 int open_flags, stream_flags, fd;
2423 TRACE("(%s,%s)\n",path,mode);
2425 /* map mode string to open() flags. "man fopen" for possibilities. */
2426 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2427 return NULL;
2429 LOCK_FILES();
2430 fd = MSVCRT__sopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2431 if (fd < 0)
2432 file = NULL;
2433 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2434 != -1)
2435 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2436 else if (file)
2438 file->_flag = 0;
2439 file = NULL;
2442 TRACE(":got (%p)\n",file);
2443 if (fd >= 0 && !file)
2444 _close(fd);
2445 UNLOCK_FILES();
2446 return file;
2449 /*********************************************************************
2450 * _wfsopen (MSVCRT.@)
2452 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2454 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2455 char *patha = MSVCRT_calloc(plen + 1, 1);
2456 char *modea = MSVCRT_calloc(mlen + 1, 1);
2458 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2460 if (patha && modea &&
2461 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2462 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2464 MSVCRT_FILE *retval = MSVCRT__fsopen(patha,modea,share);
2465 MSVCRT_free(patha);
2466 MSVCRT_free(modea);
2467 return retval;
2470 msvcrt_set_errno(GetLastError());
2471 return NULL;
2474 /*********************************************************************
2475 * fopen (MSVCRT.@)
2477 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2479 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2482 /*********************************************************************
2483 * _wfopen (MSVCRT.@)
2485 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2487 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2490 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2491 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2493 /*********************************************************************
2494 * fputc (MSVCRT.@)
2496 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2498 if(file->_cnt>0) {
2499 *file->_ptr++=c;
2500 file->_cnt--;
2501 if (c == '\n')
2503 int res = msvcrt_flush_buffer(file);
2504 return res ? res : c;
2506 else
2507 return c;
2508 } else {
2509 return MSVCRT__flsbuf(c, file);
2513 /*********************************************************************
2514 * _flsbuf (MSVCRT.@)
2516 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2518 /* Flush output buffer */
2519 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2520 msvcrt_alloc_buffer(file);
2522 if(!(file->_flag & MSVCRT__IOWRT)) {
2523 if(file->_flag & MSVCRT__IORW) {
2524 file->_flag |= MSVCRT__IOWRT;
2525 } else {
2526 return MSVCRT_EOF;
2529 if(file->_bufsiz) {
2530 int res=msvcrt_flush_buffer(file);
2531 return res?res : MSVCRT_fputc(c, file);
2532 } else {
2533 unsigned char cc=c;
2534 int len;
2535 len = _write(file->_file, &cc, 1);
2536 if (len == 1) return c;
2537 file->_flag |= MSVCRT__IOERR;
2538 return MSVCRT_EOF;
2542 /*********************************************************************
2543 * _fputchar (MSVCRT.@)
2545 int CDECL _fputchar(int c)
2547 return MSVCRT_fputc(c, MSVCRT_stdout);
2550 /*********************************************************************
2551 * fread (MSVCRT.@)
2553 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2554 { MSVCRT_size_t rcnt=size * nmemb;
2555 MSVCRT_size_t read=0;
2556 int pread=0;
2558 if(!rcnt)
2559 return 0;
2561 /* first buffered data */
2562 if(file->_cnt>0) {
2563 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2564 memcpy(ptr, file->_ptr, pcnt);
2565 file->_cnt -= pcnt;
2566 file->_ptr += pcnt;
2567 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2568 pcnt -= remove_cr(ptr,pcnt);
2569 read += pcnt ;
2570 rcnt -= pcnt ;
2571 ptr = (char*)ptr + pcnt;
2572 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2573 if(file->_flag & MSVCRT__IORW) {
2574 file->_flag |= MSVCRT__IOREAD;
2575 } else
2576 return 0;
2578 while(rcnt>0)
2580 int i;
2581 /* Fill the buffer on small reads.
2582 * TODO: Use a better buffering strategy.
2584 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2585 if (file->_bufsiz == 0) {
2586 msvcrt_alloc_buffer(file);
2588 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2589 file->_ptr = file->_base;
2590 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2591 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2592 if (i > 0 && i < file->_cnt) {
2593 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2594 file->_flag &= ~MSVCRT__IOEOF;
2596 if (i > 0) {
2597 memcpy(ptr, file->_ptr, i);
2598 file->_cnt -= i;
2599 file->_ptr += i;
2601 } else {
2602 i = _read(file->_file,ptr, rcnt);
2604 pread += i;
2605 rcnt -= i;
2606 ptr = (char *)ptr+i;
2607 /* expose feof condition in the flags
2608 * MFC tests file->_flag for feof, and doesn't call feof())
2610 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2611 file->_flag |= MSVCRT__IOEOF;
2612 else if (i == -1)
2614 file->_flag |= MSVCRT__IOERR;
2615 pread = 0;
2616 rcnt = 0;
2618 if (i < 1) break;
2620 read+=pread;
2621 return read / size;
2624 /*********************************************************************
2625 * freopen (MSVCRT.@)
2628 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2630 int open_flags, stream_flags, fd;
2632 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2634 LOCK_FILES();
2635 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2636 file = NULL;
2637 else
2639 MSVCRT_fclose(file);
2640 /* map mode string to open() flags. "man fopen" for possibilities. */
2641 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2642 file = NULL;
2643 else
2645 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2646 if (fd < 0)
2647 file = NULL;
2648 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2650 file->_flag = 0;
2651 WARN(":failed-last error (%d)\n",GetLastError());
2652 msvcrt_set_errno(GetLastError());
2653 file = NULL;
2657 UNLOCK_FILES();
2658 return file;
2661 /*********************************************************************
2662 * fsetpos (MSVCRT.@)
2664 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2666 /* Note that all this has been lifted 'as is' from fseek */
2667 if(file->_flag & MSVCRT__IOWRT)
2668 msvcrt_flush_buffer(file);
2670 /* Discard buffered input */
2671 file->_cnt = 0;
2672 file->_ptr = file->_base;
2674 /* Reset direction of i/o */
2675 if(file->_flag & MSVCRT__IORW) {
2676 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2679 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2682 /*********************************************************************
2683 * ftell (MSVCRT.@)
2685 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2687 int off=0;
2688 long pos;
2689 if(file->_bufsiz) {
2690 if( file->_flag & MSVCRT__IOWRT ) {
2691 off = file->_ptr - file->_base;
2692 } else {
2693 off = -file->_cnt;
2696 pos = _tell(file->_file);
2697 if(pos == -1) return pos;
2698 return off + pos;
2701 /*********************************************************************
2702 * fgetpos (MSVCRT.@)
2704 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2706 /* This code has been lifted form the MSVCRT_ftell function */
2707 int off=0;
2709 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2711 if (*pos == -1) return -1;
2713 if(file->_bufsiz) {
2714 if( file->_flag & MSVCRT__IOWRT ) {
2715 off = file->_ptr - file->_base;
2716 } else {
2717 off = -file->_cnt;
2720 *pos += off;
2722 return 0;
2725 /*********************************************************************
2726 * fputs (MSVCRT.@)
2728 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2730 size_t i, len = strlen(s);
2731 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2732 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2733 for (i=0; i<len; i++)
2734 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2735 return MSVCRT_EOF;
2736 return 0;
2739 /*********************************************************************
2740 * fputws (MSVCRT.@)
2742 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2744 size_t i, len = strlenW(s);
2745 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2746 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2747 for (i=0; i<len; i++)
2749 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2750 return MSVCRT_WEOF;
2751 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2752 return MSVCRT_WEOF;
2754 return 0;
2757 /*********************************************************************
2758 * getchar (MSVCRT.@)
2760 int CDECL MSVCRT_getchar(void)
2762 return MSVCRT_fgetc(MSVCRT_stdin);
2765 /*********************************************************************
2766 * getc (MSVCRT.@)
2768 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2770 return MSVCRT_fgetc(file);
2773 /*********************************************************************
2774 * gets (MSVCRT.@)
2776 char * CDECL MSVCRT_gets(char *buf)
2778 int cc;
2779 char * buf_start = buf;
2781 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2782 cc = MSVCRT_fgetc(MSVCRT_stdin))
2783 if(cc != '\r') *buf++ = (char)cc;
2785 *buf = '\0';
2787 TRACE("got '%s'\n", buf_start);
2788 return buf_start;
2791 /*********************************************************************
2792 * _getws (MSVCRT.@)
2794 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2796 MSVCRT_wint_t cc;
2797 MSVCRT_wchar_t* ws = buf;
2799 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2800 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2802 if (cc != '\r')
2803 *buf++ = (MSVCRT_wchar_t)cc;
2805 *buf = '\0';
2807 TRACE("got %s\n", debugstr_w(ws));
2808 return ws;
2811 /*********************************************************************
2812 * putc (MSVCRT.@)
2814 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2816 return MSVCRT_fputc(c, file);
2819 /*********************************************************************
2820 * putchar (MSVCRT.@)
2822 int CDECL MSVCRT_putchar(int c)
2824 return MSVCRT_fputc(c, MSVCRT_stdout);
2827 /*********************************************************************
2828 * puts (MSVCRT.@)
2830 int CDECL MSVCRT_puts(const char *s)
2832 size_t len = strlen(s);
2833 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2834 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2837 /*********************************************************************
2838 * _putws (MSVCRT.@)
2840 int CDECL _putws(const MSVCRT_wchar_t *s)
2842 static const MSVCRT_wchar_t nl = '\n';
2843 size_t len = strlenW(s);
2844 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2845 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2848 /*********************************************************************
2849 * remove (MSVCRT.@)
2851 int CDECL MSVCRT_remove(const char *path)
2853 TRACE("(%s)\n",path);
2854 if (DeleteFileA(path))
2855 return 0;
2856 TRACE(":failed (%d)\n",GetLastError());
2857 msvcrt_set_errno(GetLastError());
2858 return -1;
2861 /*********************************************************************
2862 * _wremove (MSVCRT.@)
2864 int CDECL _wremove(const MSVCRT_wchar_t *path)
2866 TRACE("(%s)\n",debugstr_w(path));
2867 if (DeleteFileW(path))
2868 return 0;
2869 TRACE(":failed (%d)\n",GetLastError());
2870 msvcrt_set_errno(GetLastError());
2871 return -1;
2874 /*********************************************************************
2875 * rename (MSVCRT.@)
2877 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2879 TRACE(":from %s to %s\n",oldpath,newpath);
2880 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2881 return 0;
2882 TRACE(":failed (%d)\n",GetLastError());
2883 msvcrt_set_errno(GetLastError());
2884 return -1;
2887 /*********************************************************************
2888 * _wrename (MSVCRT.@)
2890 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2892 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2893 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2894 return 0;
2895 TRACE(":failed (%d)\n",GetLastError());
2896 msvcrt_set_errno(GetLastError());
2897 return -1;
2900 /*********************************************************************
2901 * setvbuf (MSVCRT.@)
2903 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2905 /* TODO: Check if file busy */
2906 if(file->_bufsiz) {
2907 MSVCRT_free(file->_base);
2908 file->_bufsiz = 0;
2909 file->_cnt = 0;
2911 if(mode == MSVCRT__IOFBF) {
2912 file->_flag &= ~MSVCRT__IONBF;
2913 file->_base = file->_ptr = buf;
2914 if(buf) {
2915 file->_bufsiz = size;
2917 } else {
2918 file->_flag |= MSVCRT__IONBF;
2920 return 0;
2923 /*********************************************************************
2924 * setbuf (MSVCRT.@)
2926 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2928 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2931 /*********************************************************************
2932 * tmpnam (MSVCRT.@)
2934 char * CDECL MSVCRT_tmpnam(char *s)
2936 static int unique;
2937 char tmpstr[16];
2938 char *p;
2939 int count;
2940 if (s == 0)
2941 s = MSVCRT_tmpname;
2942 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2943 p = s + sprintf(s, "\\s%s.", tmpstr);
2944 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2946 msvcrt_int_to_base32(unique++, tmpstr);
2947 strcpy(p, tmpstr);
2948 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2949 GetLastError() == ERROR_FILE_NOT_FOUND)
2950 break;
2952 return s;
2955 /*********************************************************************
2956 * tmpfile (MSVCRT.@)
2958 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
2960 char *filename = MSVCRT_tmpnam(NULL);
2961 int fd;
2962 MSVCRT_FILE* file = NULL;
2964 LOCK_FILES();
2965 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2966 if (fd != -1 && (file = msvcrt_alloc_fp()))
2968 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2970 file->_flag = 0;
2971 file = NULL;
2973 else file->_tmpfname = _strdup(filename);
2975 UNLOCK_FILES();
2976 return file;
2979 /*********************************************************************
2980 * vfprintf (MSVCRT.@)
2982 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2984 char buf[2048], *mem = buf;
2985 int written, resize = sizeof(buf), retval;
2986 /* There are two conventions for vsnprintf failing:
2987 * Return -1 if we truncated, or
2988 * Return the number of bytes that would have been written
2989 * The code below handles both cases
2991 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
2992 written > resize)
2994 resize = (written == -1 ? resize * 2 : written + 1);
2995 if (mem != buf)
2996 MSVCRT_free (mem);
2997 if (!(mem = (char *)MSVCRT_malloc(resize)))
2998 return MSVCRT_EOF;
3000 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3001 if (mem != buf)
3002 MSVCRT_free (mem);
3003 return retval;
3006 /*********************************************************************
3007 * vfwprintf (MSVCRT.@)
3008 * FIXME:
3009 * Is final char included in written (then resize is too big) or not
3010 * (then we must test for equality too)?
3012 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
3014 MSVCRT_wchar_t buf[2048], *mem = buf;
3015 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3016 /* See vfprintf comments */
3017 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3018 written > resize)
3020 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3021 if (mem != buf)
3022 MSVCRT_free (mem);
3023 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
3024 return MSVCRT_EOF;
3026 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3027 if (mem != buf)
3028 MSVCRT_free (mem);
3029 return retval;
3032 /*********************************************************************
3033 * vprintf (MSVCRT.@)
3035 int CDECL MSVCRT_vprintf(const char *format, va_list valist)
3037 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3040 /*********************************************************************
3041 * vwprintf (MSVCRT.@)
3043 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
3045 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3048 /*********************************************************************
3049 * fprintf (MSVCRT.@)
3051 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3053 va_list valist;
3054 int res;
3055 va_start(valist, format);
3056 res = MSVCRT_vfprintf(file, format, valist);
3057 va_end(valist);
3058 return res;
3061 /*********************************************************************
3062 * fwprintf (MSVCRT.@)
3064 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3066 va_list valist;
3067 int res;
3068 va_start(valist, format);
3069 res = MSVCRT_vfwprintf(file, format, valist);
3070 va_end(valist);
3071 return res;
3074 /*********************************************************************
3075 * printf (MSVCRT.@)
3077 int CDECL MSVCRT_printf(const char *format, ...)
3079 va_list valist;
3080 int res;
3081 va_start(valist, format);
3082 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3083 va_end(valist);
3084 return res;
3087 /*********************************************************************
3088 * ungetc (MSVCRT.@)
3090 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3092 if (c == MSVCRT_EOF)
3093 return MSVCRT_EOF;
3094 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3095 msvcrt_alloc_buffer(file);
3096 file->_ptr++;
3098 if(file->_ptr>file->_base) {
3099 file->_ptr--;
3100 *file->_ptr=c;
3101 file->_cnt++;
3102 MSVCRT_clearerr(file);
3103 return c;
3105 return MSVCRT_EOF;
3108 /*********************************************************************
3109 * ungetwc (MSVCRT.@)
3111 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3113 MSVCRT_wchar_t mwc = wc;
3114 char * pp = (char *)&mwc;
3115 int i;
3116 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3117 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3118 return MSVCRT_WEOF;
3120 return mwc;
3123 /*********************************************************************
3124 * wprintf (MSVCRT.@)
3126 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3128 va_list valist;
3129 int res;
3130 va_start(valist, format);
3131 res = MSVCRT_vwprintf(format, valist);
3132 va_end(valist);
3133 return res;
3136 /*********************************************************************
3137 * _getmaxstdio (MSVCRT.@)
3139 int CDECL _getmaxstdio(void)
3141 FIXME("stub, always returns 512\n");
3142 return 512;
3145 /*********************************************************************
3146 * _setmaxstdio_ (MSVCRT.@)
3148 int CDECL _setmaxstdio(int newmax)
3150 int res;
3151 if( newmax > 2048)
3152 res = -1;
3153 else
3154 res = newmax;
3155 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3156 return res;
3159 /*********************************************************************
3160 * __pioinfo (MSVCRT.@)
3161 * FIXME: see MSVCRT_MAX_FILES define.
3163 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3164 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3165 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3166 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3167 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3168 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3169 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3170 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3171 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3172 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3173 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3174 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3177 /*********************************************************************
3178 * __badioinfo (MSVCRT.@)
3180 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };