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];
77 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
78 static int MSVCRT_fdend
= 3; /* highest allocated fd */
80 /* INTERNAL: process umask */
81 static int MSVCRT_umask
= 0;
83 /* INTERNAL: Static buffer for temp file name */
84 static char MSVCRT_tmpname
[MAX_PATH
];
86 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
87 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
88 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
89 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
91 #define TOUL(x) (ULONGLONG)(x)
92 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
93 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
94 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
95 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
97 extern CRITICAL_SECTION MSVCRT_file_cs
;
98 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
99 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
101 static void msvcrt_cp_from_stati64(const struct _stati64
*bufi64
, struct _stat
*buf
)
103 buf
->st_dev
= bufi64
->st_dev
;
104 buf
->st_ino
= bufi64
->st_ino
;
105 buf
->st_mode
= bufi64
->st_mode
;
106 buf
->st_nlink
= bufi64
->st_nlink
;
107 buf
->st_uid
= bufi64
->st_uid
;
108 buf
->st_gid
= bufi64
->st_gid
;
109 buf
->st_rdev
= bufi64
->st_rdev
;
110 buf
->st_size
= bufi64
->st_size
;
111 buf
->st_atime
= bufi64
->st_atime
;
112 buf
->st_mtime
= bufi64
->st_mtime
;
113 buf
->st_ctime
= bufi64
->st_ctime
;
116 /* INTERNAL: Get the HANDLE for a fd */
117 static HANDLE
msvcrt_fdtoh(int fd
)
119 if (fd
< 0 || fd
>= MSVCRT_fdend
||
120 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
122 WARN(":fd (%d) - no handle!\n",fd
);
124 *MSVCRT__errno() = MSVCRT_EBADF
;
125 return INVALID_HANDLE_VALUE
;
127 return MSVCRT_handles
[fd
];
130 /* INTERNAL: free a file entry fd */
131 static void msvcrt_free_fd(int fd
)
133 MSVCRT_handles
[fd
] = INVALID_HANDLE_VALUE
;
134 MSVCRT_files
[fd
] = 0;
135 MSVCRT_flags
[fd
] = 0;
136 TRACE(":fd (%d) freed\n",fd
);
138 return; /* don't use 0,1,2 for user files */
139 if (fd
== MSVCRT_fdend
- 1)
141 if (fd
< MSVCRT_fdstart
)
145 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
146 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
148 int fd
= MSVCRT_fdstart
;
150 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fd
);
151 if (fd
>= MSVCRT_MAX_FILES
)
153 WARN(":files exhausted!\n");
156 MSVCRT_handles
[fd
] = hand
;
157 MSVCRT_flags
[fd
] = flag
;
159 /* locate next free slot */
160 if (fd
== MSVCRT_fdend
)
161 MSVCRT_fdstart
= ++MSVCRT_fdend
;
163 while(MSVCRT_fdstart
< MSVCRT_fdend
&&
164 MSVCRT_handles
[MSVCRT_fdstart
] != INVALID_HANDLE_VALUE
)
170 /* INTERNAL: Allocate a FILE* for an fd slot
171 * This is done lazily to avoid memory wastage for low level open/write
172 * usage when a FILE* is not requested (but may be later).
174 static MSVCRT_FILE
* msvcrt_alloc_fp(int fd
)
176 TRACE(":fd (%d) allocating FILE*\n",fd
);
177 if (fd
< 0 || fd
>= MSVCRT_fdend
||
178 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
180 WARN(":invalid fd %d\n",fd
);
182 *MSVCRT__errno() = MSVCRT_EBADF
;
185 if (!MSVCRT_files
[fd
])
187 if ((MSVCRT_files
[fd
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
189 MSVCRT_files
[fd
]->_file
= fd
;
190 MSVCRT_files
[fd
]->_flag
= MSVCRT_flags
[fd
];
191 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOAPPEND
; /* mask out, see above */
194 TRACE(":got FILE* (%p)\n",MSVCRT_files
[fd
]);
195 return MSVCRT_files
[fd
];
199 /* INTERNAL: Set up stdin, stderr and stdout */
200 void msvcrt_init_io(void)
203 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
204 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE
),
205 GetCurrentProcess(), &MSVCRT_handles
[0], 0, FALSE
, DUPLICATE_SAME_ACCESS
);
206 MSVCRT_flags
[0] = MSVCRT__iob
[0]._flag
= MSVCRT__IOREAD
;
207 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE
),
208 GetCurrentProcess(), &MSVCRT_handles
[1], 0, FALSE
, DUPLICATE_SAME_ACCESS
);
209 MSVCRT_flags
[1] = MSVCRT__iob
[1]._flag
= MSVCRT__IOWRT
;
210 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE
),
211 GetCurrentProcess(), &MSVCRT_handles
[2], 0, FALSE
, DUPLICATE_SAME_ACCESS
);
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)
235 /* INTERNAL: Flush stdio file buffer */
236 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
239 int cnt
=file
->_ptr
-file
->_base
;
240 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
243 file
->_ptr
=file
->_base
;
244 file
->_cnt
=file
->_bufsiz
;
249 /* INTERNAL: Allocate stdio file buffer */
250 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
252 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
254 file
->_bufsiz
= MSVCRT_BUFSIZ
;
255 file
->_flag
|= MSVCRT__IOMYBUF
;
257 file
->_base
= (unsigned char *)(&file
->_charbuf
);
259 file
->_bufsiz
= sizeof(file
->_charbuf
);
261 file
->_ptr
= file
->_base
;
265 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
266 static void msvcrt_int_to_base32(int num
, char *str
)
281 *p
= (num
& 31) + '0';
283 *p
+= ('a' - '0' - 10);
288 /*********************************************************************
291 MSVCRT_FILE
*__p__iob(void)
293 return &MSVCRT__iob
[0];
296 /*********************************************************************
299 int _access(const char *filename
, int mode
)
301 DWORD attr
= GetFileAttributesA(filename
);
303 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
305 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
307 MSVCRT__set_errno(GetLastError());
310 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
312 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
318 /*********************************************************************
319 * _waccess (MSVCRT.@)
321 int _waccess(const MSVCRT_wchar_t
*filename
, int mode
)
323 DWORD attr
= GetFileAttributesW(filename
);
325 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
327 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
329 MSVCRT__set_errno(GetLastError());
332 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
334 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
340 /*********************************************************************
343 int _chmod(const char *path
, int flags
)
345 DWORD oldFlags
= GetFileAttributesA(path
);
347 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
349 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
350 oldFlags
| FILE_ATTRIBUTE_READONLY
;
352 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
355 MSVCRT__set_errno(GetLastError());
359 /*********************************************************************
362 int _wchmod(const MSVCRT_wchar_t
*path
, int flags
)
364 DWORD oldFlags
= GetFileAttributesW(path
);
366 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
368 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
369 oldFlags
| FILE_ATTRIBUTE_READONLY
;
371 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
374 MSVCRT__set_errno(GetLastError());
378 /*********************************************************************
381 int _chsize(int fd
, long size
)
383 FIXME("(fd=%d, size=%ld): stub\n", fd
, size
);
387 /*********************************************************************
392 FIXME("(od=%d): stub\n", od
);
396 /*********************************************************************
399 int _dup2(int od
, int nd
)
401 FIXME("(od=%d, nd=%d): stub\n", od
, nd
);
405 /*********************************************************************
408 int _unlink(const char *path
)
410 TRACE("(%s)\n",path
);
411 if(DeleteFileA(path
))
413 TRACE("failed (%ld)\n",GetLastError());
414 MSVCRT__set_errno(GetLastError());
418 /*********************************************************************
419 * _wunlink (MSVCRT.@)
421 int _wunlink(const MSVCRT_wchar_t
*path
)
423 TRACE("(%s)\n",debugstr_w(path
));
424 if(DeleteFileW(path
))
426 TRACE("failed (%ld)\n",GetLastError());
427 MSVCRT__set_errno(GetLastError());
431 /*********************************************************************
436 HANDLE hand
= msvcrt_fdtoh(fd
);
438 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
439 if (hand
== INVALID_HANDLE_VALUE
)
441 /* flush stdio buffers */
442 if(MSVCRT_files
[fd
]) {
443 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOWRT
)
444 MSVCRT_fflush(MSVCRT_files
[fd
]);
446 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOMYBUF
)
447 MSVCRT_free(MSVCRT_files
[fd
]->_base
);
450 /* Don't free std FILE*'s, they are not dynamic */
451 if (fd
> 2 && MSVCRT_files
[fd
])
452 MSVCRT_free(MSVCRT_files
[fd
]);
456 if (!CloseHandle(hand
))
458 WARN(":failed-last error (%ld)\n",GetLastError());
459 MSVCRT__set_errno(GetLastError());
462 if (MSVCRT_tempfiles
[fd
])
464 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles
[fd
]);
465 _unlink(MSVCRT_tempfiles
[fd
]);
466 MSVCRT_free(MSVCRT_tempfiles
[fd
]);
467 MSVCRT_tempfiles
[fd
] = NULL
;
474 /*********************************************************************
479 HANDLE hand
= msvcrt_fdtoh(fd
);
481 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
482 if (hand
== INVALID_HANDLE_VALUE
)
485 if (!FlushFileBuffers(hand
))
487 if (GetLastError() == ERROR_INVALID_HANDLE
)
489 /* FlushFileBuffers fails for console handles
490 * so we ignore this error.
494 TRACE(":failed-last error (%ld)\n",GetLastError());
495 MSVCRT__set_errno(GetLastError());
502 /*********************************************************************
508 HANDLE hand
= msvcrt_fdtoh(fd
);
510 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
512 if (hand
== INVALID_HANDLE_VALUE
)
515 /* If we have a FILE* for this file, the EOF flag
516 * will be set by the read()/write() functions.
518 if (MSVCRT_files
[fd
])
519 return MSVCRT_flags
[fd
] & MSVCRT__IOEOF
;
521 /* Otherwise we do it the hard way */
522 curpos
= SetFilePointer(hand
, 0, NULL
, SEEK_CUR
);
523 endpos
= SetFilePointer(hand
, 0, NULL
, FILE_END
);
525 if (curpos
== endpos
)
528 SetFilePointer(hand
, curpos
, 0, FILE_BEGIN
);
532 /*********************************************************************
533 * _fcloseall (MSVCRT.@)
537 int num_closed
= 0, i
;
539 for (i
= 3; i
< MSVCRT_fdend
; i
++)
540 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
546 TRACE(":closed (%d) handles\n",num_closed
);
550 /*********************************************************************
551 * _lseeki64 (MSVCRT.@)
553 __int64
_lseeki64(int fd
, __int64 offset
, int whence
)
555 DWORD ret
, hoffset
= (DWORD
) (offset
>> 32);
556 HANDLE hand
= msvcrt_fdtoh(fd
);
558 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
559 if (hand
== INVALID_HANDLE_VALUE
)
562 if (whence
< 0 || whence
> 2)
564 *MSVCRT__errno() = MSVCRT_EINVAL
;
568 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
569 fd
,hoffset
,(long)offset
,
570 (whence
==SEEK_SET
)?"SEEK_SET":
571 (whence
==SEEK_CUR
)?"SEEK_CUR":
572 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
574 if (((ret
= SetFilePointer(hand
, (long)offset
, &hoffset
,
575 whence
)) != INVALID_SET_FILE_POINTER
) || !GetLastError())
577 if (MSVCRT_files
[fd
])
578 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOEOF
;
579 /* FIXME: What if we seek _to_ EOF - is EOF set? */
581 return ((__int64
)hoffset
<< 32) | ret
;
583 TRACE(":error-last error (%ld)\n",GetLastError());
584 if (MSVCRT_files
[fd
])
585 switch(GetLastError())
587 case ERROR_NEGATIVE_SEEK
:
588 case ERROR_SEEK_ON_DEVICE
:
589 MSVCRT__set_errno(GetLastError());
590 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
598 /*********************************************************************
601 LONG
_lseek(int fd
, LONG offset
, int whence
)
603 return _lseeki64(fd
, offset
, whence
);
606 /*********************************************************************
607 * _locking (MSVCRT.@)
609 * This is untested; the underlying LockFile doesn't work yet.
611 int _locking(int fd
, int mode
, LONG nbytes
)
615 HANDLE hand
= msvcrt_fdtoh(fd
);
617 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
618 if (hand
== INVALID_HANDLE_VALUE
)
621 if (mode
< 0 || mode
> 4)
623 *MSVCRT__errno() = MSVCRT_EINVAL
;
627 TRACE(":fd (%d) by 0x%08lx mode %s\n",
628 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
629 (mode
==_LK_LOCK
)?"_LK_LOCK":
630 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
631 (mode
==_LK_RLCK
)?"_LK_RLCK":
632 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
635 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
637 FIXME ("Seek failed\n");
638 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
641 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
644 ret
= 1; /* just to satisfy gcc */
647 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
652 else if (mode
== _LK_UNLCK
)
653 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
655 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
656 /* FIXME - what about error settings? */
660 /*********************************************************************
663 void MSVCRT_rewind(MSVCRT_FILE
* file
)
665 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
666 MSVCRT_fseek(file
, 0L, SEEK_SET
);
667 MSVCRT_clearerr(file
);
670 /*********************************************************************
673 MSVCRT_FILE
* _fdopen(int fd
, const char *mode
)
675 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
677 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
682 /*********************************************************************
683 * _wfdopen (MSVCRT.@)
685 MSVCRT_FILE
* _wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
687 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
689 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
696 /*********************************************************************
697 * _filelength (MSVCRT.@)
699 LONG
_filelength(int fd
)
701 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
704 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
707 if (endPos
!= curPos
)
708 _lseek(fd
, curPos
, SEEK_SET
);
715 /*********************************************************************
716 * _filelengthi64 (MSVCRT.@)
718 __int64
_filelengthi64(int fd
)
720 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
723 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
726 if (endPos
!= curPos
)
727 _lseeki64(fd
, curPos
, SEEK_SET
);
734 /*********************************************************************
737 int _fileno(MSVCRT_FILE
* file
)
739 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
743 /*********************************************************************
744 * _flushall (MSVCRT.@)
748 int i
, num_flushed
= 0;
750 for (i
= 3; i
< MSVCRT_fdend
; i
++)
751 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
754 /* FIXME: flush, do not commit */
755 if (_commit(i
) == -1)
757 MSVCRT_files
[i
]->_flag
|= MSVCRT__IOERR
;
759 if(MSVCRT_files
[i
] && MSVCRT_files
[i
]->_flag
& MSVCRT__IOWRT
) {
760 MSVCRT_fflush(MSVCRT_files
[i
]);
765 TRACE(":flushed (%d) handles\n",num_flushed
);
769 /*********************************************************************
770 * _fstati64 (MSVCRT.@)
772 int _fstati64(int fd
, struct _stati64
* buf
)
775 BY_HANDLE_FILE_INFORMATION hfi
;
776 HANDLE hand
= msvcrt_fdtoh(fd
);
778 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
779 if (hand
== INVALID_HANDLE_VALUE
)
784 WARN(":failed-NULL buf\n");
785 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
789 memset(&hfi
, 0, sizeof(hfi
));
790 memset(buf
, 0, sizeof(struct _stati64
));
791 if (!GetFileInformationByHandle(hand
, &hfi
))
793 WARN(":failed-last error (%ld)\n",GetLastError());
794 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
797 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi
.dwFileAttributes
);
798 buf
->st_nlink
= hfi
.nNumberOfLinks
;
799 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
800 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
802 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
803 buf
->st_mtime
= buf
->st_ctime
= dw
;
807 /*********************************************************************
810 int MSVCRT__fstat(int fd
, struct _stat
* buf
)
812 struct _stati64 bufi64
;
814 ret
= _fstati64(fd
, &bufi64
);
816 msvcrt_cp_from_stati64(&bufi64
, buf
);
820 /*********************************************************************
823 int _futime(int fd
, struct _utimbuf
*t
)
825 HANDLE hand
= msvcrt_fdtoh(fd
);
830 MSVCRT_time_t currTime
;
831 MSVCRT_time(&currTime
);
832 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
833 memcpy(&wt
, &at
, sizeof(wt
));
837 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
838 if (t
->actime
== t
->modtime
)
839 memcpy(&wt
, &at
, sizeof(wt
));
841 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
844 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
846 MSVCRT__set_errno(GetLastError());
852 /*********************************************************************
853 * _get_osfhandle (MSVCRT.@)
855 long _get_osfhandle(int fd
)
857 HANDLE hand
= msvcrt_fdtoh(fd
);
858 HANDLE newhand
= hand
;
859 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
861 if (hand
!= INVALID_HANDLE_VALUE
)
863 /* FIXME: I'm not convinced that I should be copying the
864 * handle here - it may be leaked if the app doesn't
865 * close it (and the API docs don't say that it should)
866 * Not duplicating it means that it can't be inherited
867 * and so lcc's wedit doesn't cope when it passes it to
868 * child processes. I've an idea that it should either
869 * be copied by CreateProcess, or marked as inheritable
870 * when initialised, or maybe both? JG 21-9-00.
872 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
873 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
875 return (long)newhand
;
878 /*********************************************************************
883 HANDLE hand
= msvcrt_fdtoh(fd
);
885 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
886 if (hand
== INVALID_HANDLE_VALUE
)
889 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
892 /*********************************************************************
895 char *_mktemp(char *pattern
)
898 char *retVal
= pattern
;
903 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
907 id
= GetCurrentProcessId();
911 int tempNum
= id
/ 10;
912 *pattern
-- = id
- (tempNum
* 10) + '0';
918 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
919 GetLastError() == ERROR_FILE_NOT_FOUND
)
922 } while(letter
!= '|');
926 /*********************************************************************
927 * _wmktemp (MSVCRT.@)
929 MSVCRT_wchar_t
*_wmktemp(MSVCRT_wchar_t
*pattern
)
932 MSVCRT_wchar_t
*retVal
= pattern
;
934 MSVCRT_wchar_t letter
= 'a';
937 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
941 id
= GetCurrentProcessId();
945 int tempNum
= id
/ 10;
946 *pattern
-- = id
- (tempNum
* 10) + '0';
952 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
953 GetLastError() == ERROR_FILE_NOT_FOUND
)
956 } while(letter
!= '|');
960 /*********************************************************************
963 int MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
967 DWORD access
= 0, creation
= 0;
971 SECURITY_ATTRIBUTES sa
;
974 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
975 path
, oflags
, shflags
);
977 switch(oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
980 access
|= GENERIC_READ
;
981 ioflag
|= MSVCRT__IOREAD
;
984 access
|= GENERIC_WRITE
;
985 ioflag
|= MSVCRT__IOWRT
;
988 access
|= GENERIC_WRITE
| GENERIC_READ
;
989 ioflag
|= MSVCRT__IORW
;
993 if (oflags
& _O_CREAT
)
995 va_start(ap
, shflags
);
996 pmode
= va_arg(ap
, int);
999 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1000 FIXME(": pmode 0x%04x ignored\n", pmode
);
1002 WARN(": pmode 0x%04x ignored\n", pmode
);
1004 if (oflags
& _O_EXCL
)
1005 creation
= CREATE_NEW
;
1006 else if (oflags
& _O_TRUNC
)
1007 creation
= CREATE_ALWAYS
;
1009 creation
= OPEN_ALWAYS
;
1011 else /* no _O_CREAT */
1013 if (oflags
& _O_TRUNC
)
1014 creation
= TRUNCATE_EXISTING
;
1016 creation
= OPEN_EXISTING
;
1018 if (oflags
& _O_APPEND
)
1019 ioflag
|= MSVCRT__IOAPPEND
;
1021 if (oflags
& _O_BINARY
)
1022 ioflag
|= _O_BINARY
;
1023 else if (oflags
& _O_TEXT
)
1025 else if (*__p__fmode() & _O_BINARY
)
1026 ioflag
|= _O_BINARY
;
1028 ioflag
|= _O_TEXT
; /* default to TEXT*/
1036 sharing
= FILE_SHARE_READ
;
1039 sharing
= FILE_SHARE_WRITE
;
1042 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1045 ERR( "Unhandled shflags 0x%x\n", shflags
);
1049 if (oflags
& ~(_O_BINARY
|_O_TEXT
|_O_APPEND
|_O_TRUNC
|_O_EXCL
1050 |_O_CREAT
|_O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|_O_NOINHERIT
))
1051 ERR(":unsupported oflags 0x%04x\n",oflags
);
1053 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1054 sa
.lpSecurityDescriptor
= NULL
;
1055 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1057 hand
= CreateFileA(path
, access
, sharing
,
1058 &sa
, creation
, FILE_ATTRIBUTE_NORMAL
, 0);
1060 if (hand
== INVALID_HANDLE_VALUE
) {
1061 WARN(":failed-last error (%ld)\n",GetLastError());
1062 MSVCRT__set_errno(GetLastError());
1066 fd
= msvcrt_alloc_fd(hand
, ioflag
);
1068 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1072 if (oflags
& _O_TEMPORARY
)
1073 MSVCRT_tempfiles
[fd
] = _strdup(path
);
1074 if (ioflag
& MSVCRT__IOAPPEND
)
1075 _lseek(fd
, 0, FILE_END
);
1081 /*********************************************************************
1082 * _wsopen (MSVCRT.@)
1084 int MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1086 const unsigned int len
= strlenW(path
);
1087 char *patha
= MSVCRT_calloc(len
+ 1,1);
1091 va_start(ap
, shflags
);
1092 pmode
= va_arg(ap
, int);
1095 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1097 int retval
= MSVCRT__sopen(patha
,oflags
,shflags
,pmode
);
1102 MSVCRT__set_errno(GetLastError());
1106 /*********************************************************************
1109 int _open( const char *path
, int flags
, ... )
1113 if (flags
& _O_CREAT
)
1116 va_start(ap
, flags
);
1117 pmode
= va_arg(ap
, int);
1119 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
, pmode
);
1122 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
);
1125 /*********************************************************************
1128 int _wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1130 const unsigned int len
= strlenW(path
);
1131 char *patha
= MSVCRT_calloc(len
+ 1,1);
1135 va_start(ap
, flags
);
1136 pmode
= va_arg(ap
, int);
1139 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1141 int retval
= _open(patha
,flags
,pmode
);
1146 MSVCRT__set_errno(GetLastError());
1150 /*********************************************************************
1153 int _creat(const char *path
, int flags
)
1155 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1156 return _open(path
, usedFlags
);
1159 /*********************************************************************
1160 * _wcreat (MSVCRT.@)
1162 int _wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1164 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1165 return _wopen(path
, usedFlags
);
1168 /*********************************************************************
1169 * _open_osfhandle (MSVCRT.@)
1171 int _open_osfhandle(long hand
, int flags
)
1175 /* _O_RDONLY (0) always matches, so set the read flag
1176 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1177 * file, so set the write flag. It also only sets _O_TEXT if it wants
1178 * text - it never sets _O_BINARY.
1180 /* FIXME: handle more flags */
1181 flags
|= MSVCRT__IOREAD
|MSVCRT__IOWRT
;
1182 if ( !( flags
& _O_TEXT
) ) flags
|= _O_BINARY
;
1184 fd
= msvcrt_alloc_fd((HANDLE
)hand
,flags
);
1185 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand
,fd
, flags
);
1189 /*********************************************************************
1194 int num_removed
= 0, i
;
1196 for (i
= 3; i
< MSVCRT_fdend
; i
++)
1197 if (MSVCRT_tempfiles
[i
])
1204 TRACE(":removed (%d) temp files\n",num_removed
);
1208 /*********************************************************************
1209 * (internal) remove_cr
1211 * Remove all \r inplace.
1212 * return the number of \r removed
1214 static unsigned int remove_cr(char *buf
, unsigned int count
)
1218 for (i
= 0; i
< count
; i
++) if (buf
[i
] == '\r') break;
1219 for (j
= i
+ 1; j
< count
; j
++) if (buf
[j
] != '\r') buf
[i
++] = buf
[j
];
1223 /*********************************************************************
1226 int _read(int fd
, void *buf
, unsigned int count
)
1228 DWORD num_read
, all_read
=0;
1229 char *bufstart
= buf
;
1230 HANDLE hand
= msvcrt_fdtoh(fd
);
1232 /* Don't trace small reads, it gets *very* annoying */
1234 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1235 if (hand
== INVALID_HANDLE_VALUE
)
1238 /* Reading single bytes in O_TEXT mode makes things slow
1239 * So read big chunks, then remove the \r in memory and try reading
1240 * the rest until the request is satisfied or EOF is met
1242 while ( all_read
< count
)
1244 if (ReadFile(hand
, bufstart
+all_read
, count
- all_read
, &num_read
, NULL
))
1246 if (num_read
!= (count
- all_read
))
1249 if ( MSVCRT_files
[fd
])
1251 MSVCRT_flags
[fd
] |= MSVCRT__IOEOF
;
1253 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1256 if ((MSVCRT_flags
[fd
]& _O_BINARY
) != _O_BINARY
)
1257 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1258 all_read
+= num_read
;
1260 TRACE("%s\n",debugstr_an(buf
,all_read
));
1263 if ((MSVCRT_flags
[fd
]& _O_BINARY
) != _O_BINARY
)
1265 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1267 all_read
+= num_read
;
1271 TRACE(":failed-last error (%ld)\n",GetLastError());
1272 if (MSVCRT_files
[fd
])
1273 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1279 TRACE("%s\n",debugstr_an(buf
, all_read
));
1283 /*********************************************************************
1286 int _getw(MSVCRT_FILE
* file
)
1289 if (_read(file
->_file
, &i
, sizeof(int)) != 1)
1294 /*********************************************************************
1295 * _setmode (MSVCRT.@)
1297 int _setmode(int fd
,int mode
)
1299 int ret
= MSVCRT_flags
[fd
] & (_O_TEXT
| _O_BINARY
);
1300 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1301 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1302 MSVCRT_flags
[fd
] &= ~(_O_TEXT
|_O_BINARY
);
1303 MSVCRT_flags
[fd
] |= mode
& (_O_TEXT
| _O_BINARY
);
1307 /*********************************************************************
1308 * _stati64 (MSVCRT.@)
1310 int _stati64(const char* path
, struct _stati64
* buf
)
1313 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1314 unsigned short mode
= MSVCRT_S_IREAD
;
1317 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1319 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1321 TRACE("failed (%ld)\n",GetLastError());
1322 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1326 memset(buf
,0,sizeof(struct _stati64
));
1328 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1329 Bon 011120: This FIXME seems incorrect
1330 Also a letter as first char isn't enough to be classified
1333 if (isalpha(*path
)&& (*(path
+1)==':'))
1334 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1336 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1338 plen
= strlen(path
);
1340 /* Dir, or regular file? */
1341 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1342 (path
[plen
-1] == '\\'))
1343 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1348 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1350 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1351 (tolower(path
[plen
-3]) << 16);
1352 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1353 mode
|= MSVCRT_S_IEXEC
;
1357 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1358 mode
|= MSVCRT_S_IWRITE
;
1360 buf
->st_mode
= mode
;
1362 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1363 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1365 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1366 buf
->st_mtime
= buf
->st_ctime
= dw
;
1367 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1368 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1369 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1373 /*********************************************************************
1376 int MSVCRT__stat(const char* path
, struct _stat
* buf
)
1378 struct _stati64 bufi64
;
1380 ret
= _stati64( path
, &bufi64
);
1382 msvcrt_cp_from_stati64(&bufi64
, buf
);
1386 /*********************************************************************
1387 * _wstati64 (MSVCRT.@)
1389 int _wstati64(const MSVCRT_wchar_t
* path
, struct _stati64
* buf
)
1392 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1393 unsigned short mode
= MSVCRT_S_IREAD
;
1396 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1398 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1400 TRACE("failed (%ld)\n",GetLastError());
1401 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1405 memset(buf
,0,sizeof(struct _stat
));
1407 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1408 if (MSVCRT_iswalpha(*path
))
1409 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1411 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1413 plen
= strlenW(path
);
1415 /* Dir, or regular file? */
1416 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1417 (path
[plen
-1] == '\\'))
1418 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1423 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1425 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1426 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1427 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1428 mode
|= MSVCRT_S_IEXEC
;
1432 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1433 mode
|= MSVCRT_S_IWRITE
;
1435 buf
->st_mode
= mode
;
1437 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1438 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1440 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1441 buf
->st_mtime
= buf
->st_ctime
= dw
;
1442 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1443 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1444 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1448 /*********************************************************************
1451 int _wstat(const MSVCRT_wchar_t
* path
, struct _stat
* buf
)
1454 struct _stati64 bufi64
;
1456 ret
= _wstati64( path
, &bufi64
);
1457 if (!ret
) msvcrt_cp_from_stati64(&bufi64
, buf
);
1461 /*********************************************************************
1466 return _lseek(fd
, 0, SEEK_CUR
);
1469 /*********************************************************************
1470 * _telli64 (MSVCRT.@)
1472 __int64
_telli64(int fd
)
1474 return _lseeki64(fd
, 0, SEEK_CUR
);
1477 /*********************************************************************
1478 * _tempnam (MSVCRT.@)
1480 char *_tempnam(const char *dir
, const char *prefix
)
1482 char tmpbuf
[MAX_PATH
];
1484 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1485 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1487 TRACE("got name (%s)\n",tmpbuf
);
1488 DeleteFileA(tmpbuf
);
1489 return _strdup(tmpbuf
);
1491 TRACE("failed (%ld)\n",GetLastError());
1495 /*********************************************************************
1496 * _wtempnam (MSVCRT.@)
1498 MSVCRT_wchar_t
*_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
1500 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
1502 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1503 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1505 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1506 DeleteFileW(tmpbuf
);
1507 return _wcsdup(tmpbuf
);
1509 TRACE("failed (%ld)\n",GetLastError());
1513 /*********************************************************************
1516 int _umask(int umask
)
1518 int old_umask
= MSVCRT_umask
;
1519 TRACE("(%d)\n",umask
);
1520 MSVCRT_umask
= umask
;
1524 /*********************************************************************
1527 int _utime(const char* path
, struct _utimbuf
*t
)
1529 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
1533 int retVal
= _futime(fd
, t
);
1540 /*********************************************************************
1541 * _wutime (MSVCRT.@)
1543 int _wutime(const MSVCRT_wchar_t
* path
, struct _utimbuf
*t
)
1545 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
1549 int retVal
= _futime(fd
, t
);
1556 /*********************************************************************
1559 int _write(int fd
, const void* buf
, unsigned int count
)
1562 HANDLE hand
= msvcrt_fdtoh(fd
);
1564 /* Don't trace small writes, it gets *very* annoying */
1567 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1569 if (hand
== INVALID_HANDLE_VALUE
)
1571 *MSVCRT__errno() = MSVCRT_EBADF
;
1575 /* If appending, go to EOF */
1576 if (MSVCRT_flags
[fd
] & MSVCRT__IOAPPEND
)
1577 _lseek(fd
, 0, FILE_END
);
1579 if (MSVCRT_flags
[fd
] & _O_BINARY
)
1581 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1582 && (num_written
== count
))
1584 TRACE(":failed-last error (%ld)\n",GetLastError());
1585 if (MSVCRT_files
[fd
])
1587 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1588 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1593 unsigned int i
, j
, nr_lf
;
1594 char *s
=(char*)buf
, *buf_start
=(char*)buf
, *p
;
1595 /* find number of \n ( without preceeding \r */
1596 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1601 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1606 if ((p
= MSVCRT_malloc(count
+ nr_lf
)))
1608 for(s
=(char*)buf
, i
=0, j
=0; i
<count
; i
++)
1613 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1620 FIXME("Malloc failed\n");
1628 if ((WriteFile(hand
, p
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1630 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written
);
1631 if (MSVCRT_files
[fd
])
1633 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1634 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1637 return s
- buf_start
;
1650 /*********************************************************************
1653 int _putw(int val
, MSVCRT_FILE
* file
)
1655 return _write(file
->_file
, &val
, sizeof(val
)) == 1? val
: MSVCRT_EOF
;
1658 /*********************************************************************
1659 * clearerr (MSVCRT.@)
1661 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
1663 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1664 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1667 /*********************************************************************
1670 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1675 r
=_close(file
->_file
);
1676 return ((r
==MSVCRT_EOF
) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
1679 /*********************************************************************
1682 int MSVCRT_feof(MSVCRT_FILE
* file
)
1684 return file
->_flag
& MSVCRT__IOEOF
;
1687 /*********************************************************************
1690 int MSVCRT_ferror(MSVCRT_FILE
* file
)
1692 return file
->_flag
& MSVCRT__IOERR
;
1695 /*********************************************************************
1698 int MSVCRT_fflush(MSVCRT_FILE
* file
)
1704 int res
=msvcrt_flush_buffer(file
);
1709 /*********************************************************************
1712 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
1716 return *(unsigned char *)file
->_ptr
++;
1718 return _filbuf(file
);
1722 /*********************************************************************
1723 * _fgetchar (MSVCRT.@)
1727 return MSVCRT_fgetc(MSVCRT_stdin
);
1730 /*********************************************************************
1731 * _filbuf (MSVCRT.@)
1733 int _filbuf(MSVCRT_FILE
* file
)
1736 /* Allocate buffer if needed */
1737 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
1738 msvcrt_alloc_buffer(file
);
1740 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
1741 if(file
->_flag
& MSVCRT__IORW
) {
1742 file
->_flag
|= MSVCRT__IOREAD
;
1747 if(file
->_flag
& MSVCRT__IONBF
) {
1749 if (_read(file
->_file
,&c
,1) != 1) {
1750 file
->_flag
|= MSVCRT__IOEOF
;
1755 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
1756 if(file
->_cnt
<0) file
->_cnt
= 0;
1758 file
->_flag
|= MSVCRT__IOEOF
;
1762 file
->_ptr
= file
->_base
+1;
1763 return *(unsigned char *)file
->_base
;
1767 /*********************************************************************
1768 * fgetpos (MSVCRT.@)
1770 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1772 *pos
= MSVCRT_ftell(file
);
1773 return (*pos
== -1? -1 : 0);
1776 /*********************************************************************
1779 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1781 int cc
= MSVCRT_EOF
;
1782 char * buf_start
= s
;
1784 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1785 file
,file
->_file
,s
,size
);
1787 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
1792 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1794 TRACE(":nothing read\n");
1797 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
1800 TRACE(":got '%s'\n", debugstr_a(buf_start
));
1804 /*********************************************************************
1807 * In _O_TEXT mode, bultibyte characters are read from the file, dropping
1808 * the CR from CR/LF combinations
1810 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1814 if (file
->_flag
& _O_BINARY
)
1817 if (_read(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1821 c
= MSVCRT_fgetc(file
);
1822 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c
))
1824 FIXME("Treat Multibyte characters\n");
1826 if (c
== MSVCRT_EOF
)
1829 return (MSVCRT_wint_t
)c
;
1832 /*********************************************************************
1835 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
1837 return MSVCRT_fgetwc(file
);
1840 /*********************************************************************
1841 * _fgetwchar (MSVCRT.@)
1843 MSVCRT_wint_t
_fgetwchar(void)
1845 return MSVCRT_fgetwc(MSVCRT_stdin
);
1848 /*********************************************************************
1849 * getwchar (MSVCRT.@)
1851 MSVCRT_wint_t
MSVCRT_getwchar(void)
1853 return _fgetwchar();
1856 /*********************************************************************
1859 MSVCRT_wchar_t
*MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
1861 int cc
= MSVCRT_WEOF
;
1862 MSVCRT_wchar_t
* buf_start
= s
;
1864 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1865 file
,file
->_file
,s
,size
);
1867 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
1872 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1874 TRACE(":nothing read\n");
1877 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
1880 TRACE(":got %s\n", debugstr_w(buf_start
));
1885 /*********************************************************************
1888 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
1890 MSVCRT_wchar_t mwc
=wc
;
1891 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
1896 /*********************************************************************
1897 * _fputwchar (MSVCRT.@)
1899 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
1901 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
1904 /*********************************************************************
1907 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
1910 int flags
= 0, plus
= 0, fd
;
1911 const char* search
= mode
;
1913 TRACE("(%s,%s)\n",path
,mode
);
1916 if (*search
++ == '+')
1919 /* map mode string to open() flags. "man fopen" for possibilities. */
1923 flags
= (plus
? _O_RDWR
: _O_RDONLY
);
1926 flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1929 flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1944 flags
&= ~_O_BINARY
;
1949 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1952 fd
= _open(path
, flags
, _S_IREAD
| _S_IWRITE
);
1957 file
= msvcrt_alloc_fp(fd
);
1958 TRACE(":got (%p)\n",file
);
1965 /*********************************************************************
1966 * _wfopen (MSVCRT.@)
1968 MSVCRT_FILE
*_wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
1970 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
1971 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
1972 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
1974 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
1976 if (patha
&& modea
&&
1977 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
1978 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1980 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
1986 MSVCRT__set_errno(GetLastError());
1990 /*********************************************************************
1991 * _fsopen (MSVCRT.@)
1993 MSVCRT_FILE
* _fsopen(const char *path
, const char *mode
, int share
)
1995 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
1996 return MSVCRT_fopen(path
,mode
);
1999 /*********************************************************************
2000 * _wfsopen (MSVCRT.@)
2002 MSVCRT_FILE
* _wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2004 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2005 debugstr_w(path
),debugstr_w(mode
),share
);
2006 return _wfopen(path
,mode
);
2009 /*********************************************************************
2012 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2019 return _flsbuf(c
, file
);
2023 /*********************************************************************
2024 * _flsbuf (MSVCRT.@)
2026 int _flsbuf(int c
, MSVCRT_FILE
* file
)
2028 /* Flush output buffer */
2029 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2030 msvcrt_alloc_buffer(file
);
2032 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2033 if(file
->_flag
& MSVCRT__IORW
) {
2034 file
->_flag
|= MSVCRT__IOWRT
;
2040 int res
=msvcrt_flush_buffer(file
);
2041 return res
?res
: MSVCRT_fputc(c
, file
);
2044 return _write(file
->_file
, &cc
, 1) == 1? c
: MSVCRT_EOF
;
2048 /*********************************************************************
2049 * _fputchar (MSVCRT.@)
2051 int _fputchar(int c
)
2053 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2056 /*********************************************************************
2059 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2060 { MSVCRT_size_t rcnt
=size
* nmemb
;
2061 MSVCRT_size_t read
=0;
2063 /* first buffered data */
2065 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2066 memcpy(ptr
, file
->_ptr
, pcnt
);
2071 ptr
= (char*)ptr
+ pcnt
;
2072 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2073 if(file
->_flag
& MSVCRT__IORW
) {
2074 file
->_flag
|= MSVCRT__IOREAD
;
2078 if(rcnt
) pread
= _read(file
->_file
,ptr
, rcnt
);
2079 if (MSVCRT_flags
[file
->_file
] & MSVCRT__IOEOF
)
2080 /* expose feof condition in the flags
2081 MFC tests file->_flag for feof, and doesn't not call feof())
2083 file
->_flag
|= MSVCRT__IOEOF
;
2090 /*********************************************************************
2091 * freopen (MSVCRT.@)
2094 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
2096 MSVCRT_FILE
* newfile
;
2099 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2100 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2106 FIXME(":reopen on user file not implemented!\n");
2107 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED
);
2110 if(MSVCRT_fclose(file
))
2112 return MSVCRT_fopen(path
, mode
);
2115 /* first, create the new file */
2116 if ((newfile
= MSVCRT_fopen(path
,mode
)) == NULL
)
2119 if (fd
< 3 && SetStdHandle(fd
== 0 ? STD_INPUT_HANDLE
:
2120 (fd
== 1? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
),
2121 MSVCRT_handles
[newfile
->_file
]))
2123 /* Redirecting std handle to file , copy over.. */
2124 MSVCRT_handles
[fd
] = MSVCRT_handles
[newfile
->_file
];
2125 MSVCRT_flags
[fd
] = MSVCRT_flags
[newfile
->_file
];
2126 memcpy(&MSVCRT__iob
[fd
], newfile
, sizeof (MSVCRT_FILE
));
2127 MSVCRT__iob
[fd
]._file
= fd
;
2128 /* And free up the resources allocated by fopen, but
2129 * not the HANDLE we copied. */
2130 MSVCRT_free(MSVCRT_files
[fd
]);
2131 msvcrt_free_fd(newfile
->_file
);
2132 return &MSVCRT__iob
[fd
];
2135 WARN(":failed-last error (%ld)\n",GetLastError());
2136 MSVCRT_fclose(newfile
);
2137 MSVCRT__set_errno(GetLastError());
2141 /*********************************************************************
2142 * fsetpos (MSVCRT.@)
2144 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2146 return _lseek(file
->_file
,*pos
,SEEK_SET
);
2149 /*********************************************************************
2152 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
2154 /* Flush output if needed */
2155 if(file
->_flag
& MSVCRT__IOWRT
)
2156 msvcrt_flush_buffer(file
);
2158 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
2159 offset
-= file
->_cnt
;
2161 /* Discard buffered input */
2163 file
->_ptr
= file
->_base
;
2164 /* Reset direction of i/o */
2165 if(file
->_flag
& MSVCRT__IORW
) {
2166 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
2168 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
2171 /*********************************************************************
2174 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
2179 if( file
->_flag
& MSVCRT__IOWRT
) {
2180 off
= file
->_ptr
- file
->_base
;
2185 pos
= _tell(file
->_file
);
2186 if(pos
== -1) return pos
;
2190 /*********************************************************************
2193 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2195 MSVCRT_size_t wrcnt
=size
* nmemb
;
2200 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2201 memcpy(file
->_ptr
, ptr
, pcnt
);
2206 ptr
= (char*)ptr
+ pcnt
;
2207 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2208 if(file
->_flag
& MSVCRT__IORW
) {
2209 file
->_flag
|= MSVCRT__IOWRT
;
2215 int res
=msvcrt_flush_buffer(file
);
2217 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2218 if (pwritten
<= 0) pwritten
=0;
2219 written
+= pwritten
;
2222 return written
/ size
;
2225 /*********************************************************************
2228 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2230 size_t i
, len
= strlen(s
);
2231 if (file
->_flag
& _O_BINARY
)
2232 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2233 for (i
=0; i
<len
; i
++)
2234 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2239 /*********************************************************************
2242 int MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2244 size_t i
, len
= strlenW(s
);
2245 if (file
->_flag
& _O_BINARY
)
2246 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2247 for (i
=0; i
<len
; i
++)
2249 if ((s
[i
] == L
'\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2251 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2257 /*********************************************************************
2258 * getchar (MSVCRT.@)
2260 int MSVCRT_getchar(void)
2262 return MSVCRT_fgetc(MSVCRT_stdin
);
2265 /*********************************************************************
2268 int MSVCRT_getc(MSVCRT_FILE
* file
)
2270 return MSVCRT_fgetc(file
);
2273 /*********************************************************************
2276 char *MSVCRT_gets(char *buf
)
2279 char * buf_start
= buf
;
2281 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2282 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2283 if(cc
!= '\r') *buf
++ = (char)cc
;
2287 TRACE("got '%s'\n", buf_start
);
2291 /*********************************************************************
2294 MSVCRT_wchar_t
* MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2297 MSVCRT_wchar_t
* ws
= buf
;
2299 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2300 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2303 *buf
++ = (MSVCRT_wchar_t
)cc
;
2307 TRACE("got '%s'\n", debugstr_w(ws
));
2311 /*********************************************************************
2314 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2316 return MSVCRT_fputc(c
, file
);
2319 /*********************************************************************
2320 * putchar (MSVCRT.@)
2322 int MSVCRT_putchar(int c
)
2324 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2327 /*********************************************************************
2330 int MSVCRT_puts(const char *s
)
2332 size_t len
= strlen(s
);
2333 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2334 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2337 /*********************************************************************
2340 int _putws(const MSVCRT_wchar_t
*s
)
2342 static const MSVCRT_wchar_t nl
= '\n';
2343 size_t len
= strlenW(s
);
2344 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2345 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2348 /*********************************************************************
2351 int MSVCRT_remove(const char *path
)
2353 TRACE("(%s)\n",path
);
2354 if (DeleteFileA(path
))
2356 TRACE(":failed (%ld)\n",GetLastError());
2357 MSVCRT__set_errno(GetLastError());
2361 /*********************************************************************
2362 * _wremove (MSVCRT.@)
2364 int _wremove(const MSVCRT_wchar_t
*path
)
2366 TRACE("(%s)\n",debugstr_w(path
));
2367 if (DeleteFileW(path
))
2369 TRACE(":failed (%ld)\n",GetLastError());
2370 MSVCRT__set_errno(GetLastError());
2374 /*********************************************************************
2377 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
2379 TRACE(":from %s to %s\n",oldpath
,newpath
);
2380 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2382 TRACE(":failed (%ld)\n",GetLastError());
2383 MSVCRT__set_errno(GetLastError());
2387 /*********************************************************************
2388 * _wrename (MSVCRT.@)
2390 int _wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
2392 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2393 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2395 TRACE(":failed (%ld)\n",GetLastError());
2396 MSVCRT__set_errno(GetLastError());
2400 /*********************************************************************
2401 * setvbuf (MSVCRT.@)
2403 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
2405 /* TODO: Check if file busy */
2407 MSVCRT_free(file
->_base
);
2411 if(mode
== MSVCRT__IOFBF
) {
2412 file
->_flag
&= ~MSVCRT__IONBF
;
2413 file
->_base
= file
->_ptr
= buf
;
2415 file
->_bufsiz
= size
;
2418 file
->_flag
|= MSVCRT__IONBF
;
2423 /*********************************************************************
2426 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
2428 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
2431 /*********************************************************************
2434 char *MSVCRT_tmpnam(char *s
)
2442 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
2443 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2444 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
2446 msvcrt_int_to_base32(unique
++, tmpstr
);
2448 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2449 GetLastError() == ERROR_FILE_NOT_FOUND
)
2455 /*********************************************************************
2456 * tmpfile (MSVCRT.@)
2458 MSVCRT_FILE
* MSVCRT_tmpfile(void)
2460 char *filename
= MSVCRT_tmpnam(NULL
);
2462 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2464 return msvcrt_alloc_fp(fd
);
2468 /*********************************************************************
2469 * vfprintf (MSVCRT.@)
2471 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
2473 char buf
[2048], *mem
= buf
;
2474 int written
, resize
= sizeof(buf
), retval
;
2475 /* There are two conventions for vsnprintf failing:
2476 * Return -1 if we truncated, or
2477 * Return the number of bytes that would have been written
2478 * The code below handles both cases
2480 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
2483 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
2486 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
2489 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2495 /*********************************************************************
2496 * vfwprintf (MSVCRT.@)
2498 * Is final char included in written (then resize is too big) or not
2499 * (then we must test for equality too)?
2501 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, va_list valist
)
2503 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
2504 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
2505 /* See vfprintf comments */
2506 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2509 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
2512 if (!(mem
= (MSVCRT_wchar_t
*)MSVCRT_malloc(resize
*sizeof(*mem
))))
2515 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2521 /*********************************************************************
2522 * vprintf (MSVCRT.@)
2524 int MSVCRT_vprintf(const char *format
, va_list valist
)
2526 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2529 /*********************************************************************
2530 * vwprintf (MSVCRT.@)
2532 int MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, va_list valist
)
2534 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2537 /*********************************************************************
2538 * fprintf (MSVCRT.@)
2540 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2544 va_start(valist
, format
);
2545 res
= MSVCRT_vfprintf(file
, format
, valist
);
2550 /*********************************************************************
2551 * fwprintf (MSVCRT.@)
2553 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
2557 va_start(valist
, format
);
2558 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2563 /*********************************************************************
2566 int MSVCRT_printf(const char *format
, ...)
2570 va_start(valist
, format
);
2571 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2576 /*********************************************************************
2579 int MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
2581 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2582 msvcrt_alloc_buffer(file
);
2585 if(file
->_ptr
>file
->_base
) {
2594 /*********************************************************************
2595 * ungetwc (MSVCRT.@)
2597 MSVCRT_wint_t
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2599 MSVCRT_wchar_t mwc
= wc
;
2600 char * pp
= (char *)&mwc
;
2602 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
2603 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
2609 /*********************************************************************
2610 * wprintf (MSVCRT.@)
2612 int MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
2616 va_start(valist
, format
);
2617 res
= MSVCRT_vwprintf(format
, valist
);