Merge trunk head (r45466)
[reactos.git] / reactos / lib / sdk / crt / stdio / file.c
1 /*
2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/stdio/file.c
5 * PURPOSE: File CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
8 */
9
10 /*
11 * msvcrt.dll file functions
12 *
13 * Copyright 1996,1998 Marcus Meissner
14 * Copyright 1996 Jukka Iivonen
15 * Copyright 1997,2000 Uwe Bonnes
16 * Copyright 2000 Jon Griffiths
17 * Copyright 2004 Eric Pouech
18 * Copyright 2004 Juan Lang
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 *
34 * TODO
35 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
36 */
37
38 #include <precomp.h>
39 #include "wine/unicode.h"
40
41 #include <sys/utime.h>
42 #include <direct.h>
43
44 int *__p__fmode(void);
45 int *__p___mb_cur_max(void);
46
47 #ifdef feof
48 #undef feof
49 #endif
50 #ifdef _fileno
51 #undef _fileno
52 #endif
53 #ifdef ferror
54 #undef ferror
55 #endif
56 #ifdef clearerr
57 #undef clearerr
58 #endif
59
60 #undef getc
61 #undef getwc
62 #undef getchar
63 #undef getwchar
64 #undef putc
65 #undef putwc
66 #undef putchar
67 #undef putwchar
68
69 #undef vprintf
70 #undef vwprintf
71
72 /* _access() bit flags FIXME: incomplete */
73 /* defined in crt/io.h */
74
75 /* values for wxflag in file descriptor */
76 #define WX_OPEN 0x01
77 #define WX_ATEOF 0x02
78 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
79 #define WX_DONTINHERIT 0x10
80 #define WX_APPEND 0x20
81 #define WX_TEXT 0x80
82
83 /* FIXME: this should be allocated dynamically */
84 #define MAX_FILES 2048
85
86 /* ReactOS: Use _FDINFO instead! */
87 typedef struct {
88 HANDLE handle;
89 unsigned char wxflag;
90 DWORD unkn[7]; /* critical section and init flag */
91 } ioinfo;
92
93 ioinfo fdesc[MAX_FILES];
94
95 FILE _iob[3] = { { 0 } };
96
97 static int fdstart = 3; /* first unallocated fd */
98 static int fdend = 3; /* highest allocated fd */
99
100 static FILE* fstreams[2048];
101 static int stream_idx;
102
103 /* INTERNAL: process umask */
104 static int MSVCRT_umask = 0;
105
106 /* INTERNAL: Static buffer for temp file name */
107 static char tmpname[MAX_PATH];
108
109 /* This critical section protects the tables fdesc and fstreams,
110 * and their related indexes, fdstart, fdend,
111 * and stream_idx, from race conditions.
112 * It doesn't protect against race conditions manipulating the underlying files
113 * or flags; doing so would probably be better accomplished with per-file
114 * protection, rather than locking the whole table for every change.
115 */
116 static CRITICAL_SECTION FILE_cs;
117 #define LOCK_FILES() do { EnterCriticalSection(&FILE_cs); } while (0)
118 #define UNLOCK_FILES() do { LeaveCriticalSection(&FILE_cs); } while (0)
119
120 FILE *__cdecl __iob_func()
121 {
122 return _iob;
123 }
124
125 static inline BOOL is_valid_fd(int fd)
126 {
127 return fd >= 0 && fd < fdend && (fdesc[fd].wxflag & WX_OPEN);
128 }
129
130 /* INTERNAL: Get the HANDLE for a fd
131 * This doesn't lock the table, because a failure will result in
132 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
133 * it returns a valid handle which is about to be closed, a subsequent call
134 * will fail, most likely in a sane way.
135 */
136 HANDLE fdtoh(int fd)
137 {
138 if (!is_valid_fd(fd))
139 {
140 WARN(":fd (%d) - no handle!\n",fd);
141 *__doserrno() = 0;
142 *_errno() = EBADF;
143 return INVALID_HANDLE_VALUE;
144 }
145 //if (fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
146 return fdesc[fd].handle;
147 }
148
149 /* INTERNAL: free a file entry fd */
150 static void free_fd(int fd)
151 {
152 LOCK_FILES();
153 fdesc[fd].handle = INVALID_HANDLE_VALUE;
154 fdesc[fd].wxflag = 0;
155 TRACE(":fd (%d) freed\n",fd);
156 if (fd < 3) /* don't use 0,1,2 for user files */
157 {
158 switch (fd)
159 {
160 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
161 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
162 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
163 }
164 }
165 else
166 {
167 if (fd == fdend - 1)
168 fdend--;
169 if (fd < fdstart)
170 fdstart = fd;
171 }
172 UNLOCK_FILES();
173 }
174
175 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
176 /* caller must hold the files lock */
177 static int alloc_fd_from(HANDLE hand, int flag, int fd)
178 {
179 if (fd >= MAX_FILES)
180 {
181 WARN(":files exhausted!\n");
182 return -1;
183 }
184 fdesc[fd].handle = hand;
185 fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
186
187 /* locate next free slot */
188 if (fd == fdstart && fd == fdend)
189 fdstart = fdend + 1;
190 else
191 while (fdstart < fdend &&
192 fdesc[fdstart].handle != INVALID_HANDLE_VALUE)
193 fdstart++;
194 /* update last fd in use */
195 if (fd >= fdend)
196 fdend = fd + 1;
197 TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend);
198
199 switch (fd)
200 {
201 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
202 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
203 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
204 }
205
206 return fd;
207 }
208
209 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
210 /*static */int alloc_fd(HANDLE hand, int flag)
211 {
212 int ret;
213
214 LOCK_FILES();
215 TRACE(":handle (%p) allocating fd (%d)\n",hand,fdstart);
216 ret = alloc_fd_from(hand, flag, fdstart);
217 UNLOCK_FILES();
218 return ret;
219 }
220
221 /* INTERNAL: Allocate a FILE* for an fd slot */
222 /* caller must hold the files lock */
223 static FILE* alloc_fp(void)
224 {
225 unsigned int i;
226
227 for (i = 3; i < sizeof(fstreams) / sizeof(fstreams[0]); i++)
228 {
229 if (!fstreams[i] || fstreams[i]->_flag == 0)
230 {
231 if (!fstreams[i])
232 {
233 if (!(fstreams[i] = calloc(sizeof(FILE),1)))
234 return NULL;
235 if (i == stream_idx) stream_idx++;
236 }
237 return fstreams[i];
238 }
239 }
240 return NULL;
241 }
242
243 /* INTERNAL: initialize a FILE* from an open fd */
244 static int init_fp(FILE* file, int fd, unsigned stream_flags)
245 {
246 TRACE(":fd (%d) allocating FILE*\n",fd);
247 if (!is_valid_fd(fd))
248 {
249 WARN(":invalid fd %d\n",fd);
250 *__doserrno() = 0;
251 *_errno() = EBADF;
252 return -1;
253 }
254 memset(file, 0, sizeof(*file));
255 file->_file = fd;
256 file->_flag = stream_flags;
257
258 TRACE(":got FILE* (%p)\n",file);
259 return 0;
260 }
261
262 /* INTERNAL: Create an inheritance data block (for spawned process)
263 * The inheritance block is made of:
264 * 00 int nb of file descriptor (NBFD)
265 * 04 char file flags (wxflag): repeated for each fd
266 * 4+NBFD HANDLE file handle: repeated for each fd
267 */
268 unsigned create_io_inherit_block(WORD *size, BYTE **block)
269 {
270 int fd;
271 char* wxflag_ptr;
272 HANDLE* handle_ptr;
273
274 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * fdend;
275 *block = calloc(*size, 1);
276 if (!*block)
277 {
278 *size = 0;
279 return FALSE;
280 }
281 wxflag_ptr = (char*)*block + sizeof(unsigned);
282 handle_ptr = (HANDLE*)(wxflag_ptr + fdend * sizeof(char));
283
284 *(unsigned*)*block = fdend;
285 for (fd = 0; fd < fdend; fd++)
286 {
287 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
288 if ((fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
289 {
290 *wxflag_ptr = fdesc[fd].wxflag;
291 *handle_ptr = fdesc[fd].handle;
292 }
293 else
294 {
295 *wxflag_ptr = 0;
296 *handle_ptr = INVALID_HANDLE_VALUE;
297 }
298 wxflag_ptr++; handle_ptr++;
299 }
300 return TRUE;
301 }
302
303 /* INTERNAL: Set up all file descriptors,
304 * as well as default streams (stdin, stderr and stdout)
305 */
306 void msvcrt_init_io(void)
307 {
308 STARTUPINFOA si;
309 int i;
310
311 InitializeCriticalSection(&FILE_cs);
312 FILE_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FILE_cs");
313 GetStartupInfoA(&si);
314 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
315 {
316 BYTE* wxflag_ptr;
317 HANDLE* handle_ptr;
318 unsigned int count;
319
320 count = *(unsigned*)si.lpReserved2;
321 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
322 handle_ptr = (HANDLE*)(wxflag_ptr + count);
323
324 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
325 count = min(count, sizeof(fdesc) / sizeof(fdesc[0]));
326 for (i = 0; i < count; i++)
327 {
328 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
329 {
330 fdesc[i].wxflag = *wxflag_ptr;
331 fdesc[i].handle = *handle_ptr;
332 }
333 else
334 {
335 fdesc[i].wxflag = 0;
336 fdesc[i].handle = INVALID_HANDLE_VALUE;
337 }
338 wxflag_ptr++; handle_ptr++;
339 }
340 fdend = max( 3, count );
341 for (fdstart = 3; fdstart < fdend; fdstart++)
342 if (fdesc[fdstart].handle == INVALID_HANDLE_VALUE) break;
343 }
344
345 if (!(fdesc[0].wxflag & WX_OPEN) || fdesc[0].handle == INVALID_HANDLE_VALUE)
346 {
347 #ifndef __REACTOS__
348 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
349 GetCurrentProcess(), &fdesc[0].handle, 0, TRUE,
350 DUPLICATE_SAME_ACCESS);
351 #else
352 fdesc[0].handle = GetStdHandle(STD_INPUT_HANDLE);
353 if (fdesc[0].handle == NULL)
354 fdesc[0].handle = INVALID_HANDLE_VALUE;
355 #endif
356 fdesc[0].wxflag = WX_OPEN | WX_TEXT;
357 }
358 if (!(fdesc[1].wxflag & WX_OPEN) || fdesc[1].handle == INVALID_HANDLE_VALUE)
359 {
360 #ifndef __REACTOS__
361 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
362 GetCurrentProcess(), &fdesc[1].handle, 0, TRUE,
363 DUPLICATE_SAME_ACCESS);
364 #else
365 fdesc[1].handle = GetStdHandle(STD_OUTPUT_HANDLE);
366 if (fdesc[1].handle == NULL)
367 fdesc[1].handle = INVALID_HANDLE_VALUE;
368 #endif
369 fdesc[1].wxflag = WX_OPEN | WX_TEXT;
370 }
371 if (!(fdesc[2].wxflag & WX_OPEN) || fdesc[2].handle == INVALID_HANDLE_VALUE)
372 {
373 #ifndef __REACTOS__
374 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
375 GetCurrentProcess(), &fdesc[2].handle, 0, TRUE,
376 DUPLICATE_SAME_ACCESS);
377 #else
378 fdesc[2].handle = GetStdHandle(STD_ERROR_HANDLE);
379 if (fdesc[2].handle == NULL)
380 fdesc[2].handle = INVALID_HANDLE_VALUE;
381 #endif
382 fdesc[2].wxflag = WX_OPEN | WX_TEXT;
383 }
384
385 TRACE(":handles (%p)(%p)(%p)\n",fdesc[0].handle,
386 fdesc[1].handle,fdesc[2].handle);
387
388 memset(_iob,0,3*sizeof(FILE));
389 for (i = 0; i < 3; i++)
390 {
391 /* FILE structs for stdin/out/err are static and never deleted */
392 fstreams[i] = &_iob[i];
393 _iob[i]._file = i;
394 _iob[i]._tmpfname = NULL;
395 _iob[i]._flag = (i == 0) ? _IOREAD : _IOWRT;
396 }
397 stream_idx = 3;
398 }
399
400 /* INTERNAL: Flush stdio file buffer */
401 static int flush_buffer(FILE* file)
402 {
403 if(file->_bufsiz) {
404 int cnt=file->_ptr-file->_base;
405 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
406 file->_flag |= _IOERR;
407 return EOF;
408 }
409 file->_ptr=file->_base;
410 file->_cnt=file->_bufsiz;
411 }
412 return 0;
413 }
414
415 /* INTERNAL: Allocate stdio file buffer */
416 static void alloc_buffer(FILE* file)
417 {
418 file->_base = calloc(BUFSIZ,1);
419 if(file->_base) {
420 file->_bufsiz = BUFSIZ;
421 file->_flag |= _IOMYBUF;
422 } else {
423 file->_base = (char*)(&file->_charbuf);
424 /* put here 2 ??? */
425 file->_bufsiz = sizeof(file->_charbuf);
426 }
427 file->_ptr = file->_base;
428 file->_cnt = 0;
429 }
430
431 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
432 static void int_to_base32(int num, char *str)
433 {
434 char *p;
435 int n = num;
436 int digits = 0;
437
438 while (n != 0)
439 {
440 n >>= 5;
441 digits++;
442 }
443 p = str + digits;
444 *p = 0;
445 while (--p >= str)
446 {
447 *p = (num & 31) + '0';
448 if (*p > '9')
449 *p += ('a' - '0' - 10);
450 num >>= 5;
451 }
452 }
453
454 /*********************************************************************
455 * __p__iob(MSVCRT.@)
456 */
457 FILE * CDECL __p__iob(void)
458 {
459 return _iob;
460 }
461
462 /*********************************************************************
463 * _access (MSVCRT.@)
464 */
465 int CDECL _access(const char *filename, int mode)
466 {
467 DWORD attr = GetFileAttributesA(filename);
468
469 TRACE("(%s,%d) %d\n",filename,mode,attr);
470
471 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
472 {
473 _dosmaperr(GetLastError());
474 return -1;
475 }
476 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
477 {
478 _dosmaperr(ERROR_ACCESS_DENIED);
479 return -1;
480 }
481 return 0;
482 }
483
484 /*********************************************************************
485 * _waccess (MSVCRT.@)
486 */
487 int CDECL _waccess(const wchar_t *filename, int mode)
488 {
489 DWORD attr = GetFileAttributesW(filename);
490
491 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
492
493 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
494 {
495 _dosmaperr(GetLastError());
496 return -1;
497 }
498 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
499 {
500 _dosmaperr(ERROR_ACCESS_DENIED);
501 return -1;
502 }
503 return 0;
504 }
505
506 /*********************************************************************
507 * _chmod (MSVCRT.@)
508 */
509 int CDECL _chmod(const char *path, int flags)
510 {
511 DWORD oldFlags = GetFileAttributesA(path);
512
513 if (oldFlags != INVALID_FILE_ATTRIBUTES)
514 {
515 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
516 oldFlags | FILE_ATTRIBUTE_READONLY;
517
518 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
519 return 0;
520 }
521 _dosmaperr(GetLastError());
522 return -1;
523 }
524
525 /*********************************************************************
526 * _wchmod (MSVCRT.@)
527 */
528 int CDECL _wchmod(const wchar_t *path, int flags)
529 {
530 DWORD oldFlags = GetFileAttributesW(path);
531
532 if (oldFlags != INVALID_FILE_ATTRIBUTES)
533 {
534 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
535 oldFlags | FILE_ATTRIBUTE_READONLY;
536
537 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
538 return 0;
539 }
540 _dosmaperr(GetLastError());
541 return -1;
542 }
543
544 /*********************************************************************
545 * _unlink (MSVCRT.@)
546 */
547 int CDECL _unlink(const char *path)
548 {
549 TRACE("%s\n",debugstr_a(path));
550 if(DeleteFileA(path))
551 return 0;
552 TRACE("failed (%d)\n",GetLastError());
553 _dosmaperr(GetLastError());
554 return -1;
555 }
556
557 /*********************************************************************
558 * _wunlink (MSVCRT.@)
559 */
560 int CDECL _wunlink(const wchar_t *path)
561 {
562 TRACE("(%s)\n",debugstr_w(path));
563 if(DeleteFileW(path))
564 return 0;
565 TRACE("failed (%d)\n",GetLastError());
566 _dosmaperr(GetLastError());
567 return -1;
568 }
569
570 /* _flushall calls fflush which calls _flushall */
571 int CDECL fflush(FILE* file);
572
573 /*********************************************************************
574 * _flushall (MSVCRT.@)
575 */
576 int CDECL _flushall(void)
577 {
578 int i, num_flushed = 0;
579
580 LOCK_FILES();
581 for (i = 3; i < stream_idx; i++)
582 if (fstreams[i] && fstreams[i]->_flag)
583 {
584 #if 0
585 /* FIXME: flush, do not commit */
586 if (_commit(i) == -1)
587 if (fstreams[i])
588 fstreams[i]->_flag |= _IOERR;
589 #endif
590 if(fstreams[i]->_flag & _IOWRT) {
591 fflush(fstreams[i]);
592 num_flushed++;
593 }
594 }
595 UNLOCK_FILES();
596
597 TRACE(":flushed (%d) handles\n",num_flushed);
598 return num_flushed;
599 }
600
601 /*********************************************************************
602 * fflush (MSVCRT.@)
603 */
604 int CDECL fflush(FILE* file)
605 {
606 if(!file) {
607 _flushall();
608 } else if(file->_flag & _IOWRT) {
609 int res=flush_buffer(file);
610 return res;
611 }
612 return 0;
613 }
614
615 /*********************************************************************
616 * _close (MSVCRT.@)
617 */
618 int CDECL _close(int fd)
619 {
620 HANDLE hand;
621 int ret;
622
623 LOCK_FILES();
624 hand = fdtoh(fd);
625 TRACE(":fd (%d) handle (%p)\n",fd,hand);
626 if (hand == INVALID_HANDLE_VALUE)
627 ret = -1;
628 else if (!CloseHandle(hand))
629 {
630 WARN(":failed-last error (%d)\n",GetLastError());
631 _dosmaperr(GetLastError());
632 ret = -1;
633 }
634 else
635 {
636 free_fd(fd);
637 ret = 0;
638 }
639 UNLOCK_FILES();
640 TRACE(":ok\n");
641 return ret;
642 }
643
644 /*********************************************************************
645 * _commit (MSVCRT.@)
646 */
647 int CDECL _commit(int fd)
648 {
649 HANDLE hand = fdtoh(fd);
650
651 TRACE(":fd (%d) handle (%p)\n",fd,hand);
652 if (hand == INVALID_HANDLE_VALUE)
653 return -1;
654
655 if (!FlushFileBuffers(hand))
656 {
657 if (GetLastError() == ERROR_INVALID_HANDLE)
658 {
659 /* FlushFileBuffers fails for console handles
660 * so we ignore this error.
661 */
662 return 0;
663 }
664 TRACE(":failed-last error (%d)\n",GetLastError());
665 _dosmaperr(GetLastError());
666 return -1;
667 }
668 TRACE(":ok\n");
669 return 0;
670 }
671
672 /*********************************************************************
673 * _dup2 (MSVCRT.@)
674 * NOTES
675 * MSDN isn't clear on this point, but the remarks for _pipe
676 * indicate file descriptors duplicated with _dup and _dup2 are always
677 * inheritable.
678 */
679 int CDECL _dup2(int od, int nd)
680 {
681 int ret;
682
683 TRACE("(od=%d, nd=%d)\n", od, nd);
684 LOCK_FILES();
685 if (nd < MAX_FILES && is_valid_fd(od))
686 {
687 HANDLE handle;
688
689 if (DuplicateHandle(GetCurrentProcess(), fdesc[od].handle,
690 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
691 {
692 int wxflag = fdesc[od].wxflag & ~_O_NOINHERIT;
693
694 if (is_valid_fd(nd))
695 _close(nd);
696 ret = alloc_fd_from(handle, wxflag, nd);
697 if (ret == -1)
698 {
699 CloseHandle(handle);
700 *_errno() = EMFILE;
701 }
702 else
703 {
704 /* _dup2 returns 0, not nd, on success */
705 ret = 0;
706 }
707 }
708 else
709 {
710 ret = -1;
711 _dosmaperr(GetLastError());
712 }
713 }
714 else
715 {
716 *_errno() = EBADF;
717 ret = -1;
718 }
719 UNLOCK_FILES();
720 return ret;
721 }
722
723 /*********************************************************************
724 * _dup (MSVCRT.@)
725 */
726 int CDECL _dup(int od)
727 {
728 int fd, ret;
729
730 LOCK_FILES();
731 fd = fdstart;
732 if (_dup2(od, fd) == 0)
733 ret = fd;
734 else
735 ret = -1;
736 UNLOCK_FILES();
737 return ret;
738 }
739
740 /*********************************************************************
741 * _eof (MSVCRT.@)
742 */
743 int CDECL _eof(int fd)
744 {
745 DWORD curpos,endpos;
746 LONG hcurpos,hendpos;
747 HANDLE hand = fdtoh(fd);
748
749 TRACE(":fd (%d) handle (%p)\n",fd,hand);
750
751 if (hand == INVALID_HANDLE_VALUE)
752 return -1;
753
754 if (fdesc[fd].wxflag & WX_ATEOF) return TRUE;
755
756 /* Otherwise we do it the hard way */
757 hcurpos = hendpos = 0;
758 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
759 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
760
761 if (curpos == endpos && hcurpos == hendpos)
762 {
763 /* FIXME: shouldn't WX_ATEOF be set here? */
764 return TRUE;
765 }
766
767 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
768 return FALSE;
769 }
770
771 /*********************************************************************
772 * _fcloseall (MSVCRT.@)
773 */
774 int CDECL _fcloseall(void)
775 {
776 int num_closed = 0, i;
777
778 LOCK_FILES();
779 for (i = 3; i < stream_idx; i++)
780 if (fstreams[i] && fstreams[i]->_flag &&
781 !fclose(fstreams[i]))
782 num_closed++;
783 UNLOCK_FILES();
784
785 TRACE(":closed (%d) handles\n",num_closed);
786 return num_closed;
787 }
788
789 /* free everything on process exit */
790 void msvcrt_free_io(void)
791 {
792 _fcloseall();
793 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
794 * stdout, and stderr (unlike GNU), so we need to fclose() them here
795 * or they won't get flushed.
796 */
797 fclose(&_iob[0]);
798 fclose(&_iob[1]);
799 fclose(&_iob[2]);
800 FILE_cs.DebugInfo->Spare[0] = 0;
801 DeleteCriticalSection(&FILE_cs);
802 }
803
804 /*********************************************************************
805 * _lseeki64 (MSVCRT.@)
806 */
807 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
808 {
809 HANDLE hand = fdtoh(fd);
810 LARGE_INTEGER ofs, ret;
811
812 TRACE(":fd (%d) handle (%p)\n",fd,hand);
813 if (hand == INVALID_HANDLE_VALUE)
814 return -1;
815
816 if (whence < 0 || whence > 2)
817 {
818 *_errno() = EINVAL;
819 return -1;
820 }
821
822 TRACE(":fd (%d) to %s pos %s\n",
823 fd,wine_dbgstr_longlong(offset),
824 (whence==SEEK_SET)?"SEEK_SET":
825 (whence==SEEK_CUR)?"SEEK_CUR":
826 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
827
828 ofs.QuadPart = offset;
829 if (SetFilePointerEx(hand, ofs, &ret, whence))
830 {
831 fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
832 /* FIXME: What if we seek _to_ EOF - is EOF set? */
833
834 return ret.QuadPart;
835 }
836 TRACE(":error-last error (%d)\n",GetLastError());
837 _dosmaperr(GetLastError());
838 return -1;
839 }
840
841 /*********************************************************************
842 * _lseek (MSVCRT.@)
843 */
844 LONG CDECL _lseek(int fd, LONG offset, int whence)
845 {
846 return _lseeki64(fd, offset, whence);
847 }
848
849 /*********************************************************************
850 * _locking (MSVCRT.@)
851 *
852 * This is untested; the underlying LockFile doesn't work yet.
853 */
854 int CDECL _locking(int fd, int mode, LONG nbytes)
855 {
856 BOOL ret;
857 DWORD cur_locn;
858 HANDLE hand = fdtoh(fd);
859
860 TRACE(":fd (%d) handle (%p)\n",fd,hand);
861 if (hand == INVALID_HANDLE_VALUE)
862 return -1;
863
864 if (mode < 0 || mode > 4)
865 {
866 *_errno() = EINVAL;
867 return -1;
868 }
869
870 TRACE(":fd (%d) by 0x%08x mode %s\n",
871 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
872 (mode==_LK_LOCK)?"_LK_LOCK":
873 (mode==_LK_NBLCK)?"_LK_NBLCK":
874 (mode==_LK_RLCK)?"_LK_RLCK":
875 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
876 "UNKNOWN");
877
878 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
879 {
880 FIXME ("Seek failed\n");
881 *_errno() = EINVAL; /* FIXME */
882 return -1;
883 }
884 if (mode == _LK_LOCK || mode == _LK_RLCK)
885 {
886 int nretry = 10;
887 ret = 1; /* just to satisfy gcc */
888 while (nretry--)
889 {
890 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
891 if (ret) break;
892 Sleep(1);
893 }
894 }
895 else if (mode == _LK_UNLCK)
896 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
897 else
898 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
899 /* FIXME - what about error settings? */
900 return ret ? 0 : -1;
901 }
902
903 /*********************************************************************
904 * fseek (MSVCRT.@)
905 */
906 int CDECL fseek(FILE* file, long offset, int whence)
907 {
908 /* Flush output if needed */
909 if(file->_flag & _IOWRT)
910 flush_buffer(file);
911
912 if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
913 offset -= file->_cnt;
914 if (fdesc[file->_file].wxflag & WX_TEXT) {
915 /* Black magic correction for CR removal */
916 int i;
917 for (i=0; i<file->_cnt; i++) {
918 if (file->_ptr[i] == '\n')
919 offset--;
920 }
921 }
922 }
923 /* Discard buffered input */
924 file->_cnt = 0;
925 file->_ptr = file->_base;
926 /* Reset direction of i/o */
927 if(file->_flag & _IORW) {
928 file->_flag &= ~(_IOREAD|_IOWRT);
929 }
930 /* Clear end of file flag */
931 file->_flag &= ~_IOEOF;
932 return (_lseek(file->_file,offset,whence) == -1)?-1:0;
933 }
934
935 /*********************************************************************
936 * _chsize (MSVCRT.@)
937 */
938 int CDECL _chsize(int fd, long size)
939 {
940 LONG cur, pos;
941 HANDLE handle;
942 BOOL ret = FALSE;
943
944 TRACE("(fd=%d, size=%ld)\n", fd, size);
945
946 LOCK_FILES();
947
948 handle = fdtoh(fd);
949 if (handle != INVALID_HANDLE_VALUE)
950 {
951 /* save the current file pointer */
952 cur = _lseek(fd, 0, SEEK_CUR);
953 if (cur >= 0)
954 {
955 pos = _lseek(fd, size, SEEK_SET);
956 if (pos >= 0)
957 {
958 ret = SetEndOfFile(handle);
959 if (!ret) _dosmaperr(GetLastError());
960 }
961
962 /* restore the file pointer */
963 _lseek(fd, cur, SEEK_SET);
964 }
965 }
966
967 UNLOCK_FILES();
968 return ret ? 0 : -1;
969 }
970
971 /*********************************************************************
972 * clearerr (MSVCRT.@)
973 */
974 void CDECL clearerr(FILE* file)
975 {
976 TRACE(":file (%p) fd (%d)\n",file,file->_file);
977 file->_flag &= ~(_IOERR | _IOEOF);
978 }
979
980 /*********************************************************************
981 * rewind (MSVCRT.@)
982 */
983 void CDECL rewind(FILE* file)
984 {
985 TRACE(":file (%p) fd (%d)\n",file,file->_file);
986 fseek(file, 0L, SEEK_SET);
987 clearerr(file);
988 }
989
990 static int get_flags(const char* mode, int *open_flags, int* stream_flags)
991 {
992 int plus = strchr(mode, '+') != NULL;
993
994 switch(*mode++)
995 {
996 case 'R': case 'r':
997 *open_flags = plus ? _O_RDWR : _O_RDONLY;
998 *stream_flags = plus ? _IORW : _IOREAD;
999 break;
1000 case 'W': case 'w':
1001 *open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1002 *stream_flags = plus ? _IORW : _IOWRT;
1003 break;
1004 case 'A': case 'a':
1005 *open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1006 *stream_flags = plus ? _IORW : _IOWRT;
1007 break;
1008 default:
1009 return -1;
1010 }
1011
1012 while (*mode)
1013 switch (*mode++)
1014 {
1015 case 'B': case 'b':
1016 *open_flags |= _O_BINARY;
1017 *open_flags &= ~_O_TEXT;
1018 break;
1019 case 'T': case 't':
1020 *open_flags |= _O_TEXT;
1021 *open_flags &= ~_O_BINARY;
1022 break;
1023 case '+':
1024 break;
1025 default:
1026 FIXME(":unknown flag %c not supported\n",mode[-1]);
1027 }
1028 return 0;
1029 }
1030
1031 /*********************************************************************
1032 * _fdopen (MSVCRT.@)
1033 */
1034 FILE* CDECL _fdopen(int fd, const char *mode)
1035 {
1036 int open_flags, stream_flags;
1037 FILE* file;
1038
1039 if (get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1040
1041 LOCK_FILES();
1042 if (!(file = alloc_fp()))
1043 file = NULL;
1044 else if (init_fp(file, fd, stream_flags) == -1)
1045 {
1046 file->_flag = 0;
1047 file = NULL;
1048 }
1049 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
1050 UNLOCK_FILES();
1051
1052 return file;
1053 }
1054
1055 /*********************************************************************
1056 * _wfdopen (MSVCRT.@)
1057 */
1058 FILE* CDECL _wfdopen(int fd, const wchar_t *mode)
1059 {
1060 unsigned mlen = strlenW(mode);
1061 char *modea = calloc(mlen + 1, 1);
1062 FILE* file = NULL;
1063 int open_flags, stream_flags;
1064
1065 if (modea &&
1066 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
1067 {
1068 if (get_flags(modea, &open_flags, &stream_flags) == -1)
1069 {
1070 free(modea);
1071 return NULL;
1072 }
1073 LOCK_FILES();
1074 if (!(file = alloc_fp()))
1075 file = NULL;
1076 else if (init_fp(file, fd, stream_flags) == -1)
1077 {
1078 file->_flag = 0;
1079 file = NULL;
1080 }
1081 else
1082 {
1083 if (file)
1084 rewind(file); /* FIXME: is this needed ??? */
1085 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
1086 }
1087 UNLOCK_FILES();
1088 }
1089 free(modea);
1090 return file;
1091 }
1092
1093 /*********************************************************************
1094 * _filelength (MSVCRT.@)
1095 */
1096 LONG CDECL _filelength(int fd)
1097 {
1098 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1099 if (curPos != -1)
1100 {
1101 LONG endPos = _lseek(fd, 0, SEEK_END);
1102 if (endPos != -1)
1103 {
1104 if (endPos != curPos)
1105 _lseek(fd, curPos, SEEK_SET);
1106 return endPos;
1107 }
1108 }
1109 return -1;
1110 }
1111
1112 /*********************************************************************
1113 * _filelengthi64 (MSVCRT.@)
1114 */
1115 __int64 CDECL _filelengthi64(int fd)
1116 {
1117 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1118 if (curPos != -1)
1119 {
1120 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1121 if (endPos != -1)
1122 {
1123 if (endPos != curPos)
1124 _lseeki64(fd, curPos, SEEK_SET);
1125 return endPos;
1126 }
1127 }
1128 return -1;
1129 }
1130
1131 /*********************************************************************
1132 * _fileno (MSVCRT.@)
1133 */
1134 int CDECL _fileno(FILE* file)
1135 {
1136 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1137 return file->_file;
1138 }
1139
1140 /*********************************************************************
1141 * _futime (MSVCRT.@)
1142 */
1143 int CDECL _futime(int fd, struct _utimbuf *t)
1144 {
1145 HANDLE hand = fdtoh(fd);
1146 FILETIME at, wt;
1147
1148 if (hand == INVALID_HANDLE_VALUE)
1149 return -1;
1150
1151 if (!t)
1152 {
1153 time_t currTime;
1154 time(&currTime);
1155 RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
1156 wt = at;
1157 }
1158 else
1159 {
1160 RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
1161 if (t->actime == t->modtime)
1162 wt = at;
1163 else
1164 RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
1165 }
1166
1167 if (!SetFileTime(hand, NULL, &at, &wt))
1168 {
1169 _dosmaperr(GetLastError());
1170 return -1 ;
1171 }
1172 return 0;
1173 }
1174
1175 /*********************************************************************
1176 * _get_osfhandle (MSVCRT.@)
1177 */
1178 intptr_t CDECL _get_osfhandle(int fd)
1179 {
1180 HANDLE hand = fdtoh(fd);
1181 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1182
1183 return (long)(LONG_PTR)hand;
1184 }
1185
1186 /*********************************************************************
1187 * _isatty (MSVCRT.@)
1188 */
1189 int CDECL _isatty(int fd)
1190 {
1191 HANDLE hand = fdtoh(fd);
1192
1193 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1194 if (hand == INVALID_HANDLE_VALUE)
1195 return 0;
1196
1197 return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
1198 }
1199
1200 /*********************************************************************
1201 * _mktemp (MSVCRT.@)
1202 */
1203 char * CDECL _mktemp(char *pattern)
1204 {
1205 int numX = 0;
1206 char *retVal = pattern;
1207 int id;
1208 char letter = 'a';
1209
1210 while(*pattern)
1211 numX = (*pattern++ == 'X')? numX + 1 : 0;
1212 if (numX < 5)
1213 return NULL;
1214 pattern--;
1215 id = GetCurrentProcessId();
1216 numX = 6;
1217 while(numX--)
1218 {
1219 int tempNum = id / 10;
1220 *pattern-- = id - (tempNum * 10) + '0';
1221 id = tempNum;
1222 }
1223 pattern++;
1224 do
1225 {
1226 *pattern = letter++;
1227 if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
1228 GetLastError() == ERROR_FILE_NOT_FOUND)
1229 return retVal;
1230 } while(letter <= 'z');
1231 return NULL;
1232 }
1233
1234 /*********************************************************************
1235 * _wmktemp (MSVCRT.@)
1236 */
1237 wchar_t * CDECL _wmktemp(wchar_t *pattern)
1238 {
1239 int numX = 0;
1240 wchar_t *retVal = pattern;
1241 int id;
1242 wchar_t letter = 'a';
1243
1244 while(*pattern)
1245 numX = (*pattern++ == 'X')? numX + 1 : 0;
1246 if (numX < 5)
1247 return NULL;
1248 pattern--;
1249 id = GetCurrentProcessId();
1250 numX = 6;
1251 while(numX--)
1252 {
1253 int tempNum = id / 10;
1254 *pattern-- = id - (tempNum * 10) + '0';
1255 id = tempNum;
1256 }
1257 pattern++;
1258 do
1259 {
1260 if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
1261 GetLastError() == ERROR_FILE_NOT_FOUND)
1262 return retVal;
1263 *pattern = letter++;
1264 } while(letter != '|');
1265 return NULL;
1266 }
1267
1268 /*static */unsigned split_oflags(unsigned oflags)
1269 {
1270 int wxflags = 0;
1271 unsigned unsupp; /* until we support everything */
1272
1273 if (oflags & _O_APPEND) wxflags |= WX_APPEND;
1274 if (oflags & _O_BINARY) {/* Nothing to do */}
1275 else if (oflags & _O_TEXT) wxflags |= WX_TEXT;
1276 else if (*__p__fmode() & _O_BINARY) {/* Nothing to do */}
1277 else wxflags |= WX_TEXT; /* default to TEXT*/
1278 if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1279
1280 if ((unsupp = oflags & ~(
1281 _O_BINARY|_O_TEXT|_O_APPEND|
1282 _O_TRUNC|_O_EXCL|_O_CREAT|
1283 _O_RDWR|_O_WRONLY|_O_TEMPORARY|
1284 _O_NOINHERIT|
1285 _O_SEQUENTIAL|_O_RANDOM|_O_SHORT_LIVED
1286 )))
1287 ERR(":unsupported oflags 0x%04x\n",unsupp);
1288
1289 return wxflags;
1290 }
1291
1292 /*********************************************************************
1293 * _pipe (MSVCRT.@)
1294 */
1295 int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
1296 {
1297 int ret = -1;
1298 SECURITY_ATTRIBUTES sa;
1299 HANDLE readHandle, writeHandle;
1300
1301 if (!pfds)
1302 {
1303 *_errno() = EINVAL;
1304 return -1;
1305 }
1306
1307 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1308 sa.bInheritHandle = !(textmode & _O_NOINHERIT);
1309 sa.lpSecurityDescriptor = NULL;
1310 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1311 {
1312 unsigned int wxflags = split_oflags(textmode);
1313 int fd;
1314
1315 LOCK_FILES();
1316 fd = alloc_fd(readHandle, wxflags);
1317 if (fd != -1)
1318 {
1319 pfds[0] = fd;
1320 fd = alloc_fd(writeHandle, wxflags);
1321 if (fd != -1)
1322 {
1323 pfds[1] = fd;
1324 ret = 0;
1325 }
1326 else
1327 {
1328 _close(pfds[0]);
1329 CloseHandle(writeHandle);
1330 *_errno() = EMFILE;
1331 }
1332 }
1333 else
1334 {
1335 CloseHandle(readHandle);
1336 CloseHandle(writeHandle);
1337 *_errno() = EMFILE;
1338 }
1339 UNLOCK_FILES();
1340 }
1341 else
1342 _dosmaperr(GetLastError());
1343
1344 return ret;
1345 }
1346
1347 /*********************************************************************
1348 * _sopen (MSVCRT.@)
1349 */
1350 int CDECL _sopen( const char *path, int oflags, int shflags, ... )
1351 {
1352 va_list ap;
1353 int pmode;
1354 DWORD access = 0, creation = 0, attrib;
1355 DWORD sharing;
1356 int wxflag = 0, fd;
1357 HANDLE hand;
1358 SECURITY_ATTRIBUTES sa;
1359
1360
1361 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1362 path, oflags, shflags);
1363
1364 wxflag = split_oflags(oflags);
1365 switch (oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
1366 {
1367 case _O_RDONLY: access |= GENERIC_READ; break;
1368 case _O_WRONLY: access |= GENERIC_WRITE; break;
1369 case _O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1370 }
1371
1372 if (oflags & _O_CREAT)
1373 {
1374 va_start(ap, shflags);
1375 pmode = va_arg(ap, int);
1376 va_end(ap);
1377
1378 if(pmode & ~(_S_IREAD | _S_IWRITE))
1379 FIXME(": pmode 0x%04x ignored\n", pmode);
1380 else
1381 WARN(": pmode 0x%04x ignored\n", pmode);
1382
1383 if (oflags & _O_EXCL)
1384 creation = CREATE_NEW;
1385 else if (oflags & _O_TRUNC)
1386 creation = CREATE_ALWAYS;
1387 else
1388 creation = OPEN_ALWAYS;
1389 }
1390 else /* no _O_CREAT */
1391 {
1392 if (oflags & _O_TRUNC)
1393 creation = TRUNCATE_EXISTING;
1394 else
1395 creation = OPEN_EXISTING;
1396 }
1397
1398 switch( shflags )
1399 {
1400 case _SH_DENYRW:
1401 sharing = 0L;
1402 break;
1403 case _SH_DENYWR:
1404 sharing = FILE_SHARE_READ;
1405 break;
1406 case _SH_DENYRD:
1407 sharing = FILE_SHARE_WRITE;
1408 break;
1409 case _SH_DENYNO:
1410 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1411 break;
1412 default:
1413 ERR( "Unhandled shflags 0x%x\n", shflags );
1414 return -1;
1415 }
1416 attrib = FILE_ATTRIBUTE_NORMAL;
1417
1418 if (oflags & _O_TEMPORARY)
1419 {
1420 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1421 access |= DELETE;
1422 sharing |= FILE_SHARE_DELETE;
1423 }
1424
1425 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1426 sa.lpSecurityDescriptor = NULL;
1427 sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
1428
1429 hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 0);
1430
1431 if (hand == INVALID_HANDLE_VALUE) {
1432 WARN(":failed-last error (%d)\n",GetLastError());
1433 _dosmaperr(GetLastError());
1434 return -1;
1435 }
1436
1437 fd = alloc_fd(hand, wxflag);
1438
1439 TRACE(":fd (%d) handle (%p)\n",fd, hand);
1440 return fd;
1441 }
1442
1443 /*********************************************************************
1444 * _wsopen (MSVCRT.@)
1445 */
1446 int CDECL _wsopen( const wchar_t* path, int oflags, int shflags, ... )
1447 {
1448 const unsigned int len = strlenW(path);
1449 char *patha = calloc(len + 1,1);
1450 va_list ap;
1451 int pmode;
1452
1453 va_start(ap, shflags);
1454 pmode = va_arg(ap, int);
1455 va_end(ap);
1456
1457 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1458 {
1459 int retval = _sopen(patha,oflags,shflags,pmode);
1460 free(patha);
1461 return retval;
1462 }
1463 _dosmaperr(GetLastError());
1464 free(patha);
1465 return -1;
1466 }
1467
1468 /*********************************************************************
1469 * _open (MSVCRT.@)
1470 */
1471 int CDECL _open( const char *path, int flags, ... )
1472 {
1473 va_list ap;
1474
1475 if (flags & _O_CREAT)
1476 {
1477 int pmode;
1478 va_start(ap, flags);
1479 pmode = va_arg(ap, int);
1480 va_end(ap);
1481 return _sopen( path, flags, _SH_DENYNO, pmode );
1482 }
1483 else
1484 return _sopen( path, flags, _SH_DENYNO);
1485 }
1486
1487 /*********************************************************************
1488 * _wopen (MSVCRT.@)
1489 */
1490 int CDECL _wopen(const wchar_t *path,int flags,...)
1491 {
1492 const unsigned int len = strlenW(path);
1493 char *patha = calloc(len + 1,1);
1494 va_list ap;
1495 int pmode;
1496
1497 va_start(ap, flags);
1498 pmode = va_arg(ap, int);
1499 va_end(ap);
1500
1501 if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
1502 {
1503 int retval = _open(patha,flags,pmode);
1504 free(patha);
1505 return retval;
1506 }
1507 free(patha);
1508 _dosmaperr(GetLastError());
1509 return -1;
1510 }
1511
1512 /*********************************************************************
1513 * _creat (MSVCRT.@)
1514 */
1515 int CDECL _creat(const char *path, int flags)
1516 {
1517 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1518 return _open(path, usedFlags);
1519 }
1520
1521 /*********************************************************************
1522 * _wcreat (MSVCRT.@)
1523 */
1524 int CDECL _wcreat(const wchar_t *path, int flags)
1525 {
1526 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
1527 return _wopen(path, usedFlags);
1528 }
1529
1530 /*********************************************************************
1531 * _open_osfhandle (MSVCRT.@)
1532 */
1533 int CDECL _open_osfhandle(intptr_t handle, int oflags)
1534 {
1535 int fd;
1536
1537 /* _O_RDONLY (0) always matches, so set the read flag
1538 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1539 * file, so set the write flag. It also only sets _O_TEXT if it wants
1540 * text - it never sets _O_BINARY.
1541 */
1542 /* don't let split_oflags() decide the mode if no mode is passed */
1543 if (!(oflags & (_O_BINARY | _O_TEXT)))
1544 oflags |= _O_BINARY;
1545
1546 fd = alloc_fd((HANDLE)(LONG_PTR)handle, split_oflags(oflags));
1547 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
1548 return fd;
1549 }
1550
1551 /*********************************************************************
1552 * _rmtmp (MSVCRT.@)
1553 */
1554 int CDECL _rmtmp(void)
1555 {
1556 int num_removed = 0, i;
1557
1558 LOCK_FILES();
1559 for (i = 3; i < stream_idx; i++)
1560 if (fstreams[i] && fstreams[i]->_tmpfname)
1561 {
1562 fclose(fstreams[i]);
1563 num_removed++;
1564 }
1565 UNLOCK_FILES();
1566
1567 if (num_removed)
1568 TRACE(":removed (%d) temp files\n",num_removed);
1569 return num_removed;
1570 }
1571
1572 /*********************************************************************
1573 * (internal) read_i
1574 */
1575 static int read_i(int fd, void *buf, unsigned int count)
1576 {
1577 DWORD num_read;
1578 char *bufstart = buf;
1579 HANDLE hand = fdtoh(fd);
1580
1581 if (fdesc[fd].wxflag & WX_READEOF) {
1582 fdesc[fd].wxflag |= WX_ATEOF;
1583 TRACE("already at EOF, returning 0\n");
1584 return 0;
1585 }
1586 /* Don't trace small reads, it gets *very* annoying */
1587 if (count > 4)
1588 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1589 if (hand == INVALID_HANDLE_VALUE)
1590 return -1;
1591
1592 /* Reading single bytes in O_TEXT mode makes things slow
1593 * So read big chunks
1594 */
1595 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1596 {
1597 if (fdesc[fd].wxflag & WX_TEXT)
1598 {
1599 DWORD i, j;
1600 for (i=0, j=0; i<num_read; i++)
1601 {
1602 /* in text mode, a ctrl-z signals EOF */
1603 if (bufstart[i] == 0x1a)
1604 {
1605 fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1606 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1607 break;
1608 }
1609 /* in text mode, strip \r if followed by \n.
1610 * BUG: should save state across calls somehow, so CR LF that
1611 * straddles buffer boundary gets recognized properly?
1612 */
1613 if ((bufstart[i] != '\r')
1614 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1615 bufstart[j++] = bufstart[i];
1616 }
1617 num_read = j;
1618 }
1619 if (count != 0 && num_read == 0)
1620 {
1621 fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1622 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1623 }
1624 }
1625 else
1626 {
1627 if (GetLastError() == ERROR_BROKEN_PIPE)
1628 {
1629 TRACE(":end-of-pipe\n");
1630 fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1631 return 0;
1632 }
1633 else
1634 {
1635 TRACE(":failed-last error (%d)\n",GetLastError());
1636 return -1;
1637 }
1638 }
1639
1640 if (count > 4)
1641 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1642 return num_read;
1643 }
1644
1645 /*********************************************************************
1646 * _read (MSVCRT.@)
1647 */
1648 int CDECL _read(int fd, void *buf, unsigned int count)
1649 {
1650 int num_read;
1651 num_read = read_i(fd, buf, count);
1652 return num_read;
1653 }
1654
1655 /*********************************************************************
1656 * _setmode (MSVCRT.@)
1657 */
1658 int CDECL _setmode(int fd,int mode)
1659 {
1660 int ret = fdesc[fd].wxflag & WX_TEXT ? _O_TEXT : _O_BINARY;
1661 if (mode & (~(_O_TEXT|_O_BINARY)))
1662 FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
1663 if ((mode & _O_TEXT) == _O_TEXT)
1664 fdesc[fd].wxflag |= WX_TEXT;
1665 else
1666 fdesc[fd].wxflag &= ~WX_TEXT;
1667 return ret;
1668 }
1669
1670 /*********************************************************************
1671 * _tell (MSVCRT.@)
1672 */
1673 long CDECL _tell(int fd)
1674 {
1675 return _lseek(fd, 0, SEEK_CUR);
1676 }
1677
1678 /*********************************************************************
1679 * _telli64 (MSVCRT.@)
1680 */
1681 __int64 CDECL _telli64(int fd)
1682 {
1683 return _lseeki64(fd, 0, SEEK_CUR);
1684 }
1685
1686 /*********************************************************************
1687 * _tempnam (MSVCRT.@)
1688 */
1689 char * CDECL _tempnam(const char *dir, const char *prefix)
1690 {
1691 char tmpbuf[MAX_PATH];
1692 const char *tmp_dir = getenv("TMP");
1693
1694 if (tmp_dir) dir = tmp_dir;
1695
1696 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
1697 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
1698 {
1699 TRACE("got name (%s)\n",tmpbuf);
1700 DeleteFileA(tmpbuf);
1701 return _strdup(tmpbuf);
1702 }
1703 TRACE("failed (%d)\n",GetLastError());
1704 return NULL;
1705 }
1706
1707 /*********************************************************************
1708 * _wtempnam (MSVCRT.@)
1709 */
1710 wchar_t * CDECL _wtempnam(const wchar_t *dir, const wchar_t *prefix)
1711 {
1712 wchar_t tmpbuf[MAX_PATH];
1713
1714 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
1715 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
1716 {
1717 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
1718 DeleteFileW(tmpbuf);
1719 return _wcsdup(tmpbuf);
1720 }
1721 TRACE("failed (%d)\n",GetLastError());
1722 return NULL;
1723 }
1724
1725 /*********************************************************************
1726 * _umask (MSVCRT.@)
1727 */
1728 int CDECL _umask(int umask)
1729 {
1730 int old_umask = umask;
1731 TRACE("(%d)\n",umask);
1732 MSVCRT_umask = umask;
1733 return old_umask;
1734 }
1735
1736 /*********************************************************************
1737 * _utime (MSVCRT.@)
1738 */
1739 int CDECL _utime(const char* path, struct _utimbuf *t)
1740 {
1741 int fd = _open(path, _O_WRONLY | _O_BINARY);
1742
1743 if (fd > 0)
1744 {
1745 int retVal = _futime(fd, t);
1746 _close(fd);
1747 return retVal;
1748 }
1749 return -1;
1750 }
1751
1752 /*********************************************************************
1753 * _wutime (MSVCRT.@)
1754 */
1755 int CDECL _wutime(const wchar_t* path, struct _utimbuf *t)
1756 {
1757 int fd = _wopen(path, _O_WRONLY | _O_BINARY);
1758
1759 if (fd > 0)
1760 {
1761 int retVal = _futime(fd, t);
1762 _close(fd);
1763 return retVal;
1764 }
1765 return -1;
1766 }
1767
1768 /*********************************************************************
1769 * _write (MSVCRT.@)
1770 */
1771 int CDECL _write(int fd, const void* buf, unsigned int count)
1772 {
1773 DWORD num_written;
1774 HANDLE hand = fdtoh(fd);
1775
1776 /* Don't trace small writes, it gets *very* annoying */
1777 #if 0
1778 if (count > 32)
1779 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
1780 #endif
1781 if (hand == INVALID_HANDLE_VALUE)
1782 {
1783 *_errno() = EBADF;
1784 return -1;
1785 }
1786
1787 /* If appending, go to EOF */
1788 if (fdesc[fd].wxflag & WX_APPEND)
1789 _lseek(fd, 0, FILE_END);
1790
1791 if (!(fdesc[fd].wxflag & WX_TEXT))
1792 {
1793 if (WriteFile(hand, buf, count, &num_written, NULL)
1794 && (num_written == count))
1795 return num_written;
1796 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
1797 hand, GetLastError());
1798 *_errno() = ENOSPC;
1799 }
1800 else
1801 {
1802 unsigned int i, j, nr_lf;
1803 char *p = NULL;
1804 const char *q;
1805 const char *s = (const char *)buf, *buf_start = (const char *)buf;
1806 /* find number of \n ( without preceding \r ) */
1807 for ( nr_lf=0,i = 0; i <count; i++)
1808 {
1809 if (s[i]== '\n')
1810 {
1811 nr_lf++;
1812 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1813 }
1814 }
1815 if (nr_lf)
1816 {
1817 if ((q = p = malloc(count + nr_lf)))
1818 {
1819 for (s = (const char *)buf, i = 0, j = 0; i < count; i++)
1820 {
1821 if (s[i]== '\n')
1822 {
1823 p[j++] = '\r';
1824 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1825 }
1826 p[j++] = s[i];
1827 }
1828 }
1829 else
1830 {
1831 FIXME("Malloc failed\n");
1832 nr_lf =0;
1833 q = buf;
1834 }
1835 }
1836 else
1837 q = buf;
1838
1839 if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
1840 {
1841 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
1842 fd, hand, GetLastError(), num_written);
1843 *_errno() = ENOSPC;
1844 if(nr_lf)
1845 free(p);
1846 return s - buf_start;
1847 }
1848 else
1849 {
1850 if(nr_lf)
1851 free(p);
1852 return count;
1853 }
1854 }
1855 return -1;
1856 }
1857
1858 /*********************************************************************
1859 * _putw (MSVCRT.@)
1860 */
1861 int CDECL _putw(int val, FILE* file)
1862 {
1863 int len;
1864 len = _write(file->_file, &val, sizeof(val));
1865 if (len == sizeof(val)) return val;
1866 file->_flag |= _IOERR;
1867 return EOF;
1868 }
1869
1870 /*********************************************************************
1871 * fclose (MSVCRT.@)
1872 */
1873 int CDECL fclose(FILE* file)
1874 {
1875 int r, flag;
1876
1877 flag = file->_flag;
1878 free(file->_tmpfname);
1879 file->_tmpfname = NULL;
1880 /* flush stdio buffers */
1881 if(file->_flag & _IOWRT)
1882 fflush(file);
1883 if(file->_flag & _IOMYBUF)
1884 free(file->_base);
1885
1886 r=_close(file->_file);
1887
1888 file->_flag = 0;
1889
1890 return ((r == -1) || (flag & _IOERR) ? EOF : 0);
1891 }
1892
1893 /*********************************************************************
1894 * feof (MSVCRT.@)
1895 */
1896 int CDECL feof(FILE* file)
1897 {
1898 return file->_flag & _IOEOF;
1899 }
1900
1901 /*********************************************************************
1902 * ferror (MSVCRT.@)
1903 */
1904 int CDECL ferror(FILE* file)
1905 {
1906 return file->_flag & _IOERR;
1907 }
1908
1909 /*********************************************************************
1910 * _filbuf (MSVCRT.@)
1911 */
1912 int CDECL _filbuf(FILE* file)
1913 {
1914 /* Allocate buffer if needed */
1915 if(file->_bufsiz == 0 && !(file->_flag & _IONBF) ) {
1916 alloc_buffer(file);
1917 }
1918 if(!(file->_flag & _IOREAD)) {
1919 if(file->_flag & _IORW) {
1920 file->_flag |= _IOREAD;
1921 } else {
1922 return EOF;
1923 }
1924 }
1925 if(file->_flag & _IONBF) {
1926 unsigned char c;
1927 int r;
1928 if ((r = read_i(file->_file,&c,1)) != 1) {
1929 file->_flag |= (r == 0) ? _IOEOF : _IOERR;
1930 return EOF;
1931 }
1932 return c;
1933 } else {
1934 file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
1935 if(file->_cnt<=0) {
1936 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
1937 file->_cnt = 0;
1938 return EOF;
1939 }
1940 file->_cnt--;
1941 file->_ptr = file->_base+1;
1942 return *(unsigned char *)file->_base;
1943 }
1944 }
1945
1946 /*********************************************************************
1947 * fgetc (MSVCRT.@)
1948 */
1949 int CDECL fgetc(FILE* file)
1950 {
1951 unsigned char *i;
1952 unsigned int j;
1953 if (file->_cnt>0) {
1954 file->_cnt--;
1955 i = (unsigned char *)file->_ptr++;
1956 j = *i;
1957 } else
1958 j = _filbuf(file);
1959 return j;
1960 }
1961
1962 /*********************************************************************
1963 * _fgetchar (MSVCRT.@)
1964 */
1965 int CDECL _fgetchar(void)
1966 {
1967 return fgetc(stdin);
1968 }
1969
1970 /*********************************************************************
1971 * fgets (MSVCRT.@)
1972 */
1973 char * CDECL fgets(char *s, int size, FILE* file)
1974 {
1975 int cc = EOF;
1976 char * buf_start = s;
1977
1978 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1979 file,file->_file,s,size);
1980
1981 while ((size >1) && (cc = fgetc(file)) != EOF && cc != '\n')
1982 {
1983 *s++ = (char)cc;
1984 size --;
1985 }
1986 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
1987 {
1988 TRACE(":nothing read\n");
1989 return NULL;
1990 }
1991 if ((cc != EOF) && (size > 1))
1992 *s++ = cc;
1993 *s = '\0';
1994 TRACE(":got %s\n", debugstr_a(buf_start));
1995 return buf_start;
1996 }
1997
1998 /*********************************************************************
1999 * fgetwc (MSVCRT.@)
2000 *
2001 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2002 * the CR from CR/LF combinations
2003 */
2004 wint_t CDECL fgetwc(FILE* file)
2005 {
2006 char c;
2007
2008 if (!(fdesc[file->_file].wxflag & WX_TEXT))
2009 {
2010 wchar_t wc;
2011 int i,j;
2012 char *chp, *wcp;
2013 wcp = (char *)&wc;
2014 for(i=0; i<sizeof(wc); i++)
2015 {
2016 if (file->_cnt>0)
2017 {
2018 file->_cnt--;
2019 chp = file->_ptr++;
2020 wcp[i] = *chp;
2021 }
2022 else
2023 {
2024 j = _filbuf(file);
2025 if(file->_cnt<=0)
2026 {
2027 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
2028 file->_cnt = 0;
2029 return WEOF;
2030 }
2031 wcp[i] = j;
2032 }
2033 }
2034 return wc;
2035 }
2036
2037 c = fgetc(file);
2038 if ((*__p___mb_cur_max() > 1) && isleadbyte(c))
2039 {
2040 FIXME("Treat Multibyte characters\n");
2041 }
2042 if (c == EOF)
2043 return WEOF;
2044 else
2045 return (wint_t)c;
2046 }
2047
2048 /*********************************************************************
2049 * _getw (MSVCRT.@)
2050 */
2051 int CDECL _getw(FILE* file)
2052 {
2053 char *ch;
2054 int i, j, k;
2055 ch = (char *)&i;
2056 for (j=0; j<sizeof(int); j++) {
2057 k = fgetc(file);
2058 if (k == EOF) {
2059 file->_flag |= _IOEOF;
2060 return EOF;
2061 }
2062 ch[j] = k;
2063 }
2064 return i;
2065 }
2066
2067 /*********************************************************************
2068 * getwc (MSVCRT.@)
2069 */
2070 wint_t CDECL getwc(FILE* file)
2071 {
2072 return fgetwc(file);
2073 }
2074
2075 /*********************************************************************
2076 * _fgetwchar (MSVCRT.@)
2077 */
2078 wint_t CDECL _fgetwchar(void)
2079 {
2080 return fgetwc(stdin);
2081 }
2082
2083 /*********************************************************************
2084 * getwchar (MSVCRT.@)
2085 */
2086 wint_t CDECL getwchar(void)
2087 {
2088 return _fgetwchar();
2089 }
2090
2091 /*********************************************************************
2092 * fgetws (MSVCRT.@)
2093 */
2094 wchar_t * CDECL fgetws(wchar_t *s, int size, FILE* file)
2095 {
2096 int cc = WEOF;
2097 wchar_t * buf_start = s;
2098
2099 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2100 file,file->_file,s,size);
2101
2102 while ((size >1) && (cc = fgetwc(file)) != WEOF && cc != '\n')
2103 {
2104 *s++ = (char)cc;
2105 size --;
2106 }
2107 if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2108 {
2109 TRACE(":nothing read\n");
2110 return NULL;
2111 }
2112 if ((cc != WEOF) && (size > 1))
2113 *s++ = cc;
2114 *s = 0;
2115 TRACE(":got %s\n", debugstr_w(buf_start));
2116 return buf_start;
2117 }
2118
2119 /*********************************************************************
2120 * fwrite (MSVCRT.@)
2121 */
2122 size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
2123 {
2124 size_t wrcnt=size * nmemb;
2125 int written = 0;
2126 if (size == 0)
2127 return 0;
2128 if(file->_cnt) {
2129 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2130 memcpy(file->_ptr, ptr, pcnt);
2131 file->_cnt -= pcnt;
2132 file->_ptr += pcnt;
2133 written = pcnt;
2134 wrcnt -= pcnt;
2135 ptr = (const char*)ptr + pcnt;
2136 } else if(!(file->_flag & _IOWRT)) {
2137 if(file->_flag & _IORW) {
2138 file->_flag |= _IOWRT;
2139 } else
2140 return 0;
2141 }
2142 if(wrcnt) {
2143 /* Flush buffer */
2144 int res=flush_buffer(file);
2145 if(!res) {
2146 int pwritten = _write(file->_file, ptr, wrcnt);
2147 if (pwritten <= 0)
2148 {
2149 file->_flag |= _IOERR;
2150 pwritten=0;
2151 }
2152 written += pwritten;
2153 }
2154 }
2155 return written / size;
2156 }
2157
2158 /*********************************************************************
2159 * fputwc (MSVCRT.@)
2160 */
2161 wint_t CDECL fputwc(wint_t wc, FILE* file)
2162 {
2163 wchar_t mwc=wc;
2164 if (fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2165 return WEOF;
2166 return wc;
2167 }
2168
2169 /*********************************************************************
2170 * _fputwchar (MSVCRT.@)
2171 */
2172 wint_t CDECL _fputwchar(wint_t wc)
2173 {
2174 return fputwc(wc, stdout);
2175 }
2176
2177 /*********************************************************************
2178 * _fsopen (MSVCRT.@)
2179 */
2180 FILE * CDECL _fsopen(const char *path, const char *mode, int share)
2181 {
2182 FILE* file;
2183 int open_flags, stream_flags, fd;
2184
2185 TRACE("(%s,%s)\n",path,mode);
2186
2187 /* map mode string to open() flags. "man fopen" for possibilities. */
2188 if (get_flags(mode, &open_flags, &stream_flags) == -1)
2189 return NULL;
2190
2191 LOCK_FILES();
2192 fd = _sopen(path, open_flags, share, _S_IREAD | _S_IWRITE);
2193 if (fd < 0)
2194 file = NULL;
2195 else if ((file = alloc_fp()) && init_fp(file, fd, stream_flags)
2196 != -1)
2197 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2198 else if (file)
2199 {
2200 file->_flag = 0;
2201 file = NULL;
2202 }
2203
2204 TRACE(":got (%p)\n",file);
2205 if (fd >= 0 && !file)
2206 _close(fd);
2207 UNLOCK_FILES();
2208 return file;
2209 }
2210
2211 /*********************************************************************
2212 * _wfsopen (MSVCRT.@)
2213 */
2214 FILE * CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
2215 {
2216 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2217 char *patha = calloc(plen + 1, 1);
2218 char *modea = calloc(mlen + 1, 1);
2219
2220 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2221
2222 if (patha && modea &&
2223 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2224 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2225 {
2226 FILE *retval = _fsopen(patha,modea,share);
2227 free(patha);
2228 free(modea);
2229 return retval;
2230 }
2231 free(patha);
2232 free(modea);
2233 _dosmaperr(GetLastError());
2234 return NULL;
2235 }
2236
2237 /*********************************************************************
2238 * fopen (MSVCRT.@)
2239 */
2240 FILE * CDECL fopen(const char *path, const char *mode)
2241 {
2242 return _fsopen( path, mode, _SH_DENYNO );
2243 }
2244
2245 /*********************************************************************
2246 * _wfopen (MSVCRT.@)
2247 */
2248 FILE * CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
2249 {
2250 return _wfsopen( path, mode, _SH_DENYNO );
2251 }
2252
2253 /* fputc calls _flsbuf which calls fputc */
2254 int CDECL _flsbuf(int c, FILE* file);
2255
2256 /*********************************************************************
2257 * fputc (MSVCRT.@)
2258 */
2259 int CDECL fputc(int c, FILE* file)
2260 {
2261 if(file->_cnt>0) {
2262 *file->_ptr++=c;
2263 file->_cnt--;
2264 if (c == '\n')
2265 {
2266 int res = flush_buffer(file);
2267 return res ? res : c;
2268 }
2269 else
2270 return c & 0xff;
2271 } else {
2272 return _flsbuf(c, file);
2273 }
2274 }
2275
2276 /*********************************************************************
2277 * _flsbuf (MSVCRT.@)
2278 */
2279 int CDECL _flsbuf(int c, FILE* file)
2280 {
2281 /* Flush output buffer */
2282 if(file->_bufsiz == 0 && !(file->_flag & _IONBF)) {
2283 alloc_buffer(file);
2284 }
2285 if(!(file->_flag & _IOWRT)) {
2286 if(file->_flag & _IORW) {
2287 file->_flag |= _IOWRT;
2288 } else {
2289 return EOF;
2290 }
2291 }
2292 if(file->_bufsiz) {
2293 int res=flush_buffer(file);
2294 return res?res : fputc(c, file);
2295 } else {
2296 unsigned char cc=c;
2297 int len;
2298 /* set _cnt to 0 for unbuffered FILEs */
2299 file->_cnt = 0;
2300 len = _write(file->_file, &cc, 1);
2301 if (len == 1) return c & 0xff;
2302 file->_flag |= _IOERR;
2303 return EOF;
2304 }
2305 }
2306
2307 /*********************************************************************
2308 * _fputchar (MSVCRT.@)
2309 */
2310 int CDECL _fputchar(int c)
2311 {
2312 return fputc(c, stdout);
2313 }
2314
2315 /*********************************************************************
2316 * fread (MSVCRT.@)
2317 */
2318 size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
2319 { size_t rcnt=size * nmemb;
2320 size_t read=0;
2321 int pread=0;
2322
2323 if(!rcnt)
2324 return 0;
2325
2326 /* first buffered data */
2327 if(file->_cnt>0) {
2328 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2329 memcpy(ptr, file->_ptr, pcnt);
2330 file->_cnt -= pcnt;
2331 file->_ptr += pcnt;
2332 read += pcnt ;
2333 rcnt -= pcnt ;
2334 ptr = (char*)ptr + pcnt;
2335 } else if(!(file->_flag & _IOREAD )) {
2336 if(file->_flag & _IORW) {
2337 file->_flag |= _IOREAD;
2338 } else
2339 return 0;
2340 }
2341 while(rcnt>0)
2342 {
2343 int i;
2344 /* Fill the buffer on small reads.
2345 * TODO: Use a better buffering strategy.
2346 */
2347 if (!file->_cnt && size*nmemb <= BUFSIZ/2 && !(file->_flag & _IONBF)) {
2348 if (file->_bufsiz == 0) {
2349 alloc_buffer(file);
2350 }
2351 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2352 file->_ptr = file->_base;
2353 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2354 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2355 if (i > 0 && i < file->_cnt) {
2356 fdesc[file->_file].wxflag &= ~WX_ATEOF;
2357 file->_flag &= ~_IOEOF;
2358 }
2359 if (i > 0) {
2360 memcpy(ptr, file->_ptr, i);
2361 file->_cnt -= i;
2362 file->_ptr += i;
2363 }
2364 } else {
2365 i = _read(file->_file,ptr, rcnt);
2366 }
2367 pread += i;
2368 rcnt -= i;
2369 ptr = (char *)ptr+i;
2370 /* expose feof condition in the flags
2371 * MFC tests file->_flag for feof, and doesn't call feof())
2372 */
2373 if ( fdesc[file->_file].wxflag & WX_ATEOF)
2374 file->_flag |= _IOEOF;
2375 else if (i == -1)
2376 {
2377 file->_flag |= _IOERR;
2378 pread = 0;
2379 rcnt = 0;
2380 }
2381 if (i < 1) break;
2382 }
2383 read+=pread;
2384 return read / size;
2385 }
2386
2387 /*********************************************************************
2388 * freopen (MSVCRT.@)
2389 *
2390 */
2391 FILE* CDECL freopen(const char *path, const char *mode,FILE* file)
2392 {
2393 int open_flags, stream_flags, fd;
2394
2395 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2396
2397 LOCK_FILES();
2398 if (!file || ((fd = file->_file) < 0) || fd > fdend)
2399 file = NULL;
2400 else
2401 {
2402 fclose(file);
2403 /* map mode string to open() flags. "man fopen" for possibilities. */
2404 if (get_flags(mode, &open_flags, &stream_flags) == -1)
2405 file = NULL;
2406 else
2407 {
2408 fd = _open(path, open_flags, _S_IREAD | _S_IWRITE);
2409 if (fd < 0)
2410 file = NULL;
2411 else if (init_fp(file, fd, stream_flags) == -1)
2412 {
2413 file->_flag = 0;
2414 WARN(":failed-last error (%d)\n",GetLastError());
2415 _dosmaperr(GetLastError());
2416 file = NULL;
2417 }
2418 }
2419 }
2420 UNLOCK_FILES();
2421 return file;
2422 }
2423
2424 /*********************************************************************
2425 * wfreopen (MSVCRT.@)
2426 *
2427 */
2428 FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode,FILE* file)
2429 {
2430 int open_flags, stream_flags, fd;
2431
2432 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2433
2434 LOCK_FILES();
2435 if (!file || ((fd = file->_file) < 0) || fd > fdend)
2436 file = NULL;
2437 else
2438 {
2439 fclose(file);
2440 /* map mode string to open() flags. "man fopen" for possibilities. */
2441 if (get_flags((char*)mode, &open_flags, &stream_flags) == -1)
2442 file = NULL;
2443 else
2444 {
2445 fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE);
2446 if (fd < 0)
2447 file = NULL;
2448 else if (init_fp(file, fd, stream_flags) == -1)
2449 {
2450 file->_flag = 0;
2451 WARN(":failed-last error (%d)\n",GetLastError());
2452 _dosmaperr(GetLastError());
2453 file = NULL;
2454 }
2455 }
2456 }
2457 UNLOCK_FILES();
2458 return file;
2459 }
2460
2461 /*********************************************************************
2462 * fsetpos (MSVCRT.@)
2463 */
2464 int CDECL fsetpos(FILE* file, const fpos_t *pos)
2465 {
2466 /* Note that all this has been lifted 'as is' from fseek */
2467 if(file->_flag & _IOWRT)
2468 flush_buffer(file);
2469
2470 /* Discard buffered input */
2471 file->_cnt = 0;
2472 file->_ptr = file->_base;
2473
2474 /* Reset direction of i/o */
2475 if(file->_flag & _IORW) {
2476 file->_flag &= ~(_IOREAD|_IOWRT);
2477 }
2478
2479 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2480 }
2481
2482 /*********************************************************************
2483 * ftell (MSVCRT.@)
2484 */
2485 LONG CDECL ftell(FILE* file)
2486 {
2487 /* TODO: just call fgetpos and return lower half of result */
2488 int off=0;
2489 long pos;
2490 pos = _tell(file->_file);
2491 if(pos == -1) return -1;
2492 if(file->_bufsiz) {
2493 if( file->_flag & _IOWRT ) {
2494 off = file->_ptr - file->_base;
2495 } else {
2496 off = -file->_cnt;
2497 if (fdesc[file->_file].wxflag & WX_TEXT) {
2498 /* Black magic correction for CR removal */
2499 int i;
2500 for (i=0; i<file->_cnt; i++) {
2501 if (file->_ptr[i] == '\n')
2502 off--;
2503 }
2504 }
2505 }
2506 }
2507 return off + pos;
2508 }
2509
2510 /*********************************************************************
2511 * fgetpos (MSVCRT.@)
2512 */
2513 int CDECL fgetpos(FILE* file, fpos_t *pos)
2514 {
2515 int off=0;
2516 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2517 if(*pos == -1) return -1;
2518 if(file->_bufsiz) {
2519 if( file->_flag & _IOWRT ) {
2520 off = file->_ptr - file->_base;
2521 } else {
2522 off = -file->_cnt;
2523 if (fdesc[file->_file].wxflag & WX_TEXT) {
2524 /* Black magic correction for CR removal */
2525 int i;
2526 for (i=0; i<file->_cnt; i++) {
2527 if (file->_ptr[i] == '\n')
2528 off--;
2529 }
2530 }
2531 }
2532 }
2533 *pos += off;
2534 return 0;
2535 }
2536
2537 /*********************************************************************
2538 * fputs (MSVCRT.@)
2539 */
2540 int CDECL fputs(const char *s, FILE* file)
2541 {
2542 size_t i, len = strlen(s);
2543 if (!(fdesc[file->_file].wxflag & WX_TEXT))
2544 return fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
2545 for (i=0; i<len; i++)
2546 if (fputc(s[i], file) == EOF)
2547 return EOF;
2548 return 0;
2549 }
2550
2551 /*********************************************************************
2552 * fputws (MSVCRT.@)
2553 */
2554 int CDECL fputws(const wchar_t *s, FILE* file)
2555 {
2556 size_t i, len = strlenW(s);
2557 if (!(fdesc[file->_file].wxflag & WX_TEXT))
2558 return fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
2559 for (i=0; i<len; i++)
2560 {
2561 if ((s[i] == '\n') && (fputc('\r', file) == EOF))
2562 return WEOF;
2563 if (fputwc(s[i], file) == WEOF)
2564 return WEOF;
2565 }
2566 return 0;
2567 }
2568
2569 /*********************************************************************
2570 * getchar (MSVCRT.@)
2571 */
2572 int CDECL getchar(void)
2573 {
2574 return fgetc(stdin);
2575 }
2576
2577 /*********************************************************************
2578 * getc (MSVCRT.@)
2579 */
2580 int CDECL getc(FILE* file)
2581 {
2582 return fgetc(file);
2583 }
2584
2585 /*********************************************************************
2586 * gets (MSVCRT.@)
2587 */
2588 char * CDECL gets(char *buf)
2589 {
2590 int cc;
2591 char * buf_start = buf;
2592
2593 for(cc = fgetc(stdin); cc != EOF && cc != '\n';
2594 cc = fgetc(stdin))
2595 if(cc != '\r') *buf++ = (char)cc;
2596
2597 *buf = '\0';
2598
2599 TRACE("got '%s'\n", buf_start);
2600 return buf_start;
2601 }
2602
2603 /*********************************************************************
2604 * _getws (MSVCRT.@)
2605 */
2606 wchar_t* CDECL _getws(wchar_t* buf)
2607 {
2608 wint_t cc;
2609 wchar_t* ws = buf;
2610
2611 for (cc = fgetwc(stdin); cc != WEOF && cc != '\n';
2612 cc = fgetwc(stdin))
2613 {
2614 if (cc != '\r')
2615 *buf++ = (wchar_t)cc;
2616 }
2617 *buf = '\0';
2618
2619 TRACE("got %s\n", debugstr_w(ws));
2620 return ws;
2621 }
2622
2623 /*********************************************************************
2624 * putc (MSVCRT.@)
2625 */
2626 int CDECL putc(int c, FILE* file)
2627 {
2628 return fputc(c, file);
2629 }
2630
2631 /*********************************************************************
2632 * putchar (MSVCRT.@)
2633 */
2634 int CDECL putchar(int c)
2635 {
2636 return fputc(c, stdout);
2637 }
2638
2639 /*********************************************************************
2640 * puts (MSVCRT.@)
2641 */
2642 int CDECL puts(const char *s)
2643 {
2644 size_t len = strlen(s);
2645 if (fwrite(s,sizeof(*s),len,stdout) != len) return EOF;
2646 return fwrite("\n",1,1,stdout) == 1 ? 0 : EOF;
2647 }
2648
2649 /*********************************************************************
2650 * _putws (MSVCRT.@)
2651 */
2652 int CDECL _putws(const wchar_t *s)
2653 {
2654 static const wchar_t nl = '\n';
2655 size_t len = strlenW(s);
2656 if (fwrite(s,sizeof(*s),len,stdout) != len) return EOF;
2657 return fwrite(&nl,sizeof(nl),1,stdout) == 1 ? 0 : EOF;
2658 }
2659
2660 /*********************************************************************
2661 * remove (MSVCRT.@)
2662 */
2663 int CDECL remove(const char *path)
2664 {
2665 TRACE("(%s)\n",path);
2666 if (DeleteFileA(path))
2667 return 0;
2668 TRACE(":failed (%d)\n",GetLastError());
2669 _dosmaperr(GetLastError());
2670 return -1;
2671 }
2672
2673 /*********************************************************************
2674 * _wremove (MSVCRT.@)
2675 */
2676 int CDECL _wremove(const wchar_t *path)
2677 {
2678 TRACE("(%s)\n",debugstr_w(path));
2679 if (DeleteFileW(path))
2680 return 0;
2681 TRACE(":failed (%d)\n",GetLastError());
2682 _dosmaperr(GetLastError());
2683 return -1;
2684 }
2685
2686 /*********************************************************************
2687 * rename (MSVCRT.@)
2688 */
2689 int CDECL rename(const char *oldpath,const char *newpath)
2690 {
2691 TRACE(":from %s to %s\n",oldpath,newpath);
2692 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2693 return 0;
2694 TRACE(":failed (%d)\n",GetLastError());
2695 _dosmaperr(GetLastError());
2696 return -1;
2697 }
2698
2699 /*********************************************************************
2700 * _wrename (MSVCRT.@)
2701 */
2702 int CDECL _wrename(const wchar_t *oldpath,const wchar_t *newpath)
2703 {
2704 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2705 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2706 return 0;
2707 TRACE(":failed (%d)\n",GetLastError());
2708 _dosmaperr(GetLastError());
2709 return -1;
2710 }
2711
2712 /*********************************************************************
2713 * setvbuf (MSVCRT.@)
2714 */
2715 int CDECL setvbuf(FILE* file, char *buf, int mode, size_t size)
2716 {
2717 /* TODO: Check if file busy */
2718 if(file->_bufsiz) {
2719 free(file->_base);
2720 file->_bufsiz = 0;
2721 file->_cnt = 0;
2722 }
2723 if(mode == _IOFBF) {
2724 file->_flag &= ~_IONBF;
2725 file->_base = file->_ptr = buf;
2726 if(buf) {
2727 file->_bufsiz = size;
2728 }
2729 } else {
2730 file->_flag |= _IONBF;
2731 }
2732 return 0;
2733 }
2734
2735 /*********************************************************************
2736 * setbuf (MSVCRT.@)
2737 */
2738 void CDECL setbuf(FILE* file, char *buf)
2739 {
2740 setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
2741 }
2742
2743 /*********************************************************************
2744 * tmpnam (MSVCRT.@)
2745 */
2746 char * CDECL tmpnam(char *s)
2747 {
2748 static int unique;
2749 char tmpstr[16];
2750 char *p;
2751 int count;
2752 if (s == 0)
2753 s = tmpname;
2754 int_to_base32(GetCurrentProcessId(), tmpstr);
2755 p = s + sprintf(s, "\\s%s.", tmpstr);
2756 for (count = 0; count < TMP_MAX; count++)
2757 {
2758 int_to_base32(unique++, tmpstr);
2759 strcpy(p, tmpstr);
2760 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2761 GetLastError() == ERROR_FILE_NOT_FOUND)
2762 break;
2763 }
2764 return s;
2765 }
2766
2767 /*********************************************************************
2768 * wtmpnam (MSVCRT.@)
2769 */
2770 wchar_t * CDECL _wtmpnam(wchar_t *s)
2771 {
2772 ERR("UNIMPLEMENTED!\n");
2773 return NULL;
2774 }
2775
2776 /*********************************************************************
2777 * tmpfile (MSVCRT.@)
2778 */
2779 FILE* CDECL tmpfile(void)
2780 {
2781 char *filename = tmpnam(NULL);
2782 int fd;
2783 FILE* file = NULL;
2784
2785 LOCK_FILES();
2786 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2787 if (fd != -1 && (file = alloc_fp()))
2788 {
2789 if (init_fp(file, fd, _O_RDWR) == -1)
2790 {
2791 file->_flag = 0;
2792 file = NULL;
2793 }
2794 else file->_tmpfname = _strdup(filename);
2795 }
2796 UNLOCK_FILES();
2797 return file;
2798 }
2799
2800 /*********************************************************************
2801 * vfprintf (MSVCRT.@)
2802 */
2803 int CDECL vfprintf(FILE* file, const char *format, va_list valist)
2804 {
2805 char buf[2048], *mem = buf;
2806 int written, resize = sizeof(buf), retval;
2807 /* There are two conventions for vsnprintf failing:
2808 * Return -1 if we truncated, or
2809 * Return the number of bytes that would have been written
2810 * The code below handles both cases
2811 */
2812 while ((written = _vsnprintf(mem, resize, format, valist)) == -1 ||
2813 written > resize)
2814 {
2815 resize = (written == -1 ? resize * 2 : written + 1);
2816 if (mem != buf)
2817 free (mem);
2818 if (!(mem = malloc(resize)))
2819 return EOF;
2820 }
2821 retval = fwrite(mem, sizeof(*mem), written, file);
2822 if (mem != buf)
2823 free (mem);
2824 return retval;
2825 }
2826
2827 /*********************************************************************
2828 * vfwprintf (MSVCRT.@)
2829 * FIXME:
2830 * Is final char included in written (then resize is too big) or not
2831 * (then we must test for equality too)?
2832 */
2833 int CDECL vfwprintf(FILE* file, const wchar_t *format, va_list valist)
2834 {
2835 wchar_t buf[2048], *mem = buf;
2836 int written, resize = sizeof(buf) / sizeof(wchar_t), retval;
2837 /* See vfprintf comments */
2838 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2839 written > resize)
2840 {
2841 resize = (written == -1 ? resize * 2 : written + sizeof(wchar_t));
2842 if (mem != buf)
2843 free (mem);
2844 if (!(mem = malloc(resize*sizeof(*mem))))
2845 return EOF;
2846 }
2847
2848 /* Check if outputting to a text-file */
2849 if (fdesc[file->_file].wxflag & WX_TEXT)
2850 {
2851 /* Convert each character and stop at the first invalid character. Behavior verified by tests under WinXP SP2 */
2852 char chMultiByte[MB_LEN_MAX];
2853 int nReturn;
2854 wchar_t *p;
2855
2856 retval = 0;
2857
2858 for (p = mem; *p; p++)
2859 {
2860 nReturn = wctomb(chMultiByte, *p);
2861
2862 if(nReturn == -1)
2863 break;
2864
2865 retval += fwrite(chMultiByte, 1, nReturn, file);
2866 }
2867 }
2868 else
2869 {
2870 retval = fwrite(mem, sizeof(*mem), written, file);
2871 }
2872
2873 if (mem != buf)
2874 free (mem);
2875
2876 return retval;
2877 }
2878
2879 /*********************************************************************
2880 * vprintf (MSVCRT.@)
2881 */
2882 int CDECL vprintf(const char *format, va_list valist)
2883 {
2884 return vfprintf(stdout,format,valist);
2885 }
2886
2887 /*********************************************************************
2888 * vwprintf (MSVCRT.@)
2889 */
2890 int CDECL vwprintf(const wchar_t *format, va_list valist)
2891 {
2892 return vfwprintf(stdout,format,valist);
2893 }
2894
2895 /*********************************************************************
2896 * fprintf (MSVCRT.@)
2897 */
2898 int CDECL fprintf(FILE* file, const char *format, ...)
2899 {
2900 va_list valist;
2901 int res;
2902 va_start(valist, format);
2903 res = vfprintf(file, format, valist);
2904 va_end(valist);
2905 return res;
2906 }
2907
2908 /*********************************************************************
2909 * fwprintf (MSVCRT.@)
2910 */
2911 int CDECL fwprintf(FILE* file, const wchar_t *format, ...)
2912 {
2913 va_list valist;
2914 int res;
2915 va_start(valist, format);
2916 res = vfwprintf(file, format, valist);
2917 va_end(valist);
2918 return res;
2919 }
2920
2921 /*********************************************************************
2922 * printf (MSVCRT.@)
2923 */
2924 int CDECL printf(const char *format, ...)
2925 {
2926 va_list valist;
2927 int res;
2928 va_start(valist, format);
2929 res = vfprintf(stdout, format, valist);
2930 va_end(valist);
2931 return res;
2932 }
2933
2934 /*********************************************************************
2935 * ungetc (MSVCRT.@)
2936 */
2937 int CDECL ungetc(int c, FILE * file)
2938 {
2939 if (c == EOF)
2940 return EOF;
2941 if(file->_bufsiz == 0 && !(file->_flag & _IONBF)) {
2942 alloc_buffer(file);
2943 file->_ptr++;
2944 }
2945 if(file->_ptr>file->_base) {
2946 file->_ptr--;
2947 *file->_ptr=c;
2948 file->_cnt++;
2949 clearerr(file);
2950 return c;
2951 }
2952 return EOF;
2953 }
2954
2955 /*********************************************************************
2956 * ungetwc (MSVCRT.@)
2957 */
2958 wint_t CDECL ungetwc(wint_t wc, FILE * file)
2959 {
2960 wchar_t mwc = wc;
2961 char * pp = (char *)&mwc;
2962 int i;
2963 for(i=sizeof(wchar_t)-1;i>=0;i--) {
2964 if(pp[i] != ungetc(pp[i],file))
2965 return WEOF;
2966 }
2967 return mwc;
2968 }
2969
2970 /*********************************************************************
2971 * wprintf (MSVCRT.@)
2972 */
2973 int CDECL wprintf(const wchar_t *format, ...)
2974 {
2975 va_list valist;
2976 int res;
2977 va_start(valist, format);
2978 res = vwprintf(format, valist);
2979 va_end(valist);
2980 return res;
2981 }
2982
2983 /*********************************************************************
2984 * _getmaxstdio (MSVCRT.@)
2985 */
2986 int CDECL _getmaxstdio(void)
2987 {
2988 FIXME("stub, always returns 512\n");
2989 return 512;
2990 }
2991
2992 /*********************************************************************
2993 * _setmaxstdio_ (MSVCRT.@)
2994 */
2995 int CDECL _setmaxstdio(int newmax)
2996 {
2997 int res;
2998 if( newmax > 2048)
2999 res = -1;
3000 else
3001 res = newmax;
3002 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3003 return res;
3004 }
3005
3006 /*********************************************************************
3007 * __pioinfo (MSVCRT.@)
3008 * FIXME: see MAX_FILES define.
3009 */
3010 ioinfo * __pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3011 &fdesc[0 * 64], &fdesc[1 * 64], &fdesc[2 * 64],
3012 &fdesc[3 * 64], &fdesc[4 * 64], &fdesc[5 * 64],
3013 &fdesc[6 * 64], &fdesc[7 * 64], &fdesc[8 * 64],
3014 &fdesc[9 * 64], &fdesc[10 * 64], &fdesc[11 * 64],
3015 &fdesc[12 * 64], &fdesc[13 * 64], &fdesc[14 * 64],
3016 &fdesc[15 * 64], &fdesc[16 * 64], &fdesc[17 * 64],
3017 &fdesc[18 * 64], &fdesc[19 * 64], &fdesc[20 * 64],
3018 &fdesc[21 * 64], &fdesc[22 * 64], &fdesc[23 * 64],
3019 &fdesc[24 * 64], &fdesc[25 * 64], &fdesc[26 * 64],
3020 &fdesc[27 * 64], &fdesc[28 * 64], &fdesc[29 * 64],
3021 &fdesc[30 * 64], &fdesc[31 * 64]
3022 } ;
3023
3024 /*********************************************************************
3025 * __badioinfo (MSVCRT.@)
3026 */
3027 ioinfo __badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT, { 0, } };