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: Make this dynamic */
64 #define MSVCRT_MAX_FILES 257
69 } MSVCRT_fdesc
[MSVCRT_MAX_FILES
];
71 MSVCRT_FILE MSVCRT__iob
[3];
73 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
74 static int MSVCRT_fdend
= 3; /* highest allocated fd */
76 /* FIXME: make this dynamic */
77 static MSVCRT_FILE
* MSVCRT_fstreams
[1024];
78 static int MSVCRT_stream_idx
;
80 /* INTERNAL: process umask */
81 static int MSVCRT_umask
= 0;
83 /* INTERNAL: Static buffer for temp file name */
84 static char MSVCRT_tmpname
[MAX_PATH
];
86 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
87 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
88 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
89 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
91 #define TOUL(x) (ULONGLONG)(x)
92 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
93 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
94 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
95 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
97 extern CRITICAL_SECTION MSVCRT_file_cs
;
98 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
99 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
101 static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64
*bufi64
, struct MSVCRT__stat
*buf
)
103 buf
->st_dev
= bufi64
->st_dev
;
104 buf
->st_ino
= bufi64
->st_ino
;
105 buf
->st_mode
= bufi64
->st_mode
;
106 buf
->st_nlink
= bufi64
->st_nlink
;
107 buf
->st_uid
= bufi64
->st_uid
;
108 buf
->st_gid
= bufi64
->st_gid
;
109 buf
->st_rdev
= bufi64
->st_rdev
;
110 buf
->st_size
= bufi64
->st_size
;
111 buf
->st_atime
= bufi64
->st_atime
;
112 buf
->st_mtime
= bufi64
->st_mtime
;
113 buf
->st_ctime
= bufi64
->st_ctime
;
116 static inline BOOL
msvcrt_is_valid_fd(int fd
)
118 return fd
>= 0 && fd
< MSVCRT_fdend
&& (MSVCRT_fdesc
[fd
].wxflag
& WX_OPEN
);
121 /* INTERNAL: Get the HANDLE for a fd */
122 static HANDLE
msvcrt_fdtoh(int fd
)
124 if (!msvcrt_is_valid_fd(fd
))
126 WARN(":fd (%d) - no handle!\n",fd
);
127 *MSVCRT___doserrno() = 0;
128 *MSVCRT__errno() = MSVCRT_EBADF
;
129 return INVALID_HANDLE_VALUE
;
131 if (MSVCRT_fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
132 return MSVCRT_fdesc
[fd
].handle
;
135 /* INTERNAL: free a file entry fd */
136 static void msvcrt_free_fd(int fd
)
138 MSVCRT_fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
139 MSVCRT_fdesc
[fd
].wxflag
= 0;
140 TRACE(":fd (%d) freed\n",fd
);
141 if (fd
< 3) /* don't use 0,1,2 for user files */
145 case 0: SetStdHandle(STD_INPUT_HANDLE
, NULL
); break;
146 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, NULL
); break;
147 case 2: SetStdHandle(STD_ERROR_HANDLE
, NULL
); break;
152 if (fd
== MSVCRT_fdend
- 1)
154 if (fd
< MSVCRT_fdstart
)
159 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
160 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
162 int fd
= MSVCRT_fdstart
;
164 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fd
);
165 if (fd
>= MSVCRT_MAX_FILES
)
167 WARN(":files exhausted!\n");
170 MSVCRT_fdesc
[fd
].handle
= hand
;
171 MSVCRT_fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
173 /* locate next free slot */
174 if (fd
== MSVCRT_fdend
)
175 MSVCRT_fdstart
= ++MSVCRT_fdend
;
177 while (MSVCRT_fdstart
< MSVCRT_fdend
&&
178 MSVCRT_fdesc
[MSVCRT_fdstart
].handle
!= INVALID_HANDLE_VALUE
)
183 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
184 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
185 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
191 /* INTERNAL: Allocate a FILE* for an fd slot
193 static MSVCRT_FILE
* msvcrt_alloc_fp(void)
197 for (i
= 3; i
< sizeof(MSVCRT_fstreams
) / sizeof(MSVCRT_fstreams
[0]); i
++)
199 if (!MSVCRT_fstreams
[i
] || MSVCRT_fstreams
[i
]->_flag
== 0)
201 if (!MSVCRT_fstreams
[i
])
203 if (!(MSVCRT_fstreams
[i
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
205 if (i
== MSVCRT_stream_idx
) MSVCRT_stream_idx
++;
207 return MSVCRT_fstreams
[i
];
213 /* INTERNAL: initialize a FILE* from an open fd */
214 static int msvcrt_init_fp(MSVCRT_FILE
* file
, int fd
, unsigned stream_flags
)
216 TRACE(":fd (%d) allocating FILE*\n",fd
);
217 if (!msvcrt_is_valid_fd(fd
))
219 WARN(":invalid fd %d\n",fd
);
220 *MSVCRT___doserrno() = 0;
221 *MSVCRT__errno() = MSVCRT_EBADF
;
224 memset(file
, 0, sizeof(*file
));
226 file
->_flag
= stream_flags
;
228 TRACE(":got FILE* (%p)\n",file
);
232 /* INTERNAL: Create an inheritance data block (for spawned process)
233 * The inheritance block is made of:
234 * 00 int nb of file descriptor (NBFD)
235 * 04 char file flags (wxflag): repeated for each fd
236 * 4+NBFD HANDLE file handle: repeated for each fd
238 unsigned msvcrt_create_io_inherit_block(STARTUPINFOA
* si
)
244 si
->cbReserved2
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * MSVCRT_fdend
;
245 si
->lpReserved2
= MSVCRT_calloc(si
->cbReserved2
, 1);
246 if (!si
->lpReserved2
)
251 wxflag_ptr
= (char*)si
->lpReserved2
+ sizeof(unsigned);
252 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
254 *(unsigned*)si
->lpReserved2
= MSVCRT_fdend
;
255 for (fd
= 0; fd
< MSVCRT_fdend
; fd
++)
257 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
258 if ((MSVCRT_fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
260 *wxflag_ptr
= MSVCRT_fdesc
[fd
].wxflag
;
261 *handle_ptr
= MSVCRT_fdesc
[fd
].handle
;
266 *handle_ptr
= INVALID_HANDLE_VALUE
;
268 wxflag_ptr
++; handle_ptr
++;
273 /* INTERNAL: Set up all file descriptors,
274 * as well as default streams (stdin, stderr and stdout)
276 void msvcrt_init_io(void)
281 GetStartupInfoA(&si
);
282 if (si
.cbReserved2
!= 0 && si
.lpReserved2
!= NULL
)
287 MSVCRT_fdend
= *(unsigned*)si
.lpReserved2
;
289 wxflag_ptr
= (char*)(si
.lpReserved2
+ sizeof(unsigned));
290 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ MSVCRT_fdend
* sizeof(char));
292 MSVCRT_fdend
= min(MSVCRT_fdend
, sizeof(MSVCRT_fdesc
) / sizeof(MSVCRT_fdesc
[0]));
293 for (i
= 0; i
< MSVCRT_fdend
; i
++)
295 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
297 MSVCRT_fdesc
[i
].wxflag
= *wxflag_ptr
;
298 MSVCRT_fdesc
[i
].handle
= *handle_ptr
;
302 MSVCRT_fdesc
[i
].wxflag
= 0;
303 MSVCRT_fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
305 wxflag_ptr
++; handle_ptr
++;
307 for (MSVCRT_fdstart
= 3; MSVCRT_fdstart
< MSVCRT_fdend
; MSVCRT_fdstart
++)
308 if (MSVCRT_fdesc
[MSVCRT_fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
311 if (!(MSVCRT_fdesc
[0].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
313 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE
),
314 GetCurrentProcess(), &MSVCRT_fdesc
[0].handle
, 0, FALSE
,
315 DUPLICATE_SAME_ACCESS
);
316 MSVCRT_fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
318 if (!(MSVCRT_fdesc
[1].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
320 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE
),
321 GetCurrentProcess(), &MSVCRT_fdesc
[1].handle
, 0, FALSE
,
322 DUPLICATE_SAME_ACCESS
);
323 MSVCRT_fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
325 if (!(MSVCRT_fdesc
[2].wxflag
& WX_OPEN
) || MSVCRT_fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
327 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE
),
328 GetCurrentProcess(), &MSVCRT_fdesc
[2].handle
, 0, FALSE
,
329 DUPLICATE_SAME_ACCESS
);
330 MSVCRT_fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
333 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc
[0].handle
,
334 MSVCRT_fdesc
[1].handle
,MSVCRT_fdesc
[2].handle
);
336 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
337 for (i
= 0; i
< 3; i
++)
339 /* FILE structs for stdin/out/err are static and never deleted */
340 MSVCRT_fstreams
[i
] = &MSVCRT__iob
[i
];
341 MSVCRT__iob
[i
]._file
= i
;
342 MSVCRT__iob
[i
]._tmpfname
= NULL
;
343 MSVCRT__iob
[i
]._flag
= (i
== 0) ? MSVCRT__IOREAD
: MSVCRT__IOWRT
;
345 MSVCRT_stream_idx
= 3;
348 /* INTERNAL: Flush stdio file buffer */
349 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
352 int cnt
=file
->_ptr
-file
->_base
;
353 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
354 file
->_flag
|= MSVCRT__IOERR
;
357 file
->_ptr
=file
->_base
;
358 file
->_cnt
=file
->_bufsiz
;
363 /* INTERNAL: Allocate stdio file buffer */
364 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
366 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
368 file
->_bufsiz
= MSVCRT_BUFSIZ
;
369 file
->_flag
|= MSVCRT__IOMYBUF
;
371 file
->_base
= (unsigned char *)(&file
->_charbuf
);
373 file
->_bufsiz
= sizeof(file
->_charbuf
);
375 file
->_ptr
= file
->_base
;
379 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
380 static void msvcrt_int_to_base32(int num
, char *str
)
395 *p
= (num
& 31) + '0';
397 *p
+= ('a' - '0' - 10);
402 /*********************************************************************
405 MSVCRT_FILE
*__p__iob(void)
407 return &MSVCRT__iob
[0];
410 /*********************************************************************
413 int _access(const char *filename
, int mode
)
415 DWORD attr
= GetFileAttributesA(filename
);
417 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
419 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
421 msvcrt_set_errno(GetLastError());
424 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
426 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
432 /*********************************************************************
433 * _waccess (MSVCRT.@)
435 int _waccess(const MSVCRT_wchar_t
*filename
, int mode
)
437 DWORD attr
= GetFileAttributesW(filename
);
439 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
441 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
443 msvcrt_set_errno(GetLastError());
446 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
448 msvcrt_set_errno(ERROR_ACCESS_DENIED
);
454 /*********************************************************************
457 int _chmod(const char *path
, int flags
)
459 DWORD oldFlags
= GetFileAttributesA(path
);
461 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
463 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
464 oldFlags
| FILE_ATTRIBUTE_READONLY
;
466 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
469 msvcrt_set_errno(GetLastError());
473 /*********************************************************************
476 int _wchmod(const MSVCRT_wchar_t
*path
, int flags
)
478 DWORD oldFlags
= GetFileAttributesW(path
);
480 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
482 DWORD newFlags
= (flags
& MSVCRT__S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
483 oldFlags
| FILE_ATTRIBUTE_READONLY
;
485 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
488 msvcrt_set_errno(GetLastError());
492 /*********************************************************************
495 int _chsize(int fd
, long size
)
497 FIXME("(fd=%d, size=%ld): stub\n", fd
, size
);
501 /*********************************************************************
506 FIXME("(od=%d): stub\n", od
);
510 /*********************************************************************
513 int _dup2(int od
, int nd
)
515 FIXME("(od=%d, nd=%d): stub\n", od
, nd
);
519 /*********************************************************************
522 int _unlink(const char *path
)
524 TRACE("(%s)\n",path
);
525 if(DeleteFileA(path
))
527 TRACE("failed (%ld)\n",GetLastError());
528 msvcrt_set_errno(GetLastError());
532 /*********************************************************************
533 * _wunlink (MSVCRT.@)
535 int _wunlink(const MSVCRT_wchar_t
*path
)
537 TRACE("(%s)\n",debugstr_w(path
));
538 if(DeleteFileW(path
))
540 TRACE("failed (%ld)\n",GetLastError());
541 msvcrt_set_errno(GetLastError());
545 /* _flushall calls MSVCRT_fflush which calls _flushall */
546 int MSVCRT_fflush(MSVCRT_FILE
* file
);
548 /*********************************************************************
549 * _flushall (MSVCRT.@)
553 int i
, num_flushed
= 0;
555 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
556 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
)
559 /* FIXME: flush, do not commit */
560 if (_commit(i
) == -1)
561 if (MSVCRT_fstreams
[i
])
562 MSVCRT_fstreams
[i
]->_flag
|= MSVCRT__IOERR
;
564 if(MSVCRT_fstreams
[i
]->_flag
& MSVCRT__IOWRT
) {
565 MSVCRT_fflush(MSVCRT_fstreams
[i
]);
570 TRACE(":flushed (%d) handles\n",num_flushed
);
574 /*********************************************************************
577 int MSVCRT_fflush(MSVCRT_FILE
* file
)
583 int res
=msvcrt_flush_buffer(file
);
588 /*********************************************************************
593 HANDLE hand
= msvcrt_fdtoh(fd
);
595 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
596 if (hand
== INVALID_HANDLE_VALUE
)
599 if (!CloseHandle(hand
))
601 WARN(":failed-last error (%ld)\n",GetLastError());
602 msvcrt_set_errno(GetLastError());
610 /*********************************************************************
615 HANDLE hand
= msvcrt_fdtoh(fd
);
617 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
618 if (hand
== INVALID_HANDLE_VALUE
)
621 if (!FlushFileBuffers(hand
))
623 if (GetLastError() == ERROR_INVALID_HANDLE
)
625 /* FlushFileBuffers fails for console handles
626 * so we ignore this error.
630 TRACE(":failed-last error (%ld)\n",GetLastError());
631 msvcrt_set_errno(GetLastError());
638 /*********************************************************************
643 DWORD curpos
,endpos
,hcurpos
,hendpos
;
644 HANDLE hand
= msvcrt_fdtoh(fd
);
646 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
648 if (hand
== INVALID_HANDLE_VALUE
)
651 if (MSVCRT_fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
653 /* Otherwise we do it the hard way */
654 hcurpos
= hendpos
= 0;
655 curpos
= SetFilePointer(hand
, 0, &hcurpos
, SEEK_CUR
);
656 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
658 if (curpos
== endpos
&& hcurpos
== hendpos
)
661 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
665 /*********************************************************************
666 * _fcloseall (MSVCRT.@)
668 int MSVCRT__fcloseall(void)
670 int num_closed
= 0, i
;
672 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
673 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_flag
&&
674 MSVCRT_fclose(MSVCRT_fstreams
[i
]))
677 TRACE(":closed (%d) handles\n",num_closed
);
681 /* free everything on process exit */
682 void msvcrt_free_io(void)
690 /*********************************************************************
691 * _lseeki64 (MSVCRT.@)
693 __int64
_lseeki64(int fd
, __int64 offset
, int whence
)
695 DWORD ret
, hoffset
= (DWORD
) (offset
>> 32);
696 HANDLE hand
= msvcrt_fdtoh(fd
);
698 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
699 if (hand
== INVALID_HANDLE_VALUE
)
702 if (whence
< 0 || whence
> 2)
704 *MSVCRT__errno() = MSVCRT_EINVAL
;
708 TRACE(":fd (%d) to 0x%08lx%08lx pos %s\n",
709 fd
,hoffset
,(long)offset
,
710 (whence
==SEEK_SET
)?"SEEK_SET":
711 (whence
==SEEK_CUR
)?"SEEK_CUR":
712 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
714 ret
= SetFilePointer(hand
, (long)offset
, &hoffset
, whence
);
715 if (ret
!= INVALID_SET_FILE_POINTER
|| !GetLastError())
717 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_ATEOF
;
718 /* FIXME: What if we seek _to_ EOF - is EOF set? */
720 return ((__int64
)hoffset
<< 32) | ret
;
722 TRACE(":error-last error (%ld)\n",GetLastError());
723 msvcrt_set_errno(GetLastError());
727 /*********************************************************************
730 LONG
_lseek(int fd
, LONG offset
, int whence
)
732 return _lseeki64(fd
, offset
, whence
);
735 /*********************************************************************
736 * _locking (MSVCRT.@)
738 * This is untested; the underlying LockFile doesn't work yet.
740 int _locking(int fd
, int mode
, LONG nbytes
)
744 HANDLE hand
= msvcrt_fdtoh(fd
);
746 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
747 if (hand
== INVALID_HANDLE_VALUE
)
750 if (mode
< 0 || mode
> 4)
752 *MSVCRT__errno() = MSVCRT_EINVAL
;
756 TRACE(":fd (%d) by 0x%08lx mode %s\n",
757 fd
,nbytes
,(mode
==MSVCRT__LK_UNLCK
)?"_LK_UNLCK":
758 (mode
==MSVCRT__LK_LOCK
)?"_LK_LOCK":
759 (mode
==MSVCRT__LK_NBLCK
)?"_LK_NBLCK":
760 (mode
==MSVCRT__LK_RLCK
)?"_LK_RLCK":
761 (mode
==MSVCRT__LK_NBRLCK
)?"_LK_NBRLCK":
764 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
766 FIXME ("Seek failed\n");
767 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
770 if (mode
== MSVCRT__LK_LOCK
|| mode
== MSVCRT__LK_RLCK
)
773 ret
= 1; /* just to satisfy gcc */
776 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
781 else if (mode
== MSVCRT__LK_UNLCK
)
782 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
784 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
785 /* FIXME - what about error settings? */
789 /*********************************************************************
792 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
794 /* Flush output if needed */
795 if(file
->_flag
& MSVCRT__IOWRT
)
796 msvcrt_flush_buffer(file
);
798 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
799 offset
-= file
->_cnt
;
801 /* Discard buffered input */
803 file
->_ptr
= file
->_base
;
804 /* Reset direction of i/o */
805 if(file
->_flag
& MSVCRT__IORW
) {
806 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
808 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
811 /*********************************************************************
812 * clearerr (MSVCRT.@)
814 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
816 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
817 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
820 /*********************************************************************
823 void MSVCRT_rewind(MSVCRT_FILE
* file
)
825 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
826 MSVCRT_fseek(file
, 0L, SEEK_SET
);
827 MSVCRT_clearerr(file
);
830 static int msvcrt_get_flags(const char* mode
, int *open_flags
, int* stream_flags
)
832 int plus
= strchr(mode
, '+') != NULL
;
837 *open_flags
= plus
? MSVCRT__O_RDWR
: MSVCRT__O_RDONLY
;
838 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOREAD
;
841 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_TRUNC
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
842 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
845 *open_flags
= MSVCRT__O_CREAT
| MSVCRT__O_APPEND
| (plus
? MSVCRT__O_RDWR
: MSVCRT__O_WRONLY
);
846 *stream_flags
= plus
? MSVCRT__IORW
: MSVCRT__IOWRT
;
856 *open_flags
|= MSVCRT__O_BINARY
;
857 *open_flags
&= ~MSVCRT__O_TEXT
;
860 *open_flags
|= MSVCRT__O_TEXT
;
861 *open_flags
&= ~MSVCRT__O_BINARY
;
866 FIXME(":unknown flag %c not supported\n",mode
[-1]);
871 /*********************************************************************
874 MSVCRT_FILE
* MSVCRT__fdopen(int fd
, const char *mode
)
876 int open_flags
, stream_flags
;
879 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
881 if (!(file
= msvcrt_alloc_fp())) return NULL
;
882 if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
887 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
892 /*********************************************************************
893 * _wfdopen (MSVCRT.@)
895 MSVCRT_FILE
* MSVCRT__wfdopen(int fd
, const MSVCRT_wchar_t
*mode
)
897 unsigned mlen
= strlenW(mode
);
898 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
899 MSVCRT_FILE
* file
= NULL
;
900 int open_flags
, stream_flags
;
903 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
905 if (msvcrt_get_flags(modea
, &open_flags
, &stream_flags
) == -1) return NULL
;
906 if (!(file
= msvcrt_alloc_fp())) return NULL
;
907 if (msvcrt_init_fp(file
, fd
, stream_flags
) == -1)
915 MSVCRT_rewind(file
); /* FIXME: is this needed ??? */
916 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
922 /*********************************************************************
923 * _filelength (MSVCRT.@)
925 LONG
_filelength(int fd
)
927 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
930 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
933 if (endPos
!= curPos
)
934 _lseek(fd
, curPos
, SEEK_SET
);
941 /*********************************************************************
942 * _filelengthi64 (MSVCRT.@)
944 __int64
_filelengthi64(int fd
)
946 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
949 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
952 if (endPos
!= curPos
)
953 _lseeki64(fd
, curPos
, SEEK_SET
);
960 /*********************************************************************
963 int MSVCRT__fileno(MSVCRT_FILE
* file
)
965 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
969 /*********************************************************************
970 * _fstati64 (MSVCRT.@)
972 int MSVCRT__fstati64(int fd
, struct MSVCRT__stati64
* buf
)
975 BY_HANDLE_FILE_INFORMATION hfi
;
976 HANDLE hand
= msvcrt_fdtoh(fd
);
978 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
979 if (hand
== INVALID_HANDLE_VALUE
)
984 WARN(":failed-NULL buf\n");
985 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
989 memset(&hfi
, 0, sizeof(hfi
));
990 memset(buf
, 0, sizeof(struct MSVCRT__stati64
));
991 if (!GetFileInformationByHandle(hand
, &hfi
))
993 WARN(":failed-last error (%ld)\n",GetLastError());
994 msvcrt_set_errno(ERROR_INVALID_PARAMETER
);
997 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi
.dwFileAttributes
);
998 buf
->st_nlink
= hfi
.nNumberOfLinks
;
999 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1000 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1002 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1003 buf
->st_mtime
= buf
->st_ctime
= dw
;
1007 /*********************************************************************
1010 int MSVCRT__fstat(int fd
, struct MSVCRT__stat
* buf
)
1012 struct MSVCRT__stati64 bufi64
;
1014 ret
= MSVCRT__fstati64(fd
, &bufi64
);
1016 msvcrt_cp_from_stati64(&bufi64
, buf
);
1020 /*********************************************************************
1021 * _futime (MSVCRT.@)
1023 int _futime(int fd
, struct MSVCRT__utimbuf
*t
)
1025 HANDLE hand
= msvcrt_fdtoh(fd
);
1030 MSVCRT_time_t currTime
;
1031 MSVCRT_time(&currTime
);
1032 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
1033 memcpy(&wt
, &at
, sizeof(wt
));
1037 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
1038 if (t
->actime
== t
->modtime
)
1039 memcpy(&wt
, &at
, sizeof(wt
));
1041 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
1044 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1046 msvcrt_set_errno(GetLastError());
1052 /*********************************************************************
1053 * _get_osfhandle (MSVCRT.@)
1055 long _get_osfhandle(int fd
)
1057 HANDLE hand
= msvcrt_fdtoh(fd
);
1058 HANDLE newhand
= hand
;
1059 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1061 if (hand
!= INVALID_HANDLE_VALUE
)
1063 /* FIXME: I'm not convinced that I should be copying the
1064 * handle here - it may be leaked if the app doesn't
1065 * close it (and the API docs don't say that it should)
1066 * Not duplicating it means that it can't be inherited
1067 * and so lcc's wedit doesn't cope when it passes it to
1068 * child processes. I've an idea that it should either
1069 * be copied by CreateProcess, or marked as inheritable
1070 * when initialised, or maybe both? JG 21-9-00.
1072 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
1073 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
1075 return (long)newhand
;
1078 /*********************************************************************
1079 * _isatty (MSVCRT.@)
1083 HANDLE hand
= msvcrt_fdtoh(fd
);
1085 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1086 if (hand
== INVALID_HANDLE_VALUE
)
1089 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1092 /*********************************************************************
1093 * _mktemp (MSVCRT.@)
1095 char *_mktemp(char *pattern
)
1098 char *retVal
= pattern
;
1103 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1107 id
= GetCurrentProcessId();
1111 int tempNum
= id
/ 10;
1112 *pattern
-- = id
- (tempNum
* 10) + '0';
1118 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1119 GetLastError() == ERROR_FILE_NOT_FOUND
)
1121 *pattern
= letter
++;
1122 } while(letter
!= '|');
1126 /*********************************************************************
1127 * _wmktemp (MSVCRT.@)
1129 MSVCRT_wchar_t
*_wmktemp(MSVCRT_wchar_t
*pattern
)
1132 MSVCRT_wchar_t
*retVal
= pattern
;
1134 MSVCRT_wchar_t letter
= 'a';
1137 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1141 id
= GetCurrentProcessId();
1145 int tempNum
= id
/ 10;
1146 *pattern
-- = id
- (tempNum
* 10) + '0';
1152 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1153 GetLastError() == ERROR_FILE_NOT_FOUND
)
1155 *pattern
= letter
++;
1156 } while(letter
!= '|');
1160 static unsigned split_oflags(unsigned oflags
)
1164 if (oflags
& MSVCRT__O_APPEND
) wxflags
|= WX_APPEND
;
1165 if (oflags
& MSVCRT__O_BINARY
) ;
1166 else if (oflags
& MSVCRT__O_TEXT
) wxflags
|= WX_TEXT
;
1167 else if (*__p__fmode() & MSVCRT__O_BINARY
) ;
1168 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1169 if (oflags
& MSVCRT__O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1171 if (oflags
& ~(MSVCRT__O_BINARY
|MSVCRT__O_TEXT
|MSVCRT__O_APPEND
|MSVCRT__O_TRUNC
|
1172 MSVCRT__O_EXCL
|MSVCRT__O_CREAT
|MSVCRT__O_RDWR
|MSVCRT__O_WRONLY
|
1173 MSVCRT__O_TEMPORARY
|MSVCRT__O_NOINHERIT
))
1174 ERR(":unsupported oflags 0x%04x\n",oflags
);
1179 /*********************************************************************
1182 int MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
1186 DWORD access
= 0, creation
= 0, attrib
;
1190 SECURITY_ATTRIBUTES sa
;
1193 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1194 path
, oflags
, shflags
);
1196 wxflag
= split_oflags(oflags
);
1197 switch (oflags
& (MSVCRT__O_RDONLY
| MSVCRT__O_WRONLY
| MSVCRT__O_RDWR
))
1199 case MSVCRT__O_RDONLY
: access
|= GENERIC_READ
; break;
1200 case MSVCRT__O_WRONLY
: access
|= GENERIC_WRITE
; break;
1201 case MSVCRT__O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1204 if (oflags
& MSVCRT__O_CREAT
)
1206 va_start(ap
, shflags
);
1207 pmode
= va_arg(ap
, int);
1210 if(pmode
& ~(MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
))
1211 FIXME(": pmode 0x%04x ignored\n", pmode
);
1213 WARN(": pmode 0x%04x ignored\n", pmode
);
1215 if (oflags
& MSVCRT__O_EXCL
)
1216 creation
= CREATE_NEW
;
1217 else if (oflags
& MSVCRT__O_TRUNC
)
1218 creation
= CREATE_ALWAYS
;
1220 creation
= OPEN_ALWAYS
;
1222 else /* no MSVCRT__O_CREAT */
1224 if (oflags
& MSVCRT__O_TRUNC
)
1225 creation
= TRUNCATE_EXISTING
;
1227 creation
= OPEN_EXISTING
;
1232 case MSVCRT__SH_DENYRW
:
1235 case MSVCRT__SH_DENYWR
:
1236 sharing
= FILE_SHARE_READ
;
1238 case MSVCRT__SH_DENYRD
:
1239 sharing
= FILE_SHARE_WRITE
;
1241 case MSVCRT__SH_DENYNO
:
1242 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1245 ERR( "Unhandled shflags 0x%x\n", shflags
);
1248 attrib
= FILE_ATTRIBUTE_NORMAL
;
1250 if (oflags
& MSVCRT__O_TEMPORARY
)
1252 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1254 sharing
|= FILE_SHARE_DELETE
;
1257 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1258 sa
.lpSecurityDescriptor
= NULL
;
1259 sa
.bInheritHandle
= (oflags
& MSVCRT__O_NOINHERIT
) ? FALSE
: TRUE
;
1261 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1263 if (hand
== INVALID_HANDLE_VALUE
) {
1264 WARN(":failed-last error (%ld)\n",GetLastError());
1265 msvcrt_set_errno(GetLastError());
1269 fd
= msvcrt_alloc_fd(hand
, wxflag
);
1271 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1275 if (oflags
& MSVCRT__O_APPEND
)
1276 _lseek(fd
, 0, FILE_END
);
1282 /*********************************************************************
1283 * _wsopen (MSVCRT.@)
1285 int MSVCRT__wsopen( const MSVCRT_wchar_t
* path
, int oflags
, int shflags
, ... )
1287 const unsigned int len
= strlenW(path
);
1288 char *patha
= MSVCRT_calloc(len
+ 1,1);
1292 va_start(ap
, shflags
);
1293 pmode
= va_arg(ap
, int);
1296 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1298 int retval
= MSVCRT__sopen(patha
,oflags
,shflags
,pmode
);
1303 msvcrt_set_errno(GetLastError());
1307 /*********************************************************************
1310 int _open( const char *path
, int flags
, ... )
1314 if (flags
& MSVCRT__O_CREAT
)
1317 va_start(ap
, flags
);
1318 pmode
= va_arg(ap
, int);
1320 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
, pmode
);
1323 return MSVCRT__sopen( path
, flags
, MSVCRT__SH_DENYNO
);
1326 /*********************************************************************
1329 int _wopen(const MSVCRT_wchar_t
*path
,int flags
,...)
1331 const unsigned int len
= strlenW(path
);
1332 char *patha
= MSVCRT_calloc(len
+ 1,1);
1336 va_start(ap
, flags
);
1337 pmode
= va_arg(ap
, int);
1340 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1342 int retval
= _open(patha
,flags
,pmode
);
1347 msvcrt_set_errno(GetLastError());
1351 /*********************************************************************
1354 int _creat(const char *path
, int flags
)
1356 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1357 return _open(path
, usedFlags
);
1360 /*********************************************************************
1361 * _wcreat (MSVCRT.@)
1363 int _wcreat(const MSVCRT_wchar_t
*path
, int flags
)
1365 int usedFlags
= (flags
& MSVCRT__O_TEXT
)| MSVCRT__O_CREAT
| MSVCRT__O_WRONLY
| MSVCRT__O_TRUNC
;
1366 return _wopen(path
, usedFlags
);
1369 /*********************************************************************
1370 * _open_osfhandle (MSVCRT.@)
1372 int _open_osfhandle(long handle
, int oflags
)
1376 /* MSVCRT__O_RDONLY (0) always matches, so set the read flag
1377 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1378 * file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
1379 * text - it never sets MSVCRT__O_BINARY.
1381 /* FIXME: handle more flags */
1382 if (!(oflags
& (MSVCRT__O_BINARY
| MSVCRT__O_TEXT
)) && (*__p__fmode() & MSVCRT__O_BINARY
))
1383 oflags
|= MSVCRT__O_BINARY
;
1385 oflags
|= MSVCRT__O_TEXT
;
1387 fd
= msvcrt_alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1388 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1392 /*********************************************************************
1397 int num_removed
= 0, i
;
1399 for (i
= 3; i
< MSVCRT_stream_idx
; i
++)
1400 if (MSVCRT_fstreams
[i
] && MSVCRT_fstreams
[i
]->_tmpfname
)
1402 MSVCRT_fclose(MSVCRT_fstreams
[i
]);
1407 TRACE(":removed (%d) temp files\n",num_removed
);
1411 /*********************************************************************
1412 * (internal) remove_cr
1414 * Remove all \r inplace.
1415 * return the number of \r removed
1417 static unsigned int remove_cr(char *buf
, unsigned int count
)
1421 for (i
= 0; i
< count
; i
++) if (buf
[i
] == '\r') break;
1422 for (j
= i
+ 1; j
< count
; j
++) if (buf
[j
] != '\r') buf
[i
++] = buf
[j
];
1426 /*********************************************************************
1429 int _read(int fd
, void *buf
, unsigned int count
)
1431 DWORD num_read
, all_read
= 0;
1432 char *bufstart
= buf
;
1433 HANDLE hand
= msvcrt_fdtoh(fd
);
1435 /* Don't trace small reads, it gets *very* annoying */
1437 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1438 if (hand
== INVALID_HANDLE_VALUE
)
1441 /* Reading single bytes in O_TEXT mode makes things slow
1442 * So read big chunks, then remove the \r in memory and try reading
1443 * the rest until the request is satisfied or EOF is met
1445 while (all_read
< count
)
1447 if (ReadFile(hand
, bufstart
+all_read
, count
- all_read
, &num_read
, NULL
))
1449 if (num_read
!= (count
- all_read
))
1452 MSVCRT_fdesc
[fd
].wxflag
|= WX_ATEOF
;
1453 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1454 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1455 all_read
+= num_read
;
1457 TRACE("%s\n",debugstr_an(buf
,all_read
));
1460 if (MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
)
1462 num_read
-= remove_cr(bufstart
+all_read
,num_read
);
1464 all_read
+= num_read
;
1468 TRACE(":failed-last error (%ld)\n",GetLastError());
1474 TRACE("(%lu), %s\n",all_read
,debugstr_an(buf
, all_read
));
1478 /*********************************************************************
1481 int MSVCRT__getw(MSVCRT_FILE
* file
)
1484 switch (_read(file
->_file
, &i
, sizeof(int)))
1487 case 0: file
->_flag
|= MSVCRT__IOEOF
; break;
1488 default: file
->_flag
|= MSVCRT__IOERR
; break;
1493 /*********************************************************************
1494 * _setmode (MSVCRT.@)
1496 int _setmode(int fd
,int mode
)
1498 int ret
= MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
? MSVCRT__O_TEXT
: MSVCRT__O_BINARY
;
1499 if (mode
& (~(MSVCRT__O_TEXT
|MSVCRT__O_BINARY
)))
1500 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1501 if ((mode
& MSVCRT__O_TEXT
) == MSVCRT__O_TEXT
)
1502 MSVCRT_fdesc
[fd
].wxflag
|= WX_TEXT
;
1504 MSVCRT_fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1508 /*********************************************************************
1509 * _stati64 (MSVCRT.@)
1511 int MSVCRT__stati64(const char* path
, struct MSVCRT__stati64
* buf
)
1514 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1515 unsigned short mode
= ALL_S_IREAD
;
1518 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1520 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1522 TRACE("failed (%ld)\n",GetLastError());
1523 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1527 memset(buf
,0,sizeof(struct MSVCRT__stati64
));
1529 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1530 Bon 011120: This FIXME seems incorrect
1531 Also a letter as first char isn't enough to be classified
1534 if (isalpha(*path
)&& (*(path
+1)==':'))
1535 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1537 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1539 plen
= strlen(path
);
1541 /* Dir, or regular file? */
1542 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1543 (path
[plen
-1] == '\\'))
1544 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1547 mode
|= MSVCRT__S_IFREG
;
1549 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1551 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1552 (tolower(path
[plen
-3]) << 16);
1553 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1554 mode
|= ALL_S_IEXEC
;
1558 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1559 mode
|= ALL_S_IWRITE
;
1561 buf
->st_mode
= mode
;
1563 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1564 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1566 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1567 buf
->st_mtime
= buf
->st_ctime
= dw
;
1568 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1569 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1570 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1574 /*********************************************************************
1577 int MSVCRT__stat(const char* path
, struct MSVCRT__stat
* buf
)
1579 struct MSVCRT__stati64 bufi64
;
1581 ret
= MSVCRT__stati64( path
, &bufi64
);
1583 msvcrt_cp_from_stati64(&bufi64
, buf
);
1587 /*********************************************************************
1588 * _wstati64 (MSVCRT.@)
1590 int MSVCRT__wstati64(const MSVCRT_wchar_t
* path
, struct MSVCRT__stati64
* buf
)
1593 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1594 unsigned short mode
= ALL_S_IREAD
;
1597 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1599 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1601 TRACE("failed (%ld)\n",GetLastError());
1602 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1606 memset(buf
,0,sizeof(struct MSVCRT__stat
));
1608 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1609 if (MSVCRT_iswalpha(*path
))
1610 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1612 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1614 plen
= strlenW(path
);
1616 /* Dir, or regular file? */
1617 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1618 (path
[plen
-1] == '\\'))
1619 mode
|= (MSVCRT__S_IFDIR
| ALL_S_IEXEC
);
1622 mode
|= MSVCRT__S_IFREG
;
1624 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1626 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1627 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1628 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1629 mode
|= ALL_S_IEXEC
;
1633 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1634 mode
|= ALL_S_IWRITE
;
1636 buf
->st_mode
= mode
;
1638 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1639 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1641 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1642 buf
->st_mtime
= buf
->st_ctime
= dw
;
1643 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1644 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1645 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1649 /*********************************************************************
1652 int MSVCRT__wstat(const MSVCRT_wchar_t
* path
, struct MSVCRT__stat
* buf
)
1655 struct MSVCRT__stati64 bufi64
;
1657 ret
= MSVCRT__wstati64( path
, &bufi64
);
1658 if (!ret
) msvcrt_cp_from_stati64(&bufi64
, buf
);
1662 /*********************************************************************
1667 return _lseek(fd
, 0, SEEK_CUR
);
1670 /*********************************************************************
1671 * _telli64 (MSVCRT.@)
1673 __int64
_telli64(int fd
)
1675 return _lseeki64(fd
, 0, SEEK_CUR
);
1678 /*********************************************************************
1679 * _tempnam (MSVCRT.@)
1681 char *_tempnam(const char *dir
, const char *prefix
)
1683 char tmpbuf
[MAX_PATH
];
1685 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1686 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1688 TRACE("got name (%s)\n",tmpbuf
);
1689 DeleteFileA(tmpbuf
);
1690 return _strdup(tmpbuf
);
1692 TRACE("failed (%ld)\n",GetLastError());
1696 /*********************************************************************
1697 * _wtempnam (MSVCRT.@)
1699 MSVCRT_wchar_t
*_wtempnam(const MSVCRT_wchar_t
*dir
, const MSVCRT_wchar_t
*prefix
)
1701 MSVCRT_wchar_t tmpbuf
[MAX_PATH
];
1703 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1704 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1706 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1707 DeleteFileW(tmpbuf
);
1708 return _wcsdup(tmpbuf
);
1710 TRACE("failed (%ld)\n",GetLastError());
1714 /*********************************************************************
1717 int _umask(int umask
)
1719 int old_umask
= MSVCRT_umask
;
1720 TRACE("(%d)\n",umask
);
1721 MSVCRT_umask
= umask
;
1725 /*********************************************************************
1728 int _utime(const char* path
, struct MSVCRT__utimbuf
*t
)
1730 int fd
= _open(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
1734 int retVal
= _futime(fd
, t
);
1741 /*********************************************************************
1742 * _wutime (MSVCRT.@)
1744 int _wutime(const MSVCRT_wchar_t
* path
, struct MSVCRT__utimbuf
*t
)
1746 int fd
= _wopen(path
, MSVCRT__O_WRONLY
| MSVCRT__O_BINARY
);
1750 int retVal
= _futime(fd
, t
);
1757 /*********************************************************************
1760 int _write(int fd
, const void* buf
, unsigned int count
)
1763 HANDLE hand
= msvcrt_fdtoh(fd
);
1765 /* Don't trace small writes, it gets *very* annoying */
1768 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1770 if (hand
== INVALID_HANDLE_VALUE
)
1772 *MSVCRT__errno() = MSVCRT_EBADF
;
1776 /* If appending, go to EOF */
1777 if (MSVCRT_fdesc
[fd
].wxflag
& WX_APPEND
)
1778 _lseek(fd
, 0, FILE_END
);
1780 if (!(MSVCRT_fdesc
[fd
].wxflag
& WX_TEXT
))
1782 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1783 && (num_written
== count
))
1785 TRACE(":failed-last error (%ld)\n",GetLastError());
1786 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1790 unsigned int i
, j
, nr_lf
;
1791 char *s
=(char*)buf
, *buf_start
=(char*)buf
, *p
;
1792 /* find number of \n ( without preceeding \r */
1793 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
1798 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
1803 if ((p
= MSVCRT_malloc(count
+ nr_lf
)))
1805 for(s
=(char*)buf
, i
=0, j
=0; i
<count
; i
++)
1810 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
1817 FIXME("Malloc failed\n");
1825 if ((WriteFile(hand
, p
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
1827 TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written
);
1828 *MSVCRT__errno() = MSVCRT_ENOSPC
;
1831 return s
- buf_start
;
1843 /*********************************************************************
1846 int MSVCRT__putw(int val
, MSVCRT_FILE
* file
)
1849 len
= _write(file
->_file
, &val
, sizeof(val
));
1850 if (len
== sizeof(val
)) return val
;
1851 file
->_flag
|= MSVCRT__IOERR
;
1855 /*********************************************************************
1858 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1863 if (file
->_tmpfname
)
1865 MSVCRT_free(file
->_tmpfname
);
1866 file
->_tmpfname
= NULL
;
1868 /* flush stdio buffers */
1869 if(file
->_flag
& MSVCRT__IOWRT
)
1870 MSVCRT_fflush(file
);
1871 if(file
->_flag
& MSVCRT__IOMYBUF
)
1872 MSVCRT_free(file
->_base
);
1874 r
=_close(file
->_file
);
1878 return ((r
==MSVCRT_EOF
) || (flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
1881 /*********************************************************************
1884 int MSVCRT_feof(MSVCRT_FILE
* file
)
1886 return file
->_flag
& MSVCRT__IOEOF
;
1889 /*********************************************************************
1892 int MSVCRT_ferror(MSVCRT_FILE
* file
)
1894 return file
->_flag
& MSVCRT__IOERR
;
1897 /*********************************************************************
1898 * _filbuf (MSVCRT.@)
1900 int MSVCRT__filbuf(MSVCRT_FILE
* file
)
1902 /* Allocate buffer if needed */
1903 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
1904 msvcrt_alloc_buffer(file
);
1906 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
1907 if(file
->_flag
& MSVCRT__IORW
) {
1908 file
->_flag
|= MSVCRT__IOREAD
;
1913 if(file
->_flag
& MSVCRT__IONBF
) {
1916 if ((r
= _read(file
->_file
,&c
,1)) != 1) {
1917 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
1922 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
1924 file
->_flag
|= (file
->_cnt
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
1929 file
->_ptr
= file
->_base
+1;
1930 return *(unsigned char *)file
->_base
;
1934 /*********************************************************************
1937 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
1941 return *(unsigned char *)file
->_ptr
++;
1943 return MSVCRT__filbuf(file
);
1947 /*********************************************************************
1948 * _fgetchar (MSVCRT.@)
1952 return MSVCRT_fgetc(MSVCRT_stdin
);
1955 /*********************************************************************
1958 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1960 int cc
= MSVCRT_EOF
;
1961 char * buf_start
= s
;
1963 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1964 file
,file
->_file
,s
,size
);
1966 while ((size
>1) && (cc
= MSVCRT_fgetc(file
)) != MSVCRT_EOF
&& cc
!= '\n')
1971 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1973 TRACE(":nothing read\n");
1976 if ((cc
!= MSVCRT_EOF
) && (size
> 1))
1979 TRACE(":got '%s'\n", debugstr_a(buf_start
));
1983 /*********************************************************************
1986 * In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
1987 * the CR from CR/LF combinations
1989 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1993 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
1997 if ((r
= _read(file
->_file
, &wc
, sizeof(wc
))) != sizeof(wc
))
1999 file
->_flag
|= (r
== 0) ? MSVCRT__IOEOF
: MSVCRT__IOERR
;
2004 c
= MSVCRT_fgetc(file
);
2005 if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c
))
2007 FIXME("Treat Multibyte characters\n");
2009 if (c
== MSVCRT_EOF
)
2012 return (MSVCRT_wint_t
)c
;
2015 /*********************************************************************
2018 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
2020 return MSVCRT_fgetwc(file
);
2023 /*********************************************************************
2024 * _fgetwchar (MSVCRT.@)
2026 MSVCRT_wint_t
_fgetwchar(void)
2028 return MSVCRT_fgetwc(MSVCRT_stdin
);
2031 /*********************************************************************
2032 * getwchar (MSVCRT.@)
2034 MSVCRT_wint_t
MSVCRT_getwchar(void)
2036 return _fgetwchar();
2039 /*********************************************************************
2042 MSVCRT_wchar_t
*MSVCRT_fgetws(MSVCRT_wchar_t
*s
, int size
, MSVCRT_FILE
* file
)
2044 int cc
= MSVCRT_WEOF
;
2045 MSVCRT_wchar_t
* buf_start
= s
;
2047 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2048 file
,file
->_file
,s
,size
);
2050 while ((size
>1) && (cc
= MSVCRT_fgetwc(file
)) != MSVCRT_WEOF
&& cc
!= '\n')
2055 if ((cc
== MSVCRT_WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2057 TRACE(":nothing read\n");
2060 if ((cc
!= MSVCRT_WEOF
) && (size
> 1))
2063 TRACE(":got %s\n", debugstr_w(buf_start
));
2067 /*********************************************************************
2070 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2072 MSVCRT_size_t wrcnt
=size
* nmemb
;
2077 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2078 memcpy(file
->_ptr
, ptr
, pcnt
);
2083 ptr
= (const char*)ptr
+ pcnt
;
2084 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2085 if(file
->_flag
& MSVCRT__IORW
) {
2086 file
->_flag
|= MSVCRT__IOWRT
;
2092 int res
=msvcrt_flush_buffer(file
);
2094 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2097 file
->_flag
|= MSVCRT__IOERR
;
2100 written
+= pwritten
;
2103 return written
/ size
;
2106 /*********************************************************************
2109 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2111 MSVCRT_wchar_t mwc
=wc
;
2112 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2117 /*********************************************************************
2118 * _fputwchar (MSVCRT.@)
2120 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
2122 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
2125 /*********************************************************************
2128 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
2131 int open_flags
, stream_flags
, fd
;
2133 TRACE("(%s,%s)\n",path
,mode
);
2135 /* map mode string to open() flags. "man fopen" for possibilities. */
2136 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2139 fd
= _open(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2144 if ((file
= msvcrt_alloc_fp()) && msvcrt_init_fp(file
, fd
, stream_flags
) != -1)
2145 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2152 TRACE(":got (%p)\n",file
);
2158 /*********************************************************************
2159 * _wfopen (MSVCRT.@)
2161 MSVCRT_FILE
*MSVCRT__wfopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
)
2163 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
2164 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
2165 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
2167 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2169 if (patha
&& modea
&&
2170 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2171 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2173 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
2179 msvcrt_set_errno(GetLastError());
2183 /*********************************************************************
2184 * _fsopen (MSVCRT.@)
2186 MSVCRT_FILE
* MSVCRT__fsopen(const char *path
, const char *mode
, int share
)
2188 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
2189 return MSVCRT_fopen(path
,mode
);
2192 /*********************************************************************
2193 * _wfsopen (MSVCRT.@)
2195 MSVCRT_FILE
* MSVCRT__wfsopen(const MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*mode
, int share
)
2197 FIXME(":(%s,%s,%d),ignoring share mode!\n",
2198 debugstr_w(path
),debugstr_w(mode
),share
);
2199 return MSVCRT__wfopen(path
,mode
);
2202 /* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
2203 int MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
);
2205 /*********************************************************************
2208 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
2215 return MSVCRT__flsbuf(c
, file
);
2219 /*********************************************************************
2220 * _flsbuf (MSVCRT.@)
2222 int MSVCRT__flsbuf(int c
, MSVCRT_FILE
* file
)
2224 /* Flush output buffer */
2225 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2226 msvcrt_alloc_buffer(file
);
2228 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
2229 if(file
->_flag
& MSVCRT__IORW
) {
2230 file
->_flag
|= MSVCRT__IOWRT
;
2236 int res
=msvcrt_flush_buffer(file
);
2237 return res
?res
: MSVCRT_fputc(c
, file
);
2241 len
= _write(file
->_file
, &cc
, 1);
2242 if (len
== 1) return c
;
2243 file
->_flag
|= MSVCRT__IOERR
;
2248 /*********************************************************************
2249 * _fputchar (MSVCRT.@)
2251 int _fputchar(int c
)
2253 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2256 /*********************************************************************
2259 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
2260 { MSVCRT_size_t rcnt
=size
* nmemb
;
2261 MSVCRT_size_t read
=0;
2264 /* first buffered data */
2266 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2267 memcpy(ptr
, file
->_ptr
, pcnt
);
2272 ptr
= (char*)ptr
+ pcnt
;
2273 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
2274 if(file
->_flag
& MSVCRT__IORW
) {
2275 file
->_flag
|= MSVCRT__IOREAD
;
2281 pread
= _read(file
->_file
,ptr
, rcnt
);
2282 /* expose feof condition in the flags
2283 * MFC tests file->_flag for feof, and doesn't not call feof())
2286 file
->_flag
|= MSVCRT__IOEOF
;
2287 else if (pread
== -1)
2289 file
->_flag
|= MSVCRT__IOERR
;
2297 /*********************************************************************
2298 * freopen (MSVCRT.@)
2301 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
2303 int open_flags
, stream_flags
, fd
;
2305 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2306 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
2309 MSVCRT_fclose(file
);
2311 /* map mode string to open() flags. "man fopen" for possibilities. */
2312 if (msvcrt_get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2315 fd
= _open(path
, open_flags
, MSVCRT__S_IREAD
| MSVCRT__S_IWRITE
);
2319 if (msvcrt_init_fp(file
, fd
, stream_flags
) != -1)
2322 WARN(":failed-last error (%ld)\n",GetLastError());
2323 msvcrt_set_errno(GetLastError());
2329 /*********************************************************************
2330 * fsetpos (MSVCRT.@)
2332 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2334 return _lseek(file
->_file
,*pos
,SEEK_SET
);
2337 /*********************************************************************
2340 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
2345 if( file
->_flag
& MSVCRT__IOWRT
) {
2346 off
= file
->_ptr
- file
->_base
;
2351 pos
= _tell(file
->_file
);
2352 if(pos
== -1) return pos
;
2356 /*********************************************************************
2357 * fgetpos (MSVCRT.@)
2359 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
2361 *pos
= MSVCRT_ftell(file
);
2362 return (*pos
== -1? -1 : 0);
2365 /*********************************************************************
2368 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
2370 size_t i
, len
= strlen(s
);
2371 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2372 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2373 for (i
=0; i
<len
; i
++)
2374 if (MSVCRT_fputc(s
[i
], file
) == MSVCRT_EOF
)
2379 /*********************************************************************
2382 int MSVCRT_fputws(const MSVCRT_wchar_t
*s
, MSVCRT_FILE
* file
)
2384 size_t i
, len
= strlenW(s
);
2385 if (!(MSVCRT_fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2386 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2387 for (i
=0; i
<len
; i
++)
2389 if ((s
[i
] == L
'\n') && (MSVCRT_fputc('\r', file
) == MSVCRT_EOF
))
2391 if (MSVCRT_fputwc(s
[i
], file
) == MSVCRT_WEOF
)
2397 /*********************************************************************
2398 * getchar (MSVCRT.@)
2400 int MSVCRT_getchar(void)
2402 return MSVCRT_fgetc(MSVCRT_stdin
);
2405 /*********************************************************************
2408 int MSVCRT_getc(MSVCRT_FILE
* file
)
2410 return MSVCRT_fgetc(file
);
2413 /*********************************************************************
2416 char *MSVCRT_gets(char *buf
)
2419 char * buf_start
= buf
;
2421 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2422 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2423 if(cc
!= '\r') *buf
++ = (char)cc
;
2427 TRACE("got '%s'\n", buf_start
);
2431 /*********************************************************************
2434 MSVCRT_wchar_t
* MSVCRT__getws(MSVCRT_wchar_t
* buf
)
2437 MSVCRT_wchar_t
* ws
= buf
;
2439 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2440 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2443 *buf
++ = (MSVCRT_wchar_t
)cc
;
2447 TRACE("got '%s'\n", debugstr_w(ws
));
2451 /*********************************************************************
2454 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2456 return MSVCRT_fputc(c
, file
);
2459 /*********************************************************************
2460 * putchar (MSVCRT.@)
2462 int MSVCRT_putchar(int c
)
2464 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2467 /*********************************************************************
2470 int MSVCRT_puts(const char *s
)
2472 size_t len
= strlen(s
);
2473 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2474 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2477 /*********************************************************************
2480 int _putws(const MSVCRT_wchar_t
*s
)
2482 static const MSVCRT_wchar_t nl
= '\n';
2483 size_t len
= strlenW(s
);
2484 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2485 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2488 /*********************************************************************
2491 int MSVCRT_remove(const char *path
)
2493 TRACE("(%s)\n",path
);
2494 if (DeleteFileA(path
))
2496 TRACE(":failed (%ld)\n",GetLastError());
2497 msvcrt_set_errno(GetLastError());
2501 /*********************************************************************
2502 * _wremove (MSVCRT.@)
2504 int _wremove(const MSVCRT_wchar_t
*path
)
2506 TRACE("(%s)\n",debugstr_w(path
));
2507 if (DeleteFileW(path
))
2509 TRACE(":failed (%ld)\n",GetLastError());
2510 msvcrt_set_errno(GetLastError());
2514 /*********************************************************************
2517 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
2519 TRACE(":from %s to %s\n",oldpath
,newpath
);
2520 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2522 TRACE(":failed (%ld)\n",GetLastError());
2523 msvcrt_set_errno(GetLastError());
2527 /*********************************************************************
2528 * _wrename (MSVCRT.@)
2530 int _wrename(const MSVCRT_wchar_t
*oldpath
,const MSVCRT_wchar_t
*newpath
)
2532 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2533 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2535 TRACE(":failed (%ld)\n",GetLastError());
2536 msvcrt_set_errno(GetLastError());
2540 /*********************************************************************
2541 * setvbuf (MSVCRT.@)
2543 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
2545 /* TODO: Check if file busy */
2547 MSVCRT_free(file
->_base
);
2551 if(mode
== MSVCRT__IOFBF
) {
2552 file
->_flag
&= ~MSVCRT__IONBF
;
2553 file
->_base
= file
->_ptr
= buf
;
2555 file
->_bufsiz
= size
;
2558 file
->_flag
|= MSVCRT__IONBF
;
2563 /*********************************************************************
2566 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
2568 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
2571 /*********************************************************************
2574 char *MSVCRT_tmpnam(char *s
)
2582 msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr
);
2583 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
2584 for (count
= 0; count
< MSVCRT_TMP_MAX
; count
++)
2586 msvcrt_int_to_base32(unique
++, tmpstr
);
2588 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
2589 GetLastError() == ERROR_FILE_NOT_FOUND
)
2595 /*********************************************************************
2596 * tmpfile (MSVCRT.@)
2598 MSVCRT_FILE
* MSVCRT_tmpfile(void)
2600 char *filename
= MSVCRT_tmpnam(NULL
);
2602 MSVCRT_FILE
* file
= NULL
;
2604 fd
= _open(filename
, MSVCRT__O_CREAT
| MSVCRT__O_BINARY
| MSVCRT__O_RDWR
| MSVCRT__O_TEMPORARY
);
2605 if (fd
!= -1 && (file
= msvcrt_alloc_fp()))
2607 if (msvcrt_init_fp(file
, fd
, MSVCRT__O_RDWR
) == -1)
2612 else file
->_tmpfname
= _strdup(filename
);
2617 /*********************************************************************
2618 * vfprintf (MSVCRT.@)
2620 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
2622 char buf
[2048], *mem
= buf
;
2623 int written
, resize
= sizeof(buf
), retval
;
2624 /* There are two conventions for vsnprintf failing:
2625 * Return -1 if we truncated, or
2626 * Return the number of bytes that would have been written
2627 * The code below handles both cases
2629 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
2632 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
2635 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
2638 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2644 /*********************************************************************
2645 * vfwprintf (MSVCRT.@)
2647 * Is final char included in written (then resize is too big) or not
2648 * (then we must test for equality too)?
2650 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, va_list valist
)
2652 MSVCRT_wchar_t buf
[2048], *mem
= buf
;
2653 int written
, resize
= sizeof(buf
) / sizeof(MSVCRT_wchar_t
), retval
;
2654 /* See vfprintf comments */
2655 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2658 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(MSVCRT_wchar_t
));
2661 if (!(mem
= (MSVCRT_wchar_t
*)MSVCRT_malloc(resize
*sizeof(*mem
))))
2664 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2670 /*********************************************************************
2671 * vprintf (MSVCRT.@)
2673 int MSVCRT_vprintf(const char *format
, va_list valist
)
2675 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2678 /*********************************************************************
2679 * vwprintf (MSVCRT.@)
2681 int MSVCRT_vwprintf(const MSVCRT_wchar_t
*format
, va_list valist
)
2683 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2686 /*********************************************************************
2687 * fprintf (MSVCRT.@)
2689 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2693 va_start(valist
, format
);
2694 res
= MSVCRT_vfprintf(file
, format
, valist
);
2699 /*********************************************************************
2700 * fwprintf (MSVCRT.@)
2702 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const MSVCRT_wchar_t
*format
, ...)
2706 va_start(valist
, format
);
2707 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2712 /*********************************************************************
2715 int MSVCRT_printf(const char *format
, ...)
2719 va_start(valist
, format
);
2720 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2725 /*********************************************************************
2728 int MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
2730 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2731 msvcrt_alloc_buffer(file
);
2734 if(file
->_ptr
>file
->_base
) {
2743 /*********************************************************************
2744 * ungetwc (MSVCRT.@)
2746 MSVCRT_wint_t
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2748 MSVCRT_wchar_t mwc
= wc
;
2749 char * pp
= (char *)&mwc
;
2751 for(i
=sizeof(MSVCRT_wchar_t
)-1;i
>=0;i
--) {
2752 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
2758 /*********************************************************************
2759 * wprintf (MSVCRT.@)
2761 int MSVCRT_wprintf(const MSVCRT_wchar_t
*format
, ...)
2765 va_start(valist
, format
);
2766 res
= MSVCRT_vwprintf(format
, valist
);