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
25 #include "wine/port.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
);
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
);
141 return; /* don't use 0,1,2 for user files */
142 if (fd
== MSVCRT_fdend
- 1)
144 if (fd
< MSVCRT_fdstart
)
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");
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
;
166 while(MSVCRT_fdstart
< MSVCRT_fdend
&&
167 MSVCRT_handles
[MSVCRT_fdstart
] != INVALID_HANDLE_VALUE
)
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
);
185 *MSVCRT__errno() = MSVCRT_EBADF
;
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)
206 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
207 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE
),
208 GetCurrentProcess(), &MSVCRT_handles
[0], 0, FALSE
, DUPLICATE_SAME_ACCESS
);
209 MSVCRT_flags
[0] = MSVCRT__iob
[0]._flag
= MSVCRT__IOREAD
;
210 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE
),
211 GetCurrentProcess(), &MSVCRT_handles
[1], 0, FALSE
, DUPLICATE_SAME_ACCESS
);
212 MSVCRT_flags
[1] = MSVCRT__iob
[1]._flag
= MSVCRT__IOWRT
;
213 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE
),
214 GetCurrentProcess(), &MSVCRT_handles
[2], 0, FALSE
, DUPLICATE_SAME_ACCESS
);
215 MSVCRT_flags
[2] = MSVCRT__iob
[2]._flag
= MSVCRT__IOWRT
;
217 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles
[0],
218 MSVCRT_handles
[1],MSVCRT_handles
[2]);
220 for (i
= 0; i
< 3; i
++)
222 /* FILE structs for stdin/out/err are static and never deleted */
223 MSVCRT_files
[i
] = &MSVCRT__iob
[i
];
224 MSVCRT__iob
[i
]._file
= i
;
225 MSVCRT_tempfiles
[i
] = NULL
;
229 /* free everything on process exit */
230 void msvcrt_free_io(void)
238 /* INTERNAL: Flush stdio file buffer */
239 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
242 int cnt
=file
->_ptr
-file
->_base
;
243 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
246 file
->_ptr
=file
->_base
;
247 file
->_cnt
=file
->_bufsiz
;
252 /* INTERNAL: Allocate stdio file buffer */
253 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
255 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
257 file
->_bufsiz
= MSVCRT_BUFSIZ
;
258 file
->_flag
|= MSVCRT__IOMYBUF
;
260 file
->_base
= (unsigned char *)(&file
->_charbuf
);
262 file
->_bufsiz
= sizeof(file
->_charbuf
);
264 file
->_ptr
= file
->_base
;
268 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
269 static void msvcrt_int_to_base32(int num
, char *str
)
284 *p
= (num
& 31) + '0';
286 *p
+= ('a' - '0' - 10);
291 /*********************************************************************
294 MSVCRT_FILE
*__p__iob(void)
296 return &MSVCRT__iob
[0];
299 /*********************************************************************
302 int _access(const char *filename
, int mode
)
304 DWORD attr
= GetFileAttributesA(filename
);
306 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
308 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
310 MSVCRT__set_errno(GetLastError());
313 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
315 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
321 /*********************************************************************
322 * _waccess (MSVCRT.@)
324 int _waccess(const MSVCRT_wchar_t
*filename
, int mode
)
326 DWORD attr
= GetFileAttributesW(filename
);
328 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
330 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
332 MSVCRT__set_errno(GetLastError());
335 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
337 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
343 /*********************************************************************
346 int _chmod(const char *path
, int flags
)
348 DWORD oldFlags
= GetFileAttributesA(path
);
350 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
352 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
353 oldFlags
| FILE_ATTRIBUTE_READONLY
;
355 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
358 MSVCRT__set_errno(GetLastError());
362 /*********************************************************************
365 int _wchmod(const MSVCRT_wchar_t
*path
, int flags
)
367 DWORD oldFlags
= GetFileAttributesW(path
);
369 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
371 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
372 oldFlags
| FILE_ATTRIBUTE_READONLY
;
374 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
377 MSVCRT__set_errno(GetLastError());
381 /*********************************************************************
384 int _chsize(int fd
, long size
)
386 FIXME("(fd=%d, size=%ld): stub\n", fd
, size
);
390 /*********************************************************************
395 FIXME("(od=%d): stub\n", od
);
399 /*********************************************************************
402 int _dup2(int od
, int nd
)
404 FIXME("(od=%d, nd=%d): stub\n", od
, nd
);
408 /*********************************************************************
411 int _unlink(const char *path
)
413 TRACE("(%s)\n",path
);
414 if(DeleteFileA(path
))
416 TRACE("failed (%ld)\n",GetLastError());
417 MSVCRT__set_errno(GetLastError());
421 /*********************************************************************
422 * _wunlink (MSVCRT.@)
424 int _wunlink(const MSVCRT_wchar_t
*path
)
426 TRACE("(%s)\n",debugstr_w(path
));
427 if(DeleteFileW(path
))
429 TRACE("failed (%ld)\n",GetLastError());
430 MSVCRT__set_errno(GetLastError());
434 /*********************************************************************
439 HANDLE hand
= msvcrt_fdtoh(fd
);
441 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
442 if (hand
== INVALID_HANDLE_VALUE
)
444 /* flush stdio buffers */
445 if(MSVCRT_files
[fd
]) {
446 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOWRT
)
447 MSVCRT_fflush(MSVCRT_files
[fd
]);
449 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOMYBUF
)
450 MSVCRT_free(MSVCRT_files
[fd
]->_base
);
453 /* Don't free std FILE*'s, they are not dynamic */
454 if (fd
> 2 && MSVCRT_files
[fd
])
455 MSVCRT_free(MSVCRT_files
[fd
]);
459 if (!CloseHandle(hand
))
461 WARN(":failed-last error (%ld)\n",GetLastError());
462 MSVCRT__set_errno(GetLastError());
465 if (MSVCRT_tempfiles
[fd
])
467 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles
[fd
]);
468 _unlink(MSVCRT_tempfiles
[fd
]);
469 MSVCRT_free(MSVCRT_tempfiles
[fd
]);
470 MSVCRT_tempfiles
[fd
] = NULL
;
477 /*********************************************************************
482 HANDLE hand
= msvcrt_fdtoh(fd
);
484 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
485 if (hand
== INVALID_HANDLE_VALUE
)
488 if (!FlushFileBuffers(hand
))
490 if (GetLastError() == ERROR_INVALID_HANDLE
)
492 /* FlushFileBuffers fails for console handles
493 * so we ignore this error.
497 TRACE(":failed-last error (%ld)\n",GetLastError());
498 MSVCRT__set_errno(GetLastError());
505 /*********************************************************************
511 HANDLE hand
= msvcrt_fdtoh(fd
);
513 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
515 if (hand
== INVALID_HANDLE_VALUE
)
518 /* If we have a FILE* for this file, the EOF flag
519 * will be set by the read()/write() functions.
521 if (MSVCRT_files
[fd
])
522 return MSVCRT_flags
[fd
] & MSVCRT__IOEOF
;
524 /* Otherwise we do it the hard way */
525 curpos
= SetFilePointer(hand
, 0, NULL
, SEEK_CUR
);
526 endpos
= SetFilePointer(hand
, 0, NULL
, FILE_END
);
528 if (curpos
== endpos
)
531 SetFilePointer(hand
, curpos
, 0, FILE_BEGIN
);
535 /*********************************************************************
536 * _fcloseall (MSVCRT.@)
540 int num_closed
= 0, i
;
542 for (i
= 3; i
< MSVCRT_fdend
; i
++)
543 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
549 TRACE(":closed (%d) handles\n",num_closed
);
553 /*********************************************************************
554 * _lseeki64 (MSVCRT.@)
556 __int64
_lseeki64(int fd
, __int64 offset
, int whence
)
558 DWORD ret
, hoffset
= (DWORD
) (offset
>> 32);
559 HANDLE hand
= msvcrt_fdtoh(fd
);
561 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
562 if (hand
== INVALID_HANDLE_VALUE
)
565 if (whence
< 0 || whence
> 2)
567 *MSVCRT__errno() = MSVCRT_EINVAL
;
571 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
572 fd
,hoffset
,(long)offset
,
573 (whence
==SEEK_SET
)?"SEEK_SET":
574 (whence
==SEEK_CUR
)?"SEEK_CUR":
575 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
577 if (((ret
= SetFilePointer(hand
, (long)offset
, &hoffset
,
578 whence
)) != INVALID_SET_FILE_POINTER
) || !GetLastError())
580 if (MSVCRT_files
[fd
])
581 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOEOF
;
582 /* FIXME: What if we seek _to_ EOF - is EOF set? */
584 return ((__int64
)hoffset
<< 32) | ret
;
586 TRACE(":error-last error (%ld)\n",GetLastError());
587 if (MSVCRT_files
[fd
])
588 switch(GetLastError())
590 case ERROR_NEGATIVE_SEEK
:
591 case ERROR_SEEK_ON_DEVICE
:
592 MSVCRT__set_errno(GetLastError());
593 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
601 /*********************************************************************
604 LONG
_lseek(int fd
, LONG offset
, int whence
)
606 return _lseeki64(fd
, offset
, whence
);
609 /*********************************************************************
610 * _locking (MSVCRT.@)
612 * This is untested; the underlying LockFile doesn't work yet.
614 int _locking(int fd
, int mode
, LONG nbytes
)
618 HANDLE hand
= msvcrt_fdtoh(fd
);
620 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
621 if (hand
== INVALID_HANDLE_VALUE
)
624 if (mode
< 0 || mode
> 4)
626 *MSVCRT__errno() = MSVCRT_EINVAL
;
630 TRACE(":fd (%d) by 0x%08lx mode %s\n",
631 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
632 (mode
==_LK_LOCK
)?"_LK_LOCK":
633 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
634 (mode
==_LK_RLCK
)?"_LK_RLCK":
635 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
638 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
640 FIXME ("Seek failed\n");
641 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
644 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
647 ret
= 1; /* just to satisfy gcc */
650 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
655 else if (mode
== _LK_UNLCK
)
656 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
658 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
659 /* FIXME - what about error settings? */
663 /*********************************************************************
666 void MSVCRT_rewind(MSVCRT_FILE
* file
)
668 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
669 MSVCRT_fseek(file
, 0L, SEEK_SET
);
670 MSVCRT_clearerr(file
);
673 /*********************************************************************
676 MSVCRT_FILE
* _fdopen(int fd
, const char *mode
)
678 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
680 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
685 /*********************************************************************
686 * _wfdopen (MSVCRT.@)
688 MSVCRT_FILE
* _wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
690 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
692 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
699 /*********************************************************************
700 * _filelength (MSVCRT.@)
702 LONG
_filelength(int fd
)
704 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
707 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
710 if (endPos
!= curPos
)
711 _lseek(fd
, curPos
, SEEK_SET
);
718 /*********************************************************************
719 * _filelengthi64 (MSVCRT.@)
721 __int64
_filelengthi64(int fd
)
723 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
726 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
729 if (endPos
!= curPos
)
730 _lseeki64(fd
, curPos
, SEEK_SET
);
737 /*********************************************************************
740 int _fileno(MSVCRT_FILE
* file
)
742 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
746 /*********************************************************************
747 * _flushall (MSVCRT.@)
751 int num_flushed
= 0, i
= 3;
753 while(i
< MSVCRT_fdend
)
754 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
757 /* FIXME: flush, do not commit */
758 if (_commit(i
) == -1)
760 MSVCRT_files
[i
]->_flag
|= MSVCRT__IOERR
;
762 if(MSVCRT_files
[i
] && MSVCRT_files
[i
]->_flag
& MSVCRT__IOWRT
) {
763 MSVCRT_fflush(MSVCRT_files
[i
]);
768 TRACE(":flushed (%d) handles\n",num_flushed
);
772 /*********************************************************************
773 * _fstati64 (MSVCRT.@)
775 int _fstati64(int fd
, struct _stati64
* buf
)
778 BY_HANDLE_FILE_INFORMATION hfi
;
779 HANDLE hand
= msvcrt_fdtoh(fd
);
781 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
782 if (hand
== INVALID_HANDLE_VALUE
)
787 WARN(":failed-NULL buf\n");
788 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
792 memset(&hfi
, 0, sizeof(hfi
));
793 memset(buf
, 0, sizeof(struct _stati64
));
794 if (!GetFileInformationByHandle(hand
, &hfi
))
796 WARN(":failed-last error (%ld)\n",GetLastError());
797 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
800 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi
.dwFileAttributes
);
801 buf
->st_nlink
= hfi
.nNumberOfLinks
;
802 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
803 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
805 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
806 buf
->st_mtime
= buf
->st_ctime
= dw
;
810 /*********************************************************************
813 int MSVCRT__fstat(int fd
, struct _stat
* buf
)
815 struct _stati64 bufi64
;
817 ret
= _fstati64(fd
, &bufi64
);
819 msvcrt_cp_from_stati64(&bufi64
, buf
);
823 /*********************************************************************
826 int _futime(int fd
, struct _utimbuf
*t
)
828 HANDLE hand
= msvcrt_fdtoh(fd
);
833 MSVCRT_time_t currTime
;
834 MSVCRT_time(&currTime
);
835 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
836 memcpy(&wt
, &at
, sizeof(wt
));
840 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
841 if (t
->actime
== t
->modtime
)
842 memcpy(&wt
, &at
, sizeof(wt
));
844 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
847 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
849 MSVCRT__set_errno(GetLastError());
855 /*********************************************************************
856 * _get_osfhandle (MSVCRT.@)
858 long _get_osfhandle(int fd
)
860 HANDLE hand
= msvcrt_fdtoh(fd
);
861 HANDLE newhand
= hand
;
862 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
864 if (hand
!= INVALID_HANDLE_VALUE
)
866 /* FIXME: I'm not convinced that I should be copying the
867 * handle here - it may be leaked if the app doesn't
868 * close it (and the API docs don't say that it should)
869 * Not duplicating it means that it can't be inherited
870 * and so lcc's wedit doesn't cope when it passes it to
871 * child processes. I've an idea that it should either
872 * be copied by CreateProcess, or marked as inheritable
873 * when initialised, or maybe both? JG 21-9-00.
875 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
876 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
878 return (long)newhand
;
881 /*********************************************************************
886 HANDLE hand
= msvcrt_fdtoh(fd
);
888 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
889 if (hand
== INVALID_HANDLE_VALUE
)
892 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
895 /*********************************************************************
898 char *_mktemp(char *pattern
)
901 char *retVal
= pattern
;
906 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
910 id
= GetCurrentProcessId();
914 int tempNum
= id
/ 10;
915 *pattern
-- = id
- (tempNum
* 10) + '0';
921 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
922 GetLastError() == ERROR_FILE_NOT_FOUND
)
925 } while(letter
!= '|');
929 /*********************************************************************
930 * _wmktemp (MSVCRT.@)
932 MSVCRT_wchar_t
*_wmktemp(MSVCRT_wchar_t
*pattern
)
935 MSVCRT_wchar_t
*retVal
= pattern
;
937 MSVCRT_wchar_t letter
= 'a';
940 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
944 id
= GetCurrentProcessId();
948 int tempNum
= id
/ 10;
949 *pattern
-- = id
- (tempNum
* 10) + '0';
955 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
956 GetLastError() == ERROR_FILE_NOT_FOUND
)
959 } while(letter
!= '|');
963 /*********************************************************************
966 int MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
970 DWORD access
= 0, creation
= 0;
974 SECURITY_ATTRIBUTES sa
;
977 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
978 path
, oflags
, shflags
);
980 switch(oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
983 access
|= GENERIC_READ
;
984 ioflag
|= MSVCRT__IOREAD
;
987 access
|= GENERIC_WRITE
;
988 ioflag
|= MSVCRT__IOWRT
;
991 access
|= GENERIC_WRITE
| GENERIC_READ
;
992 ioflag
|= MSVCRT__IORW
;
996 if (oflags
& _O_CREAT
)
998 va_start(ap
, shflags
);
999 pmode
= va_arg(ap
, int);
1002 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1003 FIXME(": pmode 0x%04x ignored\n", pmode
);
1005 WARN(": pmode 0x%04x ignored\n", pmode
);
1007 if (oflags
& _O_EXCL
)
1008 creation
= CREATE_NEW
;
1009 else if (oflags
& _O_TRUNC
)
1010 creation
= CREATE_ALWAYS
;
1012 creation
= OPEN_ALWAYS
;
1014 else /* no _O_CREAT */
1016 if (oflags
& _O_TRUNC
)
1017 creation
= TRUNCATE_EXISTING
;
1019 creation
= OPEN_EXISTING
;
1021 if (oflags
& _O_APPEND
)
1022 ioflag
|= MSVCRT__IOAPPEND
;
1024 if (oflags
& _O_BINARY
)
1025 ioflag
|= _O_BINARY
;
1026 else if (oflags
& _O_TEXT
)
1028 else if (*__p__fmode() & _O_BINARY
)
1029 ioflag
|= _O_BINARY
;
1031 ioflag
|= _O_TEXT
; /* default to TEXT*/
1039 sharing
= FILE_SHARE_READ
;
1042 sharing
= FILE_SHARE_WRITE
;
1045 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1048 ERR( "Unhandled shflags 0x%x\n", shflags
);
1052 if (oflags
& ~(_O_BINARY
|_O_TEXT
|_O_APPEND
|_O_TRUNC
|_O_EXCL
1053 |_O_CREAT
|_O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|_O_NOINHERIT
))
1054 ERR(":unsupported oflags 0x%04x\n",oflags
);
1056 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1057 sa
.lpSecurityDescriptor
= NULL
;
1058 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1060 hand
= CreateFileA(path
, access
, sharing
,
1061 &sa
, creation
, FILE_ATTRIBUTE_NORMAL
, 0);
1063 if (hand
== INVALID_HANDLE_VALUE
) {
1064 WARN(":failed-last error (%ld)\n",GetLastError());
1065 MSVCRT__set_errno(GetLastError());
1069 fd
= msvcrt_alloc_fd(hand
, ioflag
);
1071 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1075 if (oflags
& _O_TEMPORARY
)
1076 MSVCRT_tempfiles
[fd
] = _strdup(path
);
1077 if (ioflag
& MSVCRT__IOAPPEND
)
1078 _lseek(fd
, 0, FILE_END
);
1084 /*********************************************************************
1085 * _wsopen (MSVCRT.@)
1087 int MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1089 const unsigned int len
= strlenW(path
);
1090 char *patha
= MSVCRT_calloc(len
+ 1,1);
1094 va_start(ap
, shflags
);
1095 pmode
= va_arg(ap
, int);
1098 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1100 int retval
= MSVCRT__sopen(patha
,oflags
,shflags
,pmode
);
1105 MSVCRT__set_errno(GetLastError());
1109 /*********************************************************************
1112 int _open( const char *path
, int flags
, ... )
1116 if (flags
& _O_CREAT
)
1119 va_start(ap
, flags
);
1120 pmode
= va_arg(ap
, int);
1122 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
, pmode
);
1125 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
);
1128 /*********************************************************************
1131 int _wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1133 const unsigned int len
= strlenW(path
);
1134 char *patha
= MSVCRT_calloc(len
+ 1,1);
1138 va_start(ap
, flags
);
1139 pmode
= va_arg(ap
, int);
1142 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1144 int retval
= _open(patha
,flags
,pmode
);
1149 MSVCRT__set_errno(GetLastError());
1153 /*********************************************************************
1156 int _creat(const char *path
, int flags
)
1158 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1159 return _open(path
, usedFlags
);
1162 /*********************************************************************
1163 * _wcreat (MSVCRT.@)
1165 int _wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1167 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1168 return _wopen(path
, usedFlags
);
1171 /*********************************************************************
1172 * _open_osfhandle (MSVCRT.@)
1174 int _open_osfhandle(long hand
, int flags
)
1178 /* _O_RDONLY (0) always matches, so set the read flag
1179 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1180 * file, so set the write flag. It also only sets _O_TEXT if it wants
1181 * text - it never sets _O_BINARY.
1183 /* FIXME: handle more flags */
1184 flags
|= MSVCRT__IOREAD
|MSVCRT__IOWRT
;
1185 if ( !( flags
& _O_TEXT
) ) flags
|= _O_BINARY
;
1187 fd
= msvcrt_alloc_fd((HANDLE
)hand
,flags
);
1188 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand
,fd
, flags
);
1192 /*********************************************************************
1197 int num_removed
= 0, i
;
1199 for (i
= 3; i
< MSVCRT_fdend
; i
++)
1200 if (MSVCRT_tempfiles
[i
])
1207 TRACE(":removed (%d) temp files\n",num_removed
);
1211 /*********************************************************************
1212 * (internal) remove_cr
1214 * Remove all \r inplace.
1215 * return the number of \r removed
1217 static unsigned int remove_cr(char *buf
, unsigned int count
)
1221 for (i
= 0; i
< count
; i
++) if (buf
[i
] == '\r') break;
1222 for (j
= i
+ 1; j
< count
; j
++) if (buf
[j
] != '\r') buf
[i
++] = buf
[j
];
1226 /*********************************************************************
1229 int _read(int fd
, void *buf
, unsigned int count
)
1231 DWORD num_read
, all_read
=0;
1232 char *bufstart
= buf
;
1233 HANDLE hand
= msvcrt_fdtoh(fd
);
1235 /* Don't trace small reads, it gets *very* annoying */
1237 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1238 if (hand
== INVALID_HANDLE_VALUE
)
1241 /* Reading single bytes in O_TEXT mode makes things slow
1242 * So read big chunks, then remove the \r in memory and try reading
1243 * the rest until the request is satisfied or EOF is met
1245 while ( all_read
< count
)
1247 if (ReadFile(hand
, bufstart
+all_read
, count
- all_read
, &num_read
, NULL
))
1249 if (num_read
!= (count
- all_read
))
1252 if ( MSVCRT_files
[fd
])
1254 MSVCRT_flags
[fd
] |= MSVCRT__IOEOF
;
1256 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1259 if ((MSVCRT_flags
[fd
]& _O_BINARY
) != _O_BINARY
)
1260 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1261 all_read
+= num_read
;
1263 TRACE("%s\n",debugstr_an(buf
,all_read
));
1266 if ((MSVCRT_flags
[fd
]& _O_BINARY
) != _O_BINARY
)
1268 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1270 all_read
+= num_read
;
1274 TRACE(":failed-last error (%ld)\n",GetLastError());
1275 if (MSVCRT_files
[fd
])
1276 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1282 TRACE("%s\n",debugstr_an(buf
, all_read
));
1286 /*********************************************************************
1289 int _getw(MSVCRT_FILE
* file
)
1292 if (_read(file
->_file
, &i
, sizeof(int)) != 1)
1297 /*********************************************************************
1298 * _setmode (MSVCRT.@)
1300 int _setmode(int fd
,int mode
)
1302 int ret
= MSVCRT_flags
[fd
] & (_O_TEXT
| _O_BINARY
);
1303 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1304 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1305 MSVCRT_flags
[fd
] &= ~(_O_TEXT
|_O_BINARY
);
1306 MSVCRT_flags
[fd
] |= mode
& (_O_TEXT
| _O_BINARY
);
1310 /*********************************************************************
1311 * _stati64 (MSVCRT.@)
1313 int _stati64(const char* path
, struct _stati64
* buf
)
1316 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1317 unsigned short mode
= MSVCRT_S_IREAD
;
1320 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1322 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1324 TRACE("failed (%ld)\n",GetLastError());
1325 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1329 memset(buf
,0,sizeof(struct _stati64
));
1331 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1332 Bon 011120: This FIXME seems incorrect
1333 Also a letter as first char isn't enough to be classified
1336 if (isalpha(*path
)&& (*(path
+1)==':'))
1337 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1339 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1341 plen
= strlen(path
);
1343 /* Dir, or regular file? */
1344 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1345 (path
[plen
-1] == '\\'))
1346 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1351 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1353 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1354 (tolower(path
[plen
-3]) << 16);
1355 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1356 mode
|= MSVCRT_S_IEXEC
;
1360 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1361 mode
|= MSVCRT_S_IWRITE
;
1363 buf
->st_mode
= mode
;
1365 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1366 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1368 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1369 buf
->st_mtime
= buf
->st_ctime
= dw
;
1370 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1371 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1372 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1376 /*********************************************************************
1379 int MSVCRT__stat(const char* path
, struct _stat
* buf
)
1381 struct _stati64 bufi64
;
1383 ret
= _stati64( path
, &bufi64
);
1385 msvcrt_cp_from_stati64(&bufi64
, buf
);
1389 /*********************************************************************
1390 * _wstati64 (MSVCRT.@)
1392 int _wstati64(const MSVCRT_wchar_t
* path
, struct _stati64
* buf
)
1395 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1396 unsigned short mode
= MSVCRT_S_IREAD
;
1399 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1401 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1403 TRACE("failed (%ld)\n",GetLastError());
1404 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1408 memset(buf
,0,sizeof(struct _stat
));
1410 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1411 if (MSVCRT_iswalpha(*path
))
1412 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1414 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1416 plen
= strlenW(path
);
1418 /* Dir, or regular file? */
1419 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1420 (path
[plen
-1] == '\\'))
1421 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1426 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1428 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1429 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1430 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1431 mode
|= MSVCRT_S_IEXEC
;
1435 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1436 mode
|= MSVCRT_S_IWRITE
;
1438 buf
->st_mode
= mode
;
1440 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1441 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1443 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1444 buf
->st_mtime
= buf
->st_ctime
= dw
;
1445 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1446 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1447 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1451 /*********************************************************************
1454 int _wstat(const MSVCRT_wchar_t
* path
, struct _stat
* buf
)
1457 struct _stati64 bufi64
;
1459 ret
= _wstati64( path
, &bufi64
);
1460 if (!ret
) msvcrt_cp_from_stati64(&bufi64
, buf
);
1464 /*********************************************************************
1469 return _lseek(fd
, 0, SEEK_CUR
);
1472 /*********************************************************************
1473 * _telli64 (MSVCRT.@)
1475 __int64
_telli64(int fd
)
1477 return _lseeki64(fd
, 0, SEEK_CUR
);
1480 /*********************************************************************
1481 * _tempnam (MSVCRT.@)
1483 char *_tempnam(const char *dir
, const char *prefix
)
1485 char tmpbuf
[MAX_PATH
];
1487 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1488 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1490 TRACE("got name (%s)\n",tmpbuf
);
1491 DeleteFileA(tmpbuf
);
1492 return _strdup(tmpbuf
);
1494 TRACE("failed (%ld)\n",GetLastError());
1498 /*********************************************************************
1499 * _wtempnam (MSVCRT.@)
1501 MSVCRT_wchar_t
*_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
1503 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
1505 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1506 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1508 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1509 DeleteFileW(tmpbuf
);
1510 return _wcsdup(tmpbuf
);
1512 TRACE("failed (%ld)\n",GetLastError());
1516 /*********************************************************************
1519 int _umask(int umask
)
1521 int old_umask
= MSVCRT_umask
;
1522 TRACE("(%d)\n",umask
);
1523 MSVCRT_umask
= umask
;
1527 /*********************************************************************
1530 int _utime(const char* path
, struct _utimbuf
*t
)
1532 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
1536 int retVal
= _futime(fd
, t
);
1543 /*********************************************************************
1544 * _wutime (MSVCRT.@)
1546 int _wutime(const MSVCRT_wchar_t
* path
, struct _utimbuf
*t
)
1548 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
1552 int retVal
= _futime(fd
, t
);
1559 /*********************************************************************
1562 int _write(int fd
, const void* buf
, unsigned int count
)
1565 HANDLE hand
= msvcrt_fdtoh(fd
);
1567 /* Don't trace small writes, it gets *very* annoying */
1570 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1572 if (hand
== INVALID_HANDLE_VALUE
)
1574 *MSVCRT__errno() = MSVCRT_EBADF
;
1578 /* If appending, go to EOF */
1579 if (MSVCRT_flags
[fd
] & MSVCRT__IOAPPEND
)
1580 _lseek(fd
, 0, FILE_END
);
1582 if (MSVCRT_flags
[fd
] & _O_BINARY
)
1584 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1585 && (num_written
== count
))
1587 TRACE(":failed-last error (%ld)\n",GetLastError());
1588 if (MSVCRT_files
[fd
])
1590 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1591 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1596 unsigned int i
, j
, nr_lf
;
1597 char *s
=(char*)buf
, *buf_start
=(char*)buf
, *p
;
1598 /* find number of \n ( without preceeding \r */
1599 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1604 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1609 if ((p
= MSVCRT_malloc(count
+ nr_lf
)))
1611 for(s
=(char*)buf
, i
=0, j
=0; i
<count
; i
++)
1616 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1623 FIXME("Malloc failed\n");
1631 if ((WriteFile(hand
, p
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1633 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written
);
1634 if (MSVCRT_files
[fd
])
1636 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1637 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1640 return s
- buf_start
;
1653 /*********************************************************************
1656 int _putw(int val
, MSVCRT_FILE
* file
)
1658 return _write(file
->_file
, &val
, sizeof(val
)) == 1? val
: MSVCRT_EOF
;
1661 /*********************************************************************
1662 * clearerr (MSVCRT.@)
1664 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
1666 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1667 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1670 /*********************************************************************
1673 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1678 r
=_close(file
->_file
);
1679 return ((r
==MSVCRT_EOF
) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
1682 /*********************************************************************
1685 int MSVCRT_feof(MSVCRT_FILE
* file
)
1687 return file
->_flag
& MSVCRT__IOEOF
;
1690 /*********************************************************************
1693 int MSVCRT_ferror(MSVCRT_FILE
* file
)
1695 return file
->_flag
& MSVCRT__IOERR
;
1698 /*********************************************************************
1701 int MSVCRT_fflush(MSVCRT_FILE
* file
)
1707 int res
=msvcrt_flush_buffer(file
);
1712 /*********************************************************************
1715 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
1719 return *(unsigned char *)file
->_ptr
++;
1721 return _filbuf(file
);
1725 /*********************************************************************
1726 * _fgetchar (MSVCRT.@)
1730 return MSVCRT_fgetc(MSVCRT_stdin
);
1733 /*********************************************************************
1734 * _filbuf (MSVCRT.@)
1736 int _filbuf(MSVCRT_FILE
* file
)
1739 /* Allocate buffer if needed */
1740 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
1741 msvcrt_alloc_buffer(file
);
1743 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
1744 if(file
->_flag
& MSVCRT__IORW
) {
1745 file
->_flag
|= MSVCRT__IOREAD
;
1750 if(file
->_flag
& MSVCRT__IONBF
) {
1752 if (_read(file
->_file
,&c
,1) != 1) {
1753 file
->_flag
|= MSVCRT__IOEOF
;
1758 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
1759 if(file
->_cnt
<0) file
->_cnt
= 0;
1761 file
->_flag
|= MSVCRT__IOEOF
;
1765 file
->_ptr
= file
->_base
+1;
1766 return *(unsigned char *)file
->_base
;
1770 /*********************************************************************
1771 * fgetpos (MSVCRT.@)
1773 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1775 *pos
= MSVCRT_ftell(file
);
1776 return (*pos
== -1? -1 : 0);
1779 /*********************************************************************
1782 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1784 int cc
= MSVCRT_EOF
;
1785 char * buf_start
= s
;
1787 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1788 file
,file
->_file
,s
,size
);
1790 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
1795 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1797 TRACE(":nothing read\n");
1800 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
1803 TRACE(":got '%s'\n", debugstr_a(buf_start
));
1807 /*********************************************************************
1810 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1811 * the CR from CR/LF combinations
1813 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1817 if (file
->_flag
& _O_BINARY
)
1820 if (_read(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1824 c
= MSVCRT_fgetc(file
);
1825 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c
))
1827 FIXME("Treat Multibyte characters\n");
1829 if (c
== MSVCRT_EOF
)
1832 return (MSVCRT_wint_t
)c
;
1835 /*********************************************************************
1838 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
1840 return MSVCRT_fgetwc(file
);
1843 /*********************************************************************
1844 * _fgetwchar (MSVCRT.@)
1846 MSVCRT_wint_t
_fgetwchar(void)
1848 return MSVCRT_fgetwc(MSVCRT_stdin
);
1851 /*********************************************************************
1852 * getwchar (MSVCRT.@)
1854 MSVCRT_wint_t
MSVCRT_getwchar(void)
1856 return _fgetwchar();
1859 /*********************************************************************
1862 MSVCRT_wchar_t
*MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
1864 int cc
= MSVCRT_WEOF
;
1865 MSVCRT_wchar_t
* buf_start
= s
;
1867 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1868 file
,file
->_file
,s
,size
);
1870 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
1875 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1877 TRACE(":nothing read\n");
1880 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
1883 TRACE(":got %s\n", debugstr_w(buf_start
));
1888 /*********************************************************************
1891 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
1893 MSVCRT_wchar_t mwc
=wc
;
1894 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
1899 /*********************************************************************
1900 * _fputwchar (MSVCRT.@)
1902 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
1904 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
1907 /*********************************************************************
1910 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
1913 int flags
= 0, plus
= 0, fd
;
1914 const char* search
= mode
;
1916 TRACE("(%s,%s)\n",path
,mode
);
1919 if (*search
++ == '+')
1922 /* map mode string to open() flags. "man fopen" for possibilities. */
1926 flags
= (plus
? _O_RDWR
: _O_RDONLY
);
1929 flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1932 flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1947 flags
&= ~_O_BINARY
;
1952 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1955 fd
= _open(path
, flags
, _S_IREAD
| _S_IWRITE
);
1960 file
= msvcrt_alloc_fp(fd
);
1961 TRACE(":got (%p)\n",file
);
1968 /*********************************************************************
1969 * _wfopen (MSVCRT.@)
1971 MSVCRT_FILE
*_wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
1973 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
1974 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
1975 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
1977 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
1979 if (patha
&& modea
&&
1980 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
1981 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1983 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
1989 MSVCRT__set_errno(GetLastError());
1993 /*********************************************************************
1994 * _fsopen (MSVCRT.@)
1996 MSVCRT_FILE
* _fsopen(const char *path
, const char *mode
, int share
)
1998 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
1999 return MSVCRT_fopen(path
,mode
);
2002 /*********************************************************************
2003 * _wfsopen (MSVCRT.@)
2005 MSVCRT_FILE
* _wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2007 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2008 debugstr_w(path
),debugstr_w(mode
),share
);
2009 return _wfopen(path
,mode
);
2012 /*********************************************************************
2015 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2022 return _flsbuf(c
, file
);
2026 /*********************************************************************
2027 * _flsbuf (MSVCRT.@)
2029 int _flsbuf(int c
, MSVCRT_FILE
* file
)
2031 /* Flush output buffer */
2032 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2033 msvcrt_alloc_buffer(file
);
2035 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2036 if(file
->_flag
& MSVCRT__IORW
) {
2037 file
->_flag
|= MSVCRT__IOWRT
;
2043 int res
=msvcrt_flush_buffer(file
);
2044 return res
?res
: MSVCRT_fputc(c
, file
);
2047 return _write(file
->_file
, &cc
, 1) == 1? c
: MSVCRT_EOF
;
2051 /*********************************************************************
2052 * _fputchar (MSVCRT.@)
2054 int _fputchar(int c
)
2056 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2059 /*********************************************************************
2062 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2063 { MSVCRT_size_t rcnt
=size
* nmemb
;
2064 MSVCRT_size_t read
=0;
2066 /* first buffered data */
2068 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2069 memcpy(ptr
, file
->_ptr
, pcnt
);
2074 ptr
= (char*)ptr
+ pcnt
;
2075 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2076 if(file
->_flag
& MSVCRT__IORW
) {
2077 file
->_flag
|= MSVCRT__IOREAD
;
2081 if(rcnt
) pread
= _read(file
->_file
,ptr
, rcnt
);
2082 if (MSVCRT_flags
[file
->_file
] & MSVCRT__IOEOF
)
2083 /* expose feof condition in the flags
2084 MFC tests file->_flag for feof, and doesn't not call feof())
2086 file
->_flag
|= MSVCRT__IOEOF
;
2093 /*********************************************************************
2094 * freopen (MSVCRT.@)
2097 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
2099 MSVCRT_FILE
* newfile
;
2102 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2103 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2109 FIXME(":reopen on user file not implemented!\n");
2110 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED
);
2113 if(MSVCRT_fclose(file
))
2115 return MSVCRT_fopen(path
, mode
);
2118 /* first, create the new file */
2119 if ((newfile
= MSVCRT_fopen(path
,mode
)) == NULL
)
2122 if (fd
< 3 && SetStdHandle(fd
== 0 ? STD_INPUT_HANDLE
:
2123 (fd
== 1? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
),
2124 MSVCRT_handles
[newfile
->_file
]))
2126 /* Redirecting std handle to file , copy over.. */
2127 MSVCRT_handles
[fd
] = MSVCRT_handles
[newfile
->_file
];
2128 MSVCRT_flags
[fd
] = MSVCRT_flags
[newfile
->_file
];
2129 memcpy(&MSVCRT__iob
[fd
], newfile
, sizeof (MSVCRT_FILE
));
2130 MSVCRT__iob
[fd
]._file
= fd
;
2131 /* And free up the resources allocated by fopen, but
2132 * not the HANDLE we copied. */
2133 MSVCRT_free(MSVCRT_files
[fd
]);
2134 msvcrt_free_fd(newfile
->_file
);
2135 return &MSVCRT__iob
[fd
];
2138 WARN(":failed-last error (%ld)\n",GetLastError());
2139 MSVCRT_fclose(newfile
);
2140 MSVCRT__set_errno(GetLastError());
2144 /*********************************************************************
2145 * fsetpos (MSVCRT.@)
2147 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2149 return _lseek(file
->_file
,*pos
,SEEK_SET
);
2152 /*********************************************************************
2155 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
2157 /* Flush output if needed */
2158 if(file
->_flag
& MSVCRT__IOWRT
)
2159 msvcrt_flush_buffer(file
);
2161 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
2162 offset
-= file
->_cnt
;
2164 /* Discard buffered input */
2166 file
->_ptr
= file
->_base
;
2167 /* Reset direction of i/o */
2168 if(file
->_flag
& MSVCRT__IORW
) {
2169 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2171 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
2174 /*********************************************************************
2177 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
2182 if( file
->_flag
& MSVCRT__IOWRT
) {
2183 off
= file
->_ptr
- file
->_base
;
2188 pos
= _tell(file
->_file
);
2189 if(pos
== -1) return pos
;
2193 /*********************************************************************
2196 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2198 MSVCRT_size_t wrcnt
=size
* nmemb
;
2203 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2204 memcpy(file
->_ptr
, ptr
, pcnt
);
2209 ptr
= (char*)ptr
+ pcnt
;
2210 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2211 if(file
->_flag
& MSVCRT__IORW
) {
2212 file
->_flag
|= MSVCRT__IOWRT
;
2218 int res
=msvcrt_flush_buffer(file
);
2220 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2221 if (pwritten
<= 0) pwritten
=0;
2222 written
+= pwritten
;
2225 return written
/ size
;
2228 /*********************************************************************
2231 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2233 size_t i
, len
= strlen(s
);
2234 if (file
->_flag
& _O_BINARY
)
2235 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2236 for (i
=0; i
<len
; i
++)
2237 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2242 /*********************************************************************
2245 int MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2247 size_t i
, len
= strlenW(s
);
2248 if (file
->_flag
& _O_BINARY
)
2249 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2250 for (i
=0; i
<len
; i
++)
2252 if ((s
[i
] == L
'\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2254 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2260 /*********************************************************************
2261 * getchar (MSVCRT.@)
2263 int MSVCRT_getchar(void)
2265 return MSVCRT_fgetc(MSVCRT_stdin
);
2268 /*********************************************************************
2271 int MSVCRT_getc(MSVCRT_FILE
* file
)
2273 return MSVCRT_fgetc(file
);
2276 /*********************************************************************
2279 char *MSVCRT_gets(char *buf
)
2282 char * buf_start
= buf
;
2284 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2285 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2286 if(cc
!= '\r') *buf
++ = (char)cc
;
2290 TRACE("got '%s'\n", buf_start
);
2294 /*********************************************************************
2297 MSVCRT_wchar_t
* MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2300 MSVCRT_wchar_t
* ws
= buf
;
2302 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2303 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2306 *buf
++ = (MSVCRT_wchar_t
)cc
;
2310 TRACE("got '%s'\n", debugstr_w(ws
));
2314 /*********************************************************************
2317 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2319 return MSVCRT_fputc(c
, file
);
2322 /*********************************************************************
2323 * putchar (MSVCRT.@)
2325 int MSVCRT_putchar(int c
)
2327 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2330 /*********************************************************************
2333 int MSVCRT_puts(const char *s
)
2335 size_t len
= strlen(s
);
2336 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2337 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2340 /*********************************************************************
2343 int _putws(const MSVCRT_wchar_t
*s
)
2345 static const MSVCRT_wchar_t nl
= '\n';
2346 size_t len
= strlenW(s
);
2347 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2348 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2351 /*********************************************************************
2354 int MSVCRT_remove(const char *path
)
2356 TRACE("(%s)\n",path
);
2357 if (DeleteFileA(path
))
2359 TRACE(":failed (%ld)\n",GetLastError());
2360 MSVCRT__set_errno(GetLastError());
2364 /*********************************************************************
2365 * _wremove (MSVCRT.@)
2367 int _wremove(const MSVCRT_wchar_t
*path
)
2369 TRACE("(%s)\n",debugstr_w(path
));
2370 if (DeleteFileW(path
))
2372 TRACE(":failed (%ld)\n",GetLastError());
2373 MSVCRT__set_errno(GetLastError());
2377 /*********************************************************************
2380 int MSVCRT_scanf(const char *format
, ...)
2385 va_start(valist
, format
);
2386 res
= MSVCRT_fscanf(MSVCRT_stdin
, format
, valist
);
2391 /*********************************************************************
2394 int MSVCRT_wscanf(const MSVCRT_wchar_t
*format
, ...)
2399 va_start(valist
, format
);
2400 res
= MSVCRT_fwscanf(MSVCRT_stdin
, format
, valist
);
2405 /*********************************************************************
2408 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
2410 TRACE(":from %s to %s\n",oldpath
,newpath
);
2411 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2413 TRACE(":failed (%ld)\n",GetLastError());
2414 MSVCRT__set_errno(GetLastError());
2418 /*********************************************************************
2419 * _wrename (MSVCRT.@)
2421 int _wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
2423 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2424 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2426 TRACE(":failed (%ld)\n",GetLastError());
2427 MSVCRT__set_errno(GetLastError());
2431 /*********************************************************************
2432 * setvbuf (MSVCRT.@)
2434 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
2436 /* TODO: Check if file busy */
2438 MSVCRT_free(file
->_base
);
2442 if(mode
== MSVCRT__IOFBF
) {
2443 file
->_flag
&= ~MSVCRT__IONBF
;
2444 file
->_base
= file
->_ptr
= buf
;
2446 file
->_bufsiz
= size
;
2449 file
->_flag
|= MSVCRT__IONBF
;
2454 /*********************************************************************
2457 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
2459 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
2462 /*********************************************************************
2465 char *MSVCRT_tmpnam(char *s
)
2473 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
2474 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2475 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
2477 msvcrt_int_to_base32(unique
++, tmpstr
);
2479 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2480 GetLastError() == ERROR_FILE_NOT_FOUND
)
2486 /*********************************************************************
2487 * tmpfile (MSVCRT.@)
2489 MSVCRT_FILE
* MSVCRT_tmpfile(void)
2491 char *filename
= MSVCRT_tmpnam(NULL
);
2493 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2495 return msvcrt_alloc_fp(fd
);
2499 /*********************************************************************
2500 * vfprintf (MSVCRT.@)
2502 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
2504 char buf
[2048], *mem
= buf
;
2505 int written
, resize
= sizeof(buf
), retval
;
2506 /* There are two conventions for vsnprintf failing:
2507 * Return -1 if we truncated, or
2508 * Return the number of bytes that would have been written
2509 * The code below handles both cases
2511 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
2514 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
2517 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
2520 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2526 /*********************************************************************
2527 * vfwprintf (MSVCRT.@)
2529 * Is final char included in written (then resize is too big) or not
2530 * (then we must test for equality too)?
2532 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, va_list valist
)
2534 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
2535 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
2536 /* See vfprintf comments */
2537 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2540 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
2543 if (!(mem
= (MSVCRT_wchar_t
*)MSVCRT_malloc(resize
*sizeof(*mem
))))
2546 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2552 /*********************************************************************
2553 * vprintf (MSVCRT.@)
2555 int MSVCRT_vprintf(const char *format
, va_list valist
)
2557 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2560 /*********************************************************************
2561 * vwprintf (MSVCRT.@)
2563 int MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, va_list valist
)
2565 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2568 /*********************************************************************
2569 * fprintf (MSVCRT.@)
2571 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2575 va_start(valist
, format
);
2576 res
= MSVCRT_vfprintf(file
, format
, valist
);
2581 /*********************************************************************
2582 * fwprintf (MSVCRT.@)
2584 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
2588 va_start(valist
, format
);
2589 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2594 /*********************************************************************
2597 int MSVCRT_printf(const char *format
, ...)
2601 va_start(valist
, format
);
2602 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2607 /*********************************************************************
2610 int MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
2612 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2613 msvcrt_alloc_buffer(file
);
2616 if(file
->_ptr
>file
->_base
) {
2625 /*********************************************************************
2626 * ungetwc (MSVCRT.@)
2628 MSVCRT_wint_t
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2630 MSVCRT_wchar_t mwc
= wc
;
2631 char * pp
= (char *)&mwc
;
2633 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
2634 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
2640 /*********************************************************************
2641 * wprintf (MSVCRT.@)
2643 int MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
2647 va_start(valist
, format
);
2648 res
= MSVCRT_vwprintf(format
, valist
);