Get rid of the ICOM_MSVTABLE_COMPAT support, g++ no longer requires
[wine/multimedia.git] / dlls / msvcrt / file.c
blob5feabc76d74426ee3eaeafff2397f1e2e755f33c
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <time.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <sys/types.h>
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "msvcrt.h"
42 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
48 /* for stat mode, permissions apply to all,owner and group */
49 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
50 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
51 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
53 /* _access() bit flags FIXME: incomplete */
54 #define MSVCRT_W_OK 0x02
56 /* values for xflag */
57 #define WX_OPEN 0x01
58 #define WX_ATEOF 0x02
59 #define WX_DONTINHERIT 0x10
60 #define WX_APPEND 0x20
61 #define WX_TEXT 0x80
63 /* FIXME: Make this dynamic */
64 #define MSVCRT_MAX_FILES 257
66 static struct {
67 HANDLE handle;
68 unsigned char xflag;
69 } MSVCRT_fdesc[MSVCRT_MAX_FILES];
71 MSVCRT_FILE MSVCRT__iob[3];
73 static int MSVCRT_fdstart = 3; /* first unallocated fd */
74 static int MSVCRT_fdend = 3; /* highest allocated fd */
76 /* FIXME: make this dynamic */
77 static MSVCRT_FILE* MSVCRT_fstreams[1024];
78 static int MSVCRT_stream_idx;
80 /* INTERNAL: process umask */
81 static int MSVCRT_umask = 0;
83 /* INTERNAL: Static buffer for temp file name */
84 static char MSVCRT_tmpname[MAX_PATH];
86 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
87 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
88 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
89 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
91 #define TOUL(x) (ULONGLONG)(x)
92 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
93 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
94 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
95 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
97 extern CRITICAL_SECTION MSVCRT_file_cs;
98 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
99 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
101 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
103 buf->st_dev = bufi64->st_dev;
104 buf->st_ino = bufi64->st_ino;
105 buf->st_mode = bufi64->st_mode;
106 buf->st_nlink = bufi64->st_nlink;
107 buf->st_uid = bufi64->st_uid;
108 buf->st_gid = bufi64->st_gid;
109 buf->st_rdev = bufi64->st_rdev;
110 buf->st_size = bufi64->st_size;
111 buf->st_atime = bufi64->st_atime;
112 buf->st_mtime = bufi64->st_mtime;
113 buf->st_ctime = bufi64->st_ctime;
116 static inline BOOL msvcrt_is_valid_fd(int fd)
118 return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].xflag & WX_OPEN);
121 /* INTERNAL: Get the HANDLE for a fd */
122 static HANDLE msvcrt_fdtoh(int fd)
124 if (!msvcrt_is_valid_fd(fd))
126 WARN(":fd (%d) - no handle!\n",fd);
127 *MSVCRT___doserrno() = 0;
128 *MSVCRT__errno() = MSVCRT_EBADF;
129 return INVALID_HANDLE_VALUE;
131 if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
132 return MSVCRT_fdesc[fd].handle;
135 /* INTERNAL: free a file entry fd */
136 static void msvcrt_free_fd(int fd)
138 MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
139 MSVCRT_fdesc[fd].xflag = 0;
140 TRACE(":fd (%d) freed\n",fd);
141 if (fd < 3) /* don't use 0,1,2 for user files */
143 switch (fd)
145 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
146 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
147 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
150 else
152 if (fd == MSVCRT_fdend - 1)
153 MSVCRT_fdend--;
154 if (fd < MSVCRT_fdstart)
155 MSVCRT_fdstart = fd;
159 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
160 static int msvcrt_alloc_fd(HANDLE hand, int flag)
162 int fd = MSVCRT_fdstart;
164 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
165 if (fd >= MSVCRT_MAX_FILES)
167 WARN(":files exhausted!\n");
168 return -1;
170 MSVCRT_fdesc[fd].handle = hand;
171 MSVCRT_fdesc[fd].xflag = WX_OPEN;
172 if (flag & MSVCRT__O_NOINHERIT) MSVCRT_fdesc[fd].xflag |= WX_DONTINHERIT;
173 if (flag & MSVCRT__O_APPEND) MSVCRT_fdesc[fd].xflag |= WX_APPEND;
174 if (flag & MSVCRT__O_TEXT) MSVCRT_fdesc[fd].xflag |= WX_TEXT;
176 /* locate next free slot */
177 if (fd == MSVCRT_fdend)
178 MSVCRT_fdstart = ++MSVCRT_fdend;
179 else
180 while(MSVCRT_fdstart < MSVCRT_fdend &&
181 MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
182 MSVCRT_fdstart++;
184 switch (fd)
186 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
187 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
188 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
191 return fd;
194 /* INTERNAL: Allocate a FILE* for an fd slot
196 static MSVCRT_FILE* msvcrt_alloc_fp(void)
198 int i;
200 for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
202 if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
204 if (!MSVCRT_fstreams[i])
206 if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
207 return NULL;
208 if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
210 return MSVCRT_fstreams[i];
213 return NULL;
216 /* INTERNAL: initialize a FILE* from an open fd */
217 static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
219 TRACE(":fd (%d) allocating FILE*\n",fd);
220 if (!msvcrt_is_valid_fd(fd))
222 WARN(":invalid fd %d\n",fd);
223 *MSVCRT___doserrno() = 0;
224 *MSVCRT__errno() = MSVCRT_EBADF;
225 return -1;
227 memset(file, 0, sizeof(*file));
228 file->_file = fd;
229 file->_flag = stream_flags;
231 TRACE(":got FILE* (%p)\n",file);
232 return 0;
236 /* INTERNAL: Set up stdin, stderr and stdout */
237 void msvcrt_init_io(void)
239 int i;
241 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
242 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
243 GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
244 MSVCRT_fdesc[0].xflag = WX_OPEN;
245 MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
246 MSVCRT__iob[0]._tmpfname = NULL;
247 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
248 GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
249 MSVCRT_fdesc[1].xflag = WX_OPEN;
250 MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
251 MSVCRT__iob[1]._tmpfname = NULL;
252 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
253 GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
254 MSVCRT_fdesc[2].xflag = WX_OPEN;
255 MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
256 MSVCRT__iob[2]._tmpfname = NULL;
258 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
259 MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
261 for (i = 0; i < 3; i++)
263 /* FILE structs for stdin/out/err are static and never deleted */
264 MSVCRT_fstreams[i] = &MSVCRT__iob[i];
265 MSVCRT__iob[i]._file = i;
267 MSVCRT_stream_idx = 3;
270 /* INTERNAL: Flush stdio file buffer */
271 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
273 if(file->_bufsiz) {
274 int cnt=file->_ptr-file->_base;
275 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
276 file->_flag |= MSVCRT__IOERR;
277 return MSVCRT_EOF;
279 file->_ptr=file->_base;
280 file->_cnt=file->_bufsiz;
282 return 0;
285 /* INTERNAL: Allocate stdio file buffer */
286 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
288 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
289 if(file->_base) {
290 file->_bufsiz = MSVCRT_BUFSIZ;
291 file->_flag |= MSVCRT__IOMYBUF;
292 } else {
293 file->_base = (unsigned char *)(&file->_charbuf);
294 /* put here 2 ??? */
295 file->_bufsiz = sizeof(file->_charbuf);
297 file->_ptr = file->_base;
298 file->_cnt = 0;
301 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
302 static void msvcrt_int_to_base32(int num, char *str)
304 char *p;
305 int n = num;
306 int digits = 0;
308 while (n != 0)
310 n >>= 5;
311 digits++;
313 p = str + digits;
314 *p = 0;
315 while (--p >= str)
317 *p = (num & 31) + '0';
318 if (*p > '9')
319 *p += ('a' - '0' - 10);
320 num >>= 5;
324 /*********************************************************************
325 * __p__iob(MSVCRT.@)
327 MSVCRT_FILE *__p__iob(void)
329 return &MSVCRT__iob[0];
332 /*********************************************************************
333 * _access (MSVCRT.@)
335 int _access(const char *filename, int mode)
337 DWORD attr = GetFileAttributesA(filename);
339 TRACE("(%s,%d) %ld\n",filename,mode,attr);
341 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
343 msvcrt_set_errno(GetLastError());
344 return -1;
346 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
348 msvcrt_set_errno(ERROR_ACCESS_DENIED);
349 return -1;
351 return 0;
354 /*********************************************************************
355 * _waccess (MSVCRT.@)
357 int _waccess(const MSVCRT_wchar_t *filename, int mode)
359 DWORD attr = GetFileAttributesW(filename);
361 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
363 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
365 msvcrt_set_errno(GetLastError());
366 return -1;
368 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
370 msvcrt_set_errno(ERROR_ACCESS_DENIED);
371 return -1;
373 return 0;
376 /*********************************************************************
377 * _chmod (MSVCRT.@)
379 int _chmod(const char *path, int flags)
381 DWORD oldFlags = GetFileAttributesA(path);
383 if (oldFlags != INVALID_FILE_ATTRIBUTES)
385 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
386 oldFlags | FILE_ATTRIBUTE_READONLY;
388 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
389 return 0;
391 msvcrt_set_errno(GetLastError());
392 return -1;
395 /*********************************************************************
396 * _wchmod (MSVCRT.@)
398 int _wchmod(const MSVCRT_wchar_t *path, int flags)
400 DWORD oldFlags = GetFileAttributesW(path);
402 if (oldFlags != INVALID_FILE_ATTRIBUTES)
404 DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
405 oldFlags | FILE_ATTRIBUTE_READONLY;
407 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
408 return 0;
410 msvcrt_set_errno(GetLastError());
411 return -1;
414 /*********************************************************************
415 * _chsize (MSVCRT.@)
417 int _chsize(int fd, long size)
419 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
420 return -1;
423 /*********************************************************************
424 * _dup (MSVCRT.@)
426 int _dup(int od)
428 FIXME("(od=%d): stub\n", od);
429 return -1;
432 /*********************************************************************
433 * _dup2 (MSVCRT.@)
435 int _dup2(int od, int nd)
437 FIXME("(od=%d, nd=%d): stub\n", od, nd);
438 return -1;
441 /*********************************************************************
442 * _unlink (MSVCRT.@)
444 int _unlink(const char *path)
446 TRACE("(%s)\n",path);
447 if(DeleteFileA(path))
448 return 0;
449 TRACE("failed (%ld)\n",GetLastError());
450 msvcrt_set_errno(GetLastError());
451 return -1;
454 /*********************************************************************
455 * _wunlink (MSVCRT.@)
457 int _wunlink(const MSVCRT_wchar_t *path)
459 TRACE("(%s)\n",debugstr_w(path));
460 if(DeleteFileW(path))
461 return 0;
462 TRACE("failed (%ld)\n",GetLastError());
463 msvcrt_set_errno(GetLastError());
464 return -1;
467 /* _flushall calls MSVCRT_fflush which calls _flushall */
468 int MSVCRT_fflush(MSVCRT_FILE* file);
470 /*********************************************************************
471 * _flushall (MSVCRT.@)
473 int _flushall(void)
475 int i, num_flushed = 0;
477 for (i = 3; i < MSVCRT_stream_idx; i++)
478 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
480 #if 0
481 /* FIXME: flush, do not commit */
482 if (_commit(i) == -1)
483 if (MSVCRT_fstreams[i])
484 MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
485 #endif
486 if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
487 MSVCRT_fflush(MSVCRT_fstreams[i]);
488 num_flushed++;
492 TRACE(":flushed (%d) handles\n",num_flushed);
493 return num_flushed;
496 /*********************************************************************
497 * fflush (MSVCRT.@)
499 int MSVCRT_fflush(MSVCRT_FILE* file)
501 if(!file) {
502 _flushall();
503 return 0;
504 } else {
505 int res=msvcrt_flush_buffer(file);
506 return res;
510 /*********************************************************************
511 * _close (MSVCRT.@)
513 int _close(int fd)
515 HANDLE hand = msvcrt_fdtoh(fd);
517 TRACE(":fd (%d) handle (%p)\n",fd,hand);
518 if (hand == INVALID_HANDLE_VALUE)
519 return -1;
521 if (!CloseHandle(hand))
523 WARN(":failed-last error (%ld)\n",GetLastError());
524 msvcrt_set_errno(GetLastError());
525 return -1;
527 msvcrt_free_fd(fd);
528 TRACE(":ok\n");
529 return 0;
532 /*********************************************************************
533 * _commit (MSVCRT.@)
535 int _commit(int fd)
537 HANDLE hand = msvcrt_fdtoh(fd);
539 TRACE(":fd (%d) handle (%p)\n",fd,hand);
540 if (hand == INVALID_HANDLE_VALUE)
541 return -1;
543 if (!FlushFileBuffers(hand))
545 if (GetLastError() == ERROR_INVALID_HANDLE)
547 /* FlushFileBuffers fails for console handles
548 * so we ignore this error.
550 return 0;
552 TRACE(":failed-last error (%ld)\n",GetLastError());
553 msvcrt_set_errno(GetLastError());
554 return -1;
556 TRACE(":ok\n");
557 return 0;
560 /*********************************************************************
561 * _eof (MSVCRT.@)
563 int _eof(int fd)
565 DWORD curpos,endpos,hcurpos,hendpos;
566 HANDLE hand = msvcrt_fdtoh(fd);
568 TRACE(":fd (%d) handle (%p)\n",fd,hand);
570 if (hand == INVALID_HANDLE_VALUE)
571 return -1;
573 if (MSVCRT_fdesc[fd].xflag & WX_ATEOF) return TRUE;
575 /* Otherwise we do it the hard way */
576 hcurpos = hendpos = 0;
577 curpos = SetFilePointer(hand, 0, &hcurpos, SEEK_CUR);
578 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
580 if (curpos == endpos && hcurpos == hendpos)
581 return TRUE;
583 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
584 return FALSE;
587 /*********************************************************************
588 * _fcloseall (MSVCRT.@)
590 int MSVCRT__fcloseall(void)
592 int num_closed = 0, i;
594 for (i = 3; i < MSVCRT_stream_idx; i++)
595 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
596 MSVCRT_fclose(MSVCRT_fstreams[i]))
597 num_closed++;
599 TRACE(":closed (%d) handles\n",num_closed);
600 return num_closed;
603 /* free everything on process exit */
604 void msvcrt_free_io(void)
606 MSVCRT__fcloseall();
607 _close(0);
608 _close(1);
609 _close(2);
612 /*********************************************************************
613 * _lseeki64 (MSVCRT.@)
615 __int64 _lseeki64(int fd, __int64 offset, int whence)
617 DWORD ret, hoffset = (DWORD) (offset >> 32);
618 HANDLE hand = msvcrt_fdtoh(fd);
620 TRACE(":fd (%d) handle (%p)\n",fd,hand);
621 if (hand == INVALID_HANDLE_VALUE)
622 return -1;
624 if (whence < 0 || whence > 2)
626 *MSVCRT__errno() = MSVCRT_EINVAL;
627 return -1;
630 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
631 fd,hoffset,(long)offset,
632 (whence==SEEK_SET)?"SEEK_SET":
633 (whence==SEEK_CUR)?"SEEK_CUR":
634 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
636 ret = SetFilePointer(hand, (long)offset, &hoffset, whence);
637 if (ret != INVALID_SET_FILE_POINTER || !GetLastError())
639 MSVCRT_fdesc[fd].xflag &= ~WX_ATEOF;
640 /* FIXME: What if we seek _to_ EOF - is EOF set? */
642 return ((__int64)hoffset << 32) | ret;
644 TRACE(":error-last error (%ld)\n",GetLastError());
645 msvcrt_set_errno(GetLastError());
646 return -1;
649 /*********************************************************************
650 * _lseek (MSVCRT.@)
652 LONG _lseek(int fd, LONG offset, int whence)
654 return _lseeki64(fd, offset, whence);
657 /*********************************************************************
658 * _locking (MSVCRT.@)
660 * This is untested; the underlying LockFile doesn't work yet.
662 int _locking(int fd, int mode, LONG nbytes)
664 BOOL ret;
665 DWORD cur_locn;
666 HANDLE hand = msvcrt_fdtoh(fd);
668 TRACE(":fd (%d) handle (%p)\n",fd,hand);
669 if (hand == INVALID_HANDLE_VALUE)
670 return -1;
672 if (mode < 0 || mode > 4)
674 *MSVCRT__errno() = MSVCRT_EINVAL;
675 return -1;
678 TRACE(":fd (%d) by 0x%08lx mode %s\n",
679 fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
680 (mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
681 (mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
682 (mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
683 (mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
684 "UNKNOWN");
686 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
688 FIXME ("Seek failed\n");
689 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
690 return -1;
692 if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
694 int nretry = 10;
695 ret = 1; /* just to satisfy gcc */
696 while (nretry--)
698 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
699 if (ret) break;
700 Sleep(1);
703 else if (mode == MSVCRT__LK_UNLCK)
704 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
705 else
706 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
707 /* FIXME - what about error settings? */
708 return ret ? 0 : -1;
711 /*********************************************************************
712 * fseek (MSVCRT.@)
714 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
716 /* Flush output if needed */
717 if(file->_flag & MSVCRT__IOWRT)
718 msvcrt_flush_buffer(file);
720 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
721 offset -= file->_cnt;
723 /* Discard buffered input */
724 file->_cnt = 0;
725 file->_ptr = file->_base;
726 /* Reset direction of i/o */
727 if(file->_flag & MSVCRT__IORW) {
728 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
730 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
733 /*********************************************************************
734 * clearerr (MSVCRT.@)
736 void MSVCRT_clearerr(MSVCRT_FILE* file)
738 TRACE(":file (%p) fd (%d)\n",file,file->_file);
739 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
742 /*********************************************************************
743 * rewind (MSVCRT.@)
745 void MSVCRT_rewind(MSVCRT_FILE* file)
747 TRACE(":file (%p) fd (%d)\n",file,file->_file);
748 MSVCRT_fseek(file, 0L, SEEK_SET);
749 MSVCRT_clearerr(file);
752 static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
754 int plus = strchr(mode, '+') != NULL;
756 switch(*mode++)
758 case 'R': case 'r':
759 *open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
760 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
761 break;
762 case 'W': case 'w':
763 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
764 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
765 break;
766 case 'A': case 'a':
767 *open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
768 *stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
769 break;
770 default:
771 return -1;
774 while (*mode)
775 switch (*mode++)
777 case 'B': case 'b':
778 *open_flags |= MSVCRT__O_BINARY;
779 *open_flags &= ~MSVCRT__O_TEXT;
780 break;
781 case 'T': case 't':
782 *open_flags |= MSVCRT__O_TEXT;
783 *open_flags &= ~MSVCRT__O_BINARY;
784 break;
785 case '+':
786 break;
787 default:
788 FIXME(":unknown flag %c not supported\n",mode[-1]);
790 return 0;
793 /*********************************************************************
794 * _fdopen (MSVCRT.@)
796 MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
798 int open_flags, stream_flags;
799 MSVCRT_FILE* file;
801 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
803 if (!(file = msvcrt_alloc_fp())) return NULL;
804 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
806 file->_flag = 0;
807 file = NULL;
809 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
811 return file;
814 /*********************************************************************
815 * _wfdopen (MSVCRT.@)
817 MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
819 unsigned mlen = strlenW(mode);
820 char *modea = MSVCRT_calloc(mlen + 1, 1);
821 MSVCRT_FILE* file = NULL;
822 int open_flags, stream_flags;
824 if (modea &&
825 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
827 if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
828 if (!(file = msvcrt_alloc_fp())) return NULL;
829 if (msvcrt_init_fp(file, fd, stream_flags) == -1)
831 file->_flag = 0;
832 file = NULL;
834 else
836 if (file)
837 MSVCRT_rewind(file); /* FIXME: is this needed ??? */
838 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
841 return file;
844 /*********************************************************************
845 * _filelength (MSVCRT.@)
847 LONG _filelength(int fd)
849 LONG curPos = _lseek(fd, 0, SEEK_CUR);
850 if (curPos != -1)
852 LONG endPos = _lseek(fd, 0, SEEK_END);
853 if (endPos != -1)
855 if (endPos != curPos)
856 _lseek(fd, curPos, SEEK_SET);
857 return endPos;
860 return -1;
863 /*********************************************************************
864 * _filelengthi64 (MSVCRT.@)
866 __int64 _filelengthi64(int fd)
868 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
869 if (curPos != -1)
871 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
872 if (endPos != -1)
874 if (endPos != curPos)
875 _lseeki64(fd, curPos, SEEK_SET);
876 return endPos;
879 return -1;
882 /*********************************************************************
883 * _fileno (MSVCRT.@)
885 int MSVCRT__fileno(MSVCRT_FILE* file)
887 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
888 return file->_file;
891 /*********************************************************************
892 * _fstati64 (MSVCRT.@)
894 int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* buf)
896 DWORD dw;
897 BY_HANDLE_FILE_INFORMATION hfi;
898 HANDLE hand = msvcrt_fdtoh(fd);
900 TRACE(":fd (%d) stat (%p)\n",fd,buf);
901 if (hand == INVALID_HANDLE_VALUE)
902 return -1;
904 if (!buf)
906 WARN(":failed-NULL buf\n");
907 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
908 return -1;
911 memset(&hfi, 0, sizeof(hfi));
912 memset(buf, 0, sizeof(struct MSVCRT__stati64));
913 if (!GetFileInformationByHandle(hand, &hfi))
915 WARN(":failed-last error (%ld)\n",GetLastError());
916 msvcrt_set_errno(ERROR_INVALID_PARAMETER);
917 return -1;
919 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
920 buf->st_nlink = hfi.nNumberOfLinks;
921 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
922 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
923 buf->st_atime = dw;
924 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
925 buf->st_mtime = buf->st_ctime = dw;
926 return 0;
929 /*********************************************************************
930 * _fstat (MSVCRT.@)
932 int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
933 { int ret;
934 struct MSVCRT__stati64 bufi64;
936 ret = MSVCRT__fstati64(fd, &bufi64);
937 if (!ret)
938 msvcrt_cp_from_stati64(&bufi64, buf);
939 return ret;
942 /*********************************************************************
943 * _futime (MSVCRT.@)
945 int _futime(int fd, struct MSVCRT__utimbuf *t)
947 HANDLE hand = msvcrt_fdtoh(fd);
948 FILETIME at, wt;
950 if (!t)
952 MSVCRT_time_t currTime;
953 MSVCRT_time(&currTime);
954 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
955 memcpy(&wt, &at, sizeof(wt));
957 else
959 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
960 if (t->actime == t->modtime)
961 memcpy(&wt, &at, sizeof(wt));
962 else
963 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
966 if (!SetFileTime(hand, NULL, &at, &wt))
968 msvcrt_set_errno(GetLastError());
969 return -1 ;
971 return 0;
974 /*********************************************************************
975 * _get_osfhandle (MSVCRT.@)
977 long _get_osfhandle(int fd)
979 HANDLE hand = msvcrt_fdtoh(fd);
980 HANDLE newhand = hand;
981 TRACE(":fd (%d) handle (%p)\n",fd,hand);
983 if (hand != INVALID_HANDLE_VALUE)
985 /* FIXME: I'm not convinced that I should be copying the
986 * handle here - it may be leaked if the app doesn't
987 * close it (and the API docs don't say that it should)
988 * Not duplicating it means that it can't be inherited
989 * and so lcc's wedit doesn't cope when it passes it to
990 * child processes. I've an idea that it should either
991 * be copied by CreateProcess, or marked as inheritable
992 * when initialised, or maybe both? JG 21-9-00.
994 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
995 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
997 return (long)newhand;
1000 /*********************************************************************
1001 * _isatty (MSVCRT.@)
1003 int _isatty(int fd)
1005 HANDLE hand = msvcrt_fdtoh(fd);
1007 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1008 if (hand == INVALID_HANDLE_VALUE)
1009 return 0;
1011 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1014 /*********************************************************************
1015 * _mktemp (MSVCRT.@)
1017 char *_mktemp(char *pattern)
1019 int numX = 0;
1020 char *retVal = pattern;
1021 int id;
1022 char letter = 'a';
1024 while(*pattern)
1025 numX = (*pattern++ == 'X')? numX + 1 : 0;
1026 if (numX < 5)
1027 return NULL;
1028 pattern--;
1029 id = GetCurrentProcessId();
1030 numX = 6;
1031 while(numX--)
1033 int tempNum = id / 10;
1034 *pattern-- = id - (tempNum * 10) + '0';
1035 id = tempNum;
1037 pattern++;
1040 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1041 GetLastError() == ERROR_FILE_NOT_FOUND)
1042 return retVal;
1043 *pattern = letter++;
1044 } while(letter != '|');
1045 return NULL;
1048 /*********************************************************************
1049 * _wmktemp (MSVCRT.@)
1051 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
1053 int numX = 0;
1054 MSVCRT_wchar_t *retVal = pattern;
1055 int id;
1056 MSVCRT_wchar_t letter = 'a';
1058 while(*pattern)
1059 numX = (*pattern++ == 'X')? numX + 1 : 0;
1060 if (numX < 5)
1061 return NULL;
1062 pattern--;
1063 id = GetCurrentProcessId();
1064 numX = 6;
1065 while(numX--)
1067 int tempNum = id / 10;
1068 *pattern-- = id - (tempNum * 10) + '0';
1069 id = tempNum;
1071 pattern++;
1074 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1075 GetLastError() == ERROR_FILE_NOT_FOUND)
1076 return retVal;
1077 *pattern = letter++;
1078 } while(letter != '|');
1079 return NULL;
1082 /*********************************************************************
1083 * _sopen (MSVCRT.@)
1085 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
1087 va_list ap;
1088 int pmode;
1089 DWORD access = 0, creation = 0, attrib;
1090 DWORD sharing;
1091 int ioflag = 0, fd;
1092 HANDLE hand;
1093 SECURITY_ATTRIBUTES sa;
1096 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1097 path, oflags, shflags);
1099 switch(oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
1101 case MSVCRT__O_RDONLY:
1102 access |= GENERIC_READ;
1103 ioflag |= MSVCRT__IOREAD;
1104 break;
1105 case MSVCRT__O_WRONLY:
1106 access |= GENERIC_WRITE;
1107 ioflag |= MSVCRT__IOWRT;
1108 break;
1109 case MSVCRT__O_RDWR:
1110 access |= GENERIC_WRITE | GENERIC_READ;
1111 ioflag |= MSVCRT__IORW;
1112 break;
1115 if (oflags & MSVCRT__O_CREAT)
1117 va_start(ap, shflags);
1118 pmode = va_arg(ap, int);
1119 va_end(ap);
1121 if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
1122 FIXME(": pmode 0x%04x ignored\n", pmode);
1123 else
1124 WARN(": pmode 0x%04x ignored\n", pmode);
1126 if (oflags & MSVCRT__O_EXCL)
1127 creation = CREATE_NEW;
1128 else if (oflags & MSVCRT__O_TRUNC)
1129 creation = CREATE_ALWAYS;
1130 else
1131 creation = OPEN_ALWAYS;
1133 else /* no MSVCRT__O_CREAT */
1135 if (oflags & MSVCRT__O_TRUNC)
1136 creation = TRUNCATE_EXISTING;
1137 else
1138 creation = OPEN_EXISTING;
1140 if (oflags & MSVCRT__O_APPEND)
1141 ioflag |= MSVCRT__O_APPEND;
1143 if (oflags & MSVCRT__O_BINARY)
1144 ioflag |= MSVCRT__O_BINARY;
1145 else if (oflags & MSVCRT__O_TEXT)
1146 ioflag |= MSVCRT__O_TEXT;
1147 else if (*__p__fmode() & MSVCRT__O_BINARY)
1148 ioflag |= MSVCRT__O_BINARY;
1149 else
1150 ioflag |= MSVCRT__O_TEXT; /* default to TEXT*/
1152 switch( shflags )
1154 case MSVCRT__SH_DENYRW:
1155 sharing = 0L;
1156 break;
1157 case MSVCRT__SH_DENYWR:
1158 sharing = FILE_SHARE_READ;
1159 break;
1160 case MSVCRT__SH_DENYRD:
1161 sharing = FILE_SHARE_WRITE;
1162 break;
1163 case MSVCRT__SH_DENYNO:
1164 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1165 break;
1166 default:
1167 ERR( "Unhandled shflags 0x%x\n", shflags );
1168 return -1;
1170 attrib = FILE_ATTRIBUTE_NORMAL;
1172 if (oflags & MSVCRT__O_TEMPORARY)
1174 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1175 access |= DELETE;
1176 sharing |= FILE_SHARE_DELETE;
1179 if (oflags & ~(MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|MSVCRT__O_TRUNC|MSVCRT__O_EXCL
1180 |MSVCRT__O_CREAT|MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|MSVCRT__O_NOINHERIT))
1181 ERR(":unsupported oflags 0x%04x\n",oflags);
1183 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1184 sa.lpSecurityDescriptor = NULL;
1185 sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
1187 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1189 if (hand == INVALID_HANDLE_VALUE) {
1190 WARN(":failed-last error (%ld)\n",GetLastError());
1191 msvcrt_set_errno(GetLastError());
1192 return -1;
1195 fd = msvcrt_alloc_fd(hand, ioflag);
1197 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1199 if (fd > 0)
1201 if (oflags & MSVCRT__O_APPEND)
1202 _lseek(fd, 0, FILE_END);
1205 return fd;
1208 /*********************************************************************
1209 * _wsopen (MSVCRT.@)
1211 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1213 const unsigned int len = strlenW(path);
1214 char *patha = MSVCRT_calloc(len + 1,1);
1215 va_list ap;
1216 int pmode;
1218 va_start(ap, shflags);
1219 pmode = va_arg(ap, int);
1220 va_end(ap);
1222 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1224 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1225 MSVCRT_free(patha);
1226 return retval;
1229 msvcrt_set_errno(GetLastError());
1230 return -1;
1233 /*********************************************************************
1234 * _open (MSVCRT.@)
1236 int _open( const char *path, int flags, ... )
1238 va_list ap;
1240 if (flags & MSVCRT__O_CREAT)
1242 int pmode;
1243 va_start(ap, flags);
1244 pmode = va_arg(ap, int);
1245 va_end(ap);
1246 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
1248 else
1249 return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
1252 /*********************************************************************
1253 * _wopen (MSVCRT.@)
1255 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1257 const unsigned int len = strlenW(path);
1258 char *patha = MSVCRT_calloc(len + 1,1);
1259 va_list ap;
1260 int pmode;
1262 va_start(ap, flags);
1263 pmode = va_arg(ap, int);
1264 va_end(ap);
1266 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1268 int retval = _open(patha,flags,pmode);
1269 MSVCRT_free(patha);
1270 return retval;
1273 msvcrt_set_errno(GetLastError());
1274 return -1;
1277 /*********************************************************************
1278 * _creat (MSVCRT.@)
1280 int _creat(const char *path, int flags)
1282 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1283 return _open(path, usedFlags);
1286 /*********************************************************************
1287 * _wcreat (MSVCRT.@)
1289 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1291 int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__O_TRUNC;
1292 return _wopen(path, usedFlags);
1295 /*********************************************************************
1296 * _open_osfhandle (MSVCRT.@)
1298 int _open_osfhandle(long hand, int flags)
1300 int fd;
1302 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1303 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1304 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1305 * text - it never sets MSVCRT__O_BINARY.
1307 /* FIXME: handle more flags */
1308 if (!(flags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
1309 flags |= MSVCRT__O_BINARY;
1310 else
1311 flags |= MSVCRT__O_TEXT;
1313 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1314 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd,flags);
1315 return fd;
1318 /*********************************************************************
1319 * _rmtmp (MSVCRT.@)
1321 int _rmtmp(void)
1323 int num_removed = 0, i;
1325 for (i = 3; i < MSVCRT_stream_idx; i++)
1326 if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
1328 MSVCRT_fclose(MSVCRT_fstreams[i]);
1329 num_removed++;
1332 if (num_removed)
1333 TRACE(":removed (%d) temp files\n",num_removed);
1334 return num_removed;
1337 /*********************************************************************
1338 * (internal) remove_cr
1340 * Remove all \r inplace.
1341 * return the number of \r removed
1343 static unsigned int remove_cr(char *buf, unsigned int count)
1345 unsigned int i, j;
1347 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1348 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1349 return count - i;
1352 /*********************************************************************
1353 * _read (MSVCRT.@)
1355 int _read(int fd, void *buf, unsigned int count)
1357 DWORD num_read, all_read = 0;
1358 char *bufstart = buf;
1359 HANDLE hand = msvcrt_fdtoh(fd);
1361 /* Don't trace small reads, it gets *very* annoying */
1362 if (count > 4)
1363 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1364 if (hand == INVALID_HANDLE_VALUE)
1365 return -1;
1367 /* Reading single bytes in O_TEXT mode makes things slow
1368 * So read big chunks, then remove the \r in memory and try reading
1369 * the rest until the request is satisfied or EOF is met
1371 while (all_read < count)
1373 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1375 if (num_read != (count - all_read))
1377 TRACE(":EOF\n");
1378 MSVCRT_fdesc[fd].xflag |= WX_ATEOF;
1379 if (MSVCRT_fdesc[fd].xflag & WX_TEXT)
1380 num_read -= remove_cr(bufstart+all_read,num_read);
1381 all_read += num_read;
1382 if (count > 4)
1383 TRACE("%s\n",debugstr_an(buf,all_read));
1384 return all_read;
1386 if (MSVCRT_fdesc[fd].xflag & WX_TEXT)
1388 num_read -= remove_cr(bufstart+all_read,num_read);
1390 all_read += num_read;
1392 else
1394 TRACE(":failed-last error (%ld)\n",GetLastError());
1395 return -1;
1399 if (count > 4)
1400 TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
1401 return all_read;
1404 /*********************************************************************
1405 * _getw (MSVCRT.@)
1407 int MSVCRT__getw(MSVCRT_FILE* file)
1409 int i;
1410 switch (_read(file->_file, &i, sizeof(int)))
1412 case 1: return i;
1413 case 0: file->_flag |= MSVCRT__IOEOF; break;
1414 default: file->_flag |= MSVCRT__IOERR; break;
1416 return EOF;
1419 /*********************************************************************
1420 * _setmode (MSVCRT.@)
1422 int _setmode(int fd,int mode)
1424 int ret = MSVCRT_fdesc[fd].xflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
1425 if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
1426 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1427 if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
1428 MSVCRT_fdesc[fd].xflag |= WX_TEXT;
1429 else
1430 MSVCRT_fdesc[fd].xflag &= ~WX_TEXT;
1431 return ret;
1434 /*********************************************************************
1435 * _stati64 (MSVCRT.@)
1437 int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
1439 DWORD dw;
1440 WIN32_FILE_ATTRIBUTE_DATA hfi;
1441 unsigned short mode = ALL_S_IREAD;
1442 int plen;
1444 TRACE(":file (%s) buf(%p)\n",path,buf);
1446 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1448 TRACE("failed (%ld)\n",GetLastError());
1449 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1450 return -1;
1453 memset(buf,0,sizeof(struct MSVCRT__stati64));
1455 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1456 Bon 011120: This FIXME seems incorrect
1457 Also a letter as first char isn't enough to be classified
1458 as a drive letter
1460 if (isalpha(*path)&& (*(path+1)==':'))
1461 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1462 else
1463 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1465 plen = strlen(path);
1467 /* Dir, or regular file? */
1468 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1469 (path[plen-1] == '\\'))
1470 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1471 else
1473 mode |= MSVCRT__S_IFREG;
1474 /* executable? */
1475 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1477 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1478 (tolower(path[plen-3]) << 16);
1479 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1480 mode |= ALL_S_IEXEC;
1484 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1485 mode |= ALL_S_IWRITE;
1487 buf->st_mode = mode;
1488 buf->st_nlink = 1;
1489 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1490 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1491 buf->st_atime = dw;
1492 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1493 buf->st_mtime = buf->st_ctime = dw;
1494 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1495 (long)(buf->st_size >> 32),(long)buf->st_size,
1496 buf->st_atime,buf->st_mtime, buf->st_ctime);
1497 return 0;
1500 /*********************************************************************
1501 * _stat (MSVCRT.@)
1503 int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
1504 { int ret;
1505 struct MSVCRT__stati64 bufi64;
1507 ret = MSVCRT__stati64( path, &bufi64);
1508 if (!ret)
1509 msvcrt_cp_from_stati64(&bufi64, buf);
1510 return ret;
1513 /*********************************************************************
1514 * _wstati64 (MSVCRT.@)
1516 int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
1518 DWORD dw;
1519 WIN32_FILE_ATTRIBUTE_DATA hfi;
1520 unsigned short mode = ALL_S_IREAD;
1521 int plen;
1523 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1525 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1527 TRACE("failed (%ld)\n",GetLastError());
1528 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1529 return -1;
1532 memset(buf,0,sizeof(struct MSVCRT__stat));
1534 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1535 if (MSVCRT_iswalpha(*path))
1536 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1537 else
1538 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1540 plen = strlenW(path);
1542 /* Dir, or regular file? */
1543 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1544 (path[plen-1] == '\\'))
1545 mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
1546 else
1548 mode |= MSVCRT__S_IFREG;
1549 /* executable? */
1550 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1552 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1553 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1554 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1555 mode |= ALL_S_IEXEC;
1559 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1560 mode |= ALL_S_IWRITE;
1562 buf->st_mode = mode;
1563 buf->st_nlink = 1;
1564 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1565 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1566 buf->st_atime = dw;
1567 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1568 buf->st_mtime = buf->st_ctime = dw;
1569 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1570 (long)(buf->st_size >> 32),(long)buf->st_size,
1571 buf->st_atime,buf->st_mtime, buf->st_ctime);
1572 return 0;
1575 /*********************************************************************
1576 * _wstat (MSVCRT.@)
1578 int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
1580 int ret;
1581 struct MSVCRT__stati64 bufi64;
1583 ret = MSVCRT__wstati64( path, &bufi64 );
1584 if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
1585 return ret;
1588 /*********************************************************************
1589 * _tell (MSVCRT.@)
1591 long _tell(int fd)
1593 return _lseek(fd, 0, SEEK_CUR);
1596 /*********************************************************************
1597 * _telli64 (MSVCRT.@)
1599 __int64 _telli64(int fd)
1601 return _lseeki64(fd, 0, SEEK_CUR);
1604 /*********************************************************************
1605 * _tempnam (MSVCRT.@)
1607 char *_tempnam(const char *dir, const char *prefix)
1609 char tmpbuf[MAX_PATH];
1611 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1612 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1614 TRACE("got name (%s)\n",tmpbuf);
1615 DeleteFileA(tmpbuf);
1616 return _strdup(tmpbuf);
1618 TRACE("failed (%ld)\n",GetLastError());
1619 return NULL;
1622 /*********************************************************************
1623 * _wtempnam (MSVCRT.@)
1625 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1627 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1629 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1630 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1632 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1633 DeleteFileW(tmpbuf);
1634 return _wcsdup(tmpbuf);
1636 TRACE("failed (%ld)\n",GetLastError());
1637 return NULL;
1640 /*********************************************************************
1641 * _umask (MSVCRT.@)
1643 int _umask(int umask)
1645 int old_umask = MSVCRT_umask;
1646 TRACE("(%d)\n",umask);
1647 MSVCRT_umask = umask;
1648 return old_umask;
1651 /*********************************************************************
1652 * _utime (MSVCRT.@)
1654 int _utime(const char* path, struct MSVCRT__utimbuf *t)
1656 int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1658 if (fd > 0)
1660 int retVal = _futime(fd, t);
1661 _close(fd);
1662 return retVal;
1664 return -1;
1667 /*********************************************************************
1668 * _wutime (MSVCRT.@)
1670 int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
1672 int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__O_BINARY);
1674 if (fd > 0)
1676 int retVal = _futime(fd, t);
1677 _close(fd);
1678 return retVal;
1680 return -1;
1683 /*********************************************************************
1684 * _write (MSVCRT.@)
1686 int _write(int fd, const void* buf, unsigned int count)
1688 DWORD num_written;
1689 HANDLE hand = msvcrt_fdtoh(fd);
1691 /* Don't trace small writes, it gets *very* annoying */
1692 #if 0
1693 if (count > 32)
1694 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1695 #endif
1696 if (hand == INVALID_HANDLE_VALUE)
1698 *MSVCRT__errno() = MSVCRT_EBADF;
1699 return -1;
1702 /* If appending, go to EOF */
1703 if (MSVCRT_fdesc[fd].xflag & WX_APPEND)
1704 _lseek(fd, 0, FILE_END);
1706 if (!(MSVCRT_fdesc[fd].xflag & WX_TEXT))
1708 if (WriteFile(hand, buf, count, &num_written, NULL)
1709 && (num_written == count))
1710 return num_written;
1711 TRACE(":failed-last error (%ld)\n",GetLastError());
1712 *MSVCRT__errno() = MSVCRT_ENOSPC;
1714 else
1716 unsigned int i, j, nr_lf;
1717 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1718 /* find number of \n ( without preceeding \r */
1719 for ( nr_lf=0,i = 0; i <count; i++)
1721 if (s[i]== '\n')
1723 nr_lf++;
1724 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1727 if (nr_lf)
1729 if ((p = MSVCRT_malloc(count + nr_lf)))
1731 for(s=(char*)buf, i=0, j=0; i<count; i++)
1733 if (s[i]== '\n')
1735 p[j++] = '\r';
1736 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1738 p[j++] = s[i];
1741 else
1743 FIXME("Malloc failed\n");
1744 nr_lf =0;
1745 p = (char*)buf;
1748 else
1749 p = (char*)buf;
1751 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1753 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1754 *MSVCRT__errno() = MSVCRT_ENOSPC;
1755 if(nr_lf)
1756 MSVCRT_free(p);
1757 return s - buf_start;
1759 else
1761 if(nr_lf)
1762 MSVCRT_free(p);
1763 return count;
1766 return -1;
1769 /*********************************************************************
1770 * _putw (MSVCRT.@)
1772 int MSVCRT__putw(int val, MSVCRT_FILE* file)
1774 int len;
1775 len = _write(file->_file, &val, sizeof(val));
1776 if (len == sizeof(val)) return val;
1777 file->_flag |= MSVCRT__IOERR;
1778 return MSVCRT_EOF;
1781 /*********************************************************************
1782 * fclose (MSVCRT.@)
1784 int MSVCRT_fclose(MSVCRT_FILE* file)
1786 int r, flag;
1788 flag = file->_flag;
1789 if (file->_tmpfname)
1791 MSVCRT_free(file->_tmpfname);
1792 file->_tmpfname = NULL;
1794 /* flush stdio buffers */
1795 if(file->_flag & MSVCRT__IOWRT)
1796 MSVCRT_fflush(file);
1797 if(file->_flag & MSVCRT__IOMYBUF)
1798 MSVCRT_free(file->_base);
1800 r=_close(file->_file);
1802 file->_flag = 0;
1804 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1807 /*********************************************************************
1808 * feof (MSVCRT.@)
1810 int MSVCRT_feof(MSVCRT_FILE* file)
1812 return file->_flag & MSVCRT__IOEOF;
1815 /*********************************************************************
1816 * ferror (MSVCRT.@)
1818 int MSVCRT_ferror(MSVCRT_FILE* file)
1820 return file->_flag & MSVCRT__IOERR;
1823 /*********************************************************************
1824 * _filbuf (MSVCRT.@)
1826 int MSVCRT__filbuf(MSVCRT_FILE* file)
1828 /* Allocate buffer if needed */
1829 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1830 msvcrt_alloc_buffer(file);
1832 if(!(file->_flag & MSVCRT__IOREAD)) {
1833 if(file->_flag & MSVCRT__IORW) {
1834 file->_flag |= MSVCRT__IOREAD;
1835 } else {
1836 return MSVCRT_EOF;
1839 if(file->_flag & MSVCRT__IONBF) {
1840 unsigned char c;
1841 int r;
1842 if ((r = _read(file->_file,&c,1)) != 1) {
1843 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1844 return MSVCRT_EOF;
1846 return c;
1847 } else {
1848 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1849 if(file->_cnt<=0) {
1850 file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1851 file->_cnt = 0;
1852 return MSVCRT_EOF;
1854 file->_cnt--;
1855 file->_ptr = file->_base+1;
1856 return *(unsigned char *)file->_base;
1860 /*********************************************************************
1861 * fgetc (MSVCRT.@)
1863 int MSVCRT_fgetc(MSVCRT_FILE* file)
1865 if (file->_cnt>0) {
1866 file->_cnt--;
1867 return *(unsigned char *)file->_ptr++;
1868 } else {
1869 return MSVCRT__filbuf(file);
1873 /*********************************************************************
1874 * _fgetchar (MSVCRT.@)
1876 int _fgetchar(void)
1878 return MSVCRT_fgetc(MSVCRT_stdin);
1881 /*********************************************************************
1882 * fgets (MSVCRT.@)
1884 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1886 int cc = MSVCRT_EOF;
1887 char * buf_start = s;
1889 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1890 file,file->_file,s,size);
1892 while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
1894 *s++ = (char)cc;
1895 size --;
1897 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1899 TRACE(":nothing read\n");
1900 return NULL;
1902 if ((cc != MSVCRT_EOF) && (size > 1))
1903 *s++ = cc;
1904 *s = '\0';
1905 TRACE(":got '%s'\n", debugstr_a(buf_start));
1906 return buf_start;
1909 /*********************************************************************
1910 * fgetwc (MSVCRT.@)
1912 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
1913 * the CR from CR/LF combinations
1915 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1917 char c;
1919 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
1921 MSVCRT_wchar_t wc;
1922 int r;
1923 if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
1925 file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
1926 return MSVCRT_WEOF;
1928 return wc;
1930 c = MSVCRT_fgetc(file);
1931 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1933 FIXME("Treat Multibyte characters\n");
1935 if (c == MSVCRT_EOF)
1936 return MSVCRT_WEOF;
1937 else
1938 return (MSVCRT_wint_t)c;
1941 /*********************************************************************
1942 * getwc (MSVCRT.@)
1944 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1946 return MSVCRT_fgetwc(file);
1949 /*********************************************************************
1950 * _fgetwchar (MSVCRT.@)
1952 MSVCRT_wint_t _fgetwchar(void)
1954 return MSVCRT_fgetwc(MSVCRT_stdin);
1957 /*********************************************************************
1958 * getwchar (MSVCRT.@)
1960 MSVCRT_wint_t MSVCRT_getwchar(void)
1962 return _fgetwchar();
1965 /*********************************************************************
1966 * fgetws (MSVCRT.@)
1968 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1970 int cc = MSVCRT_WEOF;
1971 MSVCRT_wchar_t * buf_start = s;
1973 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1974 file,file->_file,s,size);
1976 while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
1978 *s++ = (char)cc;
1979 size --;
1981 if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
1983 TRACE(":nothing read\n");
1984 return NULL;
1986 if ((cc != MSVCRT_WEOF) && (size > 1))
1987 *s++ = cc;
1988 *s = 0;
1989 TRACE(":got %s\n", debugstr_w(buf_start));
1990 return buf_start;
1993 /*********************************************************************
1994 * fwrite (MSVCRT.@)
1996 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
1998 MSVCRT_size_t wrcnt=size * nmemb;
1999 int written = 0;
2000 if (size == 0)
2001 return 0;
2002 if(file->_cnt) {
2003 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2004 memcpy(file->_ptr, ptr, pcnt);
2005 file->_cnt -= pcnt;
2006 file->_ptr += pcnt;
2007 written = pcnt;
2008 wrcnt -= pcnt;
2009 ptr = (char*)ptr + pcnt;
2010 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2011 if(file->_flag & MSVCRT__IORW) {
2012 file->_flag |= MSVCRT__IOWRT;
2013 } else
2014 return 0;
2016 if(wrcnt) {
2017 /* Flush buffer */
2018 int res=msvcrt_flush_buffer(file);
2019 if(!res) {
2020 int pwritten = _write(file->_file, ptr, wrcnt);
2021 if (pwritten <= 0)
2023 file->_flag |= MSVCRT__IOERR;
2024 pwritten=0;
2026 written += pwritten;
2029 return written / size;
2032 /*********************************************************************
2033 * fputwc (MSVCRT.@)
2035 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
2037 MSVCRT_wchar_t mwc=wc;
2038 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2039 return MSVCRT_WEOF;
2040 return wc;
2043 /*********************************************************************
2044 * _fputwchar (MSVCRT.@)
2046 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
2048 return MSVCRT_fputwc(wc, MSVCRT_stdout);
2051 /*********************************************************************
2052 * fopen (MSVCRT.@)
2054 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
2056 MSVCRT_FILE* file;
2057 int open_flags, stream_flags, fd;
2059 TRACE("(%s,%s)\n",path,mode);
2061 /* map mode string to open() flags. "man fopen" for possibilities. */
2062 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2063 return NULL;
2065 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2067 if (fd < 0)
2068 return NULL;
2070 if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags) != -1)
2071 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2072 else if (file)
2074 file->_flag = 0;
2075 file = NULL;
2078 TRACE(":got (%p)\n",file);
2079 if (!file)
2080 _close(fd);
2081 return file;
2084 /*********************************************************************
2085 * _wfopen (MSVCRT.@)
2087 MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
2089 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2090 char *patha = MSVCRT_calloc(plen + 1, 1);
2091 char *modea = MSVCRT_calloc(mlen + 1, 1);
2093 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2095 if (patha && modea &&
2096 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2097 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2099 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
2100 MSVCRT_free(patha);
2101 MSVCRT_free(modea);
2102 return retval;
2105 msvcrt_set_errno(GetLastError());
2106 return NULL;
2109 /*********************************************************************
2110 * _fsopen (MSVCRT.@)
2112 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
2114 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
2115 return MSVCRT_fopen(path,mode);
2118 /*********************************************************************
2119 * _wfsopen (MSVCRT.@)
2121 MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
2123 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2124 debugstr_w(path),debugstr_w(mode),share);
2125 return MSVCRT__wfopen(path,mode);
2128 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2129 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
2131 /*********************************************************************
2132 * fputc (MSVCRT.@)
2134 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
2136 if(file->_cnt>0) {
2137 *file->_ptr++=c;
2138 file->_cnt--;
2139 return c;
2140 } else {
2141 return MSVCRT__flsbuf(c, file);
2145 /*********************************************************************
2146 * _flsbuf (MSVCRT.@)
2148 int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
2150 /* Flush output buffer */
2151 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2152 msvcrt_alloc_buffer(file);
2154 if(!(file->_flag & MSVCRT__IOWRT)) {
2155 if(file->_flag & MSVCRT__IORW) {
2156 file->_flag |= MSVCRT__IOWRT;
2157 } else {
2158 return MSVCRT_EOF;
2161 if(file->_bufsiz) {
2162 int res=msvcrt_flush_buffer(file);
2163 return res?res : MSVCRT_fputc(c, file);
2164 } else {
2165 unsigned char cc=c;
2166 int len;
2167 len = _write(file->_file, &cc, 1);
2168 if (len == 1) return c;
2169 file->_flag |= MSVCRT__IOERR;
2170 return MSVCRT_EOF;
2174 /*********************************************************************
2175 * _fputchar (MSVCRT.@)
2177 int _fputchar(int c)
2179 return MSVCRT_fputc(c, MSVCRT_stdout);
2182 /*********************************************************************
2183 * fread (MSVCRT.@)
2185 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2186 { MSVCRT_size_t rcnt=size * nmemb;
2187 MSVCRT_size_t read=0;
2188 int pread=0;
2190 /* first buffered data */
2191 if(file->_cnt>0) {
2192 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2193 memcpy(ptr, file->_ptr, pcnt);
2194 file->_cnt -= pcnt;
2195 file->_ptr += pcnt;
2196 read += pcnt ;
2197 rcnt -= pcnt ;
2198 ptr = (char*)ptr + pcnt;
2199 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2200 if(file->_flag & MSVCRT__IORW) {
2201 file->_flag |= MSVCRT__IOREAD;
2202 } else
2203 return 0;
2205 if(rcnt)
2207 pread = _read(file->_file,ptr, rcnt);
2208 /* expose feof condition in the flags
2209 * MFC tests file->_flag for feof, and doesn't not call feof())
2211 if (pread == 0)
2212 file->_flag |= MSVCRT__IOEOF;
2213 else if (pread == -1)
2215 file->_flag |= MSVCRT__IOERR;
2216 pread = 0;
2219 read+=pread;
2220 return read / size;
2223 /*********************************************************************
2224 * freopen (MSVCRT.@)
2227 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2229 int open_flags, stream_flags, fd;
2231 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2232 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2233 return NULL;
2235 MSVCRT_fclose(file);
2237 /* map mode string to open() flags. "man fopen" for possibilities. */
2238 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
2239 return NULL;
2241 fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
2243 if (fd < 0)
2244 return NULL;
2245 if (msvcrt_init_fp(file, fd, stream_flags) != -1)
2247 file->_flag = 0;
2248 WARN(":failed-last error (%ld)\n",GetLastError());
2249 msvcrt_set_errno(GetLastError());
2250 return NULL;
2252 return file;
2255 /*********************************************************************
2256 * fsetpos (MSVCRT.@)
2258 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2260 return _lseek(file->_file,*pos,SEEK_SET);
2263 /*********************************************************************
2264 * ftell (MSVCRT.@)
2266 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2268 int off=0;
2269 long pos;
2270 if(file->_bufsiz) {
2271 if( file->_flag & MSVCRT__IOWRT ) {
2272 off = file->_ptr - file->_base;
2273 } else {
2274 off = -file->_cnt;
2277 pos = _tell(file->_file);
2278 if(pos == -1) return pos;
2279 return off + pos;
2282 /*********************************************************************
2283 * fgetpos (MSVCRT.@)
2285 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2287 *pos = MSVCRT_ftell(file);
2288 return (*pos == -1? -1 : 0);
2291 /*********************************************************************
2292 * fputs (MSVCRT.@)
2294 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2296 size_t i, len = strlen(s);
2297 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
2298 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2299 for (i=0; i<len; i++)
2300 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2301 return MSVCRT_EOF;
2302 return 0;
2305 /*********************************************************************
2306 * fputws (MSVCRT.@)
2308 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2310 size_t i, len = strlenW(s);
2311 if (!(MSVCRT_fdesc[file->_file].xflag & WX_TEXT))
2312 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2313 for (i=0; i<len; i++)
2315 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2316 return MSVCRT_WEOF;
2317 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2318 return MSVCRT_WEOF;
2320 return 0;
2323 /*********************************************************************
2324 * getchar (MSVCRT.@)
2326 int MSVCRT_getchar(void)
2328 return MSVCRT_fgetc(MSVCRT_stdin);
2331 /*********************************************************************
2332 * getc (MSVCRT.@)
2334 int MSVCRT_getc(MSVCRT_FILE* file)
2336 return MSVCRT_fgetc(file);
2339 /*********************************************************************
2340 * gets (MSVCRT.@)
2342 char *MSVCRT_gets(char *buf)
2344 int cc;
2345 char * buf_start = buf;
2347 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2348 cc = MSVCRT_fgetc(MSVCRT_stdin))
2349 if(cc != '\r') *buf++ = (char)cc;
2351 *buf = '\0';
2353 TRACE("got '%s'\n", buf_start);
2354 return buf_start;
2357 /*********************************************************************
2358 * _getws (MSVCRT.@)
2360 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2362 MSVCRT_wint_t cc;
2363 MSVCRT_wchar_t* ws = buf;
2365 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2366 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2368 if (cc != '\r')
2369 *buf++ = (MSVCRT_wchar_t)cc;
2371 *buf = '\0';
2373 TRACE("got '%s'\n", debugstr_w(ws));
2374 return ws;
2377 /*********************************************************************
2378 * putc (MSVCRT.@)
2380 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2382 return MSVCRT_fputc(c, file);
2385 /*********************************************************************
2386 * putchar (MSVCRT.@)
2388 int MSVCRT_putchar(int c)
2390 return MSVCRT_fputc(c, MSVCRT_stdout);
2393 /*********************************************************************
2394 * puts (MSVCRT.@)
2396 int MSVCRT_puts(const char *s)
2398 size_t len = strlen(s);
2399 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2400 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2403 /*********************************************************************
2404 * _putws (MSVCRT.@)
2406 int _putws(const MSVCRT_wchar_t *s)
2408 static const MSVCRT_wchar_t nl = '\n';
2409 size_t len = strlenW(s);
2410 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2411 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2414 /*********************************************************************
2415 * remove (MSVCRT.@)
2417 int MSVCRT_remove(const char *path)
2419 TRACE("(%s)\n",path);
2420 if (DeleteFileA(path))
2421 return 0;
2422 TRACE(":failed (%ld)\n",GetLastError());
2423 msvcrt_set_errno(GetLastError());
2424 return -1;
2427 /*********************************************************************
2428 * _wremove (MSVCRT.@)
2430 int _wremove(const MSVCRT_wchar_t *path)
2432 TRACE("(%s)\n",debugstr_w(path));
2433 if (DeleteFileW(path))
2434 return 0;
2435 TRACE(":failed (%ld)\n",GetLastError());
2436 msvcrt_set_errno(GetLastError());
2437 return -1;
2440 /*********************************************************************
2441 * rename (MSVCRT.@)
2443 int MSVCRT_rename(const char *oldpath,const char *newpath)
2445 TRACE(":from %s to %s\n",oldpath,newpath);
2446 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2447 return 0;
2448 TRACE(":failed (%ld)\n",GetLastError());
2449 msvcrt_set_errno(GetLastError());
2450 return -1;
2453 /*********************************************************************
2454 * _wrename (MSVCRT.@)
2456 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2458 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2459 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2460 return 0;
2461 TRACE(":failed (%ld)\n",GetLastError());
2462 msvcrt_set_errno(GetLastError());
2463 return -1;
2466 /*********************************************************************
2467 * setvbuf (MSVCRT.@)
2469 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2471 /* TODO: Check if file busy */
2472 if(file->_bufsiz) {
2473 MSVCRT_free(file->_base);
2474 file->_bufsiz = 0;
2475 file->_cnt = 0;
2477 if(mode == MSVCRT__IOFBF) {
2478 file->_flag &= ~MSVCRT__IONBF;
2479 file->_base = file->_ptr = buf;
2480 if(buf) {
2481 file->_bufsiz = size;
2483 } else {
2484 file->_flag |= MSVCRT__IONBF;
2486 return 0;
2489 /*********************************************************************
2490 * setbuf (MSVCRT.@)
2492 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2494 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2497 /*********************************************************************
2498 * tmpnam (MSVCRT.@)
2500 char *MSVCRT_tmpnam(char *s)
2502 static int unique;
2503 char tmpstr[16];
2504 char *p;
2505 int count;
2506 if (s == 0)
2507 s = MSVCRT_tmpname;
2508 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
2509 p = s + sprintf(s, "\\s%s.", tmpstr);
2510 for (count = 0; count < MSVCRT_TMP_MAX; count++)
2512 msvcrt_int_to_base32(unique++, tmpstr);
2513 strcpy(p, tmpstr);
2514 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2515 GetLastError() == ERROR_FILE_NOT_FOUND)
2516 break;
2518 return s;
2521 /*********************************************************************
2522 * tmpfile (MSVCRT.@)
2524 MSVCRT_FILE* MSVCRT_tmpfile(void)
2526 char *filename = MSVCRT_tmpnam(NULL);
2527 int fd;
2528 MSVCRT_FILE* file = NULL;
2530 fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
2531 if (fd != -1 && (file = msvcrt_alloc_fp()))
2533 if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
2535 file->_flag = 0;
2536 file = NULL;
2538 else file->_tmpfname = _strdup(filename);
2540 return file;
2543 /*********************************************************************
2544 * vfprintf (MSVCRT.@)
2546 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2548 char buf[2048], *mem = buf;
2549 int written, resize = sizeof(buf), retval;
2550 /* There are two conventions for vsnprintf failing:
2551 * Return -1 if we truncated, or
2552 * Return the number of bytes that would have been written
2553 * The code below handles both cases
2555 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2556 written > resize)
2558 resize = (written == -1 ? resize * 2 : written + 1);
2559 if (mem != buf)
2560 MSVCRT_free (mem);
2561 if (!(mem = (char *)MSVCRT_malloc(resize)))
2562 return MSVCRT_EOF;
2564 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2565 if (mem != buf)
2566 MSVCRT_free (mem);
2567 return retval;
2570 /*********************************************************************
2571 * vfwprintf (MSVCRT.@)
2572 * FIXME:
2573 * Is final char included in written (then resize is too big) or not
2574 * (then we must test for equality too)?
2576 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2578 MSVCRT_wchar_t buf[2048], *mem = buf;
2579 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2580 /* See vfprintf comments */
2581 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2582 written > resize)
2584 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2585 if (mem != buf)
2586 MSVCRT_free (mem);
2587 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2588 return MSVCRT_EOF;
2590 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2591 if (mem != buf)
2592 MSVCRT_free (mem);
2593 return retval;
2596 /*********************************************************************
2597 * vprintf (MSVCRT.@)
2599 int MSVCRT_vprintf(const char *format, va_list valist)
2601 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2604 /*********************************************************************
2605 * vwprintf (MSVCRT.@)
2607 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2609 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2612 /*********************************************************************
2613 * fprintf (MSVCRT.@)
2615 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2617 va_list valist;
2618 int res;
2619 va_start(valist, format);
2620 res = MSVCRT_vfprintf(file, format, valist);
2621 va_end(valist);
2622 return res;
2625 /*********************************************************************
2626 * fwprintf (MSVCRT.@)
2628 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2630 va_list valist;
2631 int res;
2632 va_start(valist, format);
2633 res = MSVCRT_vfwprintf(file, format, valist);
2634 va_end(valist);
2635 return res;
2638 /*********************************************************************
2639 * printf (MSVCRT.@)
2641 int MSVCRT_printf(const char *format, ...)
2643 va_list valist;
2644 int res;
2645 va_start(valist, format);
2646 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2647 va_end(valist);
2648 return res;
2651 /*********************************************************************
2652 * ungetc (MSVCRT.@)
2654 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2656 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2657 msvcrt_alloc_buffer(file);
2658 file->_ptr++;
2660 if(file->_ptr>file->_base) {
2661 file->_ptr--;
2662 *file->_ptr=c;
2663 file->_cnt++;
2664 return c;
2666 return MSVCRT_EOF;
2669 /*********************************************************************
2670 * ungetwc (MSVCRT.@)
2672 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2674 MSVCRT_wchar_t mwc = wc;
2675 char * pp = (char *)&mwc;
2676 int i;
2677 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2678 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2679 return MSVCRT_WEOF;
2681 return mwc;
2684 /*********************************************************************
2685 * wprintf (MSVCRT.@)
2687 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2689 va_list valist;
2690 int res;
2691 va_start(valist, format);
2692 res = MSVCRT_vwprintf(format, valist);
2693 va_end(valist);
2694 return res;