2 * msvcrt.dll file functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 * Copyright 2004 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
34 #include <sys/types.h>
42 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
48 /* for stat mode, permissions apply to all,owner and group */
49 #define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
50 #define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
51 #define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
53 /* _access() bit flags FIXME: incomplete */
54 #define MSVCRT_W_OK 0x02
56 /* values for wxflag in file descriptor */
59 #define WX_DONTINHERIT 0x10
60 #define WX_APPEND 0x20
63 /* FIXME: this should be allocated dynamically */
64 #define MSVCRT_MAX_FILES 2048
69 DWORD unkn
[7]; /* critical section and init flag */
72 static ioinfo MSVCRT_fdesc
[MSVCRT_MAX_FILES
];
74 MSVCRT_FILE MSVCRT__iob
[3];
76 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
77 static int MSVCRT_fdend
= 3; /* highest allocated fd */
79 static MSVCRT_FILE
* MSVCRT_fstreams
[2048];
80 static int MSVCRT_stream_idx
;
82 /* INTERNAL: process umask */
83 static int MSVCRT_umask
= 0;
85 /* INTERNAL: Static buffer for temp file name */
86 static char MSVCRT_tmpname
[MAX_PATH
];
88 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
89 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
90 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
91 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
93 #define TOUL(x) (ULONGLONG)(x)
94 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
95 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
96 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
97 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
99 extern CRITICAL_SECTION MSVCRT_file_cs
;
100 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
101 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
103 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64
*bufi64
, struct MSVCRT__stat
*buf
)
105 buf
->st_dev
= bufi64
->st_dev
;
106 buf
->st_ino
= bufi64
->st_ino
;
107 buf
->st_mode
= bufi64
->st_mode
;
108 buf
->st_nlink
= bufi64
->st_nlink
;
109 buf
->st_uid
= bufi64
->st_uid
;
110 buf
->st_gid
= bufi64
->st_gid
;
111 buf
->st_rdev
= bufi64
->st_rdev
;
112 buf
->st_size
= bufi64
->st_size
;
113 buf
->st_atime
= bufi64
->st_atime
;
114 buf
->st_mtime
= bufi64
->st_mtime
;
115 buf
->st_ctime
= bufi64
->st_ctime
;
118 static inline BOOL
msvcrt_is_valid_fd(int fd
)
120 return fd
>= 0 && fd
< MSVCRT_fdend
&& (MSVCRT_fdesc
[fd
].wxflag
& WX_OPEN
);
123 /* INTERNAL: Get the HANDLE for a fd */
124 static HANDLE
msvcrt_fdtoh(int fd
)
126 if (!msvcrt_is_valid_fd(fd
))
128 WARN(":fd (%d) - no handle!\n",fd
);
129 *MSVCRT___doserrno() = 0;
130 *MSVCRT__errno() = MSVCRT_EBADF
;
131 return INVALID_HANDLE_VALUE
;
133 if (MSVCRT_fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
134 return MSVCRT_fdesc
[fd
].handle
;
137 /* INTERNAL: free a file entry fd */
138 static void msvcrt_free_fd(int fd
)
140 MSVCRT_fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
141 MSVCRT_fdesc
[fd
].wxflag
= 0;
142 TRACE(":fd (%d) freed\n",fd
);
143 if (fd
< 3) /* don't use 0,1,2 for user files */
147 case 0: SetStdHandle(STD_INPUT_HANDLE
, NULL
); break;
148 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, NULL
); break;
149 case 2: SetStdHandle(STD_ERROR_HANDLE
, NULL
); break;
154 if (fd
== MSVCRT_fdend
- 1)
156 if (fd
< MSVCRT_fdstart
)
161 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
162 static int msvcrt_alloc_fd_from(HANDLE hand
, int flag
, int fd
)
164 if (fd
>= MSVCRT_MAX_FILES
)
166 WARN(":files exhausted!\n");
169 MSVCRT_fdesc
[fd
].handle
= hand
;
170 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
172 /* locate next free slot */
173 if (fd
== MSVCRT_fdstart
&& fd
== MSVCRT_fdend
)
174 MSVCRT_fdstart
= MSVCRT_fdend
+ 1;
176 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
177 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
179 /* update last fd in use */
180 if (fd
>= MSVCRT_fdend
)
181 MSVCRT_fdend
= fd
+ 1;
182 TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart
, MSVCRT_fdend
);
186 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
187 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
188 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
194 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
195 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
197 TRACE(":handle (%p) allocating fd (%d)\n",hand
,MSVCRT_fdstart
);
198 return msvcrt_alloc_fd_from(hand
, flag
, MSVCRT_fdstart
);
201 /* INTERNAL: Allocate a FILE* for an fd slot
203 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
207 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
209 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
211 if (!MSVCRT_fstreams
[i
])
213 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
215 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
217 return MSVCRT_fstreams
[i
];
223 /* INTERNAL: initialize a FILE* from an open fd */
224 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
226 TRACE(":fd (%d) allocating FILE*\n",fd
);
227 if (!msvcrt_is_valid_fd(fd
))
229 WARN(":invalid fd %d\n",fd
);
230 *MSVCRT___doserrno() = 0;
231 *MSVCRT__errno() = MSVCRT_EBADF
;
234 memset(file
, 0, sizeof(*file
));
236 file
->_flag
= stream_flags
;
238 TRACE(":got FILE* (%p)\n",file
);
242 /* INTERNAL: Create an inheritance data block (for spawned process)
243 * The inheritance block is made of:
244 * 00 int nb of file descriptor (NBFD)
245 * 04 char file flags (wxflag): repeated for each fd
246 * 4+NBFD HANDLE file handle: repeated for each fd
248 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA
* si
)
254 si
->cbReserved2
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
255 si
->lpReserved2
= MSVCRT_calloc(si
->cbReserved2
, 1);
256 if (!si
->lpReserved2
)
261 wxflag_ptr
= (char*)si
->lpReserved2
+ sizeof(unsigned);
262 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
264 *(unsigned*)si
->lpReserved2
= MSVCRT_fdend
;
265 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
267 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
268 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
270 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
271 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
276 *handle_ptr
= INVALID_HANDLE_VALUE
;
278 wxflag_ptr
++; handle_ptr
++;
283 /* INTERNAL: Set up all file descriptors,
284 * as well as default streams (stdin, stderr and stdout)
286 void msvcrt_init_io(void)
291 GetStartupInfoA(&si
);
292 if (si
.cbReserved2
!= 0 && si
.lpReserved2
!= NULL
)
297 MSVCRT_fdend
= *(unsigned*)si
.lpReserved2
;
299 wxflag_ptr
= (char*)(si
.lpReserved2
+ sizeof(unsigned));
300 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
302 MSVCRT_fdend
= min(MSVCRT_fdend
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
303 for (i
= 0; i
< MSVCRT_fdend
; i
++)
305 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
307 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
308 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
312 MSVCRT_fdesc
[i
].wxflag
= 0;
313 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
315 wxflag_ptr
++; handle_ptr
++;
317 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
318 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
321 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
323 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE
),
324 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
, 0, FALSE
,
325 DUPLICATE_SAME_ACCESS
);
326 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
328 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
330 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE
),
331 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
, 0, FALSE
,
332 DUPLICATE_SAME_ACCESS
);
333 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
335 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
337 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE
),
338 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
, 0, FALSE
,
339 DUPLICATE_SAME_ACCESS
);
340 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
343 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
344 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
346 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
347 for (i
= 0; i
< 3; i
++)
349 /* FILE structs for stdin/out/err are static and never deleted */
350 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
351 MSVCRT__iob
[i
]._file
= i
;
352 MSVCRT__iob
[i
]._tmpfname
= NULL
;
353 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
355 MSVCRT_stream_idx
= 3;
358 /* INTERNAL: Flush stdio file buffer */
359 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
362 int cnt
=file
->_ptr
-file
->_base
;
363 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
364 file
->_flag
|= MSVCRT__IOERR
;
367 file
->_ptr
=file
->_base
;
368 file
->_cnt
=file
->_bufsiz
;
373 /* INTERNAL: Allocate stdio file buffer */
374 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
376 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
378 file
->_bufsiz
= MSVCRT_BUFSIZ
;
379 file
->_flag
|= MSVCRT__IOMYBUF
;
381 file
->_base
= (unsigned char *)(&file
->_charbuf
);
383 file
->_bufsiz
= sizeof(file
->_charbuf
);
385 file
->_ptr
= file
->_base
;
389 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
390 static void msvcrt_int_to_base32(int num
, char *str
)
405 *p
= (num
& 31) + '0';
407 *p
+= ('a' - '0' - 10);
412 /*********************************************************************
415 MSVCRT_FILE
*__p__iob(void)
417 return &MSVCRT__iob
[0];
420 /*********************************************************************
423 int _access(const char *filename
, int mode
)
425 DWORD attr
= GetFileAttributesA(filename
);
427 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
429 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
431 msvcrt_set_errno(GetLastError());
434 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
436 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
442 /*********************************************************************
443 * _waccess (MSVCRT.@)
445 int _waccess(const MSVCRT_wchar_t
*filename
, int mode
)
447 DWORD attr
= GetFileAttributesW(filename
);
449 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
451 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
453 msvcrt_set_errno(GetLastError());
456 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
458 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
464 /*********************************************************************
467 int _chmod(const char *path
, int flags
)
469 DWORD oldFlags
= GetFileAttributesA(path
);
471 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
473 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
474 oldFlags
| FILE_ATTRIBUTE_READONLY
;
476 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
479 msvcrt_set_errno(GetLastError());
483 /*********************************************************************
486 int _wchmod(const MSVCRT_wchar_t
*path
, int flags
)
488 DWORD oldFlags
= GetFileAttributesW(path
);
490 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
492 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
493 oldFlags
| FILE_ATTRIBUTE_READONLY
;
495 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
498 msvcrt_set_errno(GetLastError());
502 /*********************************************************************
505 int _chsize(int fd
, long size
)
507 FIXME("(fd=%d, size=%ld): stub\n", fd
, size
);
511 /*********************************************************************
514 int _unlink(const char *path
)
516 TRACE("(%s)\n",path
);
517 if(DeleteFileA(path
))
519 TRACE("failed (%ld)\n",GetLastError());
520 msvcrt_set_errno(GetLastError());
524 /*********************************************************************
525 * _wunlink (MSVCRT.@)
527 int _wunlink(const MSVCRT_wchar_t
*path
)
529 TRACE("(%s)\n",debugstr_w(path
));
530 if(DeleteFileW(path
))
532 TRACE("failed (%ld)\n",GetLastError());
533 msvcrt_set_errno(GetLastError());
537 /* _flushall calls MSVCRT_fflush which calls _flushall */
538 int MSVCRT_fflush(MSVCRT_FILE
* file
);
540 /*********************************************************************
541 * _flushall (MSVCRT.@)
545 int i
, num_flushed
= 0;
547 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
548 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
551 /* FIXME: flush, do not commit */
552 if (_commit(i
) == -1)
553 if (MSVCRT_fstreams
[i
])
554 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
556 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
557 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
562 TRACE(":flushed (%d) handles\n",num_flushed
);
566 /*********************************************************************
569 int MSVCRT_fflush(MSVCRT_FILE
* file
)
575 int res
=msvcrt_flush_buffer(file
);
580 /*********************************************************************
585 HANDLE hand
= msvcrt_fdtoh(fd
);
587 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
588 if (hand
== INVALID_HANDLE_VALUE
)
591 if (!CloseHandle(hand
))
593 WARN(":failed-last error (%ld)\n",GetLastError());
594 msvcrt_set_errno(GetLastError());
602 /*********************************************************************
607 HANDLE hand
= msvcrt_fdtoh(fd
);
609 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
610 if (hand
== INVALID_HANDLE_VALUE
)
613 if (!FlushFileBuffers(hand
))
615 if (GetLastError() == ERROR_INVALID_HANDLE
)
617 /* FlushFileBuffers fails for console handles
618 * so we ignore this error.
622 TRACE(":failed-last error (%ld)\n",GetLastError());
623 msvcrt_set_errno(GetLastError());
630 /*********************************************************************
633 * MSDN isn't clear on this point, but the remarks for _pipe,
634 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
635 * indicate file descriptors duplicated with _dup and _dup2 are always
638 int _dup2(int od
, int nd
)
642 TRACE("(od=%d, nd=%d)\n", od
, nd
);
643 if (nd
< MSVCRT_MAX_FILES
&& msvcrt_is_valid_fd(od
))
647 if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc
[od
].handle
,
648 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
650 int wxflag
= MSVCRT_fdesc
[od
].wxflag
& ~MSVCRT__O_NOINHERIT
;
652 if (msvcrt_is_valid_fd(nd
))
654 ret
= msvcrt_alloc_fd_from(handle
, wxflag
, nd
);
658 *MSVCRT__errno() = MSVCRT_EMFILE
;
662 /* _dup2 returns 0, not nd, on success */
669 msvcrt_set_errno(GetLastError());
674 *MSVCRT__errno() = MSVCRT_EBADF
;
680 /*********************************************************************
685 int fd
= MSVCRT_fdstart
;
687 if (_dup2(od
, fd
) == 0)
692 /*********************************************************************
697 DWORD curpos
,endpos
,hcurpos
,hendpos
;
698 HANDLE hand
= msvcrt_fdtoh(fd
);
700 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
702 if (hand
== INVALID_HANDLE_VALUE
)
705 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
707 /* Otherwise we do it the hard way */
708 hcurpos
= hendpos
= 0;
709 curpos
= SetFilePointer(hand
, 0, &hcurpos
, SEEK_CUR
);
710 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
712 if (curpos
== endpos
&& hcurpos
== hendpos
)
715 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
719 /*********************************************************************
720 * _fcloseall (MSVCRT.@)
722 int MSVCRT__fcloseall(void)
724 int num_closed
= 0, i
;
726 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
727 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
728 MSVCRT_fclose(MSVCRT_fstreams
[i
]))
731 TRACE(":closed (%d) handles\n",num_closed
);
735 /* free everything on process exit */
736 void msvcrt_free_io(void)
744 /*********************************************************************
745 * _lseeki64 (MSVCRT.@)
747 __int64
_lseeki64(int fd
, __int64 offset
, int whence
)
749 DWORD ret
, hoffset
= (DWORD
) (offset
>> 32);
750 HANDLE hand
= msvcrt_fdtoh(fd
);
752 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
753 if (hand
== INVALID_HANDLE_VALUE
)
756 if (whence
< 0 || whence
> 2)
758 *MSVCRT__errno() = MSVCRT_EINVAL
;
762 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
763 fd
,hoffset
,(long)offset
,
764 (whence
==SEEK_SET
)?"SEEK_SET":
765 (whence
==SEEK_CUR
)?"SEEK_CUR":
766 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
768 ret
= SetFilePointer(hand
, (long)offset
, &hoffset
, whence
);
769 if (ret
!= INVALID_SET_FILE_POINTER
|| !GetLastError())
771 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_ATEOF
;
772 /* FIXME: What if we seek _to_ EOF - is EOF set? */
774 return ((__int64
)hoffset
<< 32) | ret
;
776 TRACE(":error-last error (%ld)\n",GetLastError());
777 msvcrt_set_errno(GetLastError());
781 /*********************************************************************
784 LONG
_lseek(int fd
, LONG offset
, int whence
)
786 return _lseeki64(fd
, offset
, whence
);
789 /*********************************************************************
790 * _locking (MSVCRT.@)
792 * This is untested; the underlying LockFile doesn't work yet.
794 int _locking(int fd
, int mode
, LONG nbytes
)
798 HANDLE hand
= msvcrt_fdtoh(fd
);
800 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
801 if (hand
== INVALID_HANDLE_VALUE
)
804 if (mode
< 0 || mode
> 4)
806 *MSVCRT__errno() = MSVCRT_EINVAL
;
810 TRACE(":fd (%d) by 0x%08lx mode %s\n",
811 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
812 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
813 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
814 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
815 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
818 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
820 FIXME ("Seek failed\n");
821 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
824 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
827 ret
= 1; /* just to satisfy gcc */
830 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
835 else if (mode
== MSVCRT__LK_UNLCK
)
836 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
838 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
839 /* FIXME - what about error settings? */
843 /*********************************************************************
846 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
848 /* Flush output if needed */
849 if(file
->_flag
& MSVCRT__IOWRT
)
850 msvcrt_flush_buffer(file
);
852 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
853 offset
-= file
->_cnt
;
855 /* Discard buffered input */
857 file
->_ptr
= file
->_base
;
858 /* Reset direction of i/o */
859 if(file
->_flag
& MSVCRT__IORW
) {
860 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
862 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
865 /*********************************************************************
866 * clearerr (MSVCRT.@)
868 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
870 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
871 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
874 /*********************************************************************
877 void MSVCRT_rewind(MSVCRT_FILE
* file
)
879 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
880 MSVCRT_fseek(file
, 0L, SEEK_SET
);
881 MSVCRT_clearerr(file
);
884 static int msvcrt_get_flags(const char* mode
, int *open_flags
, int* stream_flags
)
886 int plus
= strchr(mode
, '+') != NULL
;
891 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
892 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
895 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
896 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
899 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
900 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
910 *open_flags
|= MSVCRT__O_BINARY
;
911 *open_flags
&= ~MSVCRT__O_TEXT
;
914 *open_flags
|= MSVCRT__O_TEXT
;
915 *open_flags
&= ~MSVCRT__O_BINARY
;
920 FIXME(":unknown flag %c not supported\n",mode
[-1]);
925 /*********************************************************************
928 MSVCRT_FILE
* MSVCRT__fdopen(int fd
, const char *mode
)
930 int open_flags
, stream_flags
;
933 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
935 if (!(file
= msvcrt_alloc_fp())) return NULL
;
936 if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
941 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
946 /*********************************************************************
947 * _wfdopen (MSVCRT.@)
949 MSVCRT_FILE
* MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
951 unsigned mlen
= strlenW(mode
);
952 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
953 MSVCRT_FILE
* file
= NULL
;
954 int open_flags
, stream_flags
;
957 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
959 if (msvcrt_get_flags(modea
, &open_flags
, &stream_flags
) == -1) return NULL
;
960 if (!(file
= msvcrt_alloc_fp())) return NULL
;
961 if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
969 MSVCRT_rewind(file
); /* FIXME: is this needed ??? */
970 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
976 /*********************************************************************
977 * _filelength (MSVCRT.@)
979 LONG
_filelength(int fd
)
981 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
984 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
987 if (endPos
!= curPos
)
988 _lseek(fd
, curPos
, SEEK_SET
);
995 /*********************************************************************
996 * _filelengthi64 (MSVCRT.@)
998 __int64
_filelengthi64(int fd
)
1000 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1003 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1006 if (endPos
!= curPos
)
1007 _lseeki64(fd
, curPos
, SEEK_SET
);
1014 /*********************************************************************
1015 * _fileno (MSVCRT.@)
1017 int MSVCRT__fileno(MSVCRT_FILE
* file
)
1019 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1023 /*********************************************************************
1024 * _fstati64 (MSVCRT.@)
1026 int MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
1029 BY_HANDLE_FILE_INFORMATION hfi
;
1030 HANDLE hand
= msvcrt_fdtoh(fd
);
1032 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1033 if (hand
== INVALID_HANDLE_VALUE
)
1038 WARN(":failed-NULL buf\n");
1039 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1043 memset(&hfi
, 0, sizeof(hfi
));
1044 memset(buf
, 0, sizeof(struct MSVCRT__stati64
));
1045 if (!GetFileInformationByHandle(hand
, &hfi
))
1047 WARN(":failed-last error (%ld)\n",GetLastError());
1048 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
1051 dw
= GetFileType(hand
);
1052 buf
->st_mode
= S_IREAD
;
1053 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1054 buf
->st_mode
|= S_IWRITE
;
1055 /* interestingly, Windows never seems to set S_IFDIR */
1056 if (dw
== FILE_TYPE_CHAR
)
1057 buf
->st_mode
|= S_IFCHR
;
1058 else if (dw
== FILE_TYPE_PIPE
)
1059 buf
->st_mode
|= S_IFIFO
;
1061 buf
->st_mode
|= S_IFREG
;
1062 TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1064 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1065 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1066 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1068 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1069 buf
->st_mtime
= buf
->st_ctime
= dw
;
1073 /*********************************************************************
1076 int MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1078 struct MSVCRT__stati64 bufi64
;
1080 ret
= MSVCRT__fstati64(fd
, &bufi64
);
1082 msvcrt_cp_from_stati64(&bufi64
, buf
);
1086 /*********************************************************************
1087 * _futime (MSVCRT.@)
1089 int _futime(int fd
, struct MSVCRT__utimbuf
*t
)
1091 HANDLE hand
= msvcrt_fdtoh(fd
);
1096 MSVCRT_time_t currTime
;
1097 MSVCRT_time(&currTime
);
1098 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
1099 memcpy(&wt
, &at
, sizeof(wt
));
1103 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
1104 if (t
->actime
== t
->modtime
)
1105 memcpy(&wt
, &at
, sizeof(wt
));
1107 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
1110 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1112 msvcrt_set_errno(GetLastError());
1118 /*********************************************************************
1119 * _get_osfhandle (MSVCRT.@)
1121 long _get_osfhandle(int fd
)
1123 HANDLE hand
= msvcrt_fdtoh(fd
);
1124 HANDLE newhand
= hand
;
1125 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1127 if (hand
!= INVALID_HANDLE_VALUE
)
1129 /* FIXME: I'm not convinced that I should be copying the
1130 * handle here - it may be leaked if the app doesn't
1131 * close it (and the API docs don't say that it should)
1132 * Not duplicating it means that it can't be inherited
1133 * and so lcc's wedit doesn't cope when it passes it to
1134 * child processes. I've an idea that it should either
1135 * be copied by CreateProcess, or marked as inheritable
1136 * when initialised, or maybe both? JG 21-9-00.
1138 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
1139 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
1141 return (long)newhand
;
1144 /*********************************************************************
1145 * _isatty (MSVCRT.@)
1149 HANDLE hand
= msvcrt_fdtoh(fd
);
1151 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1152 if (hand
== INVALID_HANDLE_VALUE
)
1155 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1158 /*********************************************************************
1159 * _mktemp (MSVCRT.@)
1161 char *_mktemp(char *pattern
)
1164 char *retVal
= pattern
;
1169 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1173 id
= GetCurrentProcessId();
1177 int tempNum
= id
/ 10;
1178 *pattern
-- = id
- (tempNum
* 10) + '0';
1184 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1185 GetLastError() == ERROR_FILE_NOT_FOUND
)
1187 *pattern
= letter
++;
1188 } while(letter
!= '|');
1192 /*********************************************************************
1193 * _wmktemp (MSVCRT.@)
1195 MSVCRT_wchar_t
*_wmktemp(MSVCRT_wchar_t
*pattern
)
1198 MSVCRT_wchar_t
*retVal
= pattern
;
1200 MSVCRT_wchar_t letter
= 'a';
1203 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1207 id
= GetCurrentProcessId();
1211 int tempNum
= id
/ 10;
1212 *pattern
-- = id
- (tempNum
* 10) + '0';
1218 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1219 GetLastError() == ERROR_FILE_NOT_FOUND
)
1221 *pattern
= letter
++;
1222 } while(letter
!= '|');
1226 static unsigned split_oflags(unsigned oflags
)
1230 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1231 if (oflags
& MSVCRT__O_BINARY
) ;
1232 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1233 else if (*__p__fmode() & MSVCRT__O_BINARY
) ;
1234 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1235 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1237 if (oflags
& ~(MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|MSVCRT__O_TRUNC
|
1238 MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|
1239 MSVCRT__O_TEMPORARY
|MSVCRT__O_NOINHERIT
))
1240 ERR(":unsupported oflags 0x%04x\n",oflags
);
1245 /*********************************************************************
1248 int _pipe(int *pfds
, unsigned int psize
, int textmode
)
1251 SECURITY_ATTRIBUTES sa
;
1252 HANDLE readHandle
, writeHandle
;
1256 *MSVCRT__errno() = MSVCRT_EINVAL
;
1260 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1261 sa
.bInheritHandle
= !(textmode
& MSVCRT__O_NOINHERIT
);
1262 sa
.lpSecurityDescriptor
= NULL
;
1263 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1265 unsigned int wxflags
= split_oflags(textmode
);
1268 fd
= msvcrt_alloc_fd(readHandle
, wxflags
);
1272 fd
= msvcrt_alloc_fd(writeHandle
, wxflags
);
1281 CloseHandle(writeHandle
);
1282 *MSVCRT__errno() = MSVCRT_EMFILE
;
1287 CloseHandle(readHandle
);
1288 CloseHandle(writeHandle
);
1289 *MSVCRT__errno() = MSVCRT_EMFILE
;
1293 msvcrt_set_errno(GetLastError());
1298 /*********************************************************************
1301 int MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1305 DWORD access
= 0, creation
= 0, attrib
;
1309 SECURITY_ATTRIBUTES sa
;
1312 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1313 path
, oflags
, shflags
);
1315 wxflag
= split_oflags(oflags
);
1316 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1318 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1319 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1320 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1323 if (oflags
& MSVCRT__O_CREAT
)
1325 va_start(ap
, shflags
);
1326 pmode
= va_arg(ap
, int);
1329 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1330 FIXME(": pmode 0x%04x ignored\n", pmode
);
1332 WARN(": pmode 0x%04x ignored\n", pmode
);
1334 if (oflags
& MSVCRT__O_EXCL
)
1335 creation
= CREATE_NEW
;
1336 else if (oflags
& MSVCRT__O_TRUNC
)
1337 creation
= CREATE_ALWAYS
;
1339 creation
= OPEN_ALWAYS
;
1341 else /* no MSVCRT__O_CREAT */
1343 if (oflags
& MSVCRT__O_TRUNC
)
1344 creation
= TRUNCATE_EXISTING
;
1346 creation
= OPEN_EXISTING
;
1351 case MSVCRT__SH_DENYRW
:
1354 case MSVCRT__SH_DENYWR
:
1355 sharing
= FILE_SHARE_READ
;
1357 case MSVCRT__SH_DENYRD
:
1358 sharing
= FILE_SHARE_WRITE
;
1360 case MSVCRT__SH_DENYNO
:
1361 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1364 ERR( "Unhandled shflags 0x%x\n", shflags
);
1367 attrib
= FILE_ATTRIBUTE_NORMAL
;
1369 if (oflags
& MSVCRT__O_TEMPORARY
)
1371 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1373 sharing
|= FILE_SHARE_DELETE
;
1376 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1377 sa
.lpSecurityDescriptor
= NULL
;
1378 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1380 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1382 if (hand
== INVALID_HANDLE_VALUE
) {
1383 WARN(":failed-last error (%ld)\n",GetLastError());
1384 msvcrt_set_errno(GetLastError());
1388 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1390 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1394 if (oflags
& MSVCRT__O_APPEND
)
1395 _lseek(fd
, 0, FILE_END
);
1401 /*********************************************************************
1402 * _wsopen (MSVCRT.@)
1404 int MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1406 const unsigned int len
= strlenW(path
);
1407 char *patha
= MSVCRT_calloc(len
+ 1,1);
1411 va_start(ap
, shflags
);
1412 pmode
= va_arg(ap
, int);
1415 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1417 int retval
= MSVCRT__sopen(patha
,oflags
,shflags
,pmode
);
1422 msvcrt_set_errno(GetLastError());
1426 /*********************************************************************
1429 int _open( const char *path
, int flags
, ... )
1433 if (flags
& MSVCRT__O_CREAT
)
1436 va_start(ap
, flags
);
1437 pmode
= va_arg(ap
, int);
1439 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1442 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1445 /*********************************************************************
1448 int _wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1450 const unsigned int len
= strlenW(path
);
1451 char *patha
= MSVCRT_calloc(len
+ 1,1);
1455 va_start(ap
, flags
);
1456 pmode
= va_arg(ap
, int);
1459 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1461 int retval
= _open(patha
,flags
,pmode
);
1466 msvcrt_set_errno(GetLastError());
1470 /*********************************************************************
1473 int _creat(const char *path
, int flags
)
1475 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1476 return _open(path
, usedFlags
);
1479 /*********************************************************************
1480 * _wcreat (MSVCRT.@)
1482 int _wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1484 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1485 return _wopen(path
, usedFlags
);
1488 /*********************************************************************
1489 * _open_osfhandle (MSVCRT.@)
1491 int _open_osfhandle(long handle
, int oflags
)
1495 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1496 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1497 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1498 * text - it never sets MSVCRT__O_BINARY.
1500 /* FIXME: handle more flags */
1501 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)) && (*__p__fmode() & MSVCRT__O_BINARY
))
1502 oflags
|= MSVCRT__O_BINARY
;
1504 oflags
|= MSVCRT__O_TEXT
;
1506 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1507 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1511 /*********************************************************************
1516 int num_removed
= 0, i
;
1518 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1519 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1521 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1526 TRACE(":removed (%d) temp files\n",num_removed
);
1530 /*********************************************************************
1531 * (internal) remove_cr
1533 * Remove all \r inplace.
1534 * return the number of \r removed
1536 static unsigned int remove_cr(char *buf
, unsigned int count
)
1540 for (i
= 0; i
< count
; i
++) if (buf
[i
] == '\r') break;
1541 for (j
= i
+ 1; j
< count
; j
++) if (buf
[j
] != '\r') buf
[i
++] = buf
[j
];
1545 /*********************************************************************
1548 int _read(int fd
, void *buf
, unsigned int count
)
1550 DWORD num_read
, all_read
= 0;
1551 char *bufstart
= buf
;
1552 HANDLE hand
= msvcrt_fdtoh(fd
);
1554 /* Don't trace small reads, it gets *very* annoying */
1556 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1557 if (hand
== INVALID_HANDLE_VALUE
)
1560 /* Reading single bytes in O_TEXT mode makes things slow
1561 * So read big chunks, then remove the \r in memory and try reading
1562 * the rest until the request is satisfied or EOF is met
1564 while (all_read
< count
)
1566 if (ReadFile(hand
, bufstart
+all_read
, count
- all_read
, &num_read
, NULL
))
1568 if (num_read
!= (count
- all_read
))
1571 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1572 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1573 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1574 all_read
+= num_read
;
1576 TRACE("%s\n",debugstr_an(buf
,all_read
));
1579 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1581 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1583 all_read
+= num_read
;
1587 TRACE(":failed-last error (%ld)\n",GetLastError());
1593 TRACE("(%lu), %s\n",all_read
,debugstr_an(buf
, all_read
));
1597 /*********************************************************************
1600 int MSVCRT__getw(MSVCRT_FILE
* file
)
1603 switch (_read(file
->_file
, &i
, sizeof(int)))
1606 case 0: file
->_flag
|= MSVCRT__IOEOF
; break;
1607 default: file
->_flag
|= MSVCRT__IOERR
; break;
1612 /*********************************************************************
1613 * _setmode (MSVCRT.@)
1615 int _setmode(int fd
,int mode
)
1617 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1618 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1619 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1620 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1621 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1623 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1627 /*********************************************************************
1628 * _stati64 (MSVCRT.@)
1630 int MSVCRT__stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1633 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1634 unsigned short mode
= ALL_S_IREAD
;
1637 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1639 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1641 TRACE("failed (%ld)\n",GetLastError());
1642 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1646 memset(buf
,0,sizeof(struct MSVCRT__stati64
));
1648 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1649 Bon 011120: This FIXME seems incorrect
1650 Also a letter as first char isn't enough to be classified
1653 if (isalpha(*path
)&& (*(path
+1)==':'))
1654 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1656 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1658 plen
= strlen(path
);
1660 /* Dir, or regular file? */
1661 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1662 (path
[plen
-1] == '\\'))
1663 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1666 mode
|= MSVCRT__S_IFREG
;
1668 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1670 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1671 (tolower(path
[plen
-3]) << 16);
1672 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1673 mode
|= ALL_S_IEXEC
;
1677 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1678 mode
|= ALL_S_IWRITE
;
1680 buf
->st_mode
= mode
;
1682 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1683 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1685 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1686 buf
->st_mtime
= buf
->st_ctime
= dw
;
1687 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1688 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1689 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1693 /*********************************************************************
1696 int MSVCRT__stat(const char* path
, struct MSVCRT__stat
* buf
)
1698 struct MSVCRT__stati64 bufi64
;
1700 ret
= MSVCRT__stati64( path
, &bufi64
);
1702 msvcrt_cp_from_stati64(&bufi64
, buf
);
1706 /*********************************************************************
1707 * _wstati64 (MSVCRT.@)
1709 int MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
1712 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1713 unsigned short mode
= ALL_S_IREAD
;
1716 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1718 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1720 TRACE("failed (%ld)\n",GetLastError());
1721 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1725 memset(buf
,0,sizeof(struct MSVCRT__stat
));
1727 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1728 if (MSVCRT_iswalpha(*path
))
1729 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1731 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1733 plen
= strlenW(path
);
1735 /* Dir, or regular file? */
1736 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1737 (path
[plen
-1] == '\\'))
1738 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1741 mode
|= MSVCRT__S_IFREG
;
1743 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1745 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1746 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1747 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1748 mode
|= ALL_S_IEXEC
;
1752 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1753 mode
|= ALL_S_IWRITE
;
1755 buf
->st_mode
= mode
;
1757 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1758 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1760 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1761 buf
->st_mtime
= buf
->st_ctime
= dw
;
1762 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1763 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1764 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1768 /*********************************************************************
1771 int MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
1774 struct MSVCRT__stati64 bufi64
;
1776 ret
= MSVCRT__wstati64( path
, &bufi64
);
1777 if (!ret
) msvcrt_cp_from_stati64(&bufi64
, buf
);
1781 /*********************************************************************
1786 return _lseek(fd
, 0, SEEK_CUR
);
1789 /*********************************************************************
1790 * _telli64 (MSVCRT.@)
1792 __int64
_telli64(int fd
)
1794 return _lseeki64(fd
, 0, SEEK_CUR
);
1797 /*********************************************************************
1798 * _tempnam (MSVCRT.@)
1800 char *_tempnam(const char *dir
, const char *prefix
)
1802 char tmpbuf
[MAX_PATH
];
1804 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1805 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1807 TRACE("got name (%s)\n",tmpbuf
);
1808 DeleteFileA(tmpbuf
);
1809 return _strdup(tmpbuf
);
1811 TRACE("failed (%ld)\n",GetLastError());
1815 /*********************************************************************
1816 * _wtempnam (MSVCRT.@)
1818 MSVCRT_wchar_t
*_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
1820 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
1822 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1823 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1825 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1826 DeleteFileW(tmpbuf
);
1827 return _wcsdup(tmpbuf
);
1829 TRACE("failed (%ld)\n",GetLastError());
1833 /*********************************************************************
1836 int _umask(int umask
)
1838 int old_umask
= MSVCRT_umask
;
1839 TRACE("(%d)\n",umask
);
1840 MSVCRT_umask
= umask
;
1844 /*********************************************************************
1847 int _utime(const char* path
, struct MSVCRT__utimbuf
*t
)
1849 int fd
= _open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
1853 int retVal
= _futime(fd
, t
);
1860 /*********************************************************************
1861 * _wutime (MSVCRT.@)
1863 int _wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT__utimbuf
*t
)
1865 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
1869 int retVal
= _futime(fd
, t
);
1876 /*********************************************************************
1879 int _write(int fd
, const void* buf
, unsigned int count
)
1882 HANDLE hand
= msvcrt_fdtoh(fd
);
1884 /* Don't trace small writes, it gets *very* annoying */
1887 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1889 if (hand
== INVALID_HANDLE_VALUE
)
1891 *MSVCRT__errno() = MSVCRT_EBADF
;
1895 /* If appending, go to EOF */
1896 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
1897 _lseek(fd
, 0, FILE_END
);
1899 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
1901 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1902 && (num_written
== count
))
1904 TRACE(":failed-last error (%ld)\n",GetLastError());
1905 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1909 unsigned int i
, j
, nr_lf
;
1910 char *s
=(char*)buf
, *buf_start
=(char*)buf
, *p
;
1911 /* find number of \n ( without preceeding \r */
1912 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1917 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1922 if ((p
= MSVCRT_malloc(count
+ nr_lf
)))
1924 for(s
=(char*)buf
, i
=0, j
=0; i
<count
; i
++)
1929 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1936 FIXME("Malloc failed\n");
1944 if ((WriteFile(hand
, p
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1946 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written
);
1947 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1950 return s
- buf_start
;
1962 /*********************************************************************
1965 int MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
1968 len
= _write(file
->_file
, &val
, sizeof(val
));
1969 if (len
== sizeof(val
)) return val
;
1970 file
->_flag
|= MSVCRT__IOERR
;
1974 /*********************************************************************
1977 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1982 if (file
->_tmpfname
)
1984 MSVCRT_free(file
->_tmpfname
);
1985 file
->_tmpfname
= NULL
;
1987 /* flush stdio buffers */
1988 if(file
->_flag
& MSVCRT__IOWRT
)
1989 MSVCRT_fflush(file
);
1990 if(file
->_flag
& MSVCRT__IOMYBUF
)
1991 MSVCRT_free(file
->_base
);
1993 r
=_close(file
->_file
);
1997 return ((r
==MSVCRT_EOF
) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
2000 /*********************************************************************
2003 int MSVCRT_feof(MSVCRT_FILE
* file
)
2005 return file
->_flag
& MSVCRT__IOEOF
;
2008 /*********************************************************************
2011 int MSVCRT_ferror(MSVCRT_FILE
* file
)
2013 return file
->_flag
& MSVCRT__IOERR
;
2016 /*********************************************************************
2017 * _filbuf (MSVCRT.@)
2019 int MSVCRT__filbuf(MSVCRT_FILE
* file
)
2021 /* Allocate buffer if needed */
2022 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
2023 msvcrt_alloc_buffer(file
);
2025 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2026 if(file
->_flag
& MSVCRT__IORW
) {
2027 file
->_flag
|= MSVCRT__IOREAD
;
2032 if(file
->_flag
& MSVCRT__IONBF
) {
2035 if ((r
= _read(file
->_file
,&c
,1)) != 1) {
2036 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2041 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2043 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2048 file
->_ptr
= file
->_base
+1;
2049 return *(unsigned char *)file
->_base
;
2053 /*********************************************************************
2056 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
2060 return *(unsigned char *)file
->_ptr
++;
2062 return MSVCRT__filbuf(file
);
2066 /*********************************************************************
2067 * _fgetchar (MSVCRT.@)
2071 return MSVCRT_fgetc(MSVCRT_stdin
);
2074 /*********************************************************************
2077 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
2079 int cc
= MSVCRT_EOF
;
2080 char * buf_start
= s
;
2082 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2083 file
,file
->_file
,s
,size
);
2085 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
2090 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2092 TRACE(":nothing read\n");
2095 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
2098 TRACE(":got '%s'\n", debugstr_a(buf_start
));
2102 /*********************************************************************
2105 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
2106 * the CR from CR/LF combinations
2108 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
2112 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2116 if ((r
= _read(file
->_file
, &wc
, sizeof(wc
))) != sizeof(wc
))
2118 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2123 c
= MSVCRT_fgetc(file
);
2124 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c
))
2126 FIXME("Treat Multibyte characters\n");
2128 if (c
== MSVCRT_EOF
)
2131 return (MSVCRT_wint_t
)c
;
2134 /*********************************************************************
2137 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
2139 return MSVCRT_fgetwc(file
);
2142 /*********************************************************************
2143 * _fgetwchar (MSVCRT.@)
2145 MSVCRT_wint_t
_fgetwchar(void)
2147 return MSVCRT_fgetwc(MSVCRT_stdin
);
2150 /*********************************************************************
2151 * getwchar (MSVCRT.@)
2153 MSVCRT_wint_t
MSVCRT_getwchar(void)
2155 return _fgetwchar();
2158 /*********************************************************************
2161 MSVCRT_wchar_t
*MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2163 int cc
= MSVCRT_WEOF
;
2164 MSVCRT_wchar_t
* buf_start
= s
;
2166 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2167 file
,file
->_file
,s
,size
);
2169 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2174 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2176 TRACE(":nothing read\n");
2179 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2182 TRACE(":got %s\n", debugstr_w(buf_start
));
2186 /*********************************************************************
2189 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2191 MSVCRT_size_t wrcnt
=size
* nmemb
;
2196 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2197 memcpy(file
->_ptr
, ptr
, pcnt
);
2202 ptr
= (const char*)ptr
+ pcnt
;
2203 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2204 if(file
->_flag
& MSVCRT__IORW
) {
2205 file
->_flag
|= MSVCRT__IOWRT
;
2211 int res
=msvcrt_flush_buffer(file
);
2213 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2216 file
->_flag
|= MSVCRT__IOERR
;
2219 written
+= pwritten
;
2222 return written
/ size
;
2225 /*********************************************************************
2228 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2230 MSVCRT_wchar_t mwc
=wc
;
2231 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2236 /*********************************************************************
2237 * _fputwchar (MSVCRT.@)
2239 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
2241 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2244 /*********************************************************************
2247 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
2250 int open_flags
, stream_flags
, fd
;
2252 TRACE("(%s,%s)\n",path
,mode
);
2254 /* map mode string to open() flags. "man fopen" for possibilities. */
2255 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2258 fd
= _open(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2263 if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
) != -1)
2264 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2271 TRACE(":got (%p)\n",file
);
2277 /*********************************************************************
2278 * _wfopen (MSVCRT.@)
2280 MSVCRT_FILE
*MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2282 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
2283 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
2284 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
2286 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2288 if (patha
&& modea
&&
2289 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2290 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2292 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
2298 msvcrt_set_errno(GetLastError());
2302 /*********************************************************************
2303 * _fsopen (MSVCRT.@)
2305 MSVCRT_FILE
* MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2307 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
2308 return MSVCRT_fopen(path
,mode
);
2311 /*********************************************************************
2312 * _wfsopen (MSVCRT.@)
2314 MSVCRT_FILE
* MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2316 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2317 debugstr_w(path
),debugstr_w(mode
),share
);
2318 return MSVCRT__wfopen(path
,mode
);
2321 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2322 int MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2324 /*********************************************************************
2327 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2334 int res
= msvcrt_flush_buffer(file
);
2335 return res
? res
: c
;
2340 return MSVCRT__flsbuf(c
, file
);
2344 /*********************************************************************
2345 * _flsbuf (MSVCRT.@)
2347 int MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2349 /* Flush output buffer */
2350 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2351 msvcrt_alloc_buffer(file
);
2353 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2354 if(file
->_flag
& MSVCRT__IORW
) {
2355 file
->_flag
|= MSVCRT__IOWRT
;
2361 int res
=msvcrt_flush_buffer(file
);
2362 return res
?res
: MSVCRT_fputc(c
, file
);
2366 len
= _write(file
->_file
, &cc
, 1);
2367 if (len
== 1) return c
;
2368 file
->_flag
|= MSVCRT__IOERR
;
2373 /*********************************************************************
2374 * _fputchar (MSVCRT.@)
2376 int _fputchar(int c
)
2378 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2381 /*********************************************************************
2384 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2385 { MSVCRT_size_t rcnt
=size
* nmemb
;
2386 MSVCRT_size_t read
=0;
2389 /* first buffered data */
2391 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2392 memcpy(ptr
, file
->_ptr
, pcnt
);
2397 ptr
= (char*)ptr
+ pcnt
;
2398 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2399 if(file
->_flag
& MSVCRT__IORW
) {
2400 file
->_flag
|= MSVCRT__IOREAD
;
2406 pread
= _read(file
->_file
,ptr
, rcnt
);
2407 /* expose feof condition in the flags
2408 * MFC tests file->_flag for feof, and doesn't not call feof())
2411 file
->_flag
|= MSVCRT__IOEOF
;
2412 else if (pread
== -1)
2414 file
->_flag
|= MSVCRT__IOERR
;
2422 /*********************************************************************
2423 * freopen (MSVCRT.@)
2426 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
2428 int open_flags
, stream_flags
, fd
;
2430 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2431 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2434 MSVCRT_fclose(file
);
2436 /* map mode string to open() flags. "man fopen" for possibilities. */
2437 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2440 fd
= _open(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2444 if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
2447 WARN(":failed-last error (%ld)\n",GetLastError());
2448 msvcrt_set_errno(GetLastError());
2454 /*********************************************************************
2455 * fsetpos (MSVCRT.@)
2457 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2459 return _lseek(file
->_file
,*pos
,SEEK_SET
);
2462 /*********************************************************************
2465 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
2470 if( file
->_flag
& MSVCRT__IOWRT
) {
2471 off
= file
->_ptr
- file
->_base
;
2476 pos
= _tell(file
->_file
);
2477 if(pos
== -1) return pos
;
2481 /*********************************************************************
2482 * fgetpos (MSVCRT.@)
2484 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2486 *pos
= MSVCRT_ftell(file
);
2487 return (*pos
== -1? -1 : 0);
2490 /*********************************************************************
2493 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2495 size_t i
, len
= strlen(s
);
2496 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2497 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2498 for (i
=0; i
<len
; i
++)
2499 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2504 /*********************************************************************
2507 int MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2509 size_t i
, len
= strlenW(s
);
2510 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2511 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2512 for (i
=0; i
<len
; i
++)
2514 if ((s
[i
] == L
'\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2516 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2522 /*********************************************************************
2523 * getchar (MSVCRT.@)
2525 int MSVCRT_getchar(void)
2527 return MSVCRT_fgetc(MSVCRT_stdin
);
2530 /*********************************************************************
2533 int MSVCRT_getc(MSVCRT_FILE
* file
)
2535 return MSVCRT_fgetc(file
);
2538 /*********************************************************************
2541 char *MSVCRT_gets(char *buf
)
2544 char * buf_start
= buf
;
2546 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2547 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2548 if(cc
!= '\r') *buf
++ = (char)cc
;
2552 TRACE("got '%s'\n", buf_start
);
2556 /*********************************************************************
2559 MSVCRT_wchar_t
* MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2562 MSVCRT_wchar_t
* ws
= buf
;
2564 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2565 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2568 *buf
++ = (MSVCRT_wchar_t
)cc
;
2572 TRACE("got '%s'\n", debugstr_w(ws
));
2576 /*********************************************************************
2579 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2581 return MSVCRT_fputc(c
, file
);
2584 /*********************************************************************
2585 * putchar (MSVCRT.@)
2587 int MSVCRT_putchar(int c
)
2589 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2592 /*********************************************************************
2595 int MSVCRT_puts(const char *s
)
2597 size_t len
= strlen(s
);
2598 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2599 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2602 /*********************************************************************
2605 int _putws(const MSVCRT_wchar_t
*s
)
2607 static const MSVCRT_wchar_t nl
= '\n';
2608 size_t len
= strlenW(s
);
2609 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2610 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2613 /*********************************************************************
2616 int MSVCRT_remove(const char *path
)
2618 TRACE("(%s)\n",path
);
2619 if (DeleteFileA(path
))
2621 TRACE(":failed (%ld)\n",GetLastError());
2622 msvcrt_set_errno(GetLastError());
2626 /*********************************************************************
2627 * _wremove (MSVCRT.@)
2629 int _wremove(const MSVCRT_wchar_t
*path
)
2631 TRACE("(%s)\n",debugstr_w(path
));
2632 if (DeleteFileW(path
))
2634 TRACE(":failed (%ld)\n",GetLastError());
2635 msvcrt_set_errno(GetLastError());
2639 /*********************************************************************
2642 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
2644 TRACE(":from %s to %s\n",oldpath
,newpath
);
2645 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2647 TRACE(":failed (%ld)\n",GetLastError());
2648 msvcrt_set_errno(GetLastError());
2652 /*********************************************************************
2653 * _wrename (MSVCRT.@)
2655 int _wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
2657 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2658 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2660 TRACE(":failed (%ld)\n",GetLastError());
2661 msvcrt_set_errno(GetLastError());
2665 /*********************************************************************
2666 * setvbuf (MSVCRT.@)
2668 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
2670 /* TODO: Check if file busy */
2672 MSVCRT_free(file
->_base
);
2676 if(mode
== MSVCRT__IOFBF
) {
2677 file
->_flag
&= ~MSVCRT__IONBF
;
2678 file
->_base
= file
->_ptr
= buf
;
2680 file
->_bufsiz
= size
;
2683 file
->_flag
|= MSVCRT__IONBF
;
2688 /*********************************************************************
2691 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
2693 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
2696 /*********************************************************************
2699 char *MSVCRT_tmpnam(char *s
)
2707 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
2708 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2709 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
2711 msvcrt_int_to_base32(unique
++, tmpstr
);
2713 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2714 GetLastError() == ERROR_FILE_NOT_FOUND
)
2720 /*********************************************************************
2721 * tmpfile (MSVCRT.@)
2723 MSVCRT_FILE
* MSVCRT_tmpfile(void)
2725 char *filename
= MSVCRT_tmpnam(NULL
);
2727 MSVCRT_FILE
* file
= NULL
;
2729 fd
= _open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
2730 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
2732 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
2737 else file
->_tmpfname
= _strdup(filename
);
2742 /*********************************************************************
2743 * vfprintf (MSVCRT.@)
2745 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
2747 char buf
[2048], *mem
= buf
;
2748 int written
, resize
= sizeof(buf
), retval
;
2749 /* There are two conventions for vsnprintf failing:
2750 * Return -1 if we truncated, or
2751 * Return the number of bytes that would have been written
2752 * The code below handles both cases
2754 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
2757 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
2760 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
2763 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2769 /*********************************************************************
2770 * vfwprintf (MSVCRT.@)
2772 * Is final char included in written (then resize is too big) or not
2773 * (then we must test for equality too)?
2775 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, va_list valist
)
2777 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
2778 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
2779 /* See vfprintf comments */
2780 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2783 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
2786 if (!(mem
= (MSVCRT_wchar_t
*)MSVCRT_malloc(resize
*sizeof(*mem
))))
2789 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2795 /*********************************************************************
2796 * vprintf (MSVCRT.@)
2798 int MSVCRT_vprintf(const char *format
, va_list valist
)
2800 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2803 /*********************************************************************
2804 * vwprintf (MSVCRT.@)
2806 int MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, va_list valist
)
2808 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2811 /*********************************************************************
2812 * fprintf (MSVCRT.@)
2814 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2818 va_start(valist
, format
);
2819 res
= MSVCRT_vfprintf(file
, format
, valist
);
2824 /*********************************************************************
2825 * fwprintf (MSVCRT.@)
2827 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
2831 va_start(valist
, format
);
2832 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2837 /*********************************************************************
2840 int MSVCRT_printf(const char *format
, ...)
2844 va_start(valist
, format
);
2845 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2850 /*********************************************************************
2853 int MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
2855 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2856 msvcrt_alloc_buffer(file
);
2859 if(file
->_ptr
>file
->_base
) {
2868 /*********************************************************************
2869 * ungetwc (MSVCRT.@)
2871 MSVCRT_wint_t
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2873 MSVCRT_wchar_t mwc
= wc
;
2874 char * pp
= (char *)&mwc
;
2876 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
2877 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
2883 /*********************************************************************
2884 * wprintf (MSVCRT.@)
2886 int MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
2890 va_start(valist
, format
);
2891 res
= MSVCRT_vwprintf(format
, valist
);
2896 /*********************************************************************
2897 * __pioinfo (MSVCRT.@)
2898 * FIXME: see MSVCRT_MAX_FILES define.
2900 ioinfo
* MSVCRT___pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
2901 &MSVCRT_fdesc
[0 * 64], &MSVCRT_fdesc
[1 * 64], &MSVCRT_fdesc
[2 * 64],
2902 &MSVCRT_fdesc
[3 * 64], &MSVCRT_fdesc
[4 * 64], &MSVCRT_fdesc
[5 * 64],
2903 &MSVCRT_fdesc
[6 * 64], &MSVCRT_fdesc
[7 * 64], &MSVCRT_fdesc
[8 * 64],
2904 &MSVCRT_fdesc
[9 * 64], &MSVCRT_fdesc
[10 * 64], &MSVCRT_fdesc
[11 * 64],
2905 &MSVCRT_fdesc
[12 * 64], &MSVCRT_fdesc
[13 * 64], &MSVCRT_fdesc
[14 * 64],
2906 &MSVCRT_fdesc
[15 * 64], &MSVCRT_fdesc
[16 * 64], &MSVCRT_fdesc
[17 * 64],
2907 &MSVCRT_fdesc
[18 * 64], &MSVCRT_fdesc
[19 * 64], &MSVCRT_fdesc
[20 * 64],
2908 &MSVCRT_fdesc
[21 * 64], &MSVCRT_fdesc
[22 * 64], &MSVCRT_fdesc
[23 * 64],
2909 &MSVCRT_fdesc
[24 * 64], &MSVCRT_fdesc
[25 * 64], &MSVCRT_fdesc
[26 * 64],
2910 &MSVCRT_fdesc
[27 * 64], &MSVCRT_fdesc
[28 * 64], &MSVCRT_fdesc
[29 * 64],
2911 &MSVCRT_fdesc
[30 * 64], &MSVCRT_fdesc
[31 * 64]
2914 /*********************************************************************
2915 * __badioinfo (MSVCRT.@)
2917 ioinfo MSVCRT___badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};