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