push 3cf4bc9cdb38c4fc36232fe19b9b904fd4d068e9
[wine/hacks.git] / dlls / msvcrt / file.c
blob888f90c51ac0599e530bb3e2602c557ca74a4429
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] = { { 0 } };
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 * __iob_func(MSVCRT.@)
456 MSVCRT_FILE * CDECL MSVCRT___iob_func(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 MSVCRT__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 MSVCRT__lseek(int fd, LONG offset, int whence)
845 return MSVCRT__lseeki64(fd, offset, whence);
848 /*********************************************************************
849 * _locking (MSVCRT.@)
851 * This is untested; the underlying LockFile doesn't work yet.
853 int CDECL MSVCRT__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 /* Clear end of file flag */
922 file->_flag &= ~MSVCRT__IOEOF;
923 return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0;
926 /*********************************************************************
927 * _chsize (MSVCRT.@)
929 int CDECL _chsize(int fd, long size)
931 LONG cur, pos;
932 HANDLE handle;
933 BOOL ret = FALSE;
935 TRACE("(fd=%d, size=%ld)\n", fd, size);
937 LOCK_FILES();
939 handle = msvcrt_fdtoh(fd);
940 if (handle != INVALID_HANDLE_VALUE)
942 /* save the current file pointer */
943 cur = MSVCRT__lseek(fd, 0, SEEK_CUR);
944 if (cur >= 0)
946 pos = MSVCRT__lseek(fd, size, SEEK_SET);
947 if (pos >= 0)
949 ret = SetEndOfFile(handle);
950 if (!ret) msvcrt_set_errno(GetLastError());
953 /* restore the file pointer */
954 MSVCRT__lseek(fd, cur, SEEK_SET);
958 UNLOCK_FILES();
959 return ret ? 0 : -1;
962 /*********************************************************************
963 * clearerr (MSVCRT.@)
965 void CDECL MSVCRT_clearerr(MSVCRT_FILE* file)
967 TRACE(":file (%p) fd (%d)\n",file,file->_file);
968 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
971 /*********************************************************************
972 * rewind (MSVCRT.@)
974 void CDECL MSVCRT_rewind(MSVCRT_FILE* file)
976 TRACE(":file (%p) fd (%d)\n",file,file->_file);
977 MSVCRT_fseek(file, 0L, SEEK_SET);
978 MSVCRT_clearerr(file);
981 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
983 int plus = strchr(mode, '+') != NULL;
985 switch(*mode++)
987 case 'R': case 'r':
988 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
989 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
990 break;
991 case 'W': case 'w':
992 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
993 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
994 break;
995 case 'A': case 'a':
996 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
997 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
998 break;
999 default:
1000 return -1;
1003 while (*mode)
1004 switch (*mode++)
1006 case 'B': case 'b':
1007 *open_flags |= MSVCRT__O_BINARY;
1008 *open_flags &= ~MSVCRT__O_TEXT;
1009 break;
1010 case 'T': case 't':
1011 *open_flags |= MSVCRT__O_TEXT;
1012 *open_flags &= ~MSVCRT__O_BINARY;
1013 break;
1014 case '+':
1015 break;
1016 default:
1017 FIXME(":unknown flag %c not supported\n",mode[-1]);
1019 return 0;
1022 /*********************************************************************
1023 * _fdopen (MSVCRT.@)
1025 MSVCRT_FILE* CDECL MSVCRT__fdopen(int fd, const char *mode)
1027 int open_flags, stream_flags;
1028 MSVCRT_FILE* file;
1030 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1032 LOCK_FILES();
1033 if (!(file = msvcrt_alloc_fp()))
1034 file = NULL;
1035 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1037 file->_flag = 0;
1038 file = NULL;
1040 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1041 UNLOCK_FILES();
1043 return file;
1046 /*********************************************************************
1047 * _wfdopen (MSVCRT.@)
1049 MSVCRT_FILE* CDECL MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
1051 unsigned mlen = strlenW(mode);
1052 char *modea = MSVCRT_calloc(mlen + 1, 1);
1053 MSVCRT_FILE* file = NULL;
1054 int open_flags, stream_flags;
1056 if (modea &&
1057 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1059 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
1060 LOCK_FILES();
1061 if (!(file = msvcrt_alloc_fp()))
1062 file = NULL;
1063 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1065 file->_flag = 0;
1066 file = NULL;
1068 else
1070 if (file)
1071 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
1072 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1074 UNLOCK_FILES();
1076 return file;
1079 /*********************************************************************
1080 * _filelength (MSVCRT.@)
1082 LONG CDECL MSVCRT__filelength(int fd)
1084 LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR);
1085 if (curPos != -1)
1087 LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END);
1088 if (endPos != -1)
1090 if (endPos != curPos)
1091 MSVCRT__lseek(fd, curPos, SEEK_SET);
1092 return endPos;
1095 return -1;
1098 /*********************************************************************
1099 * _filelengthi64 (MSVCRT.@)
1101 __int64 CDECL MSVCRT__filelengthi64(int fd)
1103 __int64 curPos = MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1104 if (curPos != -1)
1106 __int64 endPos = MSVCRT__lseeki64(fd, 0, SEEK_END);
1107 if (endPos != -1)
1109 if (endPos != curPos)
1110 MSVCRT__lseeki64(fd, curPos, SEEK_SET);
1111 return endPos;
1114 return -1;
1117 /*********************************************************************
1118 * _fileno (MSVCRT.@)
1120 int CDECL MSVCRT__fileno(MSVCRT_FILE* file)
1122 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1123 return file->_file;
1126 /*********************************************************************
1127 * _fstat64 (MSVCRT.@)
1129 int CDECL MSVCRT__fstat64(int fd, struct MSVCRT__stat64* buf)
1131 DWORD dw;
1132 DWORD type;
1133 BY_HANDLE_FILE_INFORMATION hfi;
1134 HANDLE hand = msvcrt_fdtoh(fd);
1136 TRACE(":fd (%d) stat (%p)\n",fd,buf);
1137 if (hand == INVALID_HANDLE_VALUE)
1138 return -1;
1140 if (!buf)
1142 WARN(":failed-NULL buf\n");
1143 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1144 return -1;
1147 memset(&hfi, 0, sizeof(hfi));
1148 memset(buf, 0, sizeof(struct MSVCRT__stat64));
1149 type = GetFileType(hand);
1150 if (type == FILE_TYPE_PIPE)
1152 buf->st_dev = buf->st_rdev = fd;
1153 buf->st_mode = S_IFIFO;
1154 buf->st_nlink = 1;
1156 else if (type == FILE_TYPE_CHAR)
1158 buf->st_dev = buf->st_rdev = fd;
1159 buf->st_mode = S_IFCHR;
1160 buf->st_nlink = 1;
1162 else /* FILE_TYPE_DISK etc. */
1164 if (!GetFileInformationByHandle(hand, &hfi))
1166 WARN(":failed-last error (%d)\n",GetLastError());
1167 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
1168 return -1;
1170 buf->st_mode = S_IFREG | 0444;
1171 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1172 buf->st_mode |= 0222;
1173 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1174 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1175 buf->st_atime = dw;
1176 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1177 buf->st_mtime = buf->st_ctime = dw;
1178 buf->st_nlink = hfi.nNumberOfLinks;
1180 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
1181 buf->st_mode);
1182 return 0;
1185 /*********************************************************************
1186 * _fstati64 (MSVCRT.@)
1188 int CDECL MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
1190 int ret;
1191 struct MSVCRT__stat64 buf64;
1193 ret = MSVCRT__fstat64(fd, &buf64);
1194 if (!ret)
1195 msvcrt_stat64_to_stati64(&buf64, buf);
1196 return ret;
1199 /*********************************************************************
1200 * _fstat (MSVCRT.@)
1202 int CDECL MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
1203 { int ret;
1204 struct MSVCRT__stat64 buf64;
1206 ret = MSVCRT__fstat64(fd, &buf64);
1207 if (!ret)
1208 msvcrt_stat64_to_stat(&buf64, buf);
1209 return ret;
1212 /*********************************************************************
1213 * _futime (MSVCRT.@)
1215 int CDECL _futime(int fd, struct MSVCRT__utimbuf *t)
1217 HANDLE hand = msvcrt_fdtoh(fd);
1218 FILETIME at, wt;
1220 if (!t)
1222 MSVCRT_time_t currTime;
1223 MSVCRT_time(&currTime);
1224 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1225 wt = at;
1227 else
1229 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1230 if (t->actime == t->modtime)
1231 wt = at;
1232 else
1233 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1236 if (!SetFileTime(hand, NULL, &at, &wt))
1238 msvcrt_set_errno(GetLastError());
1239 return -1 ;
1241 return 0;
1244 /*********************************************************************
1245 * _get_osfhandle (MSVCRT.@)
1247 MSVCRT_intptr_t CDECL _get_osfhandle(int fd)
1249 HANDLE hand = msvcrt_fdtoh(fd);
1250 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1252 return (MSVCRT_intptr_t)hand;
1255 /*********************************************************************
1256 * _isatty (MSVCRT.@)
1258 int CDECL _isatty(int fd)
1260 HANDLE hand = msvcrt_fdtoh(fd);
1262 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1263 if (hand == INVALID_HANDLE_VALUE)
1264 return 0;
1266 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1269 /*********************************************************************
1270 * _mktemp (MSVCRT.@)
1272 char * CDECL _mktemp(char *pattern)
1274 int numX = 0;
1275 char *retVal = pattern;
1276 int id;
1277 char letter = 'a';
1279 while(*pattern)
1280 numX = (*pattern++ == 'X')? numX + 1 : 0;
1281 if (numX < 5)
1282 return NULL;
1283 pattern--;
1284 id = GetCurrentProcessId();
1285 numX = 6;
1286 while(numX--)
1288 int tempNum = id / 10;
1289 *pattern-- = id - (tempNum * 10) + '0';
1290 id = tempNum;
1292 pattern++;
1295 *pattern = letter++;
1296 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1297 GetLastError() == ERROR_FILE_NOT_FOUND)
1298 return retVal;
1299 } while(letter <= 'z');
1300 return NULL;
1303 /*********************************************************************
1304 * _wmktemp (MSVCRT.@)
1306 MSVCRT_wchar_t * CDECL _wmktemp(MSVCRT_wchar_t *pattern)
1308 int numX = 0;
1309 MSVCRT_wchar_t *retVal = pattern;
1310 int id;
1311 MSVCRT_wchar_t letter = 'a';
1313 while(*pattern)
1314 numX = (*pattern++ == 'X')? numX + 1 : 0;
1315 if (numX < 5)
1316 return NULL;
1317 pattern--;
1318 id = GetCurrentProcessId();
1319 numX = 6;
1320 while(numX--)
1322 int tempNum = id / 10;
1323 *pattern-- = id - (tempNum * 10) + '0';
1324 id = tempNum;
1326 pattern++;
1329 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1330 GetLastError() == ERROR_FILE_NOT_FOUND)
1331 return retVal;
1332 *pattern = letter++;
1333 } while(letter != '|');
1334 return NULL;
1337 static unsigned split_oflags(unsigned oflags)
1339 int wxflags = 0;
1340 unsigned unsupp; /* until we support everything */
1342 if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
1343 if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
1344 else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
1345 else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
1346 else wxflags |= WX_TEXT; /* default to TEXT*/
1347 if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1349 if ((unsupp = oflags & ~(
1350 MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
1351 MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
1352 MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
1353 MSVCRT__O_NOINHERIT|
1354 MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
1356 ERR(":unsupported oflags 0x%04x\n",unsupp);
1358 return wxflags;
1361 /*********************************************************************
1362 * _pipe (MSVCRT.@)
1364 int CDECL MSVCRT__pipe(int *pfds, unsigned int psize, int textmode)
1366 int ret = -1;
1367 SECURITY_ATTRIBUTES sa;
1368 HANDLE readHandle, writeHandle;
1370 if (!pfds)
1372 *MSVCRT__errno() = MSVCRT_EINVAL;
1373 return -1;
1376 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1377 sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
1378 sa.lpSecurityDescriptor = NULL;
1379 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1381 unsigned int wxflags = split_oflags(textmode);
1382 int fd;
1384 LOCK_FILES();
1385 fd = msvcrt_alloc_fd(readHandle, wxflags);
1386 if (fd != -1)
1388 pfds[0] = fd;
1389 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1390 if (fd != -1)
1392 pfds[1] = fd;
1393 ret = 0;
1395 else
1397 MSVCRT__close(pfds[0]);
1398 CloseHandle(writeHandle);
1399 *MSVCRT__errno() = MSVCRT_EMFILE;
1402 else
1404 CloseHandle(readHandle);
1405 CloseHandle(writeHandle);
1406 *MSVCRT__errno() = MSVCRT_EMFILE;
1408 UNLOCK_FILES();
1410 else
1411 msvcrt_set_errno(GetLastError());
1413 return ret;
1416 /*********************************************************************
1417 * _sopen (MSVCRT.@)
1419 int CDECL MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1421 va_list ap;
1422 int pmode;
1423 DWORD access = 0, creation = 0, attrib;
1424 DWORD sharing;
1425 int wxflag = 0, fd;
1426 HANDLE hand;
1427 SECURITY_ATTRIBUTES sa;
1430 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1431 path, oflags, shflags);
1433 wxflag = split_oflags(oflags);
1434 switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1436 case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
1437 case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
1438 case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1441 if (oflags & MSVCRT__O_CREAT)
1443 va_start(ap, shflags);
1444 pmode = va_arg(ap, int);
1445 va_end(ap);
1447 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1448 FIXME(": pmode 0x%04x ignored\n", pmode);
1449 else
1450 WARN(": pmode 0x%04x ignored\n", pmode);
1452 if (oflags & MSVCRT__O_EXCL)
1453 creation = CREATE_NEW;
1454 else if (oflags & MSVCRT__O_TRUNC)
1455 creation = CREATE_ALWAYS;
1456 else
1457 creation = OPEN_ALWAYS;
1459 else /* no MSVCRT__O_CREAT */
1461 if (oflags & MSVCRT__O_TRUNC)
1462 creation = TRUNCATE_EXISTING;
1463 else
1464 creation = OPEN_EXISTING;
1467 switch( shflags )
1469 case MSVCRT__SH_DENYRW:
1470 sharing = 0L;
1471 break;
1472 case MSVCRT__SH_DENYWR:
1473 sharing = FILE_SHARE_READ;
1474 break;
1475 case MSVCRT__SH_DENYRD:
1476 sharing = FILE_SHARE_WRITE;
1477 break;
1478 case MSVCRT__SH_DENYNO:
1479 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1480 break;
1481 default:
1482 ERR( "Unhandled shflags 0x%x\n", shflags );
1483 return -1;
1485 attrib = FILE_ATTRIBUTE_NORMAL;
1487 if (oflags & MSVCRT__O_TEMPORARY)
1489 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1490 access |= DELETE;
1491 sharing |= FILE_SHARE_DELETE;
1494 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1495 sa.lpSecurityDescriptor = NULL;
1496 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1498 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1500 if (hand == INVALID_HANDLE_VALUE) {
1501 WARN(":failed-last error (%d)\n",GetLastError());
1502 msvcrt_set_errno(GetLastError());
1503 return -1;
1506 fd = msvcrt_alloc_fd(hand, wxflag);
1508 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1509 return fd;
1512 /*********************************************************************
1513 * _wsopen (MSVCRT.@)
1515 int CDECL MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1517 const unsigned int len = strlenW(path);
1518 char *patha = MSVCRT_calloc(len + 1,1);
1519 va_list ap;
1520 int pmode;
1522 va_start(ap, shflags);
1523 pmode = va_arg(ap, int);
1524 va_end(ap);
1526 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1528 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1529 MSVCRT_free(patha);
1530 return retval;
1533 msvcrt_set_errno(GetLastError());
1534 return -1;
1537 /*********************************************************************
1538 * _open (MSVCRT.@)
1540 int CDECL MSVCRT__open( const char *path, int flags, ... )
1542 va_list ap;
1544 if (flags & MSVCRT__O_CREAT)
1546 int pmode;
1547 va_start(ap, flags);
1548 pmode = va_arg(ap, int);
1549 va_end(ap);
1550 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1552 else
1553 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1556 /*********************************************************************
1557 * _wopen (MSVCRT.@)
1559 int CDECL _wopen(const MSVCRT_wchar_t *path,int flags,...)
1561 const unsigned int len = strlenW(path);
1562 char *patha = MSVCRT_calloc(len + 1,1);
1563 va_list ap;
1564 int pmode;
1566 va_start(ap, flags);
1567 pmode = va_arg(ap, int);
1568 va_end(ap);
1570 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1572 int retval = MSVCRT__open(patha,flags,pmode);
1573 MSVCRT_free(patha);
1574 return retval;
1577 msvcrt_set_errno(GetLastError());
1578 return -1;
1581 /*********************************************************************
1582 * _creat (MSVCRT.@)
1584 int CDECL MSVCRT__creat(const char *path, int flags)
1586 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1587 return MSVCRT__open(path, usedFlags);
1590 /*********************************************************************
1591 * _wcreat (MSVCRT.@)
1593 int CDECL _wcreat(const MSVCRT_wchar_t *path, int flags)
1595 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1596 return _wopen(path, usedFlags);
1599 /*********************************************************************
1600 * _open_osfhandle (MSVCRT.@)
1602 int CDECL _open_osfhandle(MSVCRT_intptr_t handle, int oflags)
1604 int fd;
1606 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1607 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1608 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1609 * text - it never sets MSVCRT__O_BINARY.
1611 /* don't let split_oflags() decide the mode if no mode is passed */
1612 if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)))
1613 oflags |= MSVCRT__O_BINARY;
1615 fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
1616 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1617 return fd;
1620 /*********************************************************************
1621 * _rmtmp (MSVCRT.@)
1623 int CDECL _rmtmp(void)
1625 int num_removed = 0, i;
1627 LOCK_FILES();
1628 for (i = 3; i < MSVCRT_stream_idx; i++)
1629 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1631 MSVCRT_fclose(MSVCRT_fstreams[i]);
1632 num_removed++;
1634 UNLOCK_FILES();
1636 if (num_removed)
1637 TRACE(":removed (%d) temp files\n",num_removed);
1638 return num_removed;
1641 /*********************************************************************
1642 * (internal) remove_cr
1644 * Translate all \r\n to \n inplace.
1645 * return the number of \r removed
1646 * Corner cases required by some apps:
1647 * \r\r\n -> \r\n
1648 * BUG: should save state across calls somehow, so CR LF that
1649 * straddles buffer boundary gets recognized properly?
1651 static unsigned int remove_cr(char *buf, unsigned int count)
1653 unsigned int i, j;
1655 for (i=0, j=0; j < count; j++)
1656 if ((buf[j] != '\r') || ((j+1) < count && buf[j+1] != '\n'))
1657 buf[i++] = buf[j];
1659 return count - i;
1662 /*********************************************************************
1663 * (internal) read_i
1665 static int read_i(int fd, void *buf, unsigned int count)
1667 DWORD num_read;
1668 char *bufstart = buf;
1669 HANDLE hand = msvcrt_fdtoh(fd);
1671 if (MSVCRT_fdesc[fd].wxflag & WX_READEOF) {
1672 MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
1673 TRACE("already at EOF, returning 0\n");
1674 return 0;
1676 /* Don't trace small reads, it gets *very* annoying */
1677 if (count > 4)
1678 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1679 if (hand == INVALID_HANDLE_VALUE)
1680 return -1;
1682 /* Reading single bytes in O_TEXT mode makes things slow
1683 * So read big chunks
1685 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1687 if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1689 DWORD i;
1690 /* in text mode, a ctrl-z signals EOF */
1691 for (i=0; i<num_read; i++)
1693 if (bufstart[i] == 0x1a)
1695 num_read = i;
1696 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1697 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1698 break;
1702 if (count != 0 && num_read == 0)
1704 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1705 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1708 else
1710 if (GetLastError() == ERROR_BROKEN_PIPE)
1712 TRACE(":end-of-pipe\n");
1713 MSVCRT_fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1714 return 0;
1716 else
1718 TRACE(":failed-last error (%d)\n",GetLastError());
1719 return -1;
1723 if (count > 4)
1724 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1725 return num_read;
1728 /*********************************************************************
1729 * _read (MSVCRT.@)
1731 int CDECL MSVCRT__read(int fd, void *buf, unsigned int count)
1733 int num_read;
1734 num_read = read_i(fd, buf, count);
1735 if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
1737 num_read -= remove_cr(buf,num_read);
1739 return num_read;
1742 /*********************************************************************
1743 * _setmode (MSVCRT.@)
1745 int CDECL _setmode(int fd,int mode)
1747 int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1748 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1749 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1750 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1751 MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
1752 else
1753 MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
1754 return ret;
1757 /*********************************************************************
1758 * _stat64 (MSVCRT.@)
1760 int CDECL MSVCRT__stat64(const char* path, struct MSVCRT__stat64 * buf)
1762 DWORD dw;
1763 WIN32_FILE_ATTRIBUTE_DATA hfi;
1764 unsigned short mode = ALL_S_IREAD;
1765 int plen;
1767 TRACE(":file (%s) buf(%p)\n",path,buf);
1769 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1771 TRACE("failed (%d)\n",GetLastError());
1772 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1773 return -1;
1776 memset(buf,0,sizeof(struct MSVCRT__stat64));
1778 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1779 Bon 011120: This FIXME seems incorrect
1780 Also a letter as first char isn't enough to be classified
1781 as a drive letter
1783 if (isalpha(*path)&& (*(path+1)==':'))
1784 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1785 else
1786 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1788 plen = strlen(path);
1790 /* Dir, or regular file? */
1791 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1792 (path[plen-1] == '\\'))
1793 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1794 else
1796 mode |= MSVCRT__S_IFREG;
1797 /* executable? */
1798 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1800 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1801 (tolower(path[plen-3]) << 16);
1802 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1803 mode |= ALL_S_IEXEC;
1807 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1808 mode |= ALL_S_IWRITE;
1810 buf->st_mode = mode;
1811 buf->st_nlink = 1;
1812 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1813 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1814 buf->st_atime = dw;
1815 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1816 buf->st_mtime = buf->st_ctime = dw;
1817 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1818 (long)(buf->st_size >> 32),(long)buf->st_size,
1819 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1820 return 0;
1823 /*********************************************************************
1824 * _stati64 (MSVCRT.@)
1826 int CDECL MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1828 int ret;
1829 struct MSVCRT__stat64 buf64;
1831 ret = MSVCRT__stat64(path, &buf64);
1832 if (!ret)
1833 msvcrt_stat64_to_stati64(&buf64, buf);
1834 return ret;
1837 /*********************************************************************
1838 * _stat (MSVCRT.@)
1840 int CDECL MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1841 { int ret;
1842 struct MSVCRT__stat64 buf64;
1844 ret = MSVCRT__stat64( path, &buf64);
1845 if (!ret)
1846 msvcrt_stat64_to_stat(&buf64, buf);
1847 return ret;
1850 /*********************************************************************
1851 * _wstat64 (MSVCRT.@)
1853 int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * buf)
1855 DWORD dw;
1856 WIN32_FILE_ATTRIBUTE_DATA hfi;
1857 unsigned short mode = ALL_S_IREAD;
1858 int plen;
1860 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1862 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1864 TRACE("failed (%d)\n",GetLastError());
1865 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1866 return -1;
1869 memset(buf,0,sizeof(struct MSVCRT__stat64));
1871 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1872 if (MSVCRT_iswalpha(*path))
1873 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1874 else
1875 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1877 plen = strlenW(path);
1879 /* Dir, or regular file? */
1880 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1881 (path[plen-1] == '\\'))
1882 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1883 else
1885 mode |= MSVCRT__S_IFREG;
1886 /* executable? */
1887 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1889 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1890 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1891 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1892 mode |= ALL_S_IEXEC;
1896 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1897 mode |= ALL_S_IWRITE;
1899 buf->st_mode = mode;
1900 buf->st_nlink = 1;
1901 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1902 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1903 buf->st_atime = dw;
1904 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1905 buf->st_mtime = buf->st_ctime = dw;
1906 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1907 (long)(buf->st_size >> 32),(long)buf->st_size,
1908 (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
1909 return 0;
1912 /*********************************************************************
1913 * _wstati64 (MSVCRT.@)
1915 int CDECL MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1917 int ret;
1918 struct MSVCRT__stat64 buf64;
1920 ret = MSVCRT__wstat64(path, &buf64);
1921 if (!ret)
1922 msvcrt_stat64_to_stati64(&buf64, buf);
1923 return ret;
1926 /*********************************************************************
1927 * _wstat (MSVCRT.@)
1929 int CDECL MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1931 int ret;
1932 struct MSVCRT__stat64 buf64;
1934 ret = MSVCRT__wstat64( path, &buf64 );
1935 if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
1936 return ret;
1939 /*********************************************************************
1940 * _tell (MSVCRT.@)
1942 long CDECL _tell(int fd)
1944 return MSVCRT__lseek(fd, 0, SEEK_CUR);
1947 /*********************************************************************
1948 * _telli64 (MSVCRT.@)
1950 __int64 CDECL _telli64(int fd)
1952 return MSVCRT__lseeki64(fd, 0, SEEK_CUR);
1955 /*********************************************************************
1956 * _tempnam (MSVCRT.@)
1958 char * CDECL _tempnam(const char *dir, const char *prefix)
1960 char tmpbuf[MAX_PATH];
1961 const char *tmp_dir = MSVCRT_getenv("TMP");
1963 if (tmp_dir) dir = tmp_dir;
1965 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1966 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1968 TRACE("got name (%s)\n",tmpbuf);
1969 DeleteFileA(tmpbuf);
1970 return _strdup(tmpbuf);
1972 TRACE("failed (%d)\n",GetLastError());
1973 return NULL;
1976 /*********************************************************************
1977 * _wtempnam (MSVCRT.@)
1979 MSVCRT_wchar_t * CDECL _wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1981 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1983 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1984 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1986 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1987 DeleteFileW(tmpbuf);
1988 return _wcsdup(tmpbuf);
1990 TRACE("failed (%d)\n",GetLastError());
1991 return NULL;
1994 /*********************************************************************
1995 * _umask (MSVCRT.@)
1997 int CDECL MSVCRT__umask(int umask)
1999 int old_umask = MSVCRT_umask;
2000 TRACE("(%d)\n",umask);
2001 MSVCRT_umask = umask;
2002 return old_umask;
2005 /*********************************************************************
2006 * _utime (MSVCRT.@)
2008 int CDECL _utime(const char* path, struct MSVCRT__utimbuf *t)
2010 int fd = MSVCRT__open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2012 if (fd > 0)
2014 int retVal = _futime(fd, t);
2015 MSVCRT__close(fd);
2016 return retVal;
2018 return -1;
2021 /*********************************************************************
2022 * _wutime (MSVCRT.@)
2024 int CDECL _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
2026 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
2028 if (fd > 0)
2030 int retVal = _futime(fd, t);
2031 MSVCRT__close(fd);
2032 return retVal;
2034 return -1;
2037 /*********************************************************************
2038 * _write (MSVCRT.@)
2040 int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
2042 DWORD num_written;
2043 HANDLE hand = msvcrt_fdtoh(fd);
2045 /* Don't trace small writes, it gets *very* annoying */
2046 #if 0
2047 if (count > 32)
2048 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2049 #endif
2050 if (hand == INVALID_HANDLE_VALUE)
2052 *MSVCRT__errno() = MSVCRT_EBADF;
2053 return -1;
2056 /* If appending, go to EOF */
2057 if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
2058 MSVCRT__lseek(fd, 0, FILE_END);
2060 if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
2062 if (WriteFile(hand, buf, count, &num_written, NULL)
2063 && (num_written == count))
2064 return num_written;
2065 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2066 hand, GetLastError());
2067 *MSVCRT__errno() = MSVCRT_ENOSPC;
2069 else
2071 unsigned int i, j, nr_lf;
2072 char *p = NULL;
2073 const char *q;
2074 const char *s = (const char *)buf, *buf_start = (const char *)buf;
2075 /* find number of \n ( without preceding \r ) */
2076 for ( nr_lf=0,i = 0; i <count; i++)
2078 if (s[i]== '\n')
2080 nr_lf++;
2081 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2084 if (nr_lf)
2086 if ((q = p = MSVCRT_malloc(count + nr_lf)))
2088 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
2090 if (s[i]== '\n')
2092 p[j++] = '\r';
2093 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2095 p[j++] = s[i];
2098 else
2100 FIXME("Malloc failed\n");
2101 nr_lf =0;
2102 q = buf;
2105 else
2106 q = buf;
2108 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
2110 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2111 fd, hand, GetLastError(), num_written);
2112 *MSVCRT__errno() = MSVCRT_ENOSPC;
2113 if(nr_lf)
2114 MSVCRT_free(p);
2115 return s - buf_start;
2117 else
2119 if(nr_lf)
2120 MSVCRT_free(p);
2121 return count;
2124 return -1;
2127 /*********************************************************************
2128 * _putw (MSVCRT.@)
2130 int CDECL MSVCRT__putw(int val, MSVCRT_FILE* file)
2132 int len;
2133 len = MSVCRT__write(file->_file, &val, sizeof(val));
2134 if (len == sizeof(val)) return val;
2135 file->_flag |= MSVCRT__IOERR;
2136 return MSVCRT_EOF;
2139 /*********************************************************************
2140 * fclose (MSVCRT.@)
2142 int CDECL MSVCRT_fclose(MSVCRT_FILE* file)
2144 int r, flag;
2146 flag = file->_flag;
2147 MSVCRT_free(file->_tmpfname);
2148 file->_tmpfname = NULL;
2149 /* flush stdio buffers */
2150 if(file->_flag & MSVCRT__IOWRT)
2151 MSVCRT_fflush(file);
2152 if(file->_flag & MSVCRT__IOMYBUF)
2153 MSVCRT_free(file->_base);
2155 r=MSVCRT__close(file->_file);
2157 file->_flag = 0;
2159 return ((r == -1) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
2162 /*********************************************************************
2163 * feof (MSVCRT.@)
2165 int CDECL MSVCRT_feof(MSVCRT_FILE* file)
2167 return file->_flag & MSVCRT__IOEOF;
2170 /*********************************************************************
2171 * ferror (MSVCRT.@)
2173 int CDECL MSVCRT_ferror(MSVCRT_FILE* file)
2175 return file->_flag & MSVCRT__IOERR;
2178 /*********************************************************************
2179 * _filbuf (MSVCRT.@)
2181 int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
2183 /* Allocate buffer if needed */
2184 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
2185 msvcrt_alloc_buffer(file);
2187 if(!(file->_flag & MSVCRT__IOREAD)) {
2188 if(file->_flag & MSVCRT__IORW) {
2189 file->_flag |= MSVCRT__IOREAD;
2190 } else {
2191 return MSVCRT_EOF;
2194 if(file->_flag & MSVCRT__IONBF) {
2195 unsigned char c;
2196 int r;
2197 if ((r = read_i(file->_file,&c,1)) != 1) {
2198 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2199 return MSVCRT_EOF;
2201 return c;
2202 } else {
2203 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
2204 if(file->_cnt<=0) {
2205 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2206 file->_cnt = 0;
2207 return MSVCRT_EOF;
2209 file->_cnt--;
2210 file->_ptr = file->_base+1;
2211 return *(unsigned char *)file->_base;
2215 /*********************************************************************
2216 * fgetc (MSVCRT.@)
2218 int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
2220 unsigned char *i;
2221 unsigned int j;
2222 do {
2223 if (file->_cnt>0) {
2224 file->_cnt--;
2225 i = (unsigned char *)file->_ptr++;
2226 j = *i;
2227 } else
2228 j = MSVCRT__filbuf(file);
2229 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2230 || ((j != '\r') || (file->_cnt && file->_ptr[0] != '\n')))
2231 return j;
2232 } while(1);
2235 /*********************************************************************
2236 * _fgetchar (MSVCRT.@)
2238 int CDECL _fgetchar(void)
2240 return MSVCRT_fgetc(MSVCRT_stdin);
2243 /*********************************************************************
2244 * fgets (MSVCRT.@)
2246 char * CDECL MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
2248 int cc = MSVCRT_EOF;
2249 char * buf_start = s;
2251 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2252 file,file->_file,s,size);
2254 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
2256 *s++ = (char)cc;
2257 size --;
2259 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
2261 TRACE(":nothing read\n");
2262 return NULL;
2264 if ((cc != MSVCRT_EOF) && (size > 1))
2265 *s++ = cc;
2266 *s = '\0';
2267 TRACE(":got %s\n", debugstr_a(buf_start));
2268 return buf_start;
2271 /*********************************************************************
2272 * fgetwc (MSVCRT.@)
2274 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2275 * the CR from CR/LF combinations
2277 MSVCRT_wint_t CDECL MSVCRT_fgetwc(MSVCRT_FILE* file)
2279 char c;
2281 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2283 MSVCRT_wchar_t wc;
2284 unsigned int i;
2285 int j;
2286 char *chp, *wcp;
2287 wcp = (char *)&wc;
2288 for(i=0; i<sizeof(wc); i++)
2290 if (file->_cnt>0)
2292 file->_cnt--;
2293 chp = file->_ptr++;
2294 wcp[i] = *chp;
2296 else
2298 j = MSVCRT__filbuf(file);
2299 if(file->_cnt<=0)
2301 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
2302 file->_cnt = 0;
2303 return MSVCRT_WEOF;
2305 wcp[i] = j;
2308 return wc;
2311 c = MSVCRT_fgetc(file);
2312 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
2314 FIXME("Treat Multibyte characters\n");
2316 if (c == MSVCRT_EOF)
2317 return MSVCRT_WEOF;
2318 else
2319 return (MSVCRT_wint_t)c;
2322 /*********************************************************************
2323 * _getw (MSVCRT.@)
2325 int CDECL MSVCRT__getw(MSVCRT_FILE* file)
2327 char *ch;
2328 int i, k;
2329 unsigned int j;
2330 ch = (char *)&i;
2331 for (j=0; j<sizeof(int); j++) {
2332 k = MSVCRT_fgetc(file);
2333 if (k == MSVCRT_EOF) {
2334 file->_flag |= MSVCRT__IOEOF;
2335 return EOF;
2337 ch[j] = k;
2339 return i;
2342 /*********************************************************************
2343 * getwc (MSVCRT.@)
2345 MSVCRT_wint_t CDECL MSVCRT_getwc(MSVCRT_FILE* file)
2347 return MSVCRT_fgetwc(file);
2350 /*********************************************************************
2351 * _fgetwchar (MSVCRT.@)
2353 MSVCRT_wint_t CDECL _fgetwchar(void)
2355 return MSVCRT_fgetwc(MSVCRT_stdin);
2358 /*********************************************************************
2359 * getwchar (MSVCRT.@)
2361 MSVCRT_wint_t CDECL MSVCRT_getwchar(void)
2363 return _fgetwchar();
2366 /*********************************************************************
2367 * fgetws (MSVCRT.@)
2369 MSVCRT_wchar_t * CDECL MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
2371 int cc = MSVCRT_WEOF;
2372 MSVCRT_wchar_t * buf_start = s;
2374 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2375 file,file->_file,s,size);
2377 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
2379 *s++ = (char)cc;
2380 size --;
2382 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2384 TRACE(":nothing read\n");
2385 return NULL;
2387 if ((cc != MSVCRT_WEOF) && (size > 1))
2388 *s++ = cc;
2389 *s = 0;
2390 TRACE(":got %s\n", debugstr_w(buf_start));
2391 return buf_start;
2394 /*********************************************************************
2395 * fwrite (MSVCRT.@)
2397 MSVCRT_size_t CDECL MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2399 MSVCRT_size_t wrcnt=size * nmemb;
2400 int written = 0;
2401 if (size == 0)
2402 return 0;
2403 if(file->_cnt) {
2404 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2405 memcpy(file->_ptr, ptr, pcnt);
2406 file->_cnt -= pcnt;
2407 file->_ptr += pcnt;
2408 written = pcnt;
2409 wrcnt -= pcnt;
2410 ptr = (const char*)ptr + pcnt;
2411 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2412 if(file->_flag & MSVCRT__IORW) {
2413 file->_flag |= MSVCRT__IOWRT;
2414 } else
2415 return 0;
2417 if(wrcnt) {
2418 /* Flush buffer */
2419 int res=msvcrt_flush_buffer(file);
2420 if(!res) {
2421 int pwritten = MSVCRT__write(file->_file, ptr, wrcnt);
2422 if (pwritten <= 0)
2424 file->_flag |= MSVCRT__IOERR;
2425 pwritten=0;
2427 written += pwritten;
2430 return written / size;
2433 /*********************************************************************
2434 * fputwc (MSVCRT.@)
2436 MSVCRT_wint_t CDECL MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2438 MSVCRT_wchar_t mwc=wc;
2439 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2440 return MSVCRT_WEOF;
2441 return wc;
2444 /*********************************************************************
2445 * _fputwchar (MSVCRT.@)
2447 MSVCRT_wint_t CDECL _fputwchar(MSVCRT_wint_t wc)
2449 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2452 /*********************************************************************
2453 * _fsopen (MSVCRT.@)
2455 MSVCRT_FILE * CDECL MSVCRT__fsopen(const char *path, const char *mode, int share)
2457 MSVCRT_FILE* file;
2458 int open_flags, stream_flags, fd;
2460 TRACE("(%s,%s)\n",path,mode);
2462 /* map mode string to open() flags. "man fopen" for possibilities. */
2463 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2464 return NULL;
2466 LOCK_FILES();
2467 fd = MSVCRT__sopen(path, open_flags, share, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2468 if (fd < 0)
2469 file = NULL;
2470 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
2471 != -1)
2472 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2473 else if (file)
2475 file->_flag = 0;
2476 file = NULL;
2479 TRACE(":got (%p)\n",file);
2480 if (fd >= 0 && !file)
2481 MSVCRT__close(fd);
2482 UNLOCK_FILES();
2483 return file;
2486 /*********************************************************************
2487 * _wfsopen (MSVCRT.@)
2489 MSVCRT_FILE * CDECL MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2491 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2492 char *patha = MSVCRT_calloc(plen + 1, 1);
2493 char *modea = MSVCRT_calloc(mlen + 1, 1);
2495 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2497 if (patha && modea &&
2498 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2499 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2501 MSVCRT_FILE *retval = MSVCRT__fsopen(patha,modea,share);
2502 MSVCRT_free(patha);
2503 MSVCRT_free(modea);
2504 return retval;
2507 msvcrt_set_errno(GetLastError());
2508 return NULL;
2511 /*********************************************************************
2512 * fopen (MSVCRT.@)
2514 MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
2516 return MSVCRT__fsopen( path, mode, MSVCRT__SH_DENYNO );
2519 /*********************************************************************
2520 * _wfopen (MSVCRT.@)
2522 MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2524 return MSVCRT__wfsopen( path, mode, MSVCRT__SH_DENYNO );
2527 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2528 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2530 /*********************************************************************
2531 * fputc (MSVCRT.@)
2533 int CDECL MSVCRT_fputc(int c, MSVCRT_FILE* file)
2535 if(file->_cnt>0) {
2536 *file->_ptr++=c;
2537 file->_cnt--;
2538 if (c == '\n')
2540 int res = msvcrt_flush_buffer(file);
2541 return res ? res : c;
2543 else
2544 return c & 0xff;
2545 } else {
2546 return MSVCRT__flsbuf(c, file);
2550 /*********************************************************************
2551 * _flsbuf (MSVCRT.@)
2553 int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2555 /* Flush output buffer */
2556 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2557 msvcrt_alloc_buffer(file);
2559 if(!(file->_flag & MSVCRT__IOWRT)) {
2560 if(file->_flag & MSVCRT__IORW) {
2561 file->_flag |= MSVCRT__IOWRT;
2562 } else {
2563 return MSVCRT_EOF;
2566 if(file->_bufsiz) {
2567 int res=msvcrt_flush_buffer(file);
2568 return res?res : MSVCRT_fputc(c, file);
2569 } else {
2570 unsigned char cc=c;
2571 int len;
2572 len = MSVCRT__write(file->_file, &cc, 1);
2573 if (len == 1) return c & 0xff;
2574 file->_flag |= MSVCRT__IOERR;
2575 return MSVCRT_EOF;
2579 /*********************************************************************
2580 * _fputchar (MSVCRT.@)
2582 int CDECL _fputchar(int c)
2584 return MSVCRT_fputc(c, MSVCRT_stdout);
2587 /*********************************************************************
2588 * fread (MSVCRT.@)
2590 MSVCRT_size_t CDECL MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2591 { MSVCRT_size_t rcnt=size * nmemb;
2592 MSVCRT_size_t read=0;
2593 int pread=0;
2595 if(!rcnt)
2596 return 0;
2598 /* first buffered data */
2599 if(file->_cnt>0) {
2600 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2601 memcpy(ptr, file->_ptr, pcnt);
2602 file->_cnt -= pcnt;
2603 file->_ptr += pcnt;
2604 if (MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
2605 pcnt -= remove_cr(ptr,pcnt);
2606 read += pcnt ;
2607 rcnt -= pcnt ;
2608 ptr = (char*)ptr + pcnt;
2609 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2610 if(file->_flag & MSVCRT__IORW) {
2611 file->_flag |= MSVCRT__IOREAD;
2612 } else
2613 return 0;
2615 while(rcnt>0)
2617 int i;
2618 /* Fill the buffer on small reads.
2619 * TODO: Use a better buffering strategy.
2621 if (!file->_cnt && size*nmemb <= MSVCRT_BUFSIZ/2 && !(file->_flag & MSVCRT__IONBF)) {
2622 if (file->_bufsiz == 0) {
2623 msvcrt_alloc_buffer(file);
2625 file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
2626 file->_ptr = file->_base;
2627 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2628 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2629 if (i > 0 && i < file->_cnt) {
2630 MSVCRT_fdesc[file->_file].wxflag &= ~WX_ATEOF;
2631 file->_flag &= ~MSVCRT__IOEOF;
2633 if (i > 0) {
2634 memcpy(ptr, file->_ptr, i);
2635 file->_cnt -= i;
2636 file->_ptr += i;
2638 } else {
2639 i = MSVCRT__read(file->_file,ptr, rcnt);
2641 pread += i;
2642 rcnt -= i;
2643 ptr = (char *)ptr+i;
2644 /* expose feof condition in the flags
2645 * MFC tests file->_flag for feof, and doesn't call feof())
2647 if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
2648 file->_flag |= MSVCRT__IOEOF;
2649 else if (i == -1)
2651 file->_flag |= MSVCRT__IOERR;
2652 pread = 0;
2653 rcnt = 0;
2655 if (i < 1) break;
2657 read+=pread;
2658 return read / size;
2661 /*********************************************************************
2662 * freopen (MSVCRT.@)
2665 MSVCRT_FILE* CDECL MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2667 int open_flags, stream_flags, fd;
2669 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2671 LOCK_FILES();
2672 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2673 file = NULL;
2674 else
2676 MSVCRT_fclose(file);
2677 /* map mode string to open() flags. "man fopen" for possibilities. */
2678 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2679 file = NULL;
2680 else
2682 fd = MSVCRT__open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2683 if (fd < 0)
2684 file = NULL;
2685 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
2687 file->_flag = 0;
2688 WARN(":failed-last error (%d)\n",GetLastError());
2689 msvcrt_set_errno(GetLastError());
2690 file = NULL;
2694 UNLOCK_FILES();
2695 return file;
2698 /*********************************************************************
2699 * fsetpos (MSVCRT.@)
2701 int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2703 /* Note that all this has been lifted 'as is' from fseek */
2704 if(file->_flag & MSVCRT__IOWRT)
2705 msvcrt_flush_buffer(file);
2707 /* Discard buffered input */
2708 file->_cnt = 0;
2709 file->_ptr = file->_base;
2711 /* Reset direction of i/o */
2712 if(file->_flag & MSVCRT__IORW) {
2713 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2716 return (MSVCRT__lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2719 /*********************************************************************
2720 * ftell (MSVCRT.@)
2722 LONG CDECL MSVCRT_ftell(MSVCRT_FILE* file)
2724 int off=0;
2725 long pos;
2726 if(file->_bufsiz) {
2727 if( file->_flag & MSVCRT__IOWRT ) {
2728 off = file->_ptr - file->_base;
2729 } else {
2730 off = -file->_cnt;
2733 pos = _tell(file->_file);
2734 if(pos == -1) return pos;
2735 return off + pos;
2738 /*********************************************************************
2739 * fgetpos (MSVCRT.@)
2741 int CDECL MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2743 /* This code has been lifted form the MSVCRT_ftell function */
2744 int off=0;
2746 *pos = MSVCRT__lseeki64(file->_file,0,SEEK_CUR);
2748 if (*pos == -1) return -1;
2750 if(file->_bufsiz) {
2751 if( file->_flag & MSVCRT__IOWRT ) {
2752 off = file->_ptr - file->_base;
2753 } else {
2754 off = -file->_cnt;
2757 *pos += off;
2759 return 0;
2762 /*********************************************************************
2763 * fputs (MSVCRT.@)
2765 int CDECL MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2767 size_t i, len = strlen(s);
2768 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2769 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2770 for (i=0; i<len; i++)
2771 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2772 return MSVCRT_EOF;
2773 return 0;
2776 /*********************************************************************
2777 * fputws (MSVCRT.@)
2779 int CDECL MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2781 size_t i, len = strlenW(s);
2782 if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
2783 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2784 for (i=0; i<len; i++)
2786 if ((s[i] == '\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2787 return MSVCRT_WEOF;
2788 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2789 return MSVCRT_WEOF;
2791 return 0;
2794 /*********************************************************************
2795 * getchar (MSVCRT.@)
2797 int CDECL MSVCRT_getchar(void)
2799 return MSVCRT_fgetc(MSVCRT_stdin);
2802 /*********************************************************************
2803 * getc (MSVCRT.@)
2805 int CDECL MSVCRT_getc(MSVCRT_FILE* file)
2807 return MSVCRT_fgetc(file);
2810 /*********************************************************************
2811 * gets (MSVCRT.@)
2813 char * CDECL MSVCRT_gets(char *buf)
2815 int cc;
2816 char * buf_start = buf;
2818 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2819 cc = MSVCRT_fgetc(MSVCRT_stdin))
2820 if(cc != '\r') *buf++ = (char)cc;
2822 *buf = '\0';
2824 TRACE("got '%s'\n", buf_start);
2825 return buf_start;
2828 /*********************************************************************
2829 * _getws (MSVCRT.@)
2831 MSVCRT_wchar_t* CDECL MSVCRT__getws(MSVCRT_wchar_t* buf)
2833 MSVCRT_wint_t cc;
2834 MSVCRT_wchar_t* ws = buf;
2836 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2837 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2839 if (cc != '\r')
2840 *buf++ = (MSVCRT_wchar_t)cc;
2842 *buf = '\0';
2844 TRACE("got %s\n", debugstr_w(ws));
2845 return ws;
2848 /*********************************************************************
2849 * putc (MSVCRT.@)
2851 int CDECL MSVCRT_putc(int c, MSVCRT_FILE* file)
2853 return MSVCRT_fputc(c, file);
2856 /*********************************************************************
2857 * putchar (MSVCRT.@)
2859 int CDECL MSVCRT_putchar(int c)
2861 return MSVCRT_fputc(c, MSVCRT_stdout);
2864 /*********************************************************************
2865 * puts (MSVCRT.@)
2867 int CDECL MSVCRT_puts(const char *s)
2869 size_t len = strlen(s);
2870 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2871 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2874 /*********************************************************************
2875 * _putws (MSVCRT.@)
2877 int CDECL _putws(const MSVCRT_wchar_t *s)
2879 static const MSVCRT_wchar_t nl = '\n';
2880 size_t len = strlenW(s);
2881 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2882 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2885 /*********************************************************************
2886 * remove (MSVCRT.@)
2888 int CDECL MSVCRT_remove(const char *path)
2890 TRACE("(%s)\n",path);
2891 if (DeleteFileA(path))
2892 return 0;
2893 TRACE(":failed (%d)\n",GetLastError());
2894 msvcrt_set_errno(GetLastError());
2895 return -1;
2898 /*********************************************************************
2899 * _wremove (MSVCRT.@)
2901 int CDECL _wremove(const MSVCRT_wchar_t *path)
2903 TRACE("(%s)\n",debugstr_w(path));
2904 if (DeleteFileW(path))
2905 return 0;
2906 TRACE(":failed (%d)\n",GetLastError());
2907 msvcrt_set_errno(GetLastError());
2908 return -1;
2911 /*********************************************************************
2912 * rename (MSVCRT.@)
2914 int CDECL MSVCRT_rename(const char *oldpath,const char *newpath)
2916 TRACE(":from %s to %s\n",oldpath,newpath);
2917 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2918 return 0;
2919 TRACE(":failed (%d)\n",GetLastError());
2920 msvcrt_set_errno(GetLastError());
2921 return -1;
2924 /*********************************************************************
2925 * _wrename (MSVCRT.@)
2927 int CDECL _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2929 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2930 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2931 return 0;
2932 TRACE(":failed (%d)\n",GetLastError());
2933 msvcrt_set_errno(GetLastError());
2934 return -1;
2937 /*********************************************************************
2938 * setvbuf (MSVCRT.@)
2940 int CDECL MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2942 /* TODO: Check if file busy */
2943 if(file->_bufsiz) {
2944 MSVCRT_free(file->_base);
2945 file->_bufsiz = 0;
2946 file->_cnt = 0;
2948 if(mode == MSVCRT__IOFBF) {
2949 file->_flag &= ~MSVCRT__IONBF;
2950 file->_base = file->_ptr = buf;
2951 if(buf) {
2952 file->_bufsiz = size;
2954 } else {
2955 file->_flag |= MSVCRT__IONBF;
2957 return 0;
2960 /*********************************************************************
2961 * setbuf (MSVCRT.@)
2963 void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2965 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2968 /*********************************************************************
2969 * tmpnam (MSVCRT.@)
2971 char * CDECL MSVCRT_tmpnam(char *s)
2973 static int unique;
2974 char tmpstr[16];
2975 char *p;
2976 int count;
2977 if (s == 0)
2978 s = MSVCRT_tmpname;
2979 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2980 p = s + sprintf(s, "\\s%s.", tmpstr);
2981 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2983 msvcrt_int_to_base32(unique++, tmpstr);
2984 strcpy(p, tmpstr);
2985 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2986 GetLastError() == ERROR_FILE_NOT_FOUND)
2987 break;
2989 return s;
2992 /*********************************************************************
2993 * tmpfile (MSVCRT.@)
2995 MSVCRT_FILE* CDECL MSVCRT_tmpfile(void)
2997 char *filename = MSVCRT_tmpnam(NULL);
2998 int fd;
2999 MSVCRT_FILE* file = NULL;
3001 LOCK_FILES();
3002 fd = MSVCRT__open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
3003 if (fd != -1 && (file = msvcrt_alloc_fp()))
3005 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
3007 file->_flag = 0;
3008 file = NULL;
3010 else file->_tmpfname = _strdup(filename);
3012 UNLOCK_FILES();
3013 return file;
3016 /*********************************************************************
3017 * vfprintf (MSVCRT.@)
3019 int CDECL MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
3021 char buf[2048], *mem = buf;
3022 int written, resize = sizeof(buf), retval;
3023 /* There are two conventions for vsnprintf failing:
3024 * Return -1 if we truncated, or
3025 * Return the number of bytes that would have been written
3026 * The code below handles both cases
3028 while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
3029 written > resize)
3031 resize = (written == -1 ? resize * 2 : written + 1);
3032 if (mem != buf)
3033 MSVCRT_free (mem);
3034 if (!(mem = MSVCRT_malloc(resize)))
3035 return MSVCRT_EOF;
3037 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3038 if (mem != buf)
3039 MSVCRT_free (mem);
3040 return retval;
3043 /*********************************************************************
3044 * vfwprintf (MSVCRT.@)
3045 * FIXME:
3046 * Is final char included in written (then resize is too big) or not
3047 * (then we must test for equality too)?
3049 int CDECL MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
3051 MSVCRT_wchar_t buf[2048], *mem = buf;
3052 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
3053 /* See vfprintf comments */
3054 while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
3055 written > resize)
3057 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
3058 if (mem != buf)
3059 MSVCRT_free (mem);
3060 if (!(mem = MSVCRT_malloc(resize*sizeof(*mem))))
3061 return MSVCRT_EOF;
3063 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
3064 if (mem != buf)
3065 MSVCRT_free (mem);
3066 return retval;
3069 /*********************************************************************
3070 * vprintf (MSVCRT.@)
3072 int CDECL MSVCRT_vprintf(const char *format, va_list valist)
3074 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
3077 /*********************************************************************
3078 * vwprintf (MSVCRT.@)
3080 int CDECL MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
3082 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
3085 /*********************************************************************
3086 * fprintf (MSVCRT.@)
3088 int CDECL MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
3090 va_list valist;
3091 int res;
3092 va_start(valist, format);
3093 res = MSVCRT_vfprintf(file, format, valist);
3094 va_end(valist);
3095 return res;
3098 /*********************************************************************
3099 * fwprintf (MSVCRT.@)
3101 int CDECL MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
3103 va_list valist;
3104 int res;
3105 va_start(valist, format);
3106 res = MSVCRT_vfwprintf(file, format, valist);
3107 va_end(valist);
3108 return res;
3111 /*********************************************************************
3112 * printf (MSVCRT.@)
3114 int CDECL MSVCRT_printf(const char *format, ...)
3116 va_list valist;
3117 int res;
3118 va_start(valist, format);
3119 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
3120 va_end(valist);
3121 return res;
3124 /*********************************************************************
3125 * ungetc (MSVCRT.@)
3127 int CDECL MSVCRT_ungetc(int c, MSVCRT_FILE * file)
3129 if (c == MSVCRT_EOF)
3130 return MSVCRT_EOF;
3131 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
3132 msvcrt_alloc_buffer(file);
3133 file->_ptr++;
3135 if(file->_ptr>file->_base) {
3136 file->_ptr--;
3137 *file->_ptr=c;
3138 file->_cnt++;
3139 MSVCRT_clearerr(file);
3140 return c;
3142 return MSVCRT_EOF;
3145 /*********************************************************************
3146 * ungetwc (MSVCRT.@)
3148 MSVCRT_wint_t CDECL MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
3150 MSVCRT_wchar_t mwc = wc;
3151 char * pp = (char *)&mwc;
3152 int i;
3153 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
3154 if(pp[i] != MSVCRT_ungetc(pp[i],file))
3155 return MSVCRT_WEOF;
3157 return mwc;
3160 /*********************************************************************
3161 * wprintf (MSVCRT.@)
3163 int CDECL MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
3165 va_list valist;
3166 int res;
3167 va_start(valist, format);
3168 res = MSVCRT_vwprintf(format, valist);
3169 va_end(valist);
3170 return res;
3173 /*********************************************************************
3174 * _getmaxstdio (MSVCRT.@)
3176 int CDECL _getmaxstdio(void)
3178 FIXME("stub, always returns 512\n");
3179 return 512;
3182 /*********************************************************************
3183 * _setmaxstdio_ (MSVCRT.@)
3185 int CDECL _setmaxstdio(int newmax)
3187 int res;
3188 if( newmax > 2048)
3189 res = -1;
3190 else
3191 res = newmax;
3192 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3193 return res;
3196 /*********************************************************************
3197 * __pioinfo (MSVCRT.@)
3198 * FIXME: see MSVCRT_MAX_FILES define.
3200 ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3201 &MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
3202 &MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
3203 &MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
3204 &MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
3205 &MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
3206 &MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
3207 &MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
3208 &MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
3209 &MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
3210 &MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
3211 &MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
3214 /*********************************************************************
3215 * __badioinfo (MSVCRT.@)
3217 ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };