janitorial: Remove links to any microsoft site.
[wine/gsoc_dplay.git] / dlls / msvcrt / file.c
blobc2539c2910733ca07387e3415347371fb39a81d5
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 "winternl.h"
43 #include "msvcrt.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 */
60 #define WX_OPEN 0x01
61 #define WX_ATEOF 0x02
62 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
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(WORD *size, BYTE **block)
289 int fd;
290 char* wxflag_ptr;
291 HANDLE* handle_ptr;
293 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
294 *block = MSVCRT_calloc(*size, 1);
295 if (!*block)
297 *size = 0;
298 return FALSE;
300 wxflag_ptr = (char*)*block + sizeof(unsigned);
301 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
303 *(unsigned*)*block = 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 MSVCRT_file_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs");
332 GetStartupInfoA(&si);
333 if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
335 char* wxflag_ptr;
336 HANDLE* handle_ptr;
338 MSVCRT_fdend = *(unsigned*)si.lpReserved2;
340 wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
341 handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
343 MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
344 for (i = 0; i < MSVCRT_fdend; i++)
346 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
348 MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
349 MSVCRT_fdesc[i].handle = *handle_ptr;
351 else
353 MSVCRT_fdesc[i].wxflag = 0;
354 MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
356 wxflag_ptr++; handle_ptr++;
358 for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
359 if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
362 if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
364 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
365 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, TRUE,
366 DUPLICATE_SAME_ACCESS);
367 MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
369 if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
371 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
372 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, TRUE,
373 DUPLICATE_SAME_ACCESS);
374 MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
376 if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
378 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
379 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, TRUE,
380 DUPLICATE_SAME_ACCESS);
381 MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
384 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
385 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
387 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
388 for (i = 0; i < 3; i++)
390 /* FILE structs for stdin/out/err are static and never deleted */
391 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
392 MSVCRT__iob[i]._file = i;
393 MSVCRT__iob[i]._tmpfname = NULL;
394 MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
396 MSVCRT_stream_idx = 3;
399 /* INTERNAL: Flush stdio file buffer */
400 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
402 if(file->_bufsiz) {
403 int cnt=file->_ptr-file->_base;
404 if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
405 file->_flag |= MSVCRT__IOERR;
406 return MSVCRT_EOF;
408 file->_ptr=file->_base;
409 file->_cnt=file->_bufsiz;
411 return 0;
414 /* INTERNAL: Allocate stdio file buffer */
415 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
417 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
418 if(file->_base) {
419 file->_bufsiz = MSVCRT_BUFSIZ;
420 file->_flag |= MSVCRT__IOMYBUF;
421 } else {
422 file->_base = (char*)(&file->_charbuf);
423 /* put here 2 ??? */
424 file->_bufsiz = sizeof(file->_charbuf);
426 file->_ptr = file->_base;
427 file->_cnt = 0;
430 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
431 static void msvcrt_int_to_base32(int num, char *str)
433 char *p;
434 int n = num;
435 int digits = 0;
437 while (n != 0)
439 n >>= 5;
440 digits++;
442 p = str + digits;
443 *p = 0;
444 while (--p >= str)
446 *p = (num & 31) + '0';
447 if (*p > '9')
448 *p += ('a' - '0' - 10);
449 num >>= 5;
453 /*********************************************************************
454 * __p__iob(MSVCRT.@)
456 MSVCRT_FILE * CDECL __p__iob(void)
458 return &MSVCRT__iob[0];
461 /*********************************************************************
462 * _access (MSVCRT.@)
464 int CDECL MSVCRT__access(const char *filename, int mode)
466 DWORD attr = GetFileAttributesA(filename);
468 TRACE("(%s,%d) %d\n",filename,mode,attr);
470 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
472 msvcrt_set_errno(GetLastError());
473 return -1;
475 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
477 msvcrt_set_errno(ERROR_ACCESS_DENIED);
478 return -1;
480 return 0;
483 /*********************************************************************
484 * _waccess (MSVCRT.@)
486 int CDECL _waccess(const MSVCRT_wchar_t *filename, int mode)
488 DWORD attr = GetFileAttributesW(filename);
490 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
492 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
494 msvcrt_set_errno(GetLastError());
495 return -1;
497 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
499 msvcrt_set_errno(ERROR_ACCESS_DENIED);
500 return -1;
502 return 0;
505 /*********************************************************************
506 * _chmod (MSVCRT.@)
508 int CDECL MSVCRT__chmod(const char *path, int flags)
510 DWORD oldFlags = GetFileAttributesA(path);
512 if (oldFlags != INVALID_FILE_ATTRIBUTES)
514 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
515 oldFlags | FILE_ATTRIBUTE_READONLY;
517 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
518 return 0;
520 msvcrt_set_errno(GetLastError());
521 return -1;
524 /*********************************************************************
525 * _wchmod (MSVCRT.@)
527 int CDECL _wchmod(const MSVCRT_wchar_t *path, int flags)
529 DWORD oldFlags = GetFileAttributesW(path);
531 if (oldFlags != INVALID_FILE_ATTRIBUTES)
533 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
534 oldFlags | FILE_ATTRIBUTE_READONLY;
536 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
537 return 0;
539 msvcrt_set_errno(GetLastError());
540 return -1;
543 /*********************************************************************
544 * _unlink (MSVCRT.@)
546 int CDECL MSVCRT__unlink(const char *path)
548 TRACE("%s\n",debugstr_a(path));
549 if(DeleteFileA(path))
550 return 0;
551 TRACE("failed (%d)\n",GetLastError());
552 msvcrt_set_errno(GetLastError());
553 return -1;
556 /*********************************************************************
557 * _wunlink (MSVCRT.@)
559 int CDECL _wunlink(const MSVCRT_wchar_t *path)
561 TRACE("(%s)\n",debugstr_w(path));
562 if(DeleteFileW(path))
563 return 0;
564 TRACE("failed (%d)\n",GetLastError());
565 msvcrt_set_errno(GetLastError());
566 return -1;
569 /* _flushall calls MSVCRT_fflush which calls _flushall */
570 int CDECL MSVCRT_fflush(MSVCRT_FILE* file);
572 /*********************************************************************
573 * _flushall (MSVCRT.@)
575 int CDECL _flushall(void)
577 int i, num_flushed = 0;
579 LOCK_FILES();
580 for (i = 3; i < MSVCRT_stream_idx; i++)
581 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
583 #if 0
584 /* FIXME: flush, do not commit */
585 if (_commit(i) == -1)
586 if (MSVCRT_fstreams[i])
587 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
588 #endif
589 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
590 MSVCRT_fflush(MSVCRT_fstreams[i]);
591 num_flushed++;
594 UNLOCK_FILES();
596 TRACE(":flushed (%d) handles\n",num_flushed);
597 return num_flushed;
600 /*********************************************************************
601 * fflush (MSVCRT.@)
603 int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
605 if(!file) {
606 _flushall();
607 } else if(file->_flag & MSVCRT__IOWRT) {
608 int res=msvcrt_flush_buffer(file);
609 return res;
611 return 0;
614 /*********************************************************************
615 * _close (MSVCRT.@)
617 int CDECL MSVCRT__close(int fd)
619 HANDLE hand;
620 int ret;
622 LOCK_FILES();
623 hand = msvcrt_fdtoh(fd);
624 TRACE(":fd (%d) handle (%p)\n",fd,hand);
625 if (hand == INVALID_HANDLE_VALUE)
626 ret = -1;
627 else if (!CloseHandle(hand))
629 WARN(":failed-last error (%d)\n",GetLastError());
630 msvcrt_set_errno(GetLastError());
631 ret = -1;
633 else
635 msvcrt_free_fd(fd);
636 ret = 0;
638 UNLOCK_FILES();
639 TRACE(":ok\n");
640 return ret;
643 /*********************************************************************
644 * _commit (MSVCRT.@)
646 int CDECL _commit(int fd)
648 HANDLE hand = msvcrt_fdtoh(fd);
650 TRACE(":fd (%d) handle (%p)\n",fd,hand);
651 if (hand == INVALID_HANDLE_VALUE)
652 return -1;
654 if (!FlushFileBuffers(hand))
656 if (GetLastError() == ERROR_INVALID_HANDLE)
658 /* FlushFileBuffers fails for console handles
659 * so we ignore this error.
661 return 0;
663 TRACE(":failed-last error (%d)\n",GetLastError());
664 msvcrt_set_errno(GetLastError());
665 return -1;
667 TRACE(":ok\n");
668 return 0;
671 /*********************************************************************
672 * _dup2 (MSVCRT.@)
673 * NOTES
674 * MSDN isn't clear on this point, but the remarks for _pipe
675 * indicate file descriptors duplicated with _dup and _dup2 are always
676 * inheritable.
678 int CDECL MSVCRT__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 MSVCRT__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 MSVCRT__dup(int od)
727 int fd, ret;
729 LOCK_FILES();
730 fd = MSVCRT_fdstart;
731 if (MSVCRT__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 MSVCRT_file_cs.DebugInfo->Spare[0] = 0;
800 DeleteCriticalSection(&MSVCRT_file_cs);
803 /*********************************************************************
804 * _lseeki64 (MSVCRT.@)
806 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
808 HANDLE hand = msvcrt_fdtoh(fd);
809 LARGE_INTEGER ofs, ret;
811 TRACE(":fd (%d) handle (%p)\n",fd,hand);
812 if (hand == INVALID_HANDLE_VALUE)
813 return -1;
815 if (whence < 0 || whence > 2)
817 *MSVCRT__errno() = MSVCRT_EINVAL;
818 return -1;
821 TRACE(":fd (%d) to %s pos %s\n",
822 fd,wine_dbgstr_longlong(offset),
823 (whence==SEEK_SET)?"SEEK_SET":
824 (whence==SEEK_CUR)?"SEEK_CUR":
825 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
827 ofs.QuadPart = offset;
828 if (SetFilePointerEx(hand, ofs, &ret, whence))
830 MSVCRT_fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
831 /* FIXME: What if we seek _to_ EOF - is EOF set? */
833 return ret.QuadPart;
835 TRACE(":error-last error (%d)\n",GetLastError());
836 msvcrt_set_errno(GetLastError());
837 return -1;
840 /*********************************************************************
841 * _lseek (MSVCRT.@)
843 LONG CDECL _lseek(int fd, LONG offset, int whence)
845 return _lseeki64(fd, offset, whence);
848 /*********************************************************************
849 * _locking (MSVCRT.@)
851 * This is untested; the underlying LockFile doesn't work yet.
853 int CDECL _locking(int fd, int mode, LONG nbytes)
855 BOOL ret;
856 DWORD cur_locn;
857 HANDLE hand = msvcrt_fdtoh(fd);
859 TRACE(":fd (%d) handle (%p)\n",fd,hand);
860 if (hand == INVALID_HANDLE_VALUE)
861 return -1;
863 if (mode < 0 || mode > 4)
865 *MSVCRT__errno() = MSVCRT_EINVAL;
866 return -1;
869 TRACE(":fd (%d) by 0x%08x mode %s\n",
870 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
871 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
872 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
873 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
874 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
875 "UNKNOWN");
877 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
879 FIXME ("Seek failed\n");
880 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
881 return -1;
883 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
885 int nretry = 10;
886 ret = 1; /* just to satisfy gcc */
887 while (nretry--)
889 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
890 if (ret) break;
891 Sleep(1);
894 else if (mode == MSVCRT__LK_UNLCK)
895 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
896 else
897 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
898 /* FIXME - what about error settings? */
899 return ret ? 0 : -1;
902 /*********************************************************************
903 * fseek (MSVCRT.@)
905 int CDECL MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
907 /* Flush output if needed */
908 if(file->_flag & MSVCRT__IOWRT)
909 msvcrt_flush_buffer(file);
911 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
912 offset -= file->_cnt;
914 /* Discard buffered input */
915 file->_cnt = 0;
916 file->_ptr = file->_base;
917 /* Reset direction of i/o */
918 if(file->_flag & MSVCRT__IORW) {
919 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
921 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
924 /*********************************************************************
925 * _chsize (MSVCRT.@)
927 int CDECL _chsize(int fd, long size)
929 LONG cur, pos;
930 HANDLE handle;
931 BOOL ret = FALSE;
933 TRACE("(fd=%d, size=%ld)\n", fd, size);
935 LOCK_FILES();
937 handle = msvcrt_fdtoh(fd);
938 if (handle != INVALID_HANDLE_VALUE)
940 /* save the current file pointer */
941 cur = _lseek(fd, 0, SEEK_CUR);
942 if (cur >= 0)
944 pos = _lseek(fd, size, SEEK_SET);
945 if (pos >= 0)
947 ret = SetEndOfFile(handle);
948 if (!ret) msvcrt_set_errno(GetLastError());
951 /* restore the file pointer */
952 _lseek(fd, cur, SEEK_SET);
956 UNLOCK_FILES();
957 return ret ? 0 : -1;
960 /*********************************************************************
961 * clearerr (MSVCRT.@)
963 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
965 TRACE(":file (%p) fd (%d)\n",file,file->_file);
966 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
969 /*********************************************************************
970 * rewind (MSVCRT.@)
972 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
974 TRACE(":file (%p) fd (%d)\n",file,file->_file);
975 MSVCRT_fseek(file, 0L, SEEK_SET);
976 MSVCRT_clearerr(file);
979 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
981 int plus = strchr(mode, '+') != NULL;
983 switch(*mode++)
985 case 'R': case 'r':
986 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
987 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
988 break;
989 case 'W': case 'w':
990 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
991 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
992 break;
993 case 'A': case 'a':
994 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
995 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
996 break;
997 default:
998 return -1;
1001 while (*mode)
1002 switch (*mode++)
1004 case 'B': case 'b':
1005 *open_flags |= MSVCRT__O_BINARY;
1006 *open_flags &= ~MSVCRT__O_TEXT;
1007 break;
1008 case 'T': case 't':
1009 *open_flags |= MSVCRT__O_TEXT;
1010 *open_flags &= ~MSVCRT__O_BINARY;
1011 break;
1012 case '+':
1013 break;
1014 default:
1015 FIXME(":unknown flag %c not supported\n",mode[-1]);
1017 return 0;
1020 /*********************************************************************
1021 * _fdopen (MSVCRT.@)
1023 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1025 int open_flags, stream_flags;
1026 MSVCRT_FILE* file;
1028 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1030 LOCK_FILES();
1031 if (!(file = msvcrt_alloc_fp()))
1032 file = NULL;
1033 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1035 file->_flag = 0;
1036 file = NULL;
1038 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1039 UNLOCK_FILES();
1041 return file;
1044 /*********************************************************************
1045 * _wfdopen (MSVCRT.@)
1047 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1049 unsigned mlen = strlenW(mode);
1050 char *modea = MSVCRT_calloc(mlen + 1, 1);
1051 MSVCRT_FILE* file = NULL;
1052 int open_flags, stream_flags;
1054 if (modea &&
1055 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1057 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1058 LOCK_FILES();
1059 if (!(file = msvcrt_alloc_fp()))
1060 file = NULL;
1061 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1063 file->_flag = 0;
1064 file = NULL;
1066 else
1068 if (file)
1069 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1070 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1072 UNLOCK_FILES();
1074 return file;
1077 /*********************************************************************
1078 * _filelength (MSVCRT.@)
1080 LONG CDECL _filelength(int fd)
1082 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1083 if (curPos != -1)
1085 LONG endPos = _lseek(fd, 0, SEEK_END);
1086 if (endPos != -1)
1088 if (endPos != curPos)
1089 _lseek(fd, curPos, SEEK_SET);
1090 return endPos;
1093 return -1;
1096 /*********************************************************************
1097 * _filelengthi64 (MSVCRT.@)
1099 __int64 CDECL _filelengthi64(int fd)
1101 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1102 if (curPos != -1)
1104 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1105 if (endPos != -1)
1107 if (endPos != curPos)
1108 _lseeki64(fd, curPos, SEEK_SET);
1109 return endPos;
1112 return -1;
1115 /*********************************************************************
1116 * _fileno (MSVCRT.@)
1118 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1120 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1121 return file->_file;
1124 /*********************************************************************
1125 * _fstat64 (MSVCRT.@)
1127 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1129 DWORD dw;
1130 DWORD type;
1131 BY_HANDLE_FILE_INFORMATION hfi;
1132 HANDLE hand = msvcrt_fdtoh(fd);
1134 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1135 if (hand == INVALID_HANDLE_VALUE)
1136 return -1;
1138 if (!buf)
1140 WARN(":failed-NULL buf\n");
1141 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1142 return -1;
1145 memset(&hfi, 0, sizeof(hfi));
1146 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1147 type = GetFileType(hand);
1148 if (type == FILE_TYPE_PIPE)
1150 buf->st_dev = buf->st_rdev = fd;
1151 buf->st_mode = S_IFIFO;
1152 buf->st_nlink = 1;
1154 else if (type == FILE_TYPE_CHAR)
1156 buf->st_dev = buf->st_rdev = fd;
1157 buf->st_mode = S_IFCHR;
1158 buf->st_nlink = 1;
1160 else /* FILE_TYPE_DISK etc. */
1162 if (!GetFileInformationByHandle(hand, &hfi))
1164 WARN(":failed-last error (%d)\n",GetLastError());
1165 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1166 return -1;
1168 buf->st_mode = S_IFREG | S_IREAD;
1169 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1170 buf->st_mode |= S_IWRITE;
1171 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1172 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1173 buf->st_atime = dw;
1174 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1175 buf->st_mtime = buf->st_ctime = dw;
1176 buf->st_nlink = hfi.nNumberOfLinks;
1178 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1179 buf->st_mode);
1180 return 0;
1183 /*********************************************************************
1184 * _fstati64 (MSVCRT.@)
1186 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1188 int ret;
1189 struct MSVCRT__stat64 buf64;
1191 ret = MSVCRT__fstat64(fd, &buf64);
1192 if (!ret)
1193 msvcrt_stat64_to_stati64(&buf64, buf);
1194 return ret;
1197 /*********************************************************************
1198 * _fstat (MSVCRT.@)
1200 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1201 { int ret;
1202 struct MSVCRT__stat64 buf64;
1204 ret = MSVCRT__fstat64(fd, &buf64);
1205 if (!ret)
1206 msvcrt_stat64_to_stat(&buf64, buf);
1207 return ret;
1210 /*********************************************************************
1211 * _futime (MSVCRT.@)
1213 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1215 HANDLE hand = msvcrt_fdtoh(fd);
1216 FILETIME at, wt;
1218 if (!t)
1220 MSVCRT_time_t currTime;
1221 MSVCRT_time(&currTime);
1222 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1223 memcpy(&wt, &at, sizeof(wt));
1225 else
1227 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1228 if (t->actime == t->modtime)
1229 memcpy(&wt, &at, sizeof(wt));
1230 else
1231 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1234 if (!SetFileTime(hand, NULL, &at, &wt))
1236 msvcrt_set_errno(GetLastError());
1237 return -1 ;
1239 return 0;
1242 /*********************************************************************
1243 * _get_osfhandle (MSVCRT.@)
1245 long CDECL _get_osfhandle(int fd)
1247 HANDLE hand = msvcrt_fdtoh(fd);
1248 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1250 return (long)hand;
1253 /*********************************************************************
1254 * _isatty (MSVCRT.@)
1256 int CDECL _isatty(int fd)
1258 HANDLE hand = msvcrt_fdtoh(fd);
1260 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1261 if (hand == INVALID_HANDLE_VALUE)
1262 return 0;
1264 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1267 /*********************************************************************
1268 * _mktemp (MSVCRT.@)
1270 char * CDECL _mktemp(char *pattern)
1272 int numX = 0;
1273 char *retVal = pattern;
1274 int id;
1275 char letter = 'a';
1277 while(*pattern)
1278 numX = (*pattern++ == 'X')? numX + 1 : 0;
1279 if (numX < 5)
1280 return NULL;
1281 pattern--;
1282 id = GetCurrentProcessId();
1283 numX = 6;
1284 while(numX--)
1286 int tempNum = id / 10;
1287 *pattern-- = id - (tempNum * 10) + '0';
1288 id = tempNum;
1290 pattern++;
1293 *pattern = letter++;
1294 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1295 GetLastError() == ERROR_FILE_NOT_FOUND)
1296 return retVal;
1297 } while(letter <= 'z');
1298 return NULL;
1301 /*********************************************************************
1302 * _wmktemp (MSVCRT.@)
1304 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1306 int numX = 0;
1307 MSVCRT_wchar_t *retVal = pattern;
1308 int id;
1309 MSVCRT_wchar_t letter = 'a';
1311 while(*pattern)
1312 numX = (*pattern++ == 'X')? numX + 1 : 0;
1313 if (numX < 5)
1314 return NULL;
1315 pattern--;
1316 id = GetCurrentProcessId();
1317 numX = 6;
1318 while(numX--)
1320 int tempNum = id / 10;
1321 *pattern-- = id - (tempNum * 10) + '0';
1322 id = tempNum;
1324 pattern++;
1327 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1328 GetLastError() == ERROR_FILE_NOT_FOUND)
1329 return retVal;
1330 *pattern = letter++;
1331 } while(letter != '|');
1332 return NULL;
1335 static unsigned split_oflags(unsigned oflags)
1337 int wxflags = 0;
1338 unsigned unsupp; /* until we support everything */
1340 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1341 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1342 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1343 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1344 else wxflags |= WX_TEXT; /* default to TEXT*/
1345 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1347 if ((unsupp = oflags & ~(
1348 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1349 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1350 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1351 MSVCRT__O_NOINHERIT|
1352 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1354 ERR(":unsupported oflags 0x%04x\n",unsupp);
1356 return wxflags;
1359 /*********************************************************************
1360 * _pipe (MSVCRT.@)
1362 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1364 int ret = -1;
1365 SECURITY_ATTRIBUTES sa;
1366 HANDLE readHandle, writeHandle;
1368 if (!pfds)
1370 *MSVCRT__errno() = MSVCRT_EINVAL;
1371 return -1;
1374 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1375 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1376 sa.lpSecurityDescriptor = NULL;
1377 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1379 unsigned int wxflags = split_oflags(textmode);
1380 int fd;
1382 LOCK_FILES();
1383 fd = msvcrt_alloc_fd(readHandle, wxflags);
1384 if (fd != -1)
1386 pfds[0] = fd;
1387 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1388 if (fd != -1)
1390 pfds[1] = fd;
1391 ret = 0;
1393 else
1395 MSVCRT__close(pfds[0]);
1396 CloseHandle(writeHandle);
1397 *MSVCRT__errno() = MSVCRT_EMFILE;
1400 else
1402 CloseHandle(readHandle);
1403 CloseHandle(writeHandle);
1404 *MSVCRT__errno() = MSVCRT_EMFILE;
1406 UNLOCK_FILES();
1408 else
1409 msvcrt_set_errno(GetLastError());
1411 return ret;
1414 /*********************************************************************
1415 * _sopen (MSVCRT.@)
1417 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1419 va_list ap;
1420 int pmode;
1421 DWORD access = 0, creation = 0, attrib;
1422 DWORD sharing;
1423 int wxflag = 0, fd;
1424 HANDLE hand;
1425 SECURITY_ATTRIBUTES sa;
1428 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1429 path, oflags, shflags);
1431 wxflag = split_oflags(oflags);
1432 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1434 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1435 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1436 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1439 if (oflags & MSVCRT__O_CREAT)
1441 va_start(ap, shflags);
1442 pmode = va_arg(ap, int);
1443 va_end(ap);
1445 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1446 FIXME(": pmode 0x%04x ignored\n", pmode);
1447 else
1448 WARN(": pmode 0x%04x ignored\n", pmode);
1450 if (oflags & MSVCRT__O_EXCL)
1451 creation = CREATE_NEW;
1452 else if (oflags & MSVCRT__O_TRUNC)
1453 creation = CREATE_ALWAYS;
1454 else
1455 creation = OPEN_ALWAYS;
1457 else /* no MSVCRT__O_CREAT */
1459 if (oflags & MSVCRT__O_TRUNC)
1460 creation = TRUNCATE_EXISTING;
1461 else
1462 creation = OPEN_EXISTING;
1465 switch( shflags )
1467 case MSVCRT__SH_DENYRW:
1468 sharing = 0L;
1469 break;
1470 case MSVCRT__SH_DENYWR:
1471 sharing = FILE_SHARE_READ;
1472 break;
1473 case MSVCRT__SH_DENYRD:
1474 sharing = FILE_SHARE_WRITE;
1475 break;
1476 case MSVCRT__SH_DENYNO:
1477 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1478 break;
1479 default:
1480 ERR( "Unhandled shflags 0x%x\n", shflags );
1481 return -1;
1483 attrib = FILE_ATTRIBUTE_NORMAL;
1485 if (oflags & MSVCRT__O_TEMPORARY)
1487 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1488 access |= DELETE;
1489 sharing |= FILE_SHARE_DELETE;
1492 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1493 sa.lpSecurityDescriptor = NULL;
1494 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1496 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1498 if (hand == INVALID_HANDLE_VALUE) {
1499 WARN(":failed-last error (%d)\n",GetLastError());
1500 msvcrt_set_errno(GetLastError());
1501 return -1;
1504 fd = msvcrt_alloc_fd(hand, wxflag);
1506 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1507 return fd;
1510 /*********************************************************************
1511 * _wsopen (MSVCRT.@)
1513 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1515 const unsigned int len = strlenW(path);
1516 char *patha = MSVCRT_calloc(len + 1,1);
1517 va_list ap;
1518 int pmode;
1520 va_start(ap, shflags);
1521 pmode = va_arg(ap, int);
1522 va_end(ap);
1524 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1526 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1527 MSVCRT_free(patha);
1528 return retval;
1531 msvcrt_set_errno(GetLastError());
1532 return -1;
1535 /*********************************************************************
1536 * _open (MSVCRT.@)
1538 int CDECL MSVCRT__open( const char *path, int flags, ... )
1540 va_list ap;
1542 if (flags & MSVCRT__O_CREAT)
1544 int pmode;
1545 va_start(ap, flags);
1546 pmode = va_arg(ap, int);
1547 va_end(ap);
1548 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1550 else
1551 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1554 /*********************************************************************
1555 * _wopen (MSVCRT.@)
1557 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1559 const unsigned int len = strlenW(path);
1560 char *patha = MSVCRT_calloc(len + 1,1);
1561 va_list ap;
1562 int pmode;
1564 va_start(ap, flags);
1565 pmode = va_arg(ap, int);
1566 va_end(ap);
1568 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1570 int retval = MSVCRT__open(patha,flags,pmode);
1571 MSVCRT_free(patha);
1572 return retval;
1575 msvcrt_set_errno(GetLastError());
1576 return -1;
1579 /*********************************************************************
1580 * _creat (MSVCRT.@)
1582 int CDECL MSVCRT__creat(const char *path, int flags)
1584 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1585 return MSVCRT__open(path, usedFlags);
1588 /*********************************************************************
1589 * _wcreat (MSVCRT.@)
1591 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1593 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1594 return _wopen(path, usedFlags);
1597 /*********************************************************************
1598 * _open_osfhandle (MSVCRT.@)
1600 int CDECL _open_osfhandle(long handle, int oflags)
1602 int fd;
1604 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1605 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1606 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1607 * text - it never sets MSVCRT__O_BINARY.
1609 /* don't let split_oflags() decide the mode if no mode is passed */
1610 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1611 oflags |= MSVCRT__O_BINARY;
1613 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1614 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1615 return fd;
1618 /*********************************************************************
1619 * _rmtmp (MSVCRT.@)
1621 int CDECL _rmtmp(void)
1623 int num_removed = 0, i;
1625 LOCK_FILES();
1626 for (i = 3; i < MSVCRT_stream_idx; i++)
1627 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1629 MSVCRT_fclose(MSVCRT_fstreams[i]);
1630 num_removed++;
1632 UNLOCK_FILES();
1634 if (num_removed)
1635 TRACE(":removed (%d) temp files\n",num_removed);
1636 return num_removed;
1639 /*********************************************************************
1640 * (internal) remove_cr
1642 * Translate all \r\n to \n inplace.
1643 * return the number of \r removed
1644 * Corner cases required by some apps:
1645 * \r\r\n -> \r\n
1646 * BUG: should save state across calls somehow, so CR LF that
1647 * straddles buffer boundary gets recognized properly?
1649 static unsigned int remove_cr(char *buf, unsigned int count)
1651 unsigned int i, j;
1653 for (i=0, j=0; j < count; j++)
1654 if ((buf[j] != '\r') || ((j+1) < count && buf[j+1] != '\n'))
1655 buf[i++] = buf[j];
1657 return count - i;
1660 /*********************************************************************
1661 * (internal) read_i
1663 static int read_i(int fd, void *buf, unsigned int count)
1665 DWORD num_read;
1666 char *bufstart = buf;
1667 HANDLE hand = msvcrt_fdtoh(fd);
1669 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1670 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1671 TRACE("already at EOF, returning 0\n");
1672 return 0;
1674 /* Don't trace small reads, it gets *very* annoying */
1675 if (count > 4)
1676 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1677 if (hand == INVALID_HANDLE_VALUE)
1678 return -1;
1680 /* Reading single bytes in O_TEXT mode makes things slow
1681 * So read big chunks
1683 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1685 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1687 int i;
1688 /* in text mode, a ctrl-z signals EOF */
1689 for (i=0; i<num_read; i++)
1691 if (bufstart[i] == 0x1a)
1693 num_read = i;
1694 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1695 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1696 break;
1700 if (count != 0 && num_read == 0)
1702 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1703 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1706 else
1708 if (GetLastError() == ERROR_BROKEN_PIPE)
1710 TRACE(":end-of-pipe\n");
1711 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1712 return 0;
1714 else
1716 TRACE(":failed-last error (%d)\n",GetLastError());
1717 return -1;
1721 if (count > 4)
1722 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1723 return num_read;
1726 /*********************************************************************
1727 * _read (MSVCRT.@)
1729 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1731 int num_read;
1732 num_read = read_i(fd, buf, count);
1733 if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1735 num_read -= remove_cr(buf,num_read);
1737 return num_read;
1740 /*********************************************************************
1741 * _setmode (MSVCRT.@)
1743 int CDECL _setmode(int fd,int mode)
1745 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1746 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1747 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1748 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1749 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1750 else
1751 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1752 return ret;
1755 /*********************************************************************
1756 * _stat64 (MSVCRT.@)
1758 int CDECL MSVCRT__stat64(const char* path, struct MSVCRT__stat64 * buf)
1760 DWORD dw;
1761 WIN32_FILE_ATTRIBUTE_DATA hfi;
1762 unsigned short mode = ALL_S_IREAD;
1763 int plen;
1765 TRACE(":file (%s) buf(%p)\n",path,buf);
1767 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1769 TRACE("failed (%d)\n",GetLastError());
1770 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1771 return -1;
1774 memset(buf,0,sizeof(struct MSVCRT__stat64));
1776 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1777 Bon 011120: This FIXME seems incorrect
1778 Also a letter as first char isn't enough to be classified
1779 as a drive letter
1781 if (isalpha(*path)&& (*(path+1)==':'))
1782 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1783 else
1784 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1786 plen = strlen(path);
1788 /* Dir, or regular file? */
1789 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1790 (path[plen-1] == '\\'))
1791 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1792 else
1794 mode |= MSVCRT__S_IFREG;
1795 /* executable? */
1796 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1798 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1799 (tolower(path[plen-3]) << 16);
1800 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1801 mode |= ALL_S_IEXEC;
1805 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1806 mode |= ALL_S_IWRITE;
1808 buf->st_mode = mode;
1809 buf->st_nlink = 1;
1810 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1811 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1812 buf->st_atime = dw;
1813 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1814 buf->st_mtime = buf->st_ctime = dw;
1815 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1816 (long)(buf->st_size >> 32),(long)buf->st_size,
1817 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1818 return 0;
1821 /*********************************************************************
1822 * _stati64 (MSVCRT.@)
1824 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1826 int ret;
1827 struct MSVCRT__stat64 buf64;
1829 ret = MSVCRT__stat64(path, &buf64);
1830 if (!ret)
1831 msvcrt_stat64_to_stati64(&buf64, buf);
1832 return ret;
1835 /*********************************************************************
1836 * _stat (MSVCRT.@)
1838 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1839 { int ret;
1840 struct MSVCRT__stat64 buf64;
1842 ret = MSVCRT__stat64( path, &buf64);
1843 if (!ret)
1844 msvcrt_stat64_to_stat(&buf64, buf);
1845 return ret;
1848 /*********************************************************************
1849 * _wstat64 (MSVCRT.@)
1851 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1853 DWORD dw;
1854 WIN32_FILE_ATTRIBUTE_DATA hfi;
1855 unsigned short mode = ALL_S_IREAD;
1856 int plen;
1858 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1860 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1862 TRACE("failed (%d)\n",GetLastError());
1863 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1864 return -1;
1867 memset(buf,0,sizeof(struct MSVCRT__stat64));
1869 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1870 if (MSVCRT_iswalpha(*path))
1871 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1872 else
1873 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1875 plen = strlenW(path);
1877 /* Dir, or regular file? */
1878 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1879 (path[plen-1] == '\\'))
1880 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1881 else
1883 mode |= MSVCRT__S_IFREG;
1884 /* executable? */
1885 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1887 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1888 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1889 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1890 mode |= ALL_S_IEXEC;
1894 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1895 mode |= ALL_S_IWRITE;
1897 buf->st_mode = mode;
1898 buf->st_nlink = 1;
1899 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1900 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1901 buf->st_atime = dw;
1902 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1903 buf->st_mtime = buf->st_ctime = dw;
1904 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1905 (long)(buf->st_size >> 32),(long)buf->st_size,
1906 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1907 return 0;
1910 /*********************************************************************
1911 * _wstati64 (MSVCRT.@)
1913 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1915 int ret;
1916 struct MSVCRT__stat64 buf64;
1918 ret = MSVCRT__wstat64(path, &buf64);
1919 if (!ret)
1920 msvcrt_stat64_to_stati64(&buf64, buf);
1921 return ret;
1924 /*********************************************************************
1925 * _wstat (MSVCRT.@)
1927 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1929 int ret;
1930 struct MSVCRT__stat64 buf64;
1932 ret = MSVCRT__wstat64( path, &buf64 );
1933 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
1934 return ret;
1937 /*********************************************************************
1938 * _tell (MSVCRT.@)
1940 long CDECL _tell(int fd)
1942 return _lseek(fd, 0, SEEK_CUR);
1945 /*********************************************************************
1946 * _telli64 (MSVCRT.@)
1948 __int64 CDECL _telli64(int fd)
1950 return _lseeki64(fd, 0, SEEK_CUR);
1953 /*********************************************************************
1954 * _tempnam (MSVCRT.@)
1956 char * CDECL _tempnam(const char *dir, const char *prefix)
1958 char tmpbuf[MAX_PATH];
1959 const char *tmp_dir = MSVCRT_getenv("TMP");
1961 if (tmp_dir) dir = tmp_dir;
1963 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1964 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1966 TRACE("got name (%s)\n",tmpbuf);
1967 DeleteFileA(tmpbuf);
1968 return _strdup(tmpbuf);
1970 TRACE("failed (%d)\n",GetLastError());
1971 return NULL;
1974 /*********************************************************************
1975 * _wtempnam (MSVCRT.@)
1977 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1979 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1981 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1982 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1984 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1985 DeleteFileW(tmpbuf);
1986 return _wcsdup(tmpbuf);
1988 TRACE("failed (%d)\n",GetLastError());
1989 return NULL;
1992 /*********************************************************************
1993 * _umask (MSVCRT.@)
1995 int CDECL MSVCRT__umask(int umask)
1997 int old_umask = MSVCRT_umask;
1998 TRACE("(%d)\n",umask);
1999 MSVCRT_umask = umask;
2000 return old_umask;
2003 /*********************************************************************
2004 * _utime (MSVCRT.@)
2006 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
2008 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2010 if (fd > 0)
2012 int retVal = _futime(fd, t);
2013 MSVCRT__close(fd);
2014 return retVal;
2016 return -1;
2019 /*********************************************************************
2020 * _wutime (MSVCRT.@)
2022 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
2024 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2026 if (fd > 0)
2028 int retVal = _futime(fd, t);
2029 MSVCRT__close(fd);
2030 return retVal;
2032 return -1;
2035 /*********************************************************************
2036 * _write (MSVCRT.@)
2038 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2040 DWORD num_written;
2041 HANDLE hand = msvcrt_fdtoh(fd);
2043 /* Don't trace small writes, it gets *very* annoying */
2044 #if 0
2045 if (count > 32)
2046 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2047 #endif
2048 if (hand == INVALID_HANDLE_VALUE)
2050 *MSVCRT__errno() = MSVCRT_EBADF;
2051 return -1;
2054 /* If appending, go to EOF */
2055 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2056 _lseek(fd, 0, FILE_END);
2058 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2060 if (WriteFile(hand, buf, count, &num_written, NULL)
2061 && (num_written == count))
2062 return num_written;
2063 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2064 hand, GetLastError());
2065 *MSVCRT__errno() = MSVCRT_ENOSPC;
2067 else
2069 unsigned int i, j, nr_lf;
2070 char *p = NULL;
2071 const char *q;
2072 const char *s = (const char *)buf, *buf_start = (const char *)buf;
2073 /* find number of \n ( without preceding \r ) */
2074 for ( nr_lf=0,i = 0; i <count; i++)
2076 if (s[i]== '\n')
2078 nr_lf++;
2079 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2082 if (nr_lf)
2084 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2086 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
2088 if (s[i]== '\n')
2090 p[j++] = '\r';
2091 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2093 p[j++] = s[i];
2096 else
2098 FIXME("Malloc failed\n");
2099 nr_lf =0;
2100 q = buf;
2103 else
2104 q = buf;
2106 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2108 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2109 fd, hand, GetLastError(), num_written);
2110 *MSVCRT__errno() = MSVCRT_ENOSPC;
2111 if(nr_lf)
2112 MSVCRT_free(p);
2113 return s - buf_start;
2115 else
2117 if(nr_lf)
2118 MSVCRT_free(p);
2119 return count;
2122 return -1;
2125 /*********************************************************************
2126 * _putw (MSVCRT.@)
2128 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2130 int len;
2131 len = MSVCRT__write(file->_file, &val, sizeof(val));
2132 if (len == sizeof(val)) return val;
2133 file->_flag |= MSVCRT__IOERR;
2134 return MSVCRT_EOF;
2137 /*********************************************************************
2138 * fclose (MSVCRT.@)
2140 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2142 int r, flag;
2144 flag = file->_flag;
2145 MSVCRT_free(file->_tmpfname);
2146 file->_tmpfname = NULL;
2147 /* flush stdio buffers */
2148 if(file->_flag & MSVCRT__IOWRT)
2149 MSVCRT_fflush(file);
2150 if(file->_flag & MSVCRT__IOMYBUF)
2151 MSVCRT_free(file->_base);
2153 r=MSVCRT__close(file->_file);
2155 file->_flag = 0;
2157 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2160 /*********************************************************************
2161 * feof (MSVCRT.@)
2163 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2165 return file->_flag & MSVCRT__IOEOF;
2168 /*********************************************************************
2169 * ferror (MSVCRT.@)
2171 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2173 return file->_flag & MSVCRT__IOERR;
2176 /*********************************************************************
2177 * _filbuf (MSVCRT.@)
2179 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2181 /* Allocate buffer if needed */
2182 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2183 msvcrt_alloc_buffer(file);
2185 if(!(file->_flag & MSVCRT__IOREAD)) {
2186 if(file->_flag & MSVCRT__IORW) {
2187 file->_flag |= MSVCRT__IOREAD;
2188 } else {
2189 return MSVCRT_EOF;
2192 if(file->_flag & MSVCRT__IONBF) {
2193 unsigned char c;
2194 int r;
2195 if ((r = read_i(file->_file,&c,1)) != 1) {
2196 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2197 return MSVCRT_EOF;
2199 return c;
2200 } else {
2201 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2202 if(file->_cnt<=0) {
2203 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2204 file->_cnt = 0;
2205 return MSVCRT_EOF;
2207 file->_cnt--;
2208 file->_ptr = file->_base+1;
2209 return *(unsigned char *)file->_base;
2213 /*********************************************************************
2214 * fgetc (MSVCRT.@)
2216 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2218 unsigned char *i;
2219 unsigned int j;
2220 do {
2221 if (file->_cnt>0) {
2222 file->_cnt--;
2223 i = (unsigned char *)file->_ptr++;
2224 j = *i;
2225 } else
2226 j = MSVCRT__filbuf(file);
2227 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2228 || ((j != '\r') || (file->_cnt && file->_ptr[0] != '\n')))
2229 return j;
2230 } while(1);
2233 /*********************************************************************
2234 * _fgetchar (MSVCRT.@)
2236 int CDECL _fgetchar(void)
2238 return MSVCRT_fgetc(MSVCRT_stdin);
2241 /*********************************************************************
2242 * fgets (MSVCRT.@)
2244 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2246 int cc = MSVCRT_EOF;
2247 char * buf_start = s;
2249 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2250 file,file->_file,s,size);
2252 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2254 *s++ = (char)cc;
2255 size --;
2257 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2259 TRACE(":nothing read\n");
2260 return NULL;
2262 if ((cc != MSVCRT_EOF) && (size > 1))
2263 *s++ = cc;
2264 *s = '\0';
2265 TRACE(":got %s\n", debugstr_a(buf_start));
2266 return buf_start;
2269 /*********************************************************************
2270 * fgetwc (MSVCRT.@)
2272 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2273 * the CR from CR/LF combinations
2275 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2277 char c;
2279 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2281 MSVCRT_wchar_t wc;
2282 int i,j;
2283 char *chp, *wcp;
2284 wcp = (char *)&wc;
2285 for(i=0; i<sizeof(wc); i++)
2287 if (file->_cnt>0)
2289 file->_cnt--;
2290 chp = file->_ptr++;
2291 wcp[i] = *chp;
2293 else
2295 j = MSVCRT__filbuf(file);
2296 if(file->_cnt<=0)
2298 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2299 file->_cnt = 0;
2300 return MSVCRT_WEOF;
2302 wcp[i] = j;
2305 return wc;
2308 c = MSVCRT_fgetc(file);
2309 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2311 FIXME("Treat Multibyte characters\n");
2313 if (c == MSVCRT_EOF)
2314 return MSVCRT_WEOF;
2315 else
2316 return (MSVCRT_wint_t)c;
2319 /*********************************************************************
2320 * _getw (MSVCRT.@)
2322 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2324 char *ch;
2325 int i, j, k;
2326 ch = (char *)&i;
2327 for (j=0; j<sizeof(int); j++) {
2328 k = MSVCRT_fgetc(file);
2329 if (k == MSVCRT_EOF) {
2330 file->_flag |= MSVCRT__IOEOF;
2331 return EOF;
2333 ch[j] = k;
2335 return i;
2338 /*********************************************************************
2339 * getwc (MSVCRT.@)
2341 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2343 return MSVCRT_fgetwc(file);
2346 /*********************************************************************
2347 * _fgetwchar (MSVCRT.@)
2349 MSVCRT_wint_t CDECL _fgetwchar(void)
2351 return MSVCRT_fgetwc(MSVCRT_stdin);
2354 /*********************************************************************
2355 * getwchar (MSVCRT.@)
2357 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2359 return _fgetwchar();
2362 /*********************************************************************
2363 * fgetws (MSVCRT.@)
2365 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2367 int cc = MSVCRT_WEOF;
2368 MSVCRT_wchar_t * buf_start = s;
2370 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2371 file,file->_file,s,size);
2373 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2375 *s++ = (char)cc;
2376 size --;
2378 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2380 TRACE(":nothing read\n");
2381 return NULL;
2383 if ((cc != MSVCRT_WEOF) && (size > 1))
2384 *s++ = cc;
2385 *s = 0;
2386 TRACE(":got %s\n", debugstr_w(buf_start));
2387 return buf_start;
2390 /*********************************************************************
2391 * fwrite (MSVCRT.@)
2393 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2395 MSVCRT_size_t wrcnt=size * nmemb;
2396 int written = 0;
2397 if (size == 0)
2398 return 0;
2399 if(file->_cnt) {
2400 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2401 memcpy(file->_ptr, ptr, pcnt);
2402 file->_cnt -= pcnt;
2403 file->_ptr += pcnt;
2404 written = pcnt;
2405 wrcnt -= pcnt;
2406 ptr = (const char*)ptr + pcnt;
2407 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2408 if(file->_flag & MSVCRT__IORW) {
2409 file->_flag |= MSVCRT__IOWRT;
2410 } else
2411 return 0;
2413 if(wrcnt) {
2414 /* Flush buffer */
2415 int res=msvcrt_flush_buffer(file);
2416 if(!res) {
2417 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2418 if (pwritten <= 0)
2420 file->_flag |= MSVCRT__IOERR;
2421 pwritten=0;
2423 written += pwritten;
2426 return written / size;
2429 /*********************************************************************
2430 * fputwc (MSVCRT.@)
2432 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2434 MSVCRT_wchar_t mwc=wc;
2435 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2436 return MSVCRT_WEOF;
2437 return wc;
2440 /*********************************************************************
2441 * _fputwchar (MSVCRT.@)
2443 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2445 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2448 /*********************************************************************
2449 * _fsopen (MSVCRT.@)
2451 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2453 MSVCRT_FILE* file;
2454 int open_flags, stream_flags, fd;
2456 TRACE("(%s,%s)\n",path,mode);
2458 /* map mode string to open() flags. "man fopen" for possibilities. */
2459 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2460 return NULL;
2462 LOCK_FILES();
2463 fd = MSVCRT__sopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2464 if (fd < 0)
2465 file = NULL;
2466 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2467 != -1)
2468 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2469 else if (file)
2471 file->_flag = 0;
2472 file = NULL;
2475 TRACE(":got (%p)\n",file);
2476 if (fd >= 0 && !file)
2477 MSVCRT__close(fd);
2478 UNLOCK_FILES();
2479 return file;
2482 /*********************************************************************
2483 * _wfsopen (MSVCRT.@)
2485 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2487 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2488 char *patha = MSVCRT_calloc(plen + 1, 1);
2489 char *modea = MSVCRT_calloc(mlen + 1, 1);
2491 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2493 if (patha && modea &&
2494 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2495 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2497 MSVCRT_FILE *retval = MSVCRT__fsopen(patha,modea,share);
2498 MSVCRT_free(patha);
2499 MSVCRT_free(modea);
2500 return retval;
2503 msvcrt_set_errno(GetLastError());
2504 return NULL;
2507 /*********************************************************************
2508 * fopen (MSVCRT.@)
2510 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2512 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2515 /*********************************************************************
2516 * _wfopen (MSVCRT.@)
2518 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2520 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2523 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2524 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2526 /*********************************************************************
2527 * fputc (MSVCRT.@)
2529 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2531 if(file->_cnt>0) {
2532 *file->_ptr++=c;
2533 file->_cnt--;
2534 if (c == '\n')
2536 int res = msvcrt_flush_buffer(file);
2537 return res ? res : c;
2539 else
2540 return c;
2541 } else {
2542 return MSVCRT__flsbuf(c, file);
2546 /*********************************************************************
2547 * _flsbuf (MSVCRT.@)
2549 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2551 /* Flush output buffer */
2552 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2553 msvcrt_alloc_buffer(file);
2555 if(!(file->_flag & MSVCRT__IOWRT)) {
2556 if(file->_flag & MSVCRT__IORW) {
2557 file->_flag |= MSVCRT__IOWRT;
2558 } else {
2559 return MSVCRT_EOF;
2562 if(file->_bufsiz) {
2563 int res=msvcrt_flush_buffer(file);
2564 return res?res : MSVCRT_fputc(c, file);
2565 } else {
2566 unsigned char cc=c;
2567 int len;
2568 len = MSVCRT__write(file->_file, &cc, 1);
2569 if (len == 1) return c;
2570 file->_flag |= MSVCRT__IOERR;
2571 return MSVCRT_EOF;
2575 /*********************************************************************
2576 * _fputchar (MSVCRT.@)
2578 int CDECL _fputchar(int c)
2580 return MSVCRT_fputc(c, MSVCRT_stdout);
2583 /*********************************************************************
2584 * fread (MSVCRT.@)
2586 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2587 { MSVCRT_size_t rcnt=size * nmemb;
2588 MSVCRT_size_t read=0;
2589 int pread=0;
2591 if(!rcnt)
2592 return 0;
2594 /* first buffered data */
2595 if(file->_cnt>0) {
2596 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2597 memcpy(ptr, file->_ptr, pcnt);
2598 file->_cnt -= pcnt;
2599 file->_ptr += pcnt;
2600 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2601 pcnt -= remove_cr(ptr,pcnt);
2602 read += pcnt ;
2603 rcnt -= pcnt ;
2604 ptr = (char*)ptr + pcnt;
2605 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2606 if(file->_flag & MSVCRT__IORW) {
2607 file->_flag |= MSVCRT__IOREAD;
2608 } else
2609 return 0;
2611 while(rcnt>0)
2613 int i;
2614 /* Fill the buffer on small reads.
2615 * TODO: Use a better buffering strategy.
2617 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2618 if (file->_bufsiz == 0) {
2619 msvcrt_alloc_buffer(file);
2621 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2622 file->_ptr = file->_base;
2623 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2624 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2625 if (i > 0 && i < file->_cnt) {
2626 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2627 file->_flag &= ~MSVCRT__IOEOF;
2629 if (i > 0) {
2630 memcpy(ptr, file->_ptr, i);
2631 file->_cnt -= i;
2632 file->_ptr += i;
2634 } else {
2635 i = MSVCRT__read(file->_file,ptr, rcnt);
2637 pread += i;
2638 rcnt -= i;
2639 ptr = (char *)ptr+i;
2640 /* expose feof condition in the flags
2641 * MFC tests file->_flag for feof, and doesn't call feof())
2643 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2644 file->_flag |= MSVCRT__IOEOF;
2645 else if (i == -1)
2647 file->_flag |= MSVCRT__IOERR;
2648 pread = 0;
2649 rcnt = 0;
2651 if (i < 1) break;
2653 read+=pread;
2654 return read / size;
2657 /*********************************************************************
2658 * freopen (MSVCRT.@)
2661 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2663 int open_flags, stream_flags, fd;
2665 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2667 LOCK_FILES();
2668 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2669 file = NULL;
2670 else
2672 MSVCRT_fclose(file);
2673 /* map mode string to open() flags. "man fopen" for possibilities. */
2674 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2675 file = NULL;
2676 else
2678 fd = MSVCRT__open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2679 if (fd < 0)
2680 file = NULL;
2681 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2683 file->_flag = 0;
2684 WARN(":failed-last error (%d)\n",GetLastError());
2685 msvcrt_set_errno(GetLastError());
2686 file = NULL;
2690 UNLOCK_FILES();
2691 return file;
2694 /*********************************************************************
2695 * fsetpos (MSVCRT.@)
2697 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2699 /* Note that all this has been lifted 'as is' from fseek */
2700 if(file->_flag & MSVCRT__IOWRT)
2701 msvcrt_flush_buffer(file);
2703 /* Discard buffered input */
2704 file->_cnt = 0;
2705 file->_ptr = file->_base;
2707 /* Reset direction of i/o */
2708 if(file->_flag & MSVCRT__IORW) {
2709 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2712 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2715 /*********************************************************************
2716 * ftell (MSVCRT.@)
2718 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2720 int off=0;
2721 long pos;
2722 if(file->_bufsiz) {
2723 if( file->_flag & MSVCRT__IOWRT ) {
2724 off = file->_ptr - file->_base;
2725 } else {
2726 off = -file->_cnt;
2729 pos = _tell(file->_file);
2730 if(pos == -1) return pos;
2731 return off + pos;
2734 /*********************************************************************
2735 * fgetpos (MSVCRT.@)
2737 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2739 /* This code has been lifted form the MSVCRT_ftell function */
2740 int off=0;
2742 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2744 if (*pos == -1) return -1;
2746 if(file->_bufsiz) {
2747 if( file->_flag & MSVCRT__IOWRT ) {
2748 off = file->_ptr - file->_base;
2749 } else {
2750 off = -file->_cnt;
2753 *pos += off;
2755 return 0;
2758 /*********************************************************************
2759 * fputs (MSVCRT.@)
2761 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2763 size_t i, len = strlen(s);
2764 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2765 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2766 for (i=0; i<len; i++)
2767 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2768 return MSVCRT_EOF;
2769 return 0;
2772 /*********************************************************************
2773 * fputws (MSVCRT.@)
2775 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2777 size_t i, len = strlenW(s);
2778 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2779 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2780 for (i=0; i<len; i++)
2782 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2783 return MSVCRT_WEOF;
2784 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2785 return MSVCRT_WEOF;
2787 return 0;
2790 /*********************************************************************
2791 * getchar (MSVCRT.@)
2793 int CDECL MSVCRT_getchar(void)
2795 return MSVCRT_fgetc(MSVCRT_stdin);
2798 /*********************************************************************
2799 * getc (MSVCRT.@)
2801 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2803 return MSVCRT_fgetc(file);
2806 /*********************************************************************
2807 * gets (MSVCRT.@)
2809 char * CDECL MSVCRT_gets(char *buf)
2811 int cc;
2812 char * buf_start = buf;
2814 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2815 cc = MSVCRT_fgetc(MSVCRT_stdin))
2816 if(cc != '\r') *buf++ = (char)cc;
2818 *buf = '\0';
2820 TRACE("got '%s'\n", buf_start);
2821 return buf_start;
2824 /*********************************************************************
2825 * _getws (MSVCRT.@)
2827 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2829 MSVCRT_wint_t cc;
2830 MSVCRT_wchar_t* ws = buf;
2832 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2833 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2835 if (cc != '\r')
2836 *buf++ = (MSVCRT_wchar_t)cc;
2838 *buf = '\0';
2840 TRACE("got %s\n", debugstr_w(ws));
2841 return ws;
2844 /*********************************************************************
2845 * putc (MSVCRT.@)
2847 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2849 return MSVCRT_fputc(c, file);
2852 /*********************************************************************
2853 * putchar (MSVCRT.@)
2855 int CDECL MSVCRT_putchar(int c)
2857 return MSVCRT_fputc(c, MSVCRT_stdout);
2860 /*********************************************************************
2861 * puts (MSVCRT.@)
2863 int CDECL MSVCRT_puts(const char *s)
2865 size_t len = strlen(s);
2866 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2867 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2870 /*********************************************************************
2871 * _putws (MSVCRT.@)
2873 int CDECL _putws(const MSVCRT_wchar_t *s)
2875 static const MSVCRT_wchar_t nl = '\n';
2876 size_t len = strlenW(s);
2877 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2878 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2881 /*********************************************************************
2882 * remove (MSVCRT.@)
2884 int CDECL MSVCRT_remove(const char *path)
2886 TRACE("(%s)\n",path);
2887 if (DeleteFileA(path))
2888 return 0;
2889 TRACE(":failed (%d)\n",GetLastError());
2890 msvcrt_set_errno(GetLastError());
2891 return -1;
2894 /*********************************************************************
2895 * _wremove (MSVCRT.@)
2897 int CDECL _wremove(const MSVCRT_wchar_t *path)
2899 TRACE("(%s)\n",debugstr_w(path));
2900 if (DeleteFileW(path))
2901 return 0;
2902 TRACE(":failed (%d)\n",GetLastError());
2903 msvcrt_set_errno(GetLastError());
2904 return -1;
2907 /*********************************************************************
2908 * rename (MSVCRT.@)
2910 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2912 TRACE(":from %s to %s\n",oldpath,newpath);
2913 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2914 return 0;
2915 TRACE(":failed (%d)\n",GetLastError());
2916 msvcrt_set_errno(GetLastError());
2917 return -1;
2920 /*********************************************************************
2921 * _wrename (MSVCRT.@)
2923 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2925 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2926 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2927 return 0;
2928 TRACE(":failed (%d)\n",GetLastError());
2929 msvcrt_set_errno(GetLastError());
2930 return -1;
2933 /*********************************************************************
2934 * setvbuf (MSVCRT.@)
2936 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2938 /* TODO: Check if file busy */
2939 if(file->_bufsiz) {
2940 MSVCRT_free(file->_base);
2941 file->_bufsiz = 0;
2942 file->_cnt = 0;
2944 if(mode == MSVCRT__IOFBF) {
2945 file->_flag &= ~MSVCRT__IONBF;
2946 file->_base = file->_ptr = buf;
2947 if(buf) {
2948 file->_bufsiz = size;
2950 } else {
2951 file->_flag |= MSVCRT__IONBF;
2953 return 0;
2956 /*********************************************************************
2957 * setbuf (MSVCRT.@)
2959 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2961 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2964 /*********************************************************************
2965 * tmpnam (MSVCRT.@)
2967 char * CDECL MSVCRT_tmpnam(char *s)
2969 static int unique;
2970 char tmpstr[16];
2971 char *p;
2972 int count;
2973 if (s == 0)
2974 s = MSVCRT_tmpname;
2975 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2976 p = s + sprintf(s, "\\s%s.", tmpstr);
2977 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2979 msvcrt_int_to_base32(unique++, tmpstr);
2980 strcpy(p, tmpstr);
2981 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2982 GetLastError() == ERROR_FILE_NOT_FOUND)
2983 break;
2985 return s;
2988 /*********************************************************************
2989 * tmpfile (MSVCRT.@)
2991 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
2993 char *filename = MSVCRT_tmpnam(NULL);
2994 int fd;
2995 MSVCRT_FILE* file = NULL;
2997 LOCK_FILES();
2998 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2999 if (fd != -1 && (file = msvcrt_alloc_fp()))
3001 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3003 file->_flag = 0;
3004 file = NULL;
3006 else file->_tmpfname = _strdup(filename);
3008 UNLOCK_FILES();
3009 return file;
3012 /*********************************************************************
3013 * vfprintf (MSVCRT.@)
3015 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
3017 char buf[2048], *mem = buf;
3018 int written, resize = sizeof(buf), retval;
3019 /* There are two conventions for vsnprintf failing:
3020 * Return -1 if we truncated, or
3021 * Return the number of bytes that would have been written
3022 * The code below handles both cases
3024 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3025 written > resize)
3027 resize = (written == -1 ? resize * 2 : written + 1);
3028 if (mem != buf)
3029 MSVCRT_free (mem);
3030 if (!(mem = MSVCRT_malloc(resize)))
3031 return MSVCRT_EOF;
3033 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3034 if (mem != buf)
3035 MSVCRT_free (mem);
3036 return retval;
3039 /*********************************************************************
3040 * vfwprintf (MSVCRT.@)
3041 * FIXME:
3042 * Is final char included in written (then resize is too big) or not
3043 * (then we must test for equality too)?
3045 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
3047 MSVCRT_wchar_t buf[2048], *mem = buf;
3048 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3049 /* See vfprintf comments */
3050 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3051 written > resize)
3053 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3054 if (mem != buf)
3055 MSVCRT_free (mem);
3056 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3057 return MSVCRT_EOF;
3059 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3060 if (mem != buf)
3061 MSVCRT_free (mem);
3062 return retval;
3065 /*********************************************************************
3066 * vprintf (MSVCRT.@)
3068 int CDECL MSVCRT_vprintf(const char *format, va_list valist)
3070 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3073 /*********************************************************************
3074 * vwprintf (MSVCRT.@)
3076 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
3078 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3081 /*********************************************************************
3082 * fprintf (MSVCRT.@)
3084 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3086 va_list valist;
3087 int res;
3088 va_start(valist, format);
3089 res = MSVCRT_vfprintf(file, format, valist);
3090 va_end(valist);
3091 return res;
3094 /*********************************************************************
3095 * fwprintf (MSVCRT.@)
3097 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3099 va_list valist;
3100 int res;
3101 va_start(valist, format);
3102 res = MSVCRT_vfwprintf(file, format, valist);
3103 va_end(valist);
3104 return res;
3107 /*********************************************************************
3108 * printf (MSVCRT.@)
3110 int CDECL MSVCRT_printf(const char *format, ...)
3112 va_list valist;
3113 int res;
3114 va_start(valist, format);
3115 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3116 va_end(valist);
3117 return res;
3120 /*********************************************************************
3121 * ungetc (MSVCRT.@)
3123 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3125 if (c == MSVCRT_EOF)
3126 return MSVCRT_EOF;
3127 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3128 msvcrt_alloc_buffer(file);
3129 file->_ptr++;
3131 if(file->_ptr>file->_base) {
3132 file->_ptr--;
3133 *file->_ptr=c;
3134 file->_cnt++;
3135 MSVCRT_clearerr(file);
3136 return c;
3138 return MSVCRT_EOF;
3141 /*********************************************************************
3142 * ungetwc (MSVCRT.@)
3144 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3146 MSVCRT_wchar_t mwc = wc;
3147 char * pp = (char *)&mwc;
3148 int i;
3149 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3150 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3151 return MSVCRT_WEOF;
3153 return mwc;
3156 /*********************************************************************
3157 * wprintf (MSVCRT.@)
3159 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3161 va_list valist;
3162 int res;
3163 va_start(valist, format);
3164 res = MSVCRT_vwprintf(format, valist);
3165 va_end(valist);
3166 return res;
3169 /*********************************************************************
3170 * _getmaxstdio (MSVCRT.@)
3172 int CDECL _getmaxstdio(void)
3174 FIXME("stub, always returns 512\n");
3175 return 512;
3178 /*********************************************************************
3179 * _setmaxstdio_ (MSVCRT.@)
3181 int CDECL _setmaxstdio(int newmax)
3183 int res;
3184 if( newmax > 2048)
3185 res = -1;
3186 else
3187 res = newmax;
3188 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3189 return res;
3192 /*********************************************************************
3193 * __pioinfo (MSVCRT.@)
3194 * FIXME: see MSVCRT_MAX_FILES define.
3196 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3197 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3198 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3199 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3200 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3201 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3202 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3203 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3204 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3205 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3206 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3207 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3210 /*********************************************************************
3211 * __badioinfo (MSVCRT.@)
3213 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };