Sync with trunk (aka 'I want my virtualbox mouse integration too')
[reactos.git] / 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_READCR 0x08 /* underlying file is at \r */
80 #define WX_DONTINHERIT 0x10
81 #define WX_APPEND 0x20
82 #define WX_TEXT 0x80
83
84 /* FIXME: this should be allocated dynamically */
85 #define MAX_FILES 2048
86
87 /* ReactOS: Use _FDINFO instead! */
88 typedef struct {
89 HANDLE handle;
90 unsigned char wxflag;
91 DWORD unkn[7]; /* critical section and init flag */
92 } ioinfo;
93
94 ioinfo fdesc[MAX_FILES];
95
96 FILE _iob[3] = { { 0 } };
97
98 static int fdstart = 3; /* first unallocated fd */
99 static int fdend = 3; /* highest allocated fd */
100
101 static FILE* fstreams[2048];
102 static int stream_idx;
103
104 /* INTERNAL: process umask */
105 static int MSVCRT_umask = 0;
106
107 /* INTERNAL: Static buffer for temp file name */
108 static char tmpname[MAX_PATH];
109
110 /* This critical section protects the tables fdesc and fstreams,
111 * and their related indexes, fdstart, fdend,
112 * and stream_idx, from race conditions.
113 * It doesn't protect against race conditions manipulating the underlying files
114 * or flags; doing so would probably be better accomplished with per-file
115 * protection, rather than locking the whole table for every change.
116 */
117 static CRITICAL_SECTION FILE_cs;
118 #define LOCK_FILES() do { EnterCriticalSection(&FILE_cs); } while (0)
119 #define UNLOCK_FILES() do { LeaveCriticalSection(&FILE_cs); } while (0)
120
121 static inline BOOL is_valid_fd(int fd)
122 {
123 return fd >= 0 && fd < fdend && (fdesc[fd].wxflag & WX_OPEN);
124 }
125
126 /* INTERNAL: Get the HANDLE for a fd
127 * This doesn't lock the table, because a failure will result in
128 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
129 * it returns a valid handle which is about to be closed, a subsequent call
130 * will fail, most likely in a sane way.
131 */
132 HANDLE fdtoh(int fd)
133 {
134 if (!is_valid_fd(fd))
135 {
136 WARN(":fd (%d) - no handle!\n",fd);
137 *__doserrno() = 0;
138 *_errno() = EBADF;
139 return INVALID_HANDLE_VALUE;
140 }
141 //if (fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
142 return fdesc[fd].handle;
143 }
144
145 /* INTERNAL: free a file entry fd */
146 static void free_fd(int fd)
147 {
148 LOCK_FILES();
149 fdesc[fd].handle = INVALID_HANDLE_VALUE;
150 fdesc[fd].wxflag = 0;
151 TRACE(":fd (%d) freed\n",fd);
152 if (fd < 3) /* don't use 0,1,2 for user files */
153 {
154 switch (fd)
155 {
156 case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
157 case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
158 case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
159 }
160 }
161 else
162 {
163 if (fd == fdend - 1)
164 fdend--;
165 if (fd < fdstart)
166 fdstart = fd;
167 }
168 UNLOCK_FILES();
169 }
170
171 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
172 /* caller must hold the files lock */
173 static int alloc_fd_from(HANDLE hand, int flag, int fd)
174 {
175 if (fd >= MAX_FILES)
176 {
177 WARN(":files exhausted!\n");
178 return -1;
179 }
180 fdesc[fd].handle = hand;
181 fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
182
183 /* locate next free slot */
184 if (fd == fdstart && fd == fdend)
185 fdstart = fdend + 1;
186 else
187 while (fdstart < fdend &&
188 fdesc[fdstart].handle != INVALID_HANDLE_VALUE)
189 fdstart++;
190 /* update last fd in use */
191 if (fd >= fdend)
192 fdend = fd + 1;
193 TRACE("fdstart is %d, fdend is %d\n", fdstart, fdend);
194
195 switch (fd)
196 {
197 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
198 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
199 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
200 }
201
202 return fd;
203 }
204
205 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
206 /*static */int alloc_fd(HANDLE hand, int flag)
207 {
208 int ret;
209
210 LOCK_FILES();
211 TRACE(":handle (%p) allocating fd (%d)\n",hand,fdstart);
212 ret = alloc_fd_from(hand, flag, fdstart);
213 UNLOCK_FILES();
214 return ret;
215 }
216
217 /* INTERNAL: Allocate a FILE* for an fd slot */
218 /* caller must hold the files lock */
219 static FILE* alloc_fp(void)
220 {
221 unsigned int i;
222
223 for (i = 3; i < sizeof(fstreams) / sizeof(fstreams[0]); i++)
224 {
225 if (!fstreams[i] || fstreams[i]->_flag == 0)
226 {
227 if (!fstreams[i])
228 {
229 if (!(fstreams[i] = calloc(sizeof(FILE),1)))
230 return NULL;
231 if (i == stream_idx) stream_idx++;
232 }
233 return fstreams[i];
234 }
235 }
236 return NULL;
237 }
238
239 /* INTERNAL: initialize a FILE* from an open fd */
240 static int init_fp(FILE* file, int fd, unsigned stream_flags)
241 {
242 TRACE(":fd (%d) allocating FILE*\n",fd);
243 if (!is_valid_fd(fd))
244 {
245 WARN(":invalid fd %d\n",fd);
246 *__doserrno() = 0;
247 *_errno() = EBADF;
248 return -1;
249 }
250 memset(file, 0, sizeof(*file));
251 file->_file = fd;
252 file->_flag = stream_flags;
253
254 TRACE(":got FILE* (%p)\n",file);
255 return 0;
256 }
257
258 /* INTERNAL: Create an inheritance data block (for spawned process)
259 * The inheritance block is made of:
260 * 00 int nb of file descriptor (NBFD)
261 * 04 char file flags (wxflag): repeated for each fd
262 * 4+NBFD HANDLE file handle: repeated for each fd
263 */
264 unsigned create_io_inherit_block(WORD *size, BYTE **block)
265 {
266 int fd;
267 char* wxflag_ptr;
268 HANDLE* handle_ptr;
269
270 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * fdend;
271 *block = calloc(*size, 1);
272 if (!*block)
273 {
274 *size = 0;
275 return FALSE;
276 }
277 wxflag_ptr = (char*)*block + sizeof(unsigned);
278 handle_ptr = (HANDLE*)(wxflag_ptr + fdend * sizeof(char));
279
280 *(unsigned*)*block = fdend;
281 for (fd = 0; fd < fdend; fd++)
282 {
283 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
284 if ((fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
285 {
286 *wxflag_ptr = fdesc[fd].wxflag;
287 *handle_ptr = fdesc[fd].handle;
288 }
289 else
290 {
291 *wxflag_ptr = 0;
292 *handle_ptr = INVALID_HANDLE_VALUE;
293 }
294 wxflag_ptr++; handle_ptr++;
295 }
296 return TRUE;
297 }
298
299 /* INTERNAL: Set up all file descriptors,
300 * as well as default streams (stdin, stderr and stdout)
301 */
302 void msvcrt_init_io(void)
303 {
304 STARTUPINFOA si;
305 int i;
306
307 InitializeCriticalSection(&FILE_cs);
308 FILE_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FILE_cs");
309 GetStartupInfoA(&si);
310 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
311 {
312 BYTE* wxflag_ptr;
313 HANDLE* handle_ptr;
314 unsigned int count;
315
316 count = *(unsigned*)si.lpReserved2;
317 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
318 handle_ptr = (HANDLE*)(wxflag_ptr + count);
319
320 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
321 count = min(count, sizeof(fdesc) / sizeof(fdesc[0]));
322 for (i = 0; i < count; i++)
323 {
324 if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
325 {
326 fdesc[i].wxflag = *wxflag_ptr;
327 fdesc[i].handle = *handle_ptr;
328 }
329 else
330 {
331 fdesc[i].wxflag = 0;
332 fdesc[i].handle = INVALID_HANDLE_VALUE;
333 }
334 wxflag_ptr++; handle_ptr++;
335 }
336 fdend = max( 3, count );
337 for (fdstart = 3; fdstart < fdend; fdstart++)
338 if (fdesc[fdstart].handle == INVALID_HANDLE_VALUE) break;
339 }
340
341 if (!(fdesc[0].wxflag & WX_OPEN) || fdesc[0].handle == INVALID_HANDLE_VALUE)
342 {
343 #ifndef __REACTOS__
344 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
345 GetCurrentProcess(), &fdesc[0].handle, 0, TRUE,
346 DUPLICATE_SAME_ACCESS);
347 #else
348 fdesc[0].handle = GetStdHandle(STD_INPUT_HANDLE);
349 if (fdesc[0].handle == NULL)
350 fdesc[0].handle = INVALID_HANDLE_VALUE;
351 #endif
352 fdesc[0].wxflag = WX_OPEN | WX_TEXT;
353 }
354 if (!(fdesc[1].wxflag & WX_OPEN) || fdesc[1].handle == INVALID_HANDLE_VALUE)
355 {
356 #ifndef __REACTOS__
357 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
358 GetCurrentProcess(), &fdesc[1].handle, 0, TRUE,
359 DUPLICATE_SAME_ACCESS);
360 #else
361 fdesc[1].handle = GetStdHandle(STD_OUTPUT_HANDLE);
362 if (fdesc[1].handle == NULL)
363 fdesc[1].handle = INVALID_HANDLE_VALUE;
364 #endif
365 fdesc[1].wxflag = WX_OPEN | WX_TEXT;
366 }
367 if (!(fdesc[2].wxflag & WX_OPEN) || fdesc[2].handle == INVALID_HANDLE_VALUE)
368 {
369 #ifndef __REACTOS__
370 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
371 GetCurrentProcess(), &fdesc[2].handle, 0, TRUE,
372 DUPLICATE_SAME_ACCESS);
373 #else
374 fdesc[2].handle = GetStdHandle(STD_ERROR_HANDLE);
375 if (fdesc[2].handle == NULL)
376 fdesc[2].handle = INVALID_HANDLE_VALUE;
377 #endif
378 fdesc[2].wxflag = WX_OPEN | WX_TEXT;
379 }
380
381 TRACE(":handles (%p)(%p)(%p)\n",fdesc[0].handle,
382 fdesc[1].handle,fdesc[2].handle);
383
384 memset(_iob,0,3*sizeof(FILE));
385 for (i = 0; i < 3; i++)
386 {
387 /* FILE structs for stdin/out/err are static and never deleted */
388 fstreams[i] = &_iob[i];
389 _iob[i]._file = i;
390 _iob[i]._tmpfname = NULL;
391 _iob[i]._flag = (i == 0) ? _IOREAD : _IOWRT;
392 }
393 stream_idx = 3;
394 }
395
396 /* INTERNAL: Flush stdio file buffer */
397 static int flush_buffer(FILE* file)
398 {
399 if(file->_bufsiz) {
400 int cnt=file->_ptr-file->_base;
401 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
402 file->_flag |= _IOERR;
403 return EOF;
404 }
405 file->_ptr=file->_base;
406 file->_cnt=file->_bufsiz;
407 }
408 return 0;
409 }
410
411 /* INTERNAL: Allocate stdio file buffer */
412 static void alloc_buffer(FILE* file)
413 {
414 file->_base = calloc(BUFSIZ,1);
415 if(file->_base) {
416 file->_bufsiz = BUFSIZ;
417 file->_flag |= _IOMYBUF;
418 } else {
419 file->_base = (char*)(&file->_charbuf);
420 /* put here 2 ??? */
421 file->_bufsiz = sizeof(file->_charbuf);
422 }
423 file->_ptr = file->_base;
424 file->_cnt = 0;
425 }
426
427 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
428 static void int_to_base32(int num, char *str)
429 {
430 char *p;
431 int n = num;
432 int digits = 0;
433
434 while (n != 0)
435 {
436 n >>= 5;
437 digits++;
438 }
439 p = str + digits;
440 *p = 0;
441 while (--p >= str)
442 {
443 *p = (num & 31) + '0';
444 if (*p > '9')
445 *p += ('a' - '0' - 10);
446 num >>= 5;
447 }
448 }
449
450 /*********************************************************************
451 * __p__iob(MSVCRT.@)
452 */
453 FILE * CDECL __p__iob(void)
454 {
455 return _iob;
456 }
457
458 /*********************************************************************
459 * _access (MSVCRT.@)
460 */
461 int CDECL _access(const char *filename, int mode)
462 {
463 DWORD attr = GetFileAttributesA(filename);
464
465 TRACE("(%s,%d) %d\n",filename,mode,attr);
466
467 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
468 {
469 _dosmaperr(GetLastError());
470 return -1;
471 }
472 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
473 {
474 _dosmaperr(ERROR_ACCESS_DENIED);
475 return -1;
476 }
477 return 0;
478 }
479
480 /*********************************************************************
481 * _waccess (MSVCRT.@)
482 */
483 int CDECL _waccess(const wchar_t *filename, int mode)
484 {
485 DWORD attr = GetFileAttributesW(filename);
486
487 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
488
489 if (!filename || attr == INVALID_FILE_ATTRIBUTES)
490 {
491 _dosmaperr(GetLastError());
492 return -1;
493 }
494 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
495 {
496 _dosmaperr(ERROR_ACCESS_DENIED);
497 return -1;
498 }
499 return 0;
500 }
501
502 /*********************************************************************
503 * _chmod (MSVCRT.@)
504 */
505 int CDECL _chmod(const char *path, int flags)
506 {
507 DWORD oldFlags = GetFileAttributesA(path);
508
509 if (oldFlags != INVALID_FILE_ATTRIBUTES)
510 {
511 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
512 oldFlags | FILE_ATTRIBUTE_READONLY;
513
514 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
515 return 0;
516 }
517 _dosmaperr(GetLastError());
518 return -1;
519 }
520
521 /*********************************************************************
522 * _wchmod (MSVCRT.@)
523 */
524 int CDECL _wchmod(const wchar_t *path, int flags)
525 {
526 DWORD oldFlags = GetFileAttributesW(path);
527
528 if (oldFlags != INVALID_FILE_ATTRIBUTES)
529 {
530 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
531 oldFlags | FILE_ATTRIBUTE_READONLY;
532
533 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
534 return 0;
535 }
536 _dosmaperr(GetLastError());
537 return -1;
538 }
539
540 /*********************************************************************
541 * _unlink (MSVCRT.@)
542 */
543 int CDECL _unlink(const char *path)
544 {
545 TRACE("%s\n",debugstr_a(path));
546 if(DeleteFileA(path))
547 return 0;
548 TRACE("failed (%d)\n",GetLastError());
549 _dosmaperr(GetLastError());
550 return -1;
551 }
552
553 /*********************************************************************
554 * _wunlink (MSVCRT.@)
555 */
556 int CDECL _wunlink(const wchar_t *path)
557 {
558 TRACE("(%s)\n",debugstr_w(path));
559 if(DeleteFileW(path))
560 return 0;
561 TRACE("failed (%d)\n",GetLastError());
562 _dosmaperr(GetLastError());
563 return -1;
564 }
565
566 /* _flushall calls fflush which calls _flushall */
567 int CDECL fflush(FILE* file);
568
569 /*********************************************************************
570 * _flushall (MSVCRT.@)
571 */
572 int CDECL _flushall(void)
573 {
574 int i, num_flushed = 0;
575
576 LOCK_FILES();
577 for (i = 3; i < stream_idx; i++)
578 if (fstreams[i] && fstreams[i]->_flag)
579 {
580 #if 0
581 /* FIXME: flush, do not commit */
582 if (_commit(i) == -1)
583 if (fstreams[i])
584 fstreams[i]->_flag |= _IOERR;
585 #endif
586 if(fstreams[i]->_flag & _IOWRT) {
587 fflush(fstreams[i]);
588 num_flushed++;
589 }
590 }
591 UNLOCK_FILES();
592
593 TRACE(":flushed (%d) handles\n",num_flushed);
594 return num_flushed;
595 }
596
597 /*********************************************************************
598 * fflush (MSVCRT.@)
599 */
600 int CDECL fflush(FILE* file)
601 {
602 if(!file) {
603 _flushall();
604 } else if(file->_flag & _IOWRT) {
605 int res=flush_buffer(file);
606 return res;
607 }
608 return 0;
609 }
610
611 /*********************************************************************
612 * _close (MSVCRT.@)
613 */
614 int CDECL _close(int fd)
615 {
616 HANDLE hand;
617 int ret;
618
619 LOCK_FILES();
620 hand = fdtoh(fd);
621 TRACE(":fd (%d) handle (%p)\n",fd,hand);
622 if (hand == INVALID_HANDLE_VALUE)
623 ret = -1;
624 else if (!CloseHandle(hand))
625 {
626 WARN(":failed-last error (%d)\n",GetLastError());
627 _dosmaperr(GetLastError());
628 ret = -1;
629 }
630 else
631 {
632 free_fd(fd);
633 ret = 0;
634 }
635 UNLOCK_FILES();
636 TRACE(":ok\n");
637 return ret;
638 }
639
640 /*********************************************************************
641 * _commit (MSVCRT.@)
642 */
643 int CDECL _commit(int fd)
644 {
645 HANDLE hand = fdtoh(fd);
646
647 TRACE(":fd (%d) handle (%p)\n",fd,hand);
648 if (hand == INVALID_HANDLE_VALUE)
649 return -1;
650
651 if (!FlushFileBuffers(hand))
652 {
653 if (GetLastError() == ERROR_INVALID_HANDLE)
654 {
655 /* FlushFileBuffers fails for console handles
656 * so we ignore this error.
657 */
658 return 0;
659 }
660 TRACE(":failed-last error (%d)\n",GetLastError());
661 _dosmaperr(GetLastError());
662 return -1;
663 }
664 TRACE(":ok\n");
665 return 0;
666 }
667
668 /*********************************************************************
669 * _dup2 (MSVCRT.@)
670 * NOTES
671 * MSDN isn't clear on this point, but the remarks for _pipe
672 * indicate file descriptors duplicated with _dup and _dup2 are always
673 * inheritable.
674 */
675 int CDECL _dup2(int od, int nd)
676 {
677 int ret;
678
679 TRACE("(od=%d, nd=%d)\n", od, nd);
680 LOCK_FILES();
681 if (nd < MAX_FILES && is_valid_fd(od))
682 {
683 HANDLE handle;
684
685 if (DuplicateHandle(GetCurrentProcess(), fdesc[od].handle,
686 GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
687 {
688 int wxflag = fdesc[od].wxflag & ~_O_NOINHERIT;
689
690 if (is_valid_fd(nd))
691 _close(nd);
692 ret = alloc_fd_from(handle, wxflag, nd);
693 if (ret == -1)
694 {
695 CloseHandle(handle);
696 *_errno() = EMFILE;
697 }
698 else
699 {
700 /* _dup2 returns 0, not nd, on success */
701 ret = 0;
702 }
703 }
704 else
705 {
706 ret = -1;
707 _dosmaperr(GetLastError());
708 }
709 }
710 else
711 {
712 *_errno() = EBADF;
713 ret = -1;
714 }
715 UNLOCK_FILES();
716 return ret;
717 }
718
719 /*********************************************************************
720 * _dup (MSVCRT.@)
721 */
722 int CDECL _dup(int od)
723 {
724 int fd, ret;
725
726 LOCK_FILES();
727 fd = fdstart;
728 if (_dup2(od, fd) == 0)
729 ret = fd;
730 else
731 ret = -1;
732 UNLOCK_FILES();
733 return ret;
734 }
735
736 /*********************************************************************
737 * _eof (MSVCRT.@)
738 */
739 int CDECL _eof(int fd)
740 {
741 DWORD curpos,endpos;
742 LONG hcurpos,hendpos;
743 HANDLE hand = fdtoh(fd);
744
745 TRACE(":fd (%d) handle (%p)\n",fd,hand);
746
747 if (hand == INVALID_HANDLE_VALUE)
748 return -1;
749
750 if (fdesc[fd].wxflag & WX_ATEOF) return TRUE;
751
752 /* Otherwise we do it the hard way */
753 hcurpos = hendpos = 0;
754 curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
755 endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
756
757 if (curpos == endpos && hcurpos == hendpos)
758 {
759 /* FIXME: shouldn't WX_ATEOF be set here? */
760 return TRUE;
761 }
762
763 SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
764 return FALSE;
765 }
766
767 /*********************************************************************
768 * _fcloseall (MSVCRT.@)
769 */
770 int CDECL _fcloseall(void)
771 {
772 int num_closed = 0, i;
773
774 LOCK_FILES();
775 for (i = 3; i < stream_idx; i++)
776 if (fstreams[i] && fstreams[i]->_flag &&
777 !fclose(fstreams[i]))
778 num_closed++;
779 UNLOCK_FILES();
780
781 TRACE(":closed (%d) handles\n",num_closed);
782 return num_closed;
783 }
784
785 /* free everything on process exit */
786 void msvcrt_free_io(void)
787 {
788 _fcloseall();
789 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
790 * stdout, and stderr (unlike GNU), so we need to fclose() them here
791 * or they won't get flushed.
792 */
793 fclose(&_iob[0]);
794 fclose(&_iob[1]);
795 fclose(&_iob[2]);
796 FILE_cs.DebugInfo->Spare[0] = 0;
797 DeleteCriticalSection(&FILE_cs);
798 }
799
800 /*********************************************************************
801 * _lseeki64 (MSVCRT.@)
802 */
803 __int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
804 {
805 HANDLE hand = fdtoh(fd);
806 LARGE_INTEGER ofs, ret;
807
808 TRACE(":fd (%d) handle (%p)\n",fd,hand);
809 if (hand == INVALID_HANDLE_VALUE)
810 return -1;
811
812 if (whence < 0 || whence > 2)
813 {
814 *_errno() = EINVAL;
815 return -1;
816 }
817
818 TRACE(":fd (%d) to %s pos %s\n",
819 fd,wine_dbgstr_longlong(offset),
820 (whence==SEEK_SET)?"SEEK_SET":
821 (whence==SEEK_CUR)?"SEEK_CUR":
822 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
823
824 ofs.QuadPart = offset;
825 if (SetFilePointerEx(hand, ofs, &ret, whence))
826 {
827 fdesc[fd].wxflag &= ~(WX_ATEOF|WX_READEOF);
828 /* FIXME: What if we seek _to_ EOF - is EOF set? */
829
830 return ret.QuadPart;
831 }
832 TRACE(":error-last error (%d)\n",GetLastError());
833 _dosmaperr(GetLastError());
834 return -1;
835 }
836
837 /*********************************************************************
838 * _lseek (MSVCRT.@)
839 */
840 LONG CDECL _lseek(int fd, LONG offset, int whence)
841 {
842 return _lseeki64(fd, offset, whence);
843 }
844
845 /*********************************************************************
846 * _locking (MSVCRT.@)
847 *
848 * This is untested; the underlying LockFile doesn't work yet.
849 */
850 int CDECL _locking(int fd, int mode, LONG nbytes)
851 {
852 BOOL ret;
853 DWORD cur_locn;
854 HANDLE hand = fdtoh(fd);
855
856 TRACE(":fd (%d) handle (%p)\n",fd,hand);
857 if (hand == INVALID_HANDLE_VALUE)
858 return -1;
859
860 if (mode < 0 || mode > 4)
861 {
862 *_errno() = EINVAL;
863 return -1;
864 }
865
866 TRACE(":fd (%d) by 0x%08x mode %s\n",
867 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
868 (mode==_LK_LOCK)?"_LK_LOCK":
869 (mode==_LK_NBLCK)?"_LK_NBLCK":
870 (mode==_LK_RLCK)?"_LK_RLCK":
871 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
872 "UNKNOWN");
873
874 if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
875 {
876 FIXME ("Seek failed\n");
877 *_errno() = EINVAL; /* FIXME */
878 return -1;
879 }
880 if (mode == _LK_LOCK || mode == _LK_RLCK)
881 {
882 int nretry = 10;
883 ret = 1; /* just to satisfy gcc */
884 while (nretry--)
885 {
886 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
887 if (ret) break;
888 Sleep(1);
889 }
890 }
891 else if (mode == _LK_UNLCK)
892 ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
893 else
894 ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
895 /* FIXME - what about error settings? */
896 return ret ? 0 : -1;
897 }
898
899 /*********************************************************************
900 * fseek (MSVCRT.@)
901 */
902 int CDECL fseek(FILE* file, long offset, int whence)
903 {
904 /* Flush output if needed */
905 if(file->_flag & _IOWRT)
906 flush_buffer(file);
907
908 if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
909 offset -= file->_cnt;
910 if (fdesc[file->_file].wxflag & WX_TEXT) {
911 /* Black magic correction for CR removal */
912 int i;
913 for (i=0; i<file->_cnt; i++) {
914 if (file->_ptr[i] == '\n')
915 offset--;
916 }
917 /* Black magic when reading CR at buffer boundary*/
918 if(fdesc[file->_file].wxflag & WX_READCR)
919 offset--;
920 }
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) _dosmaperr(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 _dosmaperr(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 _dosmaperr(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 _dosmaperr(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 _dosmaperr(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 free(patha);
1507 _dosmaperr(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) read_i
1573 */
1574 static int read_i(int fd, void *buf, unsigned int count)
1575 {
1576 DWORD num_read;
1577 char *bufstart = buf;
1578 HANDLE hand = fdtoh(fd);
1579
1580 if (count == 0)
1581 return 0;
1582
1583 if (fdesc[fd].wxflag & WX_READEOF) {
1584 fdesc[fd].wxflag |= WX_ATEOF;
1585 TRACE("already at EOF, returning 0\n");
1586 return 0;
1587 }
1588 /* Don't trace small reads, it gets *very* annoying */
1589 if (count > 4)
1590 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
1591 if (hand == INVALID_HANDLE_VALUE)
1592 return -1;
1593
1594 /* Reading single bytes in O_TEXT mode makes things slow
1595 * So read big chunks
1596 */
1597 if (ReadFile(hand, bufstart, count, &num_read, NULL))
1598 {
1599 if (count != 0 && num_read == 0)
1600 {
1601 fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1602 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
1603 }
1604 else if (fdesc[fd].wxflag & WX_TEXT)
1605 {
1606 DWORD i, j;
1607 if (bufstart[num_read-1] == '\r')
1608 {
1609 if(count == 1)
1610 {
1611 fdesc[fd].wxflag &= ~WX_READCR;
1612 ReadFile(hand, bufstart, 1, &num_read, NULL);
1613 }
1614 else
1615 {
1616 fdesc[fd].wxflag |= WX_READCR;
1617 num_read--;
1618 }
1619 }
1620 else
1621 fdesc[fd].wxflag &= ~WX_READCR;
1622 for (i=0, j=0; i<num_read; i++)
1623 {
1624 /* in text mode, a ctrl-z signals EOF */
1625 if (bufstart[i] == 0x1a)
1626 {
1627 fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1628 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
1629 break;
1630 }
1631 /* in text mode, strip \r if followed by \n.
1632 * BUG: should save state across calls somehow, so CR LF that
1633 * straddles buffer boundary gets recognized properly?
1634 */
1635 if ((bufstart[i] != '\r')
1636 || ((i+1) < num_read && bufstart[i+1] != '\n'))
1637 bufstart[j++] = bufstart[i];
1638 }
1639 num_read = j;
1640 }
1641 }
1642 else
1643 {
1644 if (GetLastError() == ERROR_BROKEN_PIPE)
1645 {
1646 TRACE(":end-of-pipe\n");
1647 fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
1648 return 0;
1649 }
1650 else
1651 {
1652 TRACE(":failed-last error (%d)\n",GetLastError());
1653 return -1;
1654 }
1655 }
1656
1657 if (count > 4)
1658 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
1659 return num_read;
1660 }
1661
1662 /*********************************************************************
1663 * _read (MSVCRT.@)
1664 */
1665 int CDECL _read(int fd, void *buf, unsigned int count)
1666 {
1667 int num_read;
1668 num_read = read_i(fd, buf, count);
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 if (file->_cnt>0) {
1971 file->_cnt--;
1972 i = (unsigned char *)file->_ptr++;
1973 j = *i;
1974 } else
1975 j = _filbuf(file);
1976 return j;
1977 }
1978
1979 /*********************************************************************
1980 * _fgetchar (MSVCRT.@)
1981 */
1982 int CDECL _fgetchar(void)
1983 {
1984 return fgetc(stdin);
1985 }
1986
1987 /*********************************************************************
1988 * fgets (MSVCRT.@)
1989 */
1990 char * CDECL fgets(char *s, int size, FILE* file)
1991 {
1992 int cc = EOF;
1993 char * buf_start = s;
1994
1995 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1996 file,file->_file,s,size);
1997
1998 while ((size >1) && (cc = fgetc(file)) != EOF && cc != '\n')
1999 {
2000 *s++ = (char)cc;
2001 size --;
2002 }
2003 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
2004 {
2005 TRACE(":nothing read\n");
2006 return NULL;
2007 }
2008 if ((cc != EOF) && (size > 1))
2009 *s++ = cc;
2010 *s = '\0';
2011 TRACE(":got %s\n", debugstr_a(buf_start));
2012 return buf_start;
2013 }
2014
2015 /*********************************************************************
2016 * fgetwc (MSVCRT.@)
2017 *
2018 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2019 * the CR from CR/LF combinations
2020 */
2021 wint_t CDECL fgetwc(FILE* file)
2022 {
2023 char c;
2024
2025 if (!(fdesc[file->_file].wxflag & WX_TEXT))
2026 {
2027 wchar_t wc;
2028 int i,j;
2029 char *chp, *wcp;
2030 wcp = (char *)&wc;
2031 for(i=0; i<sizeof(wc); i++)
2032 {
2033 if (file->_cnt>0)
2034 {
2035 file->_cnt--;
2036 chp = file->_ptr++;
2037 wcp[i] = *chp;
2038 }
2039 else
2040 {
2041 j = _filbuf(file);
2042 if(file->_cnt<=0)
2043 {
2044 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
2045 file->_cnt = 0;
2046 return WEOF;
2047 }
2048 wcp[i] = j;
2049 }
2050 }
2051 return wc;
2052 }
2053
2054 c = fgetc(file);
2055 if ((*__p___mb_cur_max() > 1) && isleadbyte(c))
2056 {
2057 FIXME("Treat Multibyte characters\n");
2058 }
2059 if (c == EOF)
2060 return WEOF;
2061 else
2062 return (wint_t)c;
2063 }
2064
2065 /*********************************************************************
2066 * _getw (MSVCRT.@)
2067 */
2068 int CDECL _getw(FILE* file)
2069 {
2070 char *ch;
2071 int i, j, k;
2072 ch = (char *)&i;
2073 for (j=0; j<sizeof(int); j++) {
2074 k = fgetc(file);
2075 if (k == EOF) {
2076 file->_flag |= _IOEOF;
2077 return EOF;
2078 }
2079 ch[j] = k;
2080 }
2081 return i;
2082 }
2083
2084 /*********************************************************************
2085 * getwc (MSVCRT.@)
2086 */
2087 wint_t CDECL getwc(FILE* file)
2088 {
2089 return fgetwc(file);
2090 }
2091
2092 /*********************************************************************
2093 * _fgetwchar (MSVCRT.@)
2094 */
2095 wint_t CDECL _fgetwchar(void)
2096 {
2097 return fgetwc(stdin);
2098 }
2099
2100 /*********************************************************************
2101 * getwchar (MSVCRT.@)
2102 */
2103 wint_t CDECL getwchar(void)
2104 {
2105 return _fgetwchar();
2106 }
2107
2108 /*********************************************************************
2109 * fgetws (MSVCRT.@)
2110 */
2111 wchar_t * CDECL fgetws(wchar_t *s, int size, FILE* file)
2112 {
2113 int cc = WEOF;
2114 wchar_t * buf_start = s;
2115
2116 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2117 file,file->_file,s,size);
2118
2119 while ((size >1) && (cc = fgetwc(file)) != WEOF && cc != '\n')
2120 {
2121 *s++ = (char)cc;
2122 size --;
2123 }
2124 if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
2125 {
2126 TRACE(":nothing read\n");
2127 return NULL;
2128 }
2129 if ((cc != WEOF) && (size > 1))
2130 *s++ = cc;
2131 *s = 0;
2132 TRACE(":got %s\n", debugstr_w(buf_start));
2133 return buf_start;
2134 }
2135
2136 /*********************************************************************
2137 * fwrite (MSVCRT.@)
2138 */
2139 size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
2140 {
2141 size_t wrcnt=size * nmemb;
2142 int written = 0;
2143 if (size == 0)
2144 return 0;
2145 if(file->_cnt) {
2146 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
2147 memcpy(file->_ptr, ptr, pcnt);
2148 file->_cnt -= pcnt;
2149 file->_ptr += pcnt;
2150 written = pcnt;
2151 wrcnt -= pcnt;
2152 ptr = (const char*)ptr + pcnt;
2153 } else if(!(file->_flag & _IOWRT)) {
2154 if(file->_flag & _IORW) {
2155 file->_flag |= _IOWRT;
2156 } else
2157 return 0;
2158 }
2159 if(wrcnt) {
2160 /* Flush buffer */
2161 int res=flush_buffer(file);
2162 if(!res) {
2163 int pwritten = _write(file->_file, ptr, wrcnt);
2164 if (pwritten <= 0)
2165 {
2166 file->_flag |= _IOERR;
2167 pwritten=0;
2168 }
2169 written += pwritten;
2170 }
2171 }
2172 return written / size;
2173 }
2174
2175 /*********************************************************************
2176 * fputwc (MSVCRT.@)
2177 */
2178 wint_t CDECL fputwc(wint_t wc, FILE* file)
2179 {
2180 wchar_t mwc=wc;
2181 if (fwrite( &mwc, sizeof(mwc), 1, file) != 1)
2182 return WEOF;
2183 return wc;
2184 }
2185
2186 /*********************************************************************
2187 * _fputwchar (MSVCRT.@)
2188 */
2189 wint_t CDECL _fputwchar(wint_t wc)
2190 {
2191 return fputwc(wc, stdout);
2192 }
2193
2194 /*********************************************************************
2195 * _fsopen (MSVCRT.@)
2196 */
2197 FILE * CDECL _fsopen(const char *path, const char *mode, int share)
2198 {
2199 FILE* file;
2200 int open_flags, stream_flags, fd;
2201
2202 TRACE("(%s,%s)\n",path,mode);
2203
2204 /* map mode string to open() flags. "man fopen" for possibilities. */
2205 if (get_flags(mode, &open_flags, &stream_flags) == -1)
2206 return NULL;
2207
2208 LOCK_FILES();
2209 fd = _sopen(path, open_flags, share, _S_IREAD | _S_IWRITE);
2210 if (fd < 0)
2211 file = NULL;
2212 else if ((file = alloc_fp()) && init_fp(file, fd, stream_flags)
2213 != -1)
2214 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
2215 else if (file)
2216 {
2217 file->_flag = 0;
2218 file = NULL;
2219 }
2220
2221 TRACE(":got (%p)\n",file);
2222 if (fd >= 0 && !file)
2223 _close(fd);
2224 UNLOCK_FILES();
2225 return file;
2226 }
2227
2228 /*********************************************************************
2229 * _wfsopen (MSVCRT.@)
2230 */
2231 FILE * CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
2232 {
2233 const unsigned int plen = strlenW(path), mlen = strlenW(mode);
2234 char *patha = calloc(plen + 1, 1);
2235 char *modea = calloc(mlen + 1, 1);
2236
2237 TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
2238
2239 if (patha && modea &&
2240 WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
2241 WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
2242 {
2243 FILE *retval = _fsopen(patha,modea,share);
2244 free(patha);
2245 free(modea);
2246 return retval;
2247 }
2248 free(patha);
2249 free(modea);
2250 _dosmaperr(GetLastError());
2251 return NULL;
2252 }
2253
2254 /*********************************************************************
2255 * fopen (MSVCRT.@)
2256 */
2257 FILE * CDECL fopen(const char *path, const char *mode)
2258 {
2259 return _fsopen( path, mode, _SH_DENYNO );
2260 }
2261
2262 /*********************************************************************
2263 * _wfopen (MSVCRT.@)
2264 */
2265 FILE * CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
2266 {
2267 return _wfsopen( path, mode, _SH_DENYNO );
2268 }
2269
2270 /* fputc calls _flsbuf which calls fputc */
2271 int CDECL _flsbuf(int c, FILE* file);
2272
2273 /*********************************************************************
2274 * fputc (MSVCRT.@)
2275 */
2276 int CDECL fputc(int c, FILE* file)
2277 {
2278 if(file->_cnt>0) {
2279 *file->_ptr++=c;
2280 file->_cnt--;
2281 if (c == '\n')
2282 {
2283 int res = flush_buffer(file);
2284 return res ? res : c;
2285 }
2286 else
2287 return c & 0xff;
2288 } else {
2289 return _flsbuf(c, file);
2290 }
2291 }
2292
2293 /*********************************************************************
2294 * _flsbuf (MSVCRT.@)
2295 */
2296 int CDECL _flsbuf(int c, FILE* file)
2297 {
2298 /* Flush output buffer */
2299 if(file->_bufsiz == 0 && !(file->_flag & _IONBF)) {
2300 alloc_buffer(file);
2301 }
2302 if(!(file->_flag & _IOWRT)) {
2303 if(file->_flag & _IORW) {
2304 file->_flag |= _IOWRT;
2305 } else {
2306 return EOF;
2307 }
2308 }
2309 if(file->_bufsiz) {
2310 int res=flush_buffer(file);
2311 return res?res : fputc(c, file);
2312 } else {
2313 unsigned char cc=c;
2314 int len;
2315 /* set _cnt to 0 for unbuffered FILEs */
2316 file->_cnt = 0;
2317 len = _write(file->_file, &cc, 1);
2318 if (len == 1) return c & 0xff;
2319 file->_flag |= _IOERR;
2320 return EOF;
2321 }
2322 }
2323
2324 /*********************************************************************
2325 * _fputchar (MSVCRT.@)
2326 */
2327 int CDECL _fputchar(int c)
2328 {
2329 return fputc(c, stdout);
2330 }
2331
2332 /*********************************************************************
2333 * fread (MSVCRT.@)
2334 */
2335 size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
2336 { size_t rcnt=size * nmemb;
2337 size_t read=0;
2338 int pread=0;
2339
2340 if(!rcnt)
2341 return 0;
2342
2343 /* first buffered data */
2344 if(file->_cnt>0) {
2345 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
2346 memcpy(ptr, file->_ptr, pcnt);
2347 file->_cnt -= pcnt;
2348 file->_ptr += pcnt;
2349 read += pcnt ;
2350 rcnt -= pcnt ;
2351 ptr = (char*)ptr + pcnt;
2352 } else if(!(file->_flag & _IOREAD )) {
2353 if(file->_flag & _IORW) {
2354 file->_flag |= _IOREAD;
2355 } else
2356 return 0;
2357 }
2358 while(rcnt>0)
2359 {
2360 int i;
2361 /* Fill the buffer on small reads.
2362 * TODO: Use a better buffering strategy.
2363 */
2364 if (!file->_cnt && size*nmemb <= BUFSIZ/2 && !(file->_flag & _IONBF)) {
2365 if (file->_bufsiz == 0) {
2366 alloc_buffer(file);
2367 }
2368 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2369 file->_ptr = file->_base;
2370 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
2371 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2372 if (i > 0 && i < file->_cnt) {
2373 fdesc[file->_file].wxflag &= ~WX_ATEOF;
2374 file->_flag &= ~_IOEOF;
2375 }
2376 if (i > 0) {
2377 memcpy(ptr, file->_ptr, i);
2378 file->_cnt -= i;
2379 file->_ptr += i;
2380 }
2381 } else {
2382 i = _read(file->_file,ptr, rcnt);
2383 }
2384 pread += i;
2385 rcnt -= i;
2386 ptr = (char *)ptr+i;
2387 /* expose feof condition in the flags
2388 * MFC tests file->_flag for feof, and doesn't call feof())
2389 */
2390 if ( fdesc[file->_file].wxflag & WX_ATEOF)
2391 file->_flag |= _IOEOF;
2392 else if (i == -1)
2393 {
2394 file->_flag |= _IOERR;
2395 pread = 0;
2396 rcnt = 0;
2397 }
2398 if (i < 1) break;
2399 }
2400 read+=pread;
2401 return read / size;
2402 }
2403
2404 /*********************************************************************
2405 * freopen (MSVCRT.@)
2406 *
2407 */
2408 FILE* CDECL freopen(const char *path, const char *mode,FILE* file)
2409 {
2410 int open_flags, stream_flags, fd;
2411
2412 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
2413
2414 LOCK_FILES();
2415 if (!file || ((fd = file->_file) < 0) || fd > fdend)
2416 file = NULL;
2417 else
2418 {
2419 fclose(file);
2420 /* map mode string to open() flags. "man fopen" for possibilities. */
2421 if (get_flags(mode, &open_flags, &stream_flags) == -1)
2422 file = NULL;
2423 else
2424 {
2425 fd = _open(path, open_flags, _S_IREAD | _S_IWRITE);
2426 if (fd < 0)
2427 file = NULL;
2428 else if (init_fp(file, fd, stream_flags) == -1)
2429 {
2430 file->_flag = 0;
2431 WARN(":failed-last error (%d)\n",GetLastError());
2432 _dosmaperr(GetLastError());
2433 file = NULL;
2434 }
2435 }
2436 }
2437 UNLOCK_FILES();
2438 return file;
2439 }
2440
2441 /*********************************************************************
2442 * wfreopen (MSVCRT.@)
2443 *
2444 */
2445 FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode,FILE* file)
2446 {
2447 int open_flags, stream_flags, fd;
2448
2449 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file->_file);
2450
2451 LOCK_FILES();
2452 if (!file || ((fd = file->_file) < 0) || fd > fdend)
2453 file = NULL;
2454 else
2455 {
2456 fclose(file);
2457 /* map mode string to open() flags. "man fopen" for possibilities. */
2458 if (get_flags((char*)mode, &open_flags, &stream_flags) == -1)
2459 file = NULL;
2460 else
2461 {
2462 fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE);
2463 if (fd < 0)
2464 file = NULL;
2465 else if (init_fp(file, fd, stream_flags) == -1)
2466 {
2467 file->_flag = 0;
2468 WARN(":failed-last error (%d)\n",GetLastError());
2469 _dosmaperr(GetLastError());
2470 file = NULL;
2471 }
2472 }
2473 }
2474 UNLOCK_FILES();
2475 return file;
2476 }
2477
2478 /*********************************************************************
2479 * fsetpos (MSVCRT.@)
2480 */
2481 int CDECL fsetpos(FILE* file, const fpos_t *pos)
2482 {
2483 /* Note that all this has been lifted 'as is' from fseek */
2484 if(file->_flag & _IOWRT)
2485 flush_buffer(file);
2486
2487 /* Discard buffered input */
2488 file->_cnt = 0;
2489 file->_ptr = file->_base;
2490
2491 /* Reset direction of i/o */
2492 if(file->_flag & _IORW) {
2493 file->_flag &= ~(_IOREAD|_IOWRT);
2494 }
2495
2496 return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
2497 }
2498
2499 /*********************************************************************
2500 * ftell (MSVCRT.@)
2501 */
2502 LONG CDECL ftell(FILE* file)
2503 {
2504 /* TODO: just call fgetpos and return lower half of result */
2505 int off=0;
2506 long pos;
2507 pos = _tell(file->_file);
2508 if(pos == -1) return -1;
2509 if(file->_bufsiz) {
2510 if( file->_flag & _IOWRT ) {
2511 off = file->_ptr - file->_base;
2512 } else {
2513 off = -file->_cnt;
2514 if (fdesc[file->_file].wxflag & WX_TEXT) {
2515 /* Black magic correction for CR removal */
2516 int i;
2517 for (i=0; i<file->_cnt; i++) {
2518 if (file->_ptr[i] == '\n')
2519 off--;
2520 }
2521 }
2522 }
2523 }
2524 return off + pos;
2525 }
2526
2527 /*********************************************************************
2528 * fgetpos (MSVCRT.@)
2529 */
2530 int CDECL fgetpos(FILE* file, fpos_t *pos)
2531 {
2532 int off=0;
2533 *pos = _lseeki64(file->_file,0,SEEK_CUR);
2534 if(*pos == -1) return -1;
2535 if(file->_bufsiz) {
2536 if( file->_flag & _IOWRT ) {
2537 off = file->_ptr - file->_base;
2538 } else {
2539 off = -file->_cnt;
2540 if (fdesc[file->_file].wxflag & WX_TEXT) {
2541 /* Black magic correction for CR removal */
2542 int i;
2543 for (i=0; i<file->_cnt; i++) {
2544 if (file->_ptr[i] == '\n')
2545 off--;
2546 }
2547 }
2548 }
2549 }
2550 *pos += off;
2551 return 0;
2552 }
2553
2554 /*********************************************************************
2555 * fputs (MSVCRT.@)
2556 */
2557 int CDECL fputs(const char *s, FILE* file)
2558 {
2559 size_t i, len = strlen(s);
2560 if (!(fdesc[file->_file].wxflag & WX_TEXT))
2561 return fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
2562 for (i=0; i<len; i++)
2563 if (fputc(s[i], file) == EOF)
2564 return EOF;
2565 return 0;
2566 }
2567
2568 /*********************************************************************
2569 * fputws (MSVCRT.@)
2570 */
2571 int CDECL fputws(const wchar_t *s, FILE* file)
2572 {
2573 size_t i, len = strlenW(s);
2574 if (!(fdesc[file->_file].wxflag & WX_TEXT))
2575 return fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
2576 for (i=0; i<len; i++)
2577 {
2578 if ((s[i] == '\n') && (fputc('\r', file) == EOF))
2579 return WEOF;
2580 if (fputwc(s[i], file) == WEOF)
2581 return WEOF;
2582 }
2583 return 0;
2584 }
2585
2586 /*********************************************************************
2587 * getchar (MSVCRT.@)
2588 */
2589 int CDECL getchar(void)
2590 {
2591 return fgetc(stdin);
2592 }
2593
2594 /*********************************************************************
2595 * getc (MSVCRT.@)
2596 */
2597 int CDECL getc(FILE* file)
2598 {
2599 return fgetc(file);
2600 }
2601
2602 /*********************************************************************
2603 * gets (MSVCRT.@)
2604 */
2605 char * CDECL gets(char *buf)
2606 {
2607 int cc;
2608 char * buf_start = buf;
2609
2610 for(cc = fgetc(stdin); cc != EOF && cc != '\n';
2611 cc = fgetc(stdin))
2612 if(cc != '\r') *buf++ = (char)cc;
2613
2614 *buf = '\0';
2615
2616 TRACE("got '%s'\n", buf_start);
2617 return buf_start;
2618 }
2619
2620 /*********************************************************************
2621 * _getws (MSVCRT.@)
2622 */
2623 wchar_t* CDECL _getws(wchar_t* buf)
2624 {
2625 wint_t cc;
2626 wchar_t* ws = buf;
2627
2628 for (cc = fgetwc(stdin); cc != WEOF && cc != '\n';
2629 cc = fgetwc(stdin))
2630 {
2631 if (cc != '\r')
2632 *buf++ = (wchar_t)cc;
2633 }
2634 *buf = '\0';
2635
2636 TRACE("got %s\n", debugstr_w(ws));
2637 return ws;
2638 }
2639
2640 /*********************************************************************
2641 * putc (MSVCRT.@)
2642 */
2643 int CDECL putc(int c, FILE* file)
2644 {
2645 return fputc(c, file);
2646 }
2647
2648 /*********************************************************************
2649 * putchar (MSVCRT.@)
2650 */
2651 int CDECL putchar(int c)
2652 {
2653 return fputc(c, stdout);
2654 }
2655
2656 /*********************************************************************
2657 * puts (MSVCRT.@)
2658 */
2659 int CDECL puts(const char *s)
2660 {
2661 size_t len = strlen(s);
2662 if (fwrite(s,sizeof(*s),len,stdout) != len) return EOF;
2663 return fwrite("\n",1,1,stdout) == 1 ? 0 : EOF;
2664 }
2665
2666 /*********************************************************************
2667 * _putws (MSVCRT.@)
2668 */
2669 int CDECL _putws(const wchar_t *s)
2670 {
2671 static const wchar_t nl = '\n';
2672 size_t len = strlenW(s);
2673 if (fwrite(s,sizeof(*s),len,stdout) != len) return EOF;
2674 return fwrite(&nl,sizeof(nl),1,stdout) == 1 ? 0 : EOF;
2675 }
2676
2677 /*********************************************************************
2678 * remove (MSVCRT.@)
2679 */
2680 int CDECL remove(const char *path)
2681 {
2682 TRACE("(%s)\n",path);
2683 if (DeleteFileA(path))
2684 return 0;
2685 TRACE(":failed (%d)\n",GetLastError());
2686 _dosmaperr(GetLastError());
2687 return -1;
2688 }
2689
2690 /*********************************************************************
2691 * _wremove (MSVCRT.@)
2692 */
2693 int CDECL _wremove(const wchar_t *path)
2694 {
2695 TRACE("(%s)\n",debugstr_w(path));
2696 if (DeleteFileW(path))
2697 return 0;
2698 TRACE(":failed (%d)\n",GetLastError());
2699 _dosmaperr(GetLastError());
2700 return -1;
2701 }
2702
2703 /*********************************************************************
2704 * rename (MSVCRT.@)
2705 */
2706 int CDECL rename(const char *oldpath,const char *newpath)
2707 {
2708 TRACE(":from %s to %s\n",oldpath,newpath);
2709 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2710 return 0;
2711 TRACE(":failed (%d)\n",GetLastError());
2712 _dosmaperr(GetLastError());
2713 return -1;
2714 }
2715
2716 /*********************************************************************
2717 * _wrename (MSVCRT.@)
2718 */
2719 int CDECL _wrename(const wchar_t *oldpath,const wchar_t *newpath)
2720 {
2721 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
2722 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
2723 return 0;
2724 TRACE(":failed (%d)\n",GetLastError());
2725 _dosmaperr(GetLastError());
2726 return -1;
2727 }
2728
2729 /*********************************************************************
2730 * setvbuf (MSVCRT.@)
2731 */
2732 int CDECL setvbuf(FILE* file, char *buf, int mode, size_t size)
2733 {
2734 /* TODO: Check if file busy */
2735 if(file->_bufsiz) {
2736 free(file->_base);
2737 file->_bufsiz = 0;
2738 file->_cnt = 0;
2739 }
2740 if(mode == _IOFBF) {
2741 file->_flag &= ~_IONBF;
2742 file->_base = file->_ptr = buf;
2743 if(buf) {
2744 file->_bufsiz = size;
2745 }
2746 } else {
2747 file->_flag |= _IONBF;
2748 }
2749 return 0;
2750 }
2751
2752 /*********************************************************************
2753 * setbuf (MSVCRT.@)
2754 */
2755 void CDECL setbuf(FILE* file, char *buf)
2756 {
2757 setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
2758 }
2759
2760 /*********************************************************************
2761 * tmpnam (MSVCRT.@)
2762 */
2763 char * CDECL tmpnam(char *s)
2764 {
2765 static int unique;
2766 char tmpstr[16];
2767 char *p;
2768 int count;
2769 if (s == 0)
2770 s = tmpname;
2771 int_to_base32(GetCurrentProcessId(), tmpstr);
2772 p = s + sprintf(s, "\\s%s.", tmpstr);
2773 for (count = 0; count < TMP_MAX; count++)
2774 {
2775 int_to_base32(unique++, tmpstr);
2776 strcpy(p, tmpstr);
2777 if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
2778 GetLastError() == ERROR_FILE_NOT_FOUND)
2779 break;
2780 }
2781 return s;
2782 }
2783
2784 /*********************************************************************
2785 * wtmpnam (MSVCRT.@)
2786 */
2787 wchar_t * CDECL _wtmpnam(wchar_t *s)
2788 {
2789 ERR("UNIMPLEMENTED!\n");
2790 return NULL;
2791 }
2792
2793 /*********************************************************************
2794 * tmpfile (MSVCRT.@)
2795 */
2796 FILE* CDECL tmpfile(void)
2797 {
2798 char *filename = tmpnam(NULL);
2799 int fd;
2800 FILE* file = NULL;
2801
2802 LOCK_FILES();
2803 fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
2804 if (fd != -1 && (file = alloc_fp()))
2805 {
2806 if (init_fp(file, fd, _O_RDWR) == -1)
2807 {
2808 file->_flag = 0;
2809 file = NULL;
2810 }
2811 else file->_tmpfname = _strdup(filename);
2812 }
2813 UNLOCK_FILES();
2814 return file;
2815 }
2816
2817 /*********************************************************************
2818 * vfprintf (MSVCRT.@)
2819 */
2820 int CDECL vfprintf(FILE* file, const char *format, va_list valist)
2821 {
2822 char buf[2048], *mem = buf;
2823 int written, resize = sizeof(buf), retval;
2824 /* There are two conventions for vsnprintf failing:
2825 * Return -1 if we truncated, or
2826 * Return the number of bytes that would have been written
2827 * The code below handles both cases
2828 */
2829 while ((written = _vsnprintf(mem, resize, format, valist)) == -1 ||
2830 written > resize)
2831 {
2832 resize = (written == -1 ? resize * 2 : written + 1);
2833 if (mem != buf)
2834 free (mem);
2835 if (!(mem = malloc(resize)))
2836 return EOF;
2837 }
2838 retval = fwrite(mem, sizeof(*mem), written, file);
2839 if (mem != buf)
2840 free (mem);
2841 return retval;
2842 }
2843
2844 /*********************************************************************
2845 * vfwprintf (MSVCRT.@)
2846 * FIXME:
2847 * Is final char included in written (then resize is too big) or not
2848 * (then we must test for equality too)?
2849 */
2850 int CDECL vfwprintf(FILE* file, const wchar_t *format, va_list valist)
2851 {
2852 wchar_t buf[2048], *mem = buf;
2853 int written, resize = sizeof(buf) / sizeof(wchar_t), retval;
2854 /* See vfprintf comments */
2855 while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
2856 written > resize)
2857 {
2858 resize = (written == -1 ? resize * 2 : written + sizeof(wchar_t));
2859 if (mem != buf)
2860 free (mem);
2861 if (!(mem = malloc(resize*sizeof(*mem))))
2862 return EOF;
2863 }
2864
2865 /* Check if outputting to a text-file */
2866 if (fdesc[file->_file].wxflag & WX_TEXT)
2867 {
2868 /* Convert each character and stop at the first invalid character. Behavior verified by tests under WinXP SP2 */
2869 char chMultiByte[MB_LEN_MAX];
2870 int nReturn;
2871 wchar_t *p;
2872
2873 retval = 0;
2874
2875 for (p = mem; *p; p++)
2876 {
2877 nReturn = wctomb(chMultiByte, *p);
2878
2879 if(nReturn == -1)
2880 break;
2881
2882 retval += fwrite(chMultiByte, 1, nReturn, file);
2883 }
2884 }
2885 else
2886 {
2887 retval = fwrite(mem, sizeof(*mem), written, file);
2888 }
2889
2890 if (mem != buf)
2891 free (mem);
2892
2893 return retval;
2894 }
2895
2896 /*********************************************************************
2897 * vprintf (MSVCRT.@)
2898 */
2899 int CDECL vprintf(const char *format, va_list valist)
2900 {
2901 return vfprintf(stdout,format,valist);
2902 }
2903
2904 /*********************************************************************
2905 * vwprintf (MSVCRT.@)
2906 */
2907 int CDECL vwprintf(const wchar_t *format, va_list valist)
2908 {
2909 return vfwprintf(stdout,format,valist);
2910 }
2911
2912 /*********************************************************************
2913 * fprintf (MSVCRT.@)
2914 */
2915 int CDECL fprintf(FILE* file, const char *format, ...)
2916 {
2917 va_list valist;
2918 int res;
2919 va_start(valist, format);
2920 res = vfprintf(file, format, valist);
2921 va_end(valist);
2922 return res;
2923 }
2924
2925 /*********************************************************************
2926 * fwprintf (MSVCRT.@)
2927 */
2928 int CDECL fwprintf(FILE* file, const wchar_t *format, ...)
2929 {
2930 va_list valist;
2931 int res;
2932 va_start(valist, format);
2933 res = vfwprintf(file, format, valist);
2934 va_end(valist);
2935 return res;
2936 }
2937
2938 /*********************************************************************
2939 * printf (MSVCRT.@)
2940 */
2941 int CDECL printf(const char *format, ...)
2942 {
2943 va_list valist;
2944 int res;
2945 va_start(valist, format);
2946 res = vfprintf(stdout, format, valist);
2947 va_end(valist);
2948 return res;
2949 }
2950
2951 /*********************************************************************
2952 * ungetc (MSVCRT.@)
2953 */
2954 int CDECL ungetc(int c, FILE * file)
2955 {
2956 if (c == EOF)
2957 return EOF;
2958 if(file->_bufsiz == 0 && !(file->_flag & _IONBF)) {
2959 alloc_buffer(file);
2960 file->_ptr++;
2961 }
2962 if(file->_ptr>file->_base) {
2963 file->_ptr--;
2964 *file->_ptr=c;
2965 file->_cnt++;
2966 clearerr(file);
2967 return c;
2968 }
2969 return EOF;
2970 }
2971
2972 /*********************************************************************
2973 * ungetwc (MSVCRT.@)
2974 */
2975 wint_t CDECL ungetwc(wint_t wc, FILE * file)
2976 {
2977 wchar_t mwc = wc;
2978 char * pp = (char *)&mwc;
2979 int i;
2980 for(i=sizeof(wchar_t)-1;i>=0;i--) {
2981 if(pp[i] != ungetc(pp[i],file))
2982 return WEOF;
2983 }
2984 return mwc;
2985 }
2986
2987 /*********************************************************************
2988 * wprintf (MSVCRT.@)
2989 */
2990 int CDECL wprintf(const wchar_t *format, ...)
2991 {
2992 va_list valist;
2993 int res;
2994 va_start(valist, format);
2995 res = vwprintf(format, valist);
2996 va_end(valist);
2997 return res;
2998 }
2999
3000 /*********************************************************************
3001 * _getmaxstdio (MSVCRT.@)
3002 */
3003 int CDECL _getmaxstdio(void)
3004 {
3005 FIXME("stub, always returns 512\n");
3006 return 512;
3007 }
3008
3009 /*********************************************************************
3010 * _setmaxstdio_ (MSVCRT.@)
3011 */
3012 int CDECL _setmaxstdio(int newmax)
3013 {
3014 int res;
3015 if( newmax > 2048)
3016 res = -1;
3017 else
3018 res = newmax;
3019 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res);
3020 return res;
3021 }
3022
3023 /*********************************************************************
3024 * __pioinfo (MSVCRT.@)
3025 * FIXME: see MAX_FILES define.
3026 */
3027 ioinfo * __pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
3028 &fdesc[0 * 64], &fdesc[1 * 64], &fdesc[2 * 64],
3029 &fdesc[3 * 64], &fdesc[4 * 64], &fdesc[5 * 64],
3030 &fdesc[6 * 64], &fdesc[7 * 64], &fdesc[8 * 64],
3031 &fdesc[9 * 64], &fdesc[10 * 64], &fdesc[11 * 64],
3032 &fdesc[12 * 64], &fdesc[13 * 64], &fdesc[14 * 64],
3033 &fdesc[15 * 64], &fdesc[16 * 64], &fdesc[17 * 64],
3034 &fdesc[18 * 64], &fdesc[19 * 64], &fdesc[20 * 64],
3035 &fdesc[21 * 64], &fdesc[22 * 64], &fdesc[23 * 64],
3036 &fdesc[24 * 64], &fdesc[25 * 64], &fdesc[26 * 64],
3037 &fdesc[27 * 64], &fdesc[28 * 64], &fdesc[29 * 64],
3038 &fdesc[30 * 64], &fdesc[31 * 64]
3039 } ;
3040
3041 /*********************************************************************
3042 * __badioinfo (MSVCRT.@)
3043 */
3044 ioinfo __badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT, { 0, } };