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