_write(): Don't fragment WriteFile() for TEXT mode write.
[wine.git] / dlls / msvcrt / file.c
blob8cb8aa67c60c67d30f5e9078fed598e960227a9e
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <time.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "msvcrt.h"
39 #include "msvcrt/errno.h"
41 #include "wine/unicode.h"
42 #include "msvcrt/direct.h"
43 #include "msvcrt/fcntl.h"
44 #include "msvcrt/io.h"
45 #include "msvcrt/sys/locking.h"
46 #include "msvcrt/stdio.h"
47 #include "msvcrt/stdlib.h"
48 #include "msvcrt/string.h"
49 #include "msvcrt/sys/stat.h"
50 #include "msvcrt/sys/utime.h"
51 #include "msvcrt/time.h"
52 #include "msvcrt/share.h"
53 #include "msvcrt/wctype.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
59 /* for stat mode, permissions apply to all,owner and group */
60 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
61 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
62 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
64 /* _access() bit flags FIXME: incomplete */
65 #define MSVCRT_W_OK 0x02
68 /* FIXME: Make this dynamic */
69 #define MSVCRT_MAX_FILES 257
71 HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
72 MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
73 int MSVCRT_flags[MSVCRT_MAX_FILES];
74 char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
75 MSVCRT_FILE MSVCRT__iob[3];
76 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
77 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
78 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
80 static int MSVCRT_fdstart = 3; /* first unallocated fd */
81 static int MSVCRT_fdend = 3; /* highest allocated fd */
83 /* INTERNAL: process umask */
84 static int MSVCRT_umask = 0;
86 /* INTERNAL: Static buffer for temp file name */
87 static char MSVCRT_tmpname[MAX_PATH];
89 static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
90 static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
91 static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
92 static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
94 #define TOUL(x) (ULONGLONG)(x)
95 static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
96 static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
97 static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
98 static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
100 extern CRITICAL_SECTION MSVCRT_file_cs;
101 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
102 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
104 static void msvcrt_cp_from_stati64(const struct _stati64 *bufi64, struct _stat *buf)
106 buf->st_dev = bufi64->st_dev;
107 buf->st_ino = bufi64->st_ino;
108 buf->st_mode = bufi64->st_mode;
109 buf->st_nlink = bufi64->st_nlink;
110 buf->st_uid = bufi64->st_uid;
111 buf->st_gid = bufi64->st_gid;
112 buf->st_rdev = bufi64->st_rdev;
113 buf->st_size = bufi64->st_size;
114 buf->st_atime = bufi64->st_atime;
115 buf->st_mtime = bufi64->st_mtime;
116 buf->st_ctime = bufi64->st_ctime;
119 /* INTERNAL: Get the HANDLE for a fd */
120 static HANDLE msvcrt_fdtoh(int fd)
122 if (fd < 0 || fd >= MSVCRT_fdend ||
123 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
125 WARN(":fd (%d) - no handle!\n",fd);
126 *__doserrno() = 0;
127 *MSVCRT__errno() = MSVCRT_EBADF;
128 return INVALID_HANDLE_VALUE;
130 return MSVCRT_handles[fd];
133 /* INTERNAL: free a file entry fd */
134 static void msvcrt_free_fd(int fd)
136 MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
137 MSVCRT_files[fd] = 0;
138 MSVCRT_flags[fd] = 0;
139 TRACE(":fd (%d) freed\n",fd);
140 if (fd < 3)
141 return; /* dont use 0,1,2 for user files */
142 if (fd == MSVCRT_fdend - 1)
143 MSVCRT_fdend--;
144 if (fd < MSVCRT_fdstart)
145 MSVCRT_fdstart = fd;
148 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
149 static int msvcrt_alloc_fd(HANDLE hand, int flag)
151 int fd = MSVCRT_fdstart;
153 TRACE(":handle (%p) allocating fd (%d)\n",hand,fd);
154 if (fd >= MSVCRT_MAX_FILES)
156 WARN(":files exhausted!\n");
157 return -1;
159 MSVCRT_handles[fd] = hand;
160 MSVCRT_flags[fd] = flag;
162 /* locate next free slot */
163 if (fd == MSVCRT_fdend)
164 MSVCRT_fdstart = ++MSVCRT_fdend;
165 else
166 while(MSVCRT_fdstart < MSVCRT_fdend &&
167 MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
168 MSVCRT_fdstart++;
170 return fd;
173 /* INTERNAL: Allocate a FILE* for an fd slot
174 * This is done lazily to avoid memory wastage for low level open/write
175 * usage when a FILE* is not requested (but may be later).
177 static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
179 TRACE(":fd (%d) allocating FILE*\n",fd);
180 if (fd < 0 || fd >= MSVCRT_fdend ||
181 MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
183 WARN(":invalid fd %d\n",fd);
184 *__doserrno() = 0;
185 *MSVCRT__errno() = MSVCRT_EBADF;
186 return NULL;
188 if (!MSVCRT_files[fd])
190 if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
192 MSVCRT_files[fd]->_file = fd;
193 MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
194 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
197 TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
198 return MSVCRT_files[fd];
202 /* INTERNAL: Set up stdin, stderr and stdout */
203 void msvcrt_init_io(void)
205 int i;
206 memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
207 MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
208 MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
209 MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
210 MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
211 MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
212 MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
214 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0],
215 MSVCRT_handles[1],MSVCRT_handles[2]);
217 for (i = 0; i < 3; i++)
219 /* FILE structs for stdin/out/err are static and never deleted */
220 MSVCRT_files[i] = &MSVCRT__iob[i];
221 MSVCRT__iob[i]._file = i;
222 MSVCRT_tempfiles[i] = NULL;
226 /* free everything on process exit */
227 void msvcrt_free_io(void)
229 _fcloseall();
230 _close(0);
231 _close(1);
232 _close(2);
235 /* INTERNAL: Flush stdio file buffer */
236 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
238 if(file->_bufsiz) {
239 int cnt=file->_ptr-file->_base;
240 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
241 return MSVCRT_EOF;
243 file->_ptr=file->_base;
244 file->_cnt=file->_bufsiz;
246 return 0;
249 /* INTERNAL: Allocate stdio file buffer */
250 static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
252 file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
253 if(file->_base) {
254 file->_bufsiz = MSVCRT_BUFSIZ;
255 file->_flag |= MSVCRT__IOMYBUF;
256 } else {
257 file->_base = (unsigned char *)(&file->_charbuf);
258 /* put here 2 ??? */
259 file->_bufsiz = sizeof(file->_charbuf);
261 file->_ptr = file->_base;
262 file->_cnt = 0;
265 /*********************************************************************
266 * __p__iob(MSVCRT.@)
268 MSVCRT_FILE *__p__iob(void)
270 return &MSVCRT__iob[0];
273 /*********************************************************************
274 * _access (MSVCRT.@)
276 int _access(const char *filename, int mode)
278 DWORD attr = GetFileAttributesA(filename);
280 TRACE("(%s,%d) %ld\n",filename,mode,attr);
282 if (!filename || attr == 0xffffffff)
284 MSVCRT__set_errno(GetLastError());
285 return -1;
287 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
289 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
290 return -1;
292 return 0;
295 /*********************************************************************
296 * _waccess (MSVCRT.@)
298 int _waccess(const MSVCRT_wchar_t *filename, int mode)
300 DWORD attr = GetFileAttributesW(filename);
302 TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
304 if (!filename || attr == 0xffffffff)
306 MSVCRT__set_errno(GetLastError());
307 return -1;
309 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
311 MSVCRT__set_errno(ERROR_ACCESS_DENIED);
312 return -1;
314 return 0;
317 /*********************************************************************
318 * _chmod (MSVCRT.@)
320 int _chmod(const char *path, int flags)
322 DWORD oldFlags = GetFileAttributesA(path);
324 if (oldFlags != 0x0FFFFFFFF)
326 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
327 oldFlags | FILE_ATTRIBUTE_READONLY;
329 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
330 return 0;
332 MSVCRT__set_errno(GetLastError());
333 return -1;
336 /*********************************************************************
337 * _wchmod (MSVCRT.@)
339 int _wchmod(const MSVCRT_wchar_t *path, int flags)
341 DWORD oldFlags = GetFileAttributesW(path);
343 if (oldFlags != 0x0FFFFFFFF)
345 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
346 oldFlags | FILE_ATTRIBUTE_READONLY;
348 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
349 return 0;
351 MSVCRT__set_errno(GetLastError());
352 return -1;
355 /*********************************************************************
356 * _chsize (MSVCRT.@)
358 int _chsize(int fd, long size)
360 FIXME("(fd=%d, size=%ld): stub\n", fd, size);
361 return -1;
364 /*********************************************************************
365 * _dup (MSVCRT.@)
367 int _dup(int od)
369 FIXME("(od=%d): stub\n", od);
370 return -1;
373 /*********************************************************************
374 * _dup2 (MSVCRT.@)
376 int _dup2(int od, int nd)
378 FIXME("(od=%d, nd=%d): stub\n", od, nd);
379 return -1;
382 /*********************************************************************
383 * _unlink (MSVCRT.@)
385 int _unlink(const char *path)
387 TRACE("(%s)\n",path);
388 if(DeleteFileA(path))
389 return 0;
390 TRACE("failed (%ld)\n",GetLastError());
391 MSVCRT__set_errno(GetLastError());
392 return -1;
395 /*********************************************************************
396 * _wunlink (MSVCRT.@)
398 int _wunlink(const MSVCRT_wchar_t *path)
400 TRACE("(%s)\n",debugstr_w(path));
401 if(DeleteFileW(path))
402 return 0;
403 TRACE("failed (%ld)\n",GetLastError());
404 MSVCRT__set_errno(GetLastError());
405 return -1;
408 /*********************************************************************
409 * _close (MSVCRT.@)
411 int _close(int fd)
413 HANDLE hand = msvcrt_fdtoh(fd);
415 TRACE(":fd (%d) handle (%p)\n",fd,hand);
416 if (hand == INVALID_HANDLE_VALUE)
417 return -1;
418 /* flush stdio buffers */
419 if(MSVCRT_files[fd]) {
420 if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT)
421 MSVCRT_fflush(MSVCRT_files[fd]);
423 if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF)
424 MSVCRT_free(MSVCRT_files[fd]->_base);
427 /* Dont free std FILE*'s, they are not dynamic */
428 if (fd > 2 && MSVCRT_files[fd])
429 MSVCRT_free(MSVCRT_files[fd]);
431 msvcrt_free_fd(fd);
433 if (!CloseHandle(hand))
435 WARN(":failed-last error (%ld)\n",GetLastError());
436 MSVCRT__set_errno(GetLastError());
437 return -1;
439 if (MSVCRT_tempfiles[fd])
441 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
442 _unlink(MSVCRT_tempfiles[fd]);
443 MSVCRT_free(MSVCRT_tempfiles[fd]);
444 MSVCRT_tempfiles[fd] = NULL;
447 TRACE(":ok\n");
448 return 0;
451 /*********************************************************************
452 * _commit (MSVCRT.@)
454 int _commit(int fd)
456 HANDLE hand = msvcrt_fdtoh(fd);
458 TRACE(":fd (%d) handle (%p)\n",fd,hand);
459 if (hand == INVALID_HANDLE_VALUE)
460 return -1;
462 if (!FlushFileBuffers(hand))
464 if (GetLastError() == ERROR_INVALID_HANDLE)
466 /* FlushFileBuffers fails for console handles
467 * so we ignore this error.
469 return 0;
471 TRACE(":failed-last error (%ld)\n",GetLastError());
472 MSVCRT__set_errno(GetLastError());
473 return -1;
475 TRACE(":ok\n");
476 return 0;
479 /*********************************************************************
480 * _eof (MSVCRT.@)
482 int _eof(int fd)
484 DWORD curpos,endpos;
485 HANDLE hand = msvcrt_fdtoh(fd);
487 TRACE(":fd (%d) handle (%p)\n",fd,hand);
489 if (hand == INVALID_HANDLE_VALUE)
490 return -1;
492 /* If we have a FILE* for this file, the EOF flag
493 * will be set by the read()/write() functions.
495 if (MSVCRT_files[fd])
496 return MSVCRT_flags[fd] & MSVCRT__IOEOF;
498 /* Otherwise we do it the hard way */
499 curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
500 endpos = SetFilePointer(hand, 0, NULL, FILE_END);
502 if (curpos == endpos)
503 return TRUE;
505 SetFilePointer(hand, curpos, 0, FILE_BEGIN);
506 return FALSE;
509 /*********************************************************************
510 * _fcloseall (MSVCRT.@)
512 int _fcloseall(void)
514 int num_closed = 0, i;
516 for (i = 3; i < MSVCRT_fdend; i++)
517 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
519 _close(i);
520 num_closed++;
523 TRACE(":closed (%d) handles\n",num_closed);
524 return num_closed;
527 /*********************************************************************
528 * _lseek (MSVCRT.@)
530 __int64 _lseeki64(int fd, __int64 offset, int whence)
532 DWORD ret, hoffset = (DWORD) (offset >> 32);
533 HANDLE hand = msvcrt_fdtoh(fd);
535 TRACE(":fd (%d) handle (%p)\n",fd,hand);
536 if (hand == INVALID_HANDLE_VALUE)
537 return -1;
539 if (whence < 0 || whence > 2)
541 *MSVCRT__errno() = MSVCRT_EINVAL;
542 return -1;
545 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
546 fd,hoffset,(long)offset,
547 (whence==SEEK_SET)?"SEEK_SET":
548 (whence==SEEK_CUR)?"SEEK_CUR":
549 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
551 if (((ret = SetFilePointer(hand, (long)offset, &hoffset,
552 whence)) != INVALID_SET_FILE_POINTER) || !GetLastError())
554 if (MSVCRT_files[fd])
555 MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
556 /* FIXME: What if we seek _to_ EOF - is EOF set? */
558 return ((__int64)hoffset << 32) | ret;
560 TRACE(":error-last error (%ld)\n",GetLastError());
561 if (MSVCRT_files[fd])
562 switch(GetLastError())
564 case ERROR_NEGATIVE_SEEK:
565 case ERROR_SEEK_ON_DEVICE:
566 MSVCRT__set_errno(GetLastError());
567 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
568 break;
569 default:
570 break;
572 return -1;
575 /*********************************************************************
576 * _lseek (MSVCRT.@)
578 LONG _lseek(int fd, LONG offset, int whence)
580 return _lseeki64(fd, offset, whence);
583 /*********************************************************************
584 * _locking (MSVCRT.@)
586 * This is untested; the underlying LockFile doesn't work yet.
588 int _locking(int fd, int mode, LONG nbytes)
590 BOOL ret;
591 DWORD cur_locn;
592 HANDLE hand = msvcrt_fdtoh(fd);
594 TRACE(":fd (%d) handle (%p)\n",fd,hand);
595 if (hand == INVALID_HANDLE_VALUE)
596 return -1;
598 if (mode < 0 || mode > 4)
600 *MSVCRT__errno() = MSVCRT_EINVAL;
601 return -1;
604 TRACE(":fd (%d) by 0x%08lx mode %s\n",
605 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
606 (mode==_LK_LOCK)?"_LK_LOCK":
607 (mode==_LK_NBLCK)?"_LK_NBLCK":
608 (mode==_LK_RLCK)?"_LK_RLCK":
609 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
610 "UNKNOWN");
612 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
614 FIXME ("Seek failed\n");
615 *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
616 return -1;
618 if (mode == _LK_LOCK || mode == _LK_RLCK)
620 int nretry = 10;
621 ret = 1; /* just to satisfy gcc */
622 while (nretry--)
624 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
625 if (ret) break;
626 sleep (1);
629 else if (mode == _LK_UNLCK)
630 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
631 else
632 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
633 /* FIXME - what about error settings? */
634 return ret ? 0 : -1;
637 /*********************************************************************
638 * rewind (MSVCRT.@)
640 void MSVCRT_rewind(MSVCRT_FILE* file)
642 TRACE(":file (%p) fd (%d)\n",file,file->_file);
643 MSVCRT_fseek(file, 0L, SEEK_SET);
644 MSVCRT_clearerr(file);
647 /*********************************************************************
648 * _fdopen (MSVCRT.@)
650 MSVCRT_FILE* _fdopen(int fd, const char *mode)
652 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
654 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
656 return file;
659 /*********************************************************************
660 * _wfdopen (MSVCRT.@)
662 MSVCRT_FILE* _wfdopen(int fd, const MSVCRT_wchar_t *mode)
664 MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
666 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
667 if (file)
668 MSVCRT_rewind(file);
670 return file;
673 /*********************************************************************
674 * _filelength (MSVCRT.@)
676 LONG _filelength(int fd)
678 LONG curPos = _lseek(fd, 0, SEEK_CUR);
679 if (curPos != -1)
681 LONG endPos = _lseek(fd, 0, SEEK_END);
682 if (endPos != -1)
684 if (endPos != curPos)
685 _lseek(fd, curPos, SEEK_SET);
686 return endPos;
689 return -1;
692 /*********************************************************************
693 * _fileno (MSVCRT.@)
695 int _fileno(MSVCRT_FILE* file)
697 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
698 return file->_file;
701 /*********************************************************************
702 * _flushall (MSVCRT.@)
704 int _flushall(void)
706 int num_flushed = 0, i = 3;
708 while(i < MSVCRT_fdend)
709 if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
711 #if 0
712 /* FIXME: flush, do not commit */
713 if (_commit(i) == -1)
714 if (MSVCRT_files[i])
715 MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
716 #endif
717 if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) {
718 MSVCRT_fflush(MSVCRT_files[i]);
719 num_flushed++;
723 TRACE(":flushed (%d) handles\n",num_flushed);
724 return num_flushed;
727 /*********************************************************************
728 * _fstati64 (MSVCRT.@)
730 int _fstati64(int fd, struct _stati64* buf)
732 DWORD dw;
733 BY_HANDLE_FILE_INFORMATION hfi;
734 HANDLE hand = msvcrt_fdtoh(fd);
736 TRACE(":fd (%d) stat (%p)\n",fd,buf);
737 if (hand == INVALID_HANDLE_VALUE)
738 return -1;
740 if (!buf)
742 WARN(":failed-NULL buf\n");
743 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
744 return -1;
747 memset(&hfi, 0, sizeof(hfi));
748 memset(buf, 0, sizeof(struct _stati64));
749 if (!GetFileInformationByHandle(hand, &hfi))
751 WARN(":failed-last error (%ld)\n",GetLastError());
752 MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
753 return -1;
755 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
756 buf->st_nlink = hfi.nNumberOfLinks;
757 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
758 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
759 buf->st_atime = dw;
760 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
761 buf->st_mtime = buf->st_ctime = dw;
762 return 0;
765 /*********************************************************************
766 * _fstat (MSVCRT.@)
768 int MSVCRT__fstat(int fd, struct _stat* buf)
769 { int ret;
770 struct _stati64 bufi64;
772 ret = _fstati64(fd, &bufi64);
773 if (!ret)
774 msvcrt_cp_from_stati64(&bufi64, buf);
775 return ret;
778 /*********************************************************************
779 * _futime (MSVCRT.@)
781 int _futime(int fd, struct _utimbuf *t)
783 HANDLE hand = msvcrt_fdtoh(fd);
784 FILETIME at, wt;
786 if (!t)
788 MSVCRT_time_t currTime;
789 MSVCRT_time(&currTime);
790 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
791 memcpy(&wt, &at, sizeof(wt));
793 else
795 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
796 if (t->actime == t->modtime)
797 memcpy(&wt, &at, sizeof(wt));
798 else
799 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
802 if (!SetFileTime(hand, NULL, &at, &wt))
804 MSVCRT__set_errno(GetLastError());
805 return -1 ;
807 return 0;
810 /*********************************************************************
811 * _get_osfhandle (MSVCRT.@)
813 long _get_osfhandle(int fd)
815 HANDLE hand = msvcrt_fdtoh(fd);
816 HANDLE newhand = hand;
817 TRACE(":fd (%d) handle (%p)\n",fd,hand);
819 if (hand != INVALID_HANDLE_VALUE)
821 /* FIXME: I'm not convinced that I should be copying the
822 * handle here - it may be leaked if the app doesn't
823 * close it (and the API docs dont say that it should)
824 * Not duplicating it means that it can't be inherited
825 * and so lcc's wedit doesn't cope when it passes it to
826 * child processes. I've an idea that it should either
827 * be copied by CreateProcess, or marked as inheritable
828 * when initialised, or maybe both? JG 21-9-00.
830 DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
831 &newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
833 return (long)newhand;
836 /*********************************************************************
837 * _isatty (MSVCRT.@)
839 int _isatty(int fd)
841 HANDLE hand = msvcrt_fdtoh(fd);
843 TRACE(":fd (%d) handle (%p)\n",fd,hand);
844 if (hand == INVALID_HANDLE_VALUE)
845 return 0;
847 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
850 /*********************************************************************
851 * _mktemp (MSVCRT.@)
853 char *_mktemp(char *pattern)
855 int numX = 0;
856 char *retVal = pattern;
857 int id;
858 char letter = 'a';
860 while(*pattern)
861 numX = (*pattern++ == 'X')? numX + 1 : 0;
862 if (numX < 5)
863 return NULL;
864 pattern--;
865 id = GetCurrentProcessId();
866 numX = 6;
867 while(numX--)
869 int tempNum = id / 10;
870 *pattern-- = id - (tempNum * 10) + '0';
871 id = tempNum;
873 pattern++;
876 if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
877 GetLastError() == ERROR_FILE_NOT_FOUND)
878 return retVal;
879 *pattern = letter++;
880 } while(letter != '|');
881 return NULL;
884 /*********************************************************************
885 * _wmktemp (MSVCRT.@)
887 MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
889 int numX = 0;
890 MSVCRT_wchar_t *retVal = pattern;
891 int id;
892 MSVCRT_wchar_t letter = 'a';
894 while(*pattern)
895 numX = (*pattern++ == 'X')? numX + 1 : 0;
896 if (numX < 5)
897 return NULL;
898 pattern--;
899 id = GetCurrentProcessId();
900 numX = 6;
901 while(numX--)
903 int tempNum = id / 10;
904 *pattern-- = id - (tempNum * 10) + '0';
905 id = tempNum;
907 pattern++;
910 if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
911 GetLastError() == ERROR_FILE_NOT_FOUND)
912 return retVal;
913 *pattern = letter++;
914 } while(letter != '|');
915 return NULL;
918 /*********************************************************************
919 * _sopen (MSVCRT.@)
921 int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
923 va_list ap;
924 int pmode;
925 DWORD access = 0, creation = 0;
926 DWORD sharing;
927 int ioflag = 0, fd;
928 HANDLE hand;
929 SECURITY_ATTRIBUTES sa;
932 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
933 path, oflags, shflags);
935 switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
937 case _O_RDONLY:
938 access |= GENERIC_READ;
939 ioflag |= MSVCRT__IOREAD;
940 break;
941 case _O_WRONLY:
942 access |= GENERIC_WRITE;
943 ioflag |= MSVCRT__IOWRT;
944 break;
945 case _O_RDWR:
946 access |= GENERIC_WRITE | GENERIC_READ;
947 ioflag |= MSVCRT__IORW;
948 break;
951 if (oflags & _O_CREAT)
953 va_start(ap, shflags);
954 pmode = va_arg(ap, int);
955 va_end(ap);
957 if(pmode & ~(_S_IREAD | _S_IWRITE))
958 FIXME(": pmode 0x%04x ignored\n", pmode);
959 else
960 WARN(": pmode 0x%04x ignored\n", pmode);
962 if (oflags & _O_EXCL)
963 creation = CREATE_NEW;
964 else if (oflags & _O_TRUNC)
965 creation = CREATE_ALWAYS;
966 else
967 creation = OPEN_ALWAYS;
969 else /* no _O_CREAT */
971 if (oflags & _O_TRUNC)
972 creation = TRUNCATE_EXISTING;
973 else
974 creation = OPEN_EXISTING;
976 if (oflags & _O_APPEND)
977 ioflag |= MSVCRT__IOAPPEND;
979 if (oflags & _O_BINARY)
980 ioflag |= _O_BINARY;
981 else if (oflags & _O_TEXT)
982 ioflag |= _O_TEXT;
983 else if (*__p__fmode() & _O_BINARY)
984 ioflag |= _O_BINARY;
985 else
986 ioflag |= _O_TEXT; /* default to TEXT*/
988 switch( shflags )
990 case _SH_DENYRW:
991 sharing = 0L;
992 break;
993 case _SH_DENYWR:
994 sharing = FILE_SHARE_READ;
995 break;
996 case _SH_DENYRD:
997 sharing = FILE_SHARE_WRITE;
998 break;
999 case _SH_DENYNO:
1000 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1001 break;
1002 default:
1003 ERR( "Unhandled shflags 0x%x\n", shflags );
1004 return -1;
1007 if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
1008 |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
1009 TRACE(":unsupported oflags 0x%04x\n",oflags);
1011 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1012 sa.lpSecurityDescriptor = NULL;
1013 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1015 hand = CreateFileA(path, access, sharing,
1016 &sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
1018 if (hand == INVALID_HANDLE_VALUE) {
1019 WARN(":failed-last error (%ld)\n",GetLastError());
1020 MSVCRT__set_errno(GetLastError());
1021 return -1;
1024 fd = msvcrt_alloc_fd(hand, ioflag);
1026 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1028 if (fd > 0)
1030 if (oflags & _O_TEMPORARY)
1031 MSVCRT_tempfiles[fd] = _strdup(path);
1032 if (ioflag & MSVCRT__IOAPPEND)
1033 _lseek(fd, 0, FILE_END);
1036 return fd;
1039 /*********************************************************************
1040 * _wsopen (MSVCRT.@)
1042 int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
1044 const unsigned int len = strlenW(path);
1045 char *patha = MSVCRT_calloc(len + 1,1);
1046 va_list ap;
1047 int pmode;
1049 va_start(ap, shflags);
1050 pmode = va_arg(ap, int);
1051 va_end(ap);
1053 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1055 int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
1056 MSVCRT_free(patha);
1057 return retval;
1060 MSVCRT__set_errno(GetLastError());
1061 return -1;
1064 /*********************************************************************
1065 * _open (MSVCRT.@)
1067 int _open( const char *path, int flags, ... )
1069 va_list ap;
1071 if (flags & _O_CREAT)
1073 int pmode;
1074 va_start(ap, flags);
1075 pmode = va_arg(ap, int);
1076 va_end(ap);
1077 return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
1079 else
1080 return MSVCRT__sopen( path, flags, _SH_DENYNO);
1083 /*********************************************************************
1084 * _wopen (MSVCRT.@)
1086 int _wopen(const MSVCRT_wchar_t *path,int flags,...)
1088 const unsigned int len = strlenW(path);
1089 char *patha = MSVCRT_calloc(len + 1,1);
1090 va_list ap;
1091 int pmode;
1093 va_start(ap, flags);
1094 pmode = va_arg(ap, int);
1095 va_end(ap);
1097 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1099 int retval = _open(patha,flags,pmode);
1100 MSVCRT_free(patha);
1101 return retval;
1104 MSVCRT__set_errno(GetLastError());
1105 return -1;
1108 /*********************************************************************
1109 * _creat (MSVCRT.@)
1111 int _creat(const char *path, int flags)
1113 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1114 return _open(path, usedFlags);
1117 /*********************************************************************
1118 * _wcreat (MSVCRT.@)
1120 int _wcreat(const MSVCRT_wchar_t *path, int flags)
1122 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1123 return _wopen(path, usedFlags);
1126 /*********************************************************************
1127 * _open_osfhandle (MSVCRT.@)
1129 int _open_osfhandle(long hand, int flags)
1131 int fd;
1133 /* _O_RDONLY (0) always matches, so set the read flag
1134 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1135 * file, so set the write flag. It also only sets _O_TEXT if it wants
1136 * text - it never sets _O_BINARY.
1138 /* FIXME: handle more flags */
1139 flags |= MSVCRT__IOREAD|MSVCRT__IOWRT;
1140 if ( !( flags & _O_TEXT ) ) flags |= _O_BINARY;
1142 fd = msvcrt_alloc_fd((HANDLE)hand,flags);
1143 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags);
1144 return fd;
1147 /*********************************************************************
1148 * _rmtmp (MSVCRT.@)
1150 int _rmtmp(void)
1152 int num_removed = 0, i;
1154 for (i = 3; i < MSVCRT_fdend; i++)
1155 if (MSVCRT_tempfiles[i])
1157 _close(i);
1158 num_removed++;
1161 if (num_removed)
1162 TRACE(":removed (%d) temp files\n",num_removed);
1163 return num_removed;
1166 /*********************************************************************
1167 * (internal) remove_cr
1169 * Remove all \r inplace.
1170 * return the number of \r removed
1172 static unsigned int remove_cr(char *buf, unsigned int count)
1174 unsigned int i, j;
1176 for (i = 0; i < count; i++) if (buf[i] == '\r') break;
1177 for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
1178 return count - i;
1181 /*********************************************************************
1182 * _read (MSVCRT.@)
1184 int _read(int fd, void *buf, unsigned int count)
1186 DWORD num_read, all_read =0;
1187 char *bufstart = buf;
1188 HANDLE hand = msvcrt_fdtoh(fd);
1190 /* Dont trace small reads, it gets *very* annoying */
1191 if (count > 4)
1192 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1193 if (hand == INVALID_HANDLE_VALUE)
1194 return -1;
1196 /* Reading single bytes in O_TEXT mode makes things slow
1197 * So read big chunks, then remove the \r in memory and try reading
1198 * the rest until the request is satisfied or EOF is met
1200 while ( all_read < count)
1202 if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
1204 if (num_read != (count- all_read))
1206 TRACE(":EOF\n");
1207 if ( MSVCRT_files[fd])
1209 MSVCRT_flags[fd] |= MSVCRT__IOEOF;
1211 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1214 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1215 num_read -= remove_cr(bufstart+all_read,num_read);
1216 all_read += num_read;
1217 if (count > 4)
1218 TRACE("%s\n",debugstr_an(buf,all_read));
1219 return all_read;
1221 if ((MSVCRT_flags[fd]& _O_BINARY ) != _O_BINARY )
1223 num_read -= remove_cr(bufstart+all_read,num_read);
1225 all_read += num_read;
1227 else
1229 TRACE(":failed-last error (%ld)\n",GetLastError());
1230 if (MSVCRT_files[fd])
1231 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1232 return -1;
1236 if (count > 4)
1237 TRACE("%s\n",debugstr_an(buf, all_read));
1238 return all_read;
1241 /*********************************************************************
1242 * _getw (MSVCRT.@)
1244 int _getw(MSVCRT_FILE* file)
1246 int i;
1247 if (_read(file->_file, &i, sizeof(int)) != 1)
1248 return MSVCRT_EOF;
1249 return i;
1252 /*********************************************************************
1253 * _setmode (MSVCRT.@)
1255 int _setmode(int fd,int mode)
1257 int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
1258 if (mode & (~(_O_TEXT|_O_BINARY)))
1259 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1260 MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY);
1261 MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY);
1262 return ret;
1265 /*********************************************************************
1266 * _stati64 (MSVCRT.@)
1268 int _stati64(const char* path, struct _stati64 * buf)
1270 DWORD dw;
1271 WIN32_FILE_ATTRIBUTE_DATA hfi;
1272 unsigned short mode = MSVCRT_S_IREAD;
1273 int plen;
1275 TRACE(":file (%s) buf(%p)\n",path,buf);
1277 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
1279 TRACE("failed (%ld)\n",GetLastError());
1280 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1281 return -1;
1284 memset(buf,0,sizeof(struct _stati64));
1286 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1287 Bon 011120: This FIXME seems incorrect
1288 Also a letter as first char isn't enough to be classify
1289 as drive letter
1291 if (isalpha(*path)&& (*(path+1)==':'))
1292 buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
1293 else
1294 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1296 plen = strlen(path);
1298 /* Dir, or regular file? */
1299 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1300 (path[plen-1] == '\\'))
1301 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1302 else
1304 mode |= _S_IFREG;
1305 /* executable? */
1306 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1308 unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
1309 (tolower(path[plen-3]) << 16);
1310 if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
1311 mode |= MSVCRT_S_IEXEC;
1315 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1316 mode |= MSVCRT_S_IWRITE;
1318 buf->st_mode = mode;
1319 buf->st_nlink = 1;
1320 buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
1321 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1322 buf->st_atime = dw;
1323 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1324 buf->st_mtime = buf->st_ctime = dw;
1325 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
1326 (long)(buf->st_size >> 32),(long)buf->st_size,
1327 buf->st_atime,buf->st_mtime, buf->st_ctime);
1328 return 0;
1331 /*********************************************************************
1332 * _stat (MSVCRT.@)
1334 int MSVCRT__stat(const char* path, struct _stat * buf)
1335 { int ret;
1336 struct _stati64 bufi64;
1338 ret = _stati64( path, &bufi64);
1339 if (!ret)
1340 msvcrt_cp_from_stati64(&bufi64, buf);
1341 return ret;
1344 /*********************************************************************
1345 * _wstat (MSVCRT.@)
1347 int _wstat(const MSVCRT_wchar_t* path, struct _stat * buf)
1349 DWORD dw;
1350 WIN32_FILE_ATTRIBUTE_DATA hfi;
1351 unsigned short mode = MSVCRT_S_IREAD;
1352 int plen;
1354 TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
1356 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
1358 TRACE("failed (%ld)\n",GetLastError());
1359 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
1360 return -1;
1363 memset(buf,0,sizeof(struct _stat));
1365 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1366 if (MSVCRT_iswalpha(*path))
1367 buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
1368 else
1369 buf->st_dev = buf->st_rdev = _getdrive() - 1;
1371 plen = strlenW(path);
1373 /* Dir, or regular file? */
1374 if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1375 (path[plen-1] == '\\'))
1376 mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
1377 else
1379 mode |= _S_IFREG;
1380 /* executable? */
1381 if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
1383 ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
1384 ((ULONGLONG)tolowerW(path[plen-3]) << 32);
1385 if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
1386 mode |= MSVCRT_S_IEXEC;
1390 if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
1391 mode |= MSVCRT_S_IWRITE;
1393 buf->st_mode = mode;
1394 buf->st_nlink = 1;
1395 buf->st_size = hfi.nFileSizeLow;
1396 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
1397 buf->st_atime = dw;
1398 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
1399 buf->st_mtime = buf->st_ctime = dw;
1400 TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
1401 buf->st_atime,buf->st_mtime, buf->st_ctime);
1402 return 0;
1405 /*********************************************************************
1406 * _tell (MSVCRT.@)
1408 LONG _tell(int fd)
1410 return _lseek(fd, 0, SEEK_CUR);
1413 /*********************************************************************
1414 * _tempnam (MSVCRT.@)
1416 char *_tempnam(const char *dir, const char *prefix)
1418 char tmpbuf[MAX_PATH];
1420 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1421 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1423 TRACE("got name (%s)\n",tmpbuf);
1424 DeleteFileA(tmpbuf);
1425 return _strdup(tmpbuf);
1427 TRACE("failed (%ld)\n",GetLastError());
1428 return NULL;
1431 /*********************************************************************
1432 * _wtempnam (MSVCRT.@)
1434 MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
1436 MSVCRT_wchar_t tmpbuf[MAX_PATH];
1438 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1439 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1441 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1442 DeleteFileW(tmpbuf);
1443 return _wcsdup(tmpbuf);
1445 TRACE("failed (%ld)\n",GetLastError());
1446 return NULL;
1449 /*********************************************************************
1450 * _umask (MSVCRT.@)
1452 int _umask(int umask)
1454 int old_umask = MSVCRT_umask;
1455 TRACE("(%d)\n",umask);
1456 MSVCRT_umask = umask;
1457 return old_umask;
1460 /*********************************************************************
1461 * _utime (MSVCRT.@)
1463 int _utime(const char* path, struct _utimbuf *t)
1465 int fd = _open(path, _O_WRONLY | _O_BINARY);
1467 if (fd > 0)
1469 int retVal = _futime(fd, t);
1470 _close(fd);
1471 return retVal;
1473 return -1;
1476 /*********************************************************************
1477 * _wutime (MSVCRT.@)
1479 int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t)
1481 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1483 if (fd > 0)
1485 int retVal = _futime(fd, t);
1486 _close(fd);
1487 return retVal;
1489 return -1;
1492 /*********************************************************************
1493 * _write (MSVCRT.@)
1495 int _write(int fd, const void* buf, unsigned int count)
1497 DWORD num_written;
1498 HANDLE hand = msvcrt_fdtoh(fd);
1500 /* Dont trace small writes, it gets *very* annoying */
1501 #if 0
1502 if (count > 32)
1503 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1504 #endif
1505 if (hand == INVALID_HANDLE_VALUE)
1507 *MSVCRT__errno() = MSVCRT_EBADF;
1508 return -1;
1511 /* If appending, go to EOF */
1512 if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
1513 _lseek(fd, 0, FILE_END);
1515 if (MSVCRT_flags[fd] & _O_BINARY)
1517 if (WriteFile(hand, buf, count, &num_written, NULL)
1518 && (num_written == count))
1519 return num_written;
1520 TRACE(":failed-last error (%ld)\n",GetLastError());
1521 if (MSVCRT_files[fd])
1523 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1524 *MSVCRT__errno() = MSVCRT_ENOSPC;
1527 else
1529 unsigned int i, j, nr_lf;
1530 char *s=(char*)buf, *buf_start=(char*)buf, *p;
1531 /* find number of \n ( without preceeding \r */
1532 for ( nr_lf=0,i = 0; i <count; i++)
1534 if (s[i]== '\n')
1536 nr_lf++;
1537 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1540 if (nr_lf)
1542 if ((p = MSVCRT_malloc(count + nr_lf)))
1544 for(s=(char*)buf, i=0, j=0; i<count; i++)
1546 if (s[i]== '\n')
1548 p[j++] = '\r';
1549 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1551 p[j++] = s[i];
1554 else
1556 FIXME("Malloc failed\n");
1557 nr_lf =0;
1558 p = (char*)buf;
1561 else
1562 p = (char*)buf;
1564 if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1566 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
1567 if (MSVCRT_files[fd])
1569 MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
1570 *MSVCRT__errno() = MSVCRT_ENOSPC;
1571 if(nr_lf)
1572 MSVCRT_free(p);
1573 return s - buf_start;
1576 else
1578 if(nr_lf)
1579 MSVCRT_free(p);
1580 return count;
1583 return -1;
1586 /*********************************************************************
1587 * _putw (MSVCRT.@)
1589 int _putw(int val, MSVCRT_FILE* file)
1591 return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
1594 /*********************************************************************
1595 * clearerr (MSVCRT.@)
1597 void MSVCRT_clearerr(MSVCRT_FILE* file)
1599 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1600 file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
1603 /*********************************************************************
1604 * fclose (MSVCRT.@)
1606 int MSVCRT_fclose(MSVCRT_FILE* file)
1608 int r, flag;
1610 flag = file->_flag;
1611 r=_close(file->_file);
1612 return ((r==MSVCRT_EOF) || (flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
1615 /*********************************************************************
1616 * feof (MSVCRT.@)
1618 int MSVCRT_feof(MSVCRT_FILE* file)
1620 return file->_flag & MSVCRT__IOEOF;
1623 /*********************************************************************
1624 * ferror (MSVCRT.@)
1626 int MSVCRT_ferror(MSVCRT_FILE* file)
1628 return file->_flag & MSVCRT__IOERR;
1631 /*********************************************************************
1632 * fflush (MSVCRT.@)
1634 int MSVCRT_fflush(MSVCRT_FILE* file)
1636 if(!file) {
1637 _flushall();
1638 return 0;
1639 } else {
1640 int res=msvcrt_flush_buffer(file);
1641 return res;
1645 /*********************************************************************
1646 * fgetc (MSVCRT.@)
1648 int MSVCRT_fgetc(MSVCRT_FILE* file)
1650 if (file->_cnt>0) {
1651 file->_cnt--;
1652 return *(unsigned char *)file->_ptr++;
1653 } else {
1654 return _filbuf(file);
1658 /*********************************************************************
1659 * _fgetchar (MSVCRT.@)
1661 int _fgetchar(void)
1663 return MSVCRT_fgetc(MSVCRT_stdin);
1666 /*********************************************************************
1667 * _filbuf (MSVCRT.@)
1669 int _filbuf(MSVCRT_FILE* file)
1672 /* Allocate buffer if needed */
1673 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
1674 msvcrt_alloc_buffer(file);
1676 if(!(file->_flag & MSVCRT__IOREAD)) {
1677 if(file->_flag & MSVCRT__IORW) {
1678 file->_flag |= MSVCRT__IOREAD;
1679 } else {
1680 return MSVCRT_EOF;
1683 if(file->_flag & MSVCRT__IONBF) {
1684 unsigned char c;
1685 if (_read(file->_file,&c,1) != 1) {
1686 file->_flag |= MSVCRT__IOEOF;
1687 return MSVCRT_EOF;
1689 return c;
1690 } else {
1691 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
1692 if(file->_cnt<0) file->_cnt = 0;
1693 if(!file->_cnt) {
1694 file->_flag |= MSVCRT__IOEOF;
1695 return MSVCRT_EOF;
1697 file->_cnt--;
1698 file->_ptr = file->_base+1;
1699 return *(unsigned char *)file->_base;
1703 /*********************************************************************
1704 * fgetpos (MSVCRT.@)
1706 int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
1708 *pos = MSVCRT_ftell(file);
1709 return (*pos == -1? -1 : 0);
1712 /*********************************************************************
1713 * fgets (MSVCRT.@)
1715 char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
1717 int cc;
1718 char * buf_start = s;
1720 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1721 file,file->_file,s,size);
1723 for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
1724 cc = MSVCRT_fgetc(file))
1725 /* _read already handled the translation */
1727 if (--size <= 0) break;
1728 *s++ = (char)cc;
1730 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1732 TRACE(":nothing read\n");
1733 return 0;
1735 if (cc == '\n')
1736 if (--size > 0)
1737 *s++ = '\n';
1738 *s = '\0';
1739 TRACE(":got '%s'\n", debugstr_a(buf_start));
1740 return buf_start;
1743 /*********************************************************************
1744 * fgetwc (MSVCRT.@)
1746 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1747 * the CR from CR/LF combinations
1749 MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
1751 char c;
1753 if (file->_flag & _O_BINARY)
1755 MSVCRT_wchar_t wc;
1756 if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
1757 return MSVCRT_WEOF;
1758 return wc;
1760 c = MSVCRT_fgetc(file);
1761 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
1763 FIXME("Treat Multibyte characters\n");
1765 if (c == MSVCRT_EOF)
1766 return MSVCRT_WEOF;
1767 else
1768 return (MSVCRT_wint_t)c;
1771 /*********************************************************************
1772 * getwc (MSVCRT.@)
1774 MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
1776 return MSVCRT_fgetwc(file);
1779 /*********************************************************************
1780 * _fgetwchar (MSVCRT.@)
1782 MSVCRT_wint_t _fgetwchar(void)
1784 return MSVCRT_fgetwc(MSVCRT_stdin);
1787 /*********************************************************************
1788 * getwchar (MSVCRT.@)
1790 MSVCRT_wint_t MSVCRT_getwchar(void)
1792 return _fgetwchar();
1795 /*********************************************************************
1796 * fgetws (MSVCRT.@)
1798 MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
1800 int cc;
1801 MSVCRT_wchar_t * buf_start = s;
1803 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1804 file,file->_file,s,size);
1806 for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
1807 cc = MSVCRT_fgetwc(file))
1808 /* _read already handled the translation */
1810 if (--size <= 0) break;
1811 *s++ = cc;
1813 if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
1815 TRACE(":nothing read\n");
1816 return 0;
1818 if (cc == L'\n')
1819 if (--size > 0)
1820 *s++ = '\n';
1821 *s = '\0';
1822 /* TRACE(":got '%s'\n", buf_start); */
1823 return buf_start;
1827 /*********************************************************************
1828 * fputwc (MSVCRT.@)
1830 MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
1832 MSVCRT_wchar_t mwc=wc;
1833 if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
1834 return MSVCRT_WEOF;
1835 return wc;
1838 /*********************************************************************
1839 * _fputwchar (MSVCRT.@)
1841 MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
1843 return MSVCRT_fputwc(wc, MSVCRT_stdout);
1846 /*********************************************************************
1847 * fopen (MSVCRT.@)
1849 MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
1851 MSVCRT_FILE* file;
1852 int flags = 0, plus = 0, fd;
1853 const char* search = mode;
1855 TRACE("(%s,%s)\n",path,mode);
1857 while (*search)
1858 if (*search++ == '+')
1859 plus = 1;
1861 /* map mode string to open() flags. "man fopen" for possibilities. */
1862 switch(*mode++)
1864 case 'R': case 'r':
1865 flags = (plus ? _O_RDWR : _O_RDONLY);
1866 break;
1867 case 'W': case 'w':
1868 flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1869 break;
1870 case 'A': case 'a':
1871 flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1872 break;
1873 default:
1874 return NULL;
1877 while (*mode)
1878 switch (*mode++)
1880 case 'B': case 'b':
1881 flags |= _O_BINARY;
1882 flags &= ~_O_TEXT;
1883 break;
1884 case 'T': case 't':
1885 flags |= _O_TEXT;
1886 flags &= ~_O_BINARY;
1887 break;
1888 case '+':
1889 break;
1890 default:
1891 FIXME(":unknown flag %c not supported\n",mode[-1]);
1894 fd = _open(path, flags);
1896 if (fd < 0)
1897 return NULL;
1899 file = msvcrt_alloc_fp(fd);
1900 TRACE(":got (%p)\n",file);
1901 if (!file)
1902 _close(fd);
1904 return file;
1907 /*********************************************************************
1908 * _wfopen (MSVCRT.@)
1910 MSVCRT_FILE *_wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode)
1912 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
1913 char *patha = MSVCRT_calloc(plen + 1, 1);
1914 char *modea = MSVCRT_calloc(mlen + 1, 1);
1916 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
1918 if (patha && modea &&
1919 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
1920 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1922 MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
1923 MSVCRT_free(patha);
1924 MSVCRT_free(modea);
1925 return retval;
1928 MSVCRT__set_errno(GetLastError());
1929 return NULL;
1932 /*********************************************************************
1933 * _fsopen (MSVCRT.@)
1935 MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
1937 FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
1938 return MSVCRT_fopen(path,mode);
1941 /*********************************************************************
1942 * _wfsopen (MSVCRT.@)
1944 MSVCRT_FILE* _wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share)
1946 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1947 debugstr_w(path),debugstr_w(mode),share);
1948 return _wfopen(path,mode);
1951 /*********************************************************************
1952 * fputc (MSVCRT.@)
1954 int MSVCRT_fputc(int c, MSVCRT_FILE* file)
1956 if(file->_cnt>0) {
1957 *file->_ptr++=c;
1958 file->_cnt--;
1959 return c;
1960 } else {
1961 return _flsbuf(c, file);
1965 /*********************************************************************
1966 * _flsbuf (MSVCRT.@)
1968 int _flsbuf(int c, MSVCRT_FILE* file)
1970 /* Flush output buffer */
1971 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
1972 msvcrt_alloc_buffer(file);
1974 if(!(file->_flag & MSVCRT__IOWRT)) {
1975 if(file->_flag & MSVCRT__IORW) {
1976 file->_flag |= MSVCRT__IOWRT;
1977 } else {
1978 return MSVCRT_EOF;
1981 if(file->_bufsiz) {
1982 int res=msvcrt_flush_buffer(file);
1983 return res?res : MSVCRT_fputc(c, file);
1984 } else {
1985 unsigned char cc=c;
1986 return _write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF;
1990 /*********************************************************************
1991 * _fputchar (MSVCRT.@)
1993 int _fputchar(int c)
1995 return MSVCRT_fputc(c, MSVCRT_stdout);
1998 /*********************************************************************
1999 * fread (MSVCRT.@)
2001 MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2002 { MSVCRT_size_t rcnt=size * nmemb;
2003 MSVCRT_size_t read=0;
2004 int pread=0;
2005 /* first buffered data */
2006 if(file->_cnt>0) {
2007 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2008 memcpy(ptr, file->_ptr, pcnt);
2009 file->_cnt -= pcnt;
2010 file->_ptr += pcnt;
2011 read += pcnt ;
2012 rcnt -= pcnt ;
2013 ptr = (char*)ptr + pcnt;
2014 } else if(!(file->_flag & MSVCRT__IOREAD )) {
2015 if(file->_flag & MSVCRT__IORW) {
2016 file->_flag |= MSVCRT__IOREAD;
2017 } else
2018 return 0;
2020 if(rcnt) pread = _read(file->_file,ptr, rcnt);
2021 if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF)
2022 /* expose feof condition in the flags
2023 MFC tests file->_flag for feof, and doesn't not call feof())
2025 file->_flag |= MSVCRT__IOEOF;
2026 if (pread <= 0)
2027 pread = 0;
2028 read+=pread;
2029 return read / size;
2032 /*********************************************************************
2033 * freopen (MSVCRT.@)
2036 MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
2038 MSVCRT_FILE* newfile;
2039 int fd;
2041 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2042 if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
2043 return NULL;
2045 if (fd > 2)
2047 #if 0
2048 FIXME(":reopen on user file not implemented!\n");
2049 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
2050 return NULL;
2051 #endif
2052 if(MSVCRT_fclose(file))
2053 return NULL;
2054 return MSVCRT_fopen(path, mode);
2057 /* first, create the new file */
2058 if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
2059 return NULL;
2061 if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
2062 (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
2063 MSVCRT_handles[newfile->_file]))
2065 /* Redirecting std handle to file , copy over.. */
2066 MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
2067 MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
2068 memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
2069 MSVCRT__iob[fd]._file = fd;
2070 /* And free up the resources allocated by fopen, but
2071 * not the HANDLE we copied. */
2072 MSVCRT_free(MSVCRT_files[fd]);
2073 msvcrt_free_fd(newfile->_file);
2074 return &MSVCRT__iob[fd];
2077 WARN(":failed-last error (%ld)\n",GetLastError());
2078 MSVCRT_fclose(newfile);
2079 MSVCRT__set_errno(GetLastError());
2080 return NULL;
2083 /*********************************************************************
2084 * fsetpos (MSVCRT.@)
2086 int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
2088 return _lseek(file->_file,*pos,SEEK_SET);
2091 /*********************************************************************
2092 * fseek (MSVCRT.@)
2094 int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
2096 /* Flush output if needed */
2097 if(file->_flag & MSVCRT__IOWRT)
2098 msvcrt_flush_buffer(file);
2100 if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
2101 offset -= file->_cnt;
2103 /* Discard buffered input */
2104 file->_cnt = 0;
2105 file->_ptr = file->_base;
2106 /* Reset direction of i/o */
2107 if(file->_flag & MSVCRT__IORW) {
2108 file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
2110 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
2113 /*********************************************************************
2114 * ftell (MSVCRT.@)
2116 LONG MSVCRT_ftell(MSVCRT_FILE* file)
2118 int off=0;
2119 long pos;
2120 if(file->_bufsiz) {
2121 if( file->_flag & MSVCRT__IOWRT ) {
2122 off = file->_ptr - file->_base;
2123 } else {
2124 off = -file->_cnt;
2127 pos = _tell(file->_file);
2128 if(pos == -1) return pos;
2129 return off + pos;
2132 /*********************************************************************
2133 * fwrite (MSVCRT.@)
2135 MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
2137 MSVCRT_size_t wrcnt=size * nmemb;
2138 int written = 0;
2139 if (size == 0)
2140 return 0;
2141 if(file->_cnt) {
2142 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2143 memcpy(file->_ptr, ptr, pcnt);
2144 file->_cnt -= pcnt;
2145 file->_ptr += pcnt;
2146 written = pcnt;
2147 wrcnt -= pcnt;
2148 ptr = (char*)ptr + pcnt;
2149 } else if(!(file->_flag & MSVCRT__IOWRT)) {
2150 if(file->_flag & MSVCRT__IORW) {
2151 file->_flag |= MSVCRT__IOWRT;
2152 } else
2153 return 0;
2155 if(wrcnt) {
2156 /* Flush buffer */
2157 int res=msvcrt_flush_buffer(file);
2158 if(!res) {
2159 int pwritten = _write(file->_file, ptr, wrcnt);
2160 if (pwritten <= 0) pwritten=0;
2161 written += pwritten;
2164 return written / size;
2167 /*********************************************************************
2168 * fputs (MSVCRT.@)
2170 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
2172 size_t i, len = strlen(s);
2173 if (file->_flag & _O_BINARY)
2174 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2175 for (i=0; i<len; i++)
2176 if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
2177 return MSVCRT_EOF;
2178 return 0;
2181 /*********************************************************************
2182 * fputws (MSVCRT.@)
2184 int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
2186 size_t i, len = strlenW(s);
2187 if (file->_flag & _O_BINARY)
2188 return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
2189 for (i=0; i<len; i++)
2191 if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
2192 return MSVCRT_WEOF;
2193 if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
2194 return MSVCRT_WEOF;
2196 return 0;
2199 /*********************************************************************
2200 * getchar (MSVCRT.@)
2202 int MSVCRT_getchar(void)
2204 return MSVCRT_fgetc(MSVCRT_stdin);
2207 /*********************************************************************
2208 * getc (MSVCRT.@)
2210 int MSVCRT_getc(MSVCRT_FILE* file)
2212 return MSVCRT_fgetc(file);
2215 /*********************************************************************
2216 * gets (MSVCRT.@)
2218 char *MSVCRT_gets(char *buf)
2220 int cc;
2221 char * buf_start = buf;
2223 for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
2224 cc = MSVCRT_fgetc(MSVCRT_stdin))
2225 if(cc != '\r') *buf++ = (char)cc;
2227 *buf = '\0';
2229 TRACE("got '%s'\n", buf_start);
2230 return buf_start;
2233 /*********************************************************************
2234 * _getws (MSVCRT.@)
2236 MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
2238 MSVCRT_wint_t cc;
2239 MSVCRT_wchar_t* ws = buf;
2241 for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
2242 cc = MSVCRT_fgetwc(MSVCRT_stdin))
2244 if (cc != '\r')
2245 *buf++ = (MSVCRT_wchar_t)cc;
2247 *buf = '\0';
2249 TRACE("got '%s'\n", debugstr_w(ws));
2250 return ws;
2253 /*********************************************************************
2254 * putc (MSVCRT.@)
2256 int MSVCRT_putc(int c, MSVCRT_FILE* file)
2258 return MSVCRT_fputc(c, file);
2261 /*********************************************************************
2262 * putchar (MSVCRT.@)
2264 int MSVCRT_putchar(int c)
2266 return MSVCRT_fputc(c, MSVCRT_stdout);
2269 /*********************************************************************
2270 * puts (MSVCRT.@)
2272 int MSVCRT_puts(const char *s)
2274 size_t len = strlen(s);
2275 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2276 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2279 /*********************************************************************
2280 * _putws (MSVCRT.@)
2282 int _putws(const MSVCRT_wchar_t *s)
2284 static const MSVCRT_wchar_t nl = '\n';
2285 size_t len = strlenW(s);
2286 if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
2287 return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
2290 /*********************************************************************
2291 * remove (MSVCRT.@)
2293 int MSVCRT_remove(const char *path)
2295 TRACE("(%s)\n",path);
2296 if (DeleteFileA(path))
2297 return 0;
2298 TRACE(":failed (%ld)\n",GetLastError());
2299 MSVCRT__set_errno(GetLastError());
2300 return -1;
2303 /*********************************************************************
2304 * _wremove (MSVCRT.@)
2306 int _wremove(const MSVCRT_wchar_t *path)
2308 TRACE("(%s)\n",debugstr_w(path));
2309 if (DeleteFileW(path))
2310 return 0;
2311 TRACE(":failed (%ld)\n",GetLastError());
2312 MSVCRT__set_errno(GetLastError());
2313 return -1;
2316 /*********************************************************************
2317 * scanf (MSVCRT.@)
2319 int MSVCRT_scanf(const char *format, ...)
2321 va_list valist;
2322 int res;
2324 va_start(valist, format);
2325 res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
2326 va_end(valist);
2327 return res;
2330 /*********************************************************************
2331 * wscanf (MSVCRT.@)
2333 int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
2335 va_list valist;
2336 int res;
2338 va_start(valist, format);
2339 res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist);
2340 va_end(valist);
2341 return res;
2344 /*********************************************************************
2345 * rename (MSVCRT.@)
2347 int MSVCRT_rename(const char *oldpath,const char *newpath)
2349 TRACE(":from %s to %s\n",oldpath,newpath);
2350 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2351 return 0;
2352 TRACE(":failed (%ld)\n",GetLastError());
2353 MSVCRT__set_errno(GetLastError());
2354 return -1;
2357 /*********************************************************************
2358 * _wrename (MSVCRT.@)
2360 int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
2362 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2363 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2364 return 0;
2365 TRACE(":failed (%ld)\n",GetLastError());
2366 MSVCRT__set_errno(GetLastError());
2367 return -1;
2370 /*********************************************************************
2371 * setvbuf (MSVCRT.@)
2373 int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
2375 /* TODO: Check if file busy */
2376 if(file->_bufsiz) {
2377 MSVCRT_free(file->_base);
2378 file->_bufsiz = 0;
2379 file->_cnt = 0;
2381 if(mode == MSVCRT__IOFBF) {
2382 file->_flag &= ~MSVCRT__IONBF;
2383 file->_base = file->_ptr = buf;
2384 if(buf) {
2385 file->_bufsiz = size;
2387 } else {
2388 file->_flag |= MSVCRT__IONBF;
2390 return 0;
2393 /*********************************************************************
2394 * setbuf (MSVCRT.@)
2396 void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
2398 MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
2401 /*********************************************************************
2402 * tmpnam (MSVCRT.@)
2404 char *MSVCRT_tmpnam(char *s)
2406 char tmpbuf[MAX_PATH];
2407 char* prefix = "TMP";
2408 if (!GetTempPathA(MAX_PATH,tmpbuf) ||
2409 !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
2411 TRACE(":failed-last error (%ld)\n",GetLastError());
2412 return NULL;
2414 TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
2415 s = MSVCRT_tmpname;
2416 return s;
2419 /*********************************************************************
2420 * tmpfile (MSVCRT.@)
2422 MSVCRT_FILE* MSVCRT_tmpfile(void)
2424 char *filename = MSVCRT_tmpnam(NULL);
2425 int fd;
2426 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2427 if (fd != -1)
2428 return msvcrt_alloc_fp(fd);
2429 return NULL;
2432 /*********************************************************************
2433 * vfprintf (MSVCRT.@)
2435 int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
2437 char buf[2048], *mem = buf;
2438 int written, resize = sizeof(buf), retval;
2439 /* There are two conventions for vsnprintf failing:
2440 * Return -1 if we truncated, or
2441 * Return the number of bytes that would have been written
2442 * The code below handles both cases
2444 while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
2445 written > resize)
2447 resize = (written == -1 ? resize * 2 : written + 1);
2448 if (mem != buf)
2449 MSVCRT_free (mem);
2450 if (!(mem = (char *)MSVCRT_malloc(resize)))
2451 return MSVCRT_EOF;
2453 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2454 if (mem != buf)
2455 MSVCRT_free (mem);
2456 return retval;
2459 /*********************************************************************
2460 * vfwprintf (MSVCRT.@)
2461 * FIXME:
2462 * Is final char included in written (then resize is too big) or not
2463 * (then we must test for equality too)?
2465 int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
2467 MSVCRT_wchar_t buf[2048], *mem = buf;
2468 int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
2469 /* See vfprintf comments */
2470 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2471 written > resize)
2473 resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
2474 if (mem != buf)
2475 MSVCRT_free (mem);
2476 if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
2477 return MSVCRT_EOF;
2479 retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
2480 if (mem != buf)
2481 MSVCRT_free (mem);
2482 return retval;
2485 /*********************************************************************
2486 * vprintf (MSVCRT.@)
2488 int MSVCRT_vprintf(const char *format, va_list valist)
2490 return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
2493 /*********************************************************************
2494 * vwprintf (MSVCRT.@)
2496 int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist)
2498 return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
2501 /*********************************************************************
2502 * fprintf (MSVCRT.@)
2504 int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
2506 va_list valist;
2507 int res;
2508 va_start(valist, format);
2509 res = MSVCRT_vfprintf(file, format, valist);
2510 va_end(valist);
2511 return res;
2514 /*********************************************************************
2515 * fwprintf (MSVCRT.@)
2517 int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...)
2519 va_list valist;
2520 int res;
2521 va_start(valist, format);
2522 res = MSVCRT_vfwprintf(file, format, valist);
2523 va_end(valist);
2524 return res;
2527 /*********************************************************************
2528 * printf (MSVCRT.@)
2530 int MSVCRT_printf(const char *format, ...)
2532 va_list valist;
2533 int res;
2534 va_start(valist, format);
2535 res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
2536 va_end(valist);
2537 return res;
2540 /*********************************************************************
2541 * ungetc (MSVCRT.@)
2543 int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
2545 if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
2546 msvcrt_alloc_buffer(file);
2547 file->_ptr++;
2549 if(file->_ptr>file->_base) {
2550 file->_ptr--;
2551 *file->_ptr=c;
2552 file->_cnt++;
2553 return c;
2555 return MSVCRT_EOF;
2558 /*********************************************************************
2559 * ungetwc (MSVCRT.@)
2561 MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
2563 MSVCRT_wchar_t mwc = wc;
2564 char * pp = (char *)&mwc;
2565 int i;
2566 for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
2567 if(pp[i] != MSVCRT_ungetc(pp[i],file))
2568 return MSVCRT_WEOF;
2570 return mwc;
2573 /*********************************************************************
2574 * wprintf (MSVCRT.@)
2576 int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...)
2578 va_list valist;
2579 int res;
2580 va_start(valist, format);
2581 res = MSVCRT_vwprintf(format, valist);
2582 va_end(valist);
2583 return res;