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