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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
35 #include "msvcrt/errno.h"
37 #include "wine/unicode.h"
38 #include "msvcrt/direct.h"
39 #include "msvcrt/fcntl.h"
40 #include "msvcrt/io.h"
41 #include "msvcrt/sys/locking.h"
42 #include "msvcrt/stdio.h"
43 #include "msvcrt/stdlib.h"
44 #include "msvcrt/string.h"
45 #include "msvcrt/sys/stat.h"
46 #include "msvcrt/sys/utime.h"
47 #include "msvcrt/time.h"
48 #include "msvcrt/share.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
54 /* for stat mode, permissions apply to all,owner and group */
55 #define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
56 #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
57 #define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
59 /* _access() bit flags FIXME: incomplete */
60 #define MSVCRT_W_OK 0x02
63 /* FIXME: Make this dynamic */
64 #define MSVCRT_MAX_FILES 257
66 HANDLE MSVCRT_handles
[MSVCRT_MAX_FILES
];
67 MSVCRT_FILE
* MSVCRT_files
[MSVCRT_MAX_FILES
];
68 int MSVCRT_flags
[MSVCRT_MAX_FILES
];
69 char *MSVCRT_tempfiles
[MSVCRT_MAX_FILES
];
70 MSVCRT_FILE MSVCRT__iob
[3];
71 #define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
72 #define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
73 #define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
75 static int MSVCRT_fdstart
= 3; /* first unallocated fd */
76 static int MSVCRT_fdend
= 3; /* highest allocated fd */
78 /* INTERNAL: process umask */
79 static int MSVCRT_umask
= 0;
81 /* INTERNAL: Static buffer for temp file name */
82 static char MSVCRT_tmpname
[MAX_PATH
];
84 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
85 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
86 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
87 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
89 #define TOUL(x) (ULONGLONG)((WCHAR)L##x)
90 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
91 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
92 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
93 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
95 extern CRITICAL_SECTION MSVCRT_file_cs
;
96 #define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
97 #define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
99 static void msvcrt_cp_from_stati64(const struct _stati64
*bufi64
, struct _stat
*buf
)
101 buf
->st_dev
= bufi64
->st_dev
;
102 buf
->st_ino
= bufi64
->st_ino
;
103 buf
->st_mode
= bufi64
->st_mode
;
104 buf
->st_nlink
= bufi64
->st_nlink
;
105 buf
->st_uid
= bufi64
->st_uid
;
106 buf
->st_gid
= bufi64
->st_gid
;
107 buf
->st_rdev
= bufi64
->st_rdev
;
108 buf
->st_size
= bufi64
->st_size
;
109 buf
->st_atime
= bufi64
->st_atime
;
110 buf
->st_mtime
= bufi64
->st_mtime
;
111 buf
->st_ctime
= bufi64
->st_ctime
;
114 /* INTERNAL: Get the HANDLE for a fd */
115 static HANDLE
msvcrt_fdtoh(int fd
)
117 if (fd
< 0 || fd
>= MSVCRT_fdend
||
118 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
120 WARN(":fd (%d) - no handle!\n",fd
);
122 *MSVCRT__errno() = MSVCRT_EBADF
;
123 return INVALID_HANDLE_VALUE
;
125 return MSVCRT_handles
[fd
];
128 /* INTERNAL: free a file entry fd */
129 static void msvcrt_free_fd(int fd
)
131 MSVCRT_handles
[fd
] = INVALID_HANDLE_VALUE
;
132 MSVCRT_files
[fd
] = 0;
133 MSVCRT_flags
[fd
] = 0;
134 TRACE(":fd (%d) freed\n",fd
);
136 return; /* dont use 0,1,2 for user files */
137 if (fd
== MSVCRT_fdend
- 1)
139 if (fd
< MSVCRT_fdstart
)
143 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
144 static int msvcrt_alloc_fd(HANDLE hand
, int flag
)
146 int fd
= MSVCRT_fdstart
;
148 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fd
);
149 if (fd
>= MSVCRT_MAX_FILES
)
151 WARN(":files exhausted!\n");
154 MSVCRT_handles
[fd
] = hand
;
155 MSVCRT_flags
[fd
] = flag
;
157 /* locate next free slot */
158 if (fd
== MSVCRT_fdend
)
159 MSVCRT_fdstart
= ++MSVCRT_fdend
;
161 while(MSVCRT_fdstart
< MSVCRT_fdend
&&
162 MSVCRT_handles
[MSVCRT_fdstart
] != INVALID_HANDLE_VALUE
)
168 /* INTERNAL: Allocate a FILE* for an fd slot
169 * This is done lazily to avoid memory wastage for low level open/write
170 * usage when a FILE* is not requested (but may be later).
172 static MSVCRT_FILE
* msvcrt_alloc_fp(int fd
)
174 TRACE(":fd (%d) allocating FILE*\n",fd
);
175 if (fd
< 0 || fd
>= MSVCRT_fdend
||
176 MSVCRT_handles
[fd
] == INVALID_HANDLE_VALUE
)
178 WARN(":invalid fd %d\n",fd
);
180 *MSVCRT__errno() = MSVCRT_EBADF
;
183 if (!MSVCRT_files
[fd
])
185 if ((MSVCRT_files
[fd
] = MSVCRT_calloc(sizeof(MSVCRT_FILE
),1)))
187 MSVCRT_files
[fd
]->_file
= fd
;
188 MSVCRT_files
[fd
]->_flag
= MSVCRT_flags
[fd
];
189 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOAPPEND
; /* mask out, see above */
192 TRACE(":got FILE* (%p)\n",MSVCRT_files
[fd
]);
193 return MSVCRT_files
[fd
];
197 /* INTERNAL: Set up stdin, stderr and stdout */
198 void msvcrt_init_io(void)
201 memset(MSVCRT__iob
,0,3*sizeof(MSVCRT_FILE
));
202 MSVCRT_handles
[0] = GetStdHandle(STD_INPUT_HANDLE
);
203 MSVCRT_flags
[0] = MSVCRT__iob
[0]._flag
= MSVCRT__IOREAD
;
204 MSVCRT_handles
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
205 MSVCRT_flags
[1] = MSVCRT__iob
[1]._flag
= MSVCRT__IOWRT
;
206 MSVCRT_handles
[2] = GetStdHandle(STD_ERROR_HANDLE
);
207 MSVCRT_flags
[2] = MSVCRT__iob
[2]._flag
= MSVCRT__IOWRT
;
209 TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles
[0],
210 MSVCRT_handles
[1],MSVCRT_handles
[2]);
212 for (i
= 0; i
< 3; i
++)
214 /* FILE structs for stdin/out/err are static and never deleted */
215 MSVCRT_files
[i
] = &MSVCRT__iob
[i
];
216 MSVCRT__iob
[i
]._file
= i
;
217 MSVCRT_tempfiles
[i
] = NULL
;
221 /* free everything on process exit */
222 void msvcrt_free_io(void)
230 /* INTERNAL: Flush stdio file buffer */
231 static int msvcrt_flush_buffer(MSVCRT_FILE
* file
)
234 int cnt
=file
->_ptr
-file
->_base
;
235 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
238 file
->_ptr
=file
->_base
;
239 file
->_cnt
=file
->_bufsiz
;
244 /* INTERNAL: Allocate stdio file buffer */
245 static void msvcrt_alloc_buffer(MSVCRT_FILE
* file
)
247 file
->_base
= MSVCRT_calloc(MSVCRT_BUFSIZ
,1);
249 file
->_bufsiz
= MSVCRT_BUFSIZ
;
250 file
->_flag
|= MSVCRT__IOMYBUF
;
252 file
->_base
= (unsigned char *)(&file
->_charbuf
);
254 file
->_bufsiz
= sizeof(file
->_charbuf
);
256 file
->_ptr
= file
->_base
;
260 /*********************************************************************
263 MSVCRT_FILE
*__p__iob(void)
265 return &MSVCRT__iob
[0];
268 /*********************************************************************
271 int _access(const char *filename
, int mode
)
273 DWORD attr
= GetFileAttributesA(filename
);
275 TRACE("(%s,%d) %ld\n",filename
,mode
,attr
);
277 if (!filename
|| attr
== 0xffffffff)
279 MSVCRT__set_errno(GetLastError());
282 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
284 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
290 /*********************************************************************
291 * _waccess (MSVCRT.@)
293 int _waccess(const WCHAR
*filename
, int mode
)
295 DWORD attr
= GetFileAttributesW(filename
);
297 TRACE("(%s,%d) %ld\n",debugstr_w(filename
),mode
,attr
);
299 if (!filename
|| attr
== 0xffffffff)
301 MSVCRT__set_errno(GetLastError());
304 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& MSVCRT_W_OK
))
306 MSVCRT__set_errno(ERROR_ACCESS_DENIED
);
312 /*********************************************************************
315 int _chmod(const char *path
, int flags
)
317 DWORD oldFlags
= GetFileAttributesA(path
);
319 if (oldFlags
!= 0x0FFFFFFFF)
321 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
322 oldFlags
| FILE_ATTRIBUTE_READONLY
;
324 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
327 MSVCRT__set_errno(GetLastError());
331 /*********************************************************************
334 int _wchmod(const WCHAR
*path
, int flags
)
336 DWORD oldFlags
= GetFileAttributesW(path
);
338 if (oldFlags
!= 0x0FFFFFFFF)
340 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
341 oldFlags
| FILE_ATTRIBUTE_READONLY
;
343 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
346 MSVCRT__set_errno(GetLastError());
350 /*********************************************************************
353 int _unlink(const char *path
)
355 TRACE("(%s)\n",path
);
356 if(DeleteFileA(path
))
358 TRACE("failed (%ld)\n",GetLastError());
359 MSVCRT__set_errno(GetLastError());
363 /*********************************************************************
364 * _wunlink (MSVCRT.@)
366 int _wunlink(const WCHAR
*path
)
368 TRACE("(%s)\n",debugstr_w(path
));
369 if(DeleteFileW(path
))
371 TRACE("failed (%ld)\n",GetLastError());
372 MSVCRT__set_errno(GetLastError());
376 /*********************************************************************
381 HANDLE hand
= msvcrt_fdtoh(fd
);
383 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
384 if (hand
== INVALID_HANDLE_VALUE
)
386 /* flush stdio buffers */
387 if(MSVCRT_files
[fd
]) {
388 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOWRT
)
389 MSVCRT_fflush(MSVCRT_files
[fd
]);
391 if(MSVCRT_files
[fd
]->_flag
& MSVCRT__IOMYBUF
)
392 MSVCRT_free(MSVCRT_files
[fd
]->_base
);
395 /* Dont free std FILE*'s, they are not dynamic */
396 if (fd
> 2 && MSVCRT_files
[fd
])
397 MSVCRT_free(MSVCRT_files
[fd
]);
401 if (!CloseHandle(hand
))
403 WARN(":failed-last error (%ld)\n",GetLastError());
404 MSVCRT__set_errno(GetLastError());
407 if (MSVCRT_tempfiles
[fd
])
409 TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles
[fd
]);
410 _unlink(MSVCRT_tempfiles
[fd
]);
411 MSVCRT_free(MSVCRT_tempfiles
[fd
]);
412 MSVCRT_tempfiles
[fd
] = NULL
;
419 /*********************************************************************
424 HANDLE hand
= msvcrt_fdtoh(fd
);
426 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
427 if (hand
== INVALID_HANDLE_VALUE
)
430 if (!FlushFileBuffers(hand
))
432 if (GetLastError() == ERROR_INVALID_HANDLE
)
434 /* FlushFileBuffers fails for console handles
435 * so we ignore this error.
439 TRACE(":failed-last error (%ld)\n",GetLastError());
440 MSVCRT__set_errno(GetLastError());
447 /*********************************************************************
453 HANDLE hand
= msvcrt_fdtoh(fd
);
455 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
457 if (hand
== INVALID_HANDLE_VALUE
)
460 /* If we have a FILE* for this file, the EOF flag
461 * will be set by the read()/write() functions.
463 if (MSVCRT_files
[fd
])
464 return MSVCRT_flags
[fd
] & MSVCRT__IOEOF
;
466 /* Otherwise we do it the hard way */
467 curpos
= SetFilePointer(hand
, 0, NULL
, SEEK_CUR
);
468 endpos
= SetFilePointer(hand
, 0, NULL
, FILE_END
);
470 if (curpos
== endpos
)
473 SetFilePointer(hand
, curpos
, 0, FILE_BEGIN
);
477 /*********************************************************************
478 * _fcloseall (MSVCRT.@)
482 int num_closed
= 0, i
;
484 for (i
= 3; i
< MSVCRT_fdend
; i
++)
485 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
491 TRACE(":closed (%d) handles\n",num_closed
);
495 /*********************************************************************
498 LONG
_lseek(int fd
, LONG offset
, int whence
)
501 HANDLE hand
= msvcrt_fdtoh(fd
);
503 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
504 if (hand
== INVALID_HANDLE_VALUE
)
507 if (whence
< 0 || whence
> 2)
509 *MSVCRT__errno() = MSVCRT_EINVAL
;
513 TRACE(":fd (%d) to 0x%08lx pos %s\n",
514 fd
,offset
,(whence
==SEEK_SET
)?"SEEK_SET":
515 (whence
==SEEK_CUR
)?"SEEK_CUR":
516 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
518 if ((ret
= SetFilePointer(hand
, offset
, NULL
, whence
)) != 0xffffffff)
520 if (MSVCRT_files
[fd
])
521 MSVCRT_files
[fd
]->_flag
&= ~MSVCRT__IOEOF
;
522 /* FIXME: What if we seek _to_ EOF - is EOF set? */
525 TRACE(":error-last error (%ld)\n",GetLastError());
526 if (MSVCRT_files
[fd
])
527 switch(GetLastError())
529 case ERROR_NEGATIVE_SEEK
:
530 case ERROR_SEEK_ON_DEVICE
:
531 MSVCRT__set_errno(GetLastError());
532 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
540 /*********************************************************************
541 * _locking (MSVCRT.@)
543 * This is untested; the underlying LockFile doesn't work yet.
545 int _locking(int fd
, int mode
, LONG nbytes
)
549 HANDLE hand
= msvcrt_fdtoh(fd
);
551 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
552 if (hand
== INVALID_HANDLE_VALUE
)
555 if (mode
< 0 || mode
> 4)
557 *MSVCRT__errno() = MSVCRT_EINVAL
;
561 TRACE(":fd (%d) by 0x%08lx mode %s\n",
562 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
563 (mode
==_LK_LOCK
)?"_LK_LOCK":
564 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
565 (mode
==_LK_RLCK
)?"_LK_RLCK":
566 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
569 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == 0xffffffff)
571 FIXME ("Seek failed\n");
572 *MSVCRT__errno() = MSVCRT_EINVAL
; /* FIXME */
575 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
578 ret
= 1; /* just to satisfy gcc */
581 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
586 else if (mode
== _LK_UNLCK
)
587 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
589 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
590 /* FIXME - what about error settings? */
594 /*********************************************************************
597 void MSVCRT_rewind(MSVCRT_FILE
* file
)
599 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
600 MSVCRT_fseek(file
, 0L, SEEK_SET
);
601 MSVCRT_clearerr(file
);
604 /*********************************************************************
607 MSVCRT_FILE
* _fdopen(int fd
, const char *mode
)
609 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
611 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
618 /*********************************************************************
619 * _wfdopen (MSVCRT.@)
621 MSVCRT_FILE
* _wfdopen(int fd
, const WCHAR
*mode
)
623 MSVCRT_FILE
* file
= msvcrt_alloc_fp(fd
);
625 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
632 /*********************************************************************
633 * _filelength (MSVCRT.@)
635 LONG
_filelength(int fd
)
637 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
640 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
643 if (endPos
!= curPos
)
644 _lseek(fd
, curPos
, SEEK_SET
);
651 /*********************************************************************
654 int _fileno(MSVCRT_FILE
* file
)
656 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
660 /*********************************************************************
661 * _flushall (MSVCRT.@)
665 int num_flushed
= 0, i
= 3;
667 while(i
< MSVCRT_fdend
)
668 if (MSVCRT_handles
[i
] != INVALID_HANDLE_VALUE
)
671 /* FIXME: flush, do not commit */
672 if (_commit(i
) == -1)
674 MSVCRT_files
[i
]->_flag
|= MSVCRT__IOERR
;
676 if(MSVCRT_files
[i
] && MSVCRT_files
[i
]->_flag
& MSVCRT__IOWRT
) {
677 MSVCRT_fflush(MSVCRT_files
[i
]);
682 TRACE(":flushed (%d) handles\n",num_flushed
);
686 /*********************************************************************
687 * _fstati64 (MSVCRT.@)
689 int _fstati64(int fd
, struct _stati64
* buf
)
692 BY_HANDLE_FILE_INFORMATION hfi
;
693 HANDLE hand
= msvcrt_fdtoh(fd
);
695 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
696 if (hand
== INVALID_HANDLE_VALUE
)
701 WARN(":failed-NULL buf\n");
702 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
706 memset(&hfi
, 0, sizeof(hfi
));
707 memset(buf
, 0, sizeof(struct _stati64
));
708 if (!GetFileInformationByHandle(hand
, &hfi
))
710 WARN(":failed-last error (%ld)\n",GetLastError());
711 MSVCRT__set_errno(ERROR_INVALID_PARAMETER
);
714 FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi
.dwFileAttributes
);
715 buf
->st_nlink
= hfi
.nNumberOfLinks
;
716 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
717 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
719 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
720 buf
->st_mtime
= buf
->st_ctime
= dw
;
724 /*********************************************************************
727 int MSVCRT__fstat(int fd
, struct _stat
* buf
)
729 struct _stati64 bufi64
;
731 ret
= _fstati64(fd
, &bufi64
);
733 msvcrt_cp_from_stati64(&bufi64
, buf
);
737 /*********************************************************************
740 int _futime(int fd
, struct _utimbuf
*t
)
742 HANDLE hand
= msvcrt_fdtoh(fd
);
747 MSVCRT_time_t currTime
;
748 MSVCRT_time(&currTime
);
749 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
750 memcpy(&wt
, &at
, sizeof(wt
));
754 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
755 if (t
->actime
== t
->modtime
)
756 memcpy(&wt
, &at
, sizeof(wt
));
758 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
761 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
763 MSVCRT__set_errno(GetLastError());
769 /*********************************************************************
770 * _get_osfhandle (MSVCRT.@)
772 long _get_osfhandle(int fd
)
774 HANDLE hand
= msvcrt_fdtoh(fd
);
775 HANDLE newhand
= hand
;
776 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
778 if (hand
!= INVALID_HANDLE_VALUE
)
780 /* FIXME: I'm not convinced that I should be copying the
781 * handle here - it may be leaked if the app doesn't
782 * close it (and the API docs dont say that it should)
783 * Not duplicating it means that it can't be inherited
784 * and so lcc's wedit doesn't cope when it passes it to
785 * child processes. I've an idea that it should either
786 * be copied by CreateProcess, or marked as inheritable
787 * when initialised, or maybe both? JG 21-9-00.
789 DuplicateHandle(GetCurrentProcess(),hand
,GetCurrentProcess(),
790 &newhand
,0,TRUE
,DUPLICATE_SAME_ACCESS
);
792 return (long)newhand
;
795 /*********************************************************************
800 HANDLE hand
= msvcrt_fdtoh(fd
);
802 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
803 if (hand
== INVALID_HANDLE_VALUE
)
806 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
809 /*********************************************************************
812 char *_mktemp(char *pattern
)
815 char *retVal
= pattern
;
820 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
824 id
= GetCurrentProcessId();
828 int tempNum
= id
/ 10;
829 *pattern
-- = id
- (tempNum
* 10) + '0';
835 if (GetFileAttributesA(retVal
) == 0xFFFFFFFF &&
836 GetLastError() == ERROR_FILE_NOT_FOUND
)
839 } while(letter
!= '|');
843 /*********************************************************************
844 * _wmktemp (MSVCRT.@)
846 WCHAR
*_wmktemp(WCHAR
*pattern
)
849 WCHAR
*retVal
= pattern
;
851 WCHAR letter
= (WCHAR
)L
'a';
854 numX
= (*pattern
++ == (WCHAR
)L
'X')? numX
+ 1 : 0;
858 id
= GetCurrentProcessId();
862 int tempNum
= id
/ 10;
863 *pattern
-- = id
- (tempNum
* 10) + (WCHAR
)L
'0';
869 if (GetFileAttributesW(retVal
) == 0xFFFFFFFF &&
870 GetLastError() == ERROR_FILE_NOT_FOUND
)
873 } while(letter
!= (WCHAR
)L
'|');
877 /*********************************************************************
880 int MSVCRT__sopen( const char *path
, int oflags
, int shflags
, ... )
884 DWORD access
= 0, creation
= 0;
888 SECURITY_ATTRIBUTES sa
;
891 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
892 path
, oflags
, shflags
);
894 switch(oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
897 access
|= GENERIC_READ
;
898 ioflag
|= MSVCRT__IOREAD
;
901 access
|= GENERIC_WRITE
;
902 ioflag
|= MSVCRT__IOWRT
;
905 access
|= GENERIC_WRITE
| GENERIC_READ
;
906 ioflag
|= MSVCRT__IORW
;
910 if (oflags
& _O_CREAT
)
912 va_start(ap
, shflags
);
913 pmode
= va_arg(ap
, int);
916 FIXME(": pmode 0x%04x ignored\n", pmode
);
918 if (oflags
& _O_EXCL
)
919 creation
= CREATE_NEW
;
920 else if (oflags
& _O_TRUNC
)
921 creation
= CREATE_ALWAYS
;
923 creation
= OPEN_ALWAYS
;
925 else /* no _O_CREAT */
927 if (oflags
& _O_TRUNC
)
928 creation
= TRUNCATE_EXISTING
;
930 creation
= OPEN_EXISTING
;
932 if (oflags
& _O_APPEND
)
933 ioflag
|= MSVCRT__IOAPPEND
;
936 oflags
|= _O_BINARY
; /* FIXME: Default to text */
938 if (oflags
& _O_TEXT
)
940 /* Dont warn when writing */
941 if (ioflag
& GENERIC_READ
)
942 FIXME(":TEXT node not implemented\n");
952 sharing
= FILE_SHARE_READ
;
955 sharing
= FILE_SHARE_WRITE
;
958 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
961 ERR( "Unhandled shflags 0x%x\n", shflags
);
965 if (oflags
& ~(_O_BINARY
|_O_TEXT
|_O_APPEND
|_O_TRUNC
|_O_EXCL
966 |_O_CREAT
|_O_RDWR
|_O_TEMPORARY
|_O_NOINHERIT
))
967 TRACE(":unsupported oflags 0x%04x\n",oflags
);
969 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
970 sa
.lpSecurityDescriptor
= NULL
;
971 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
973 hand
= CreateFileA(path
, access
, sharing
,
974 &sa
, creation
, FILE_ATTRIBUTE_NORMAL
, 0);
976 if (hand
== INVALID_HANDLE_VALUE
) {
977 WARN(":failed-last error (%ld)\n",GetLastError());
978 MSVCRT__set_errno(GetLastError());
982 fd
= msvcrt_alloc_fd(hand
, ioflag
);
984 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
988 if (oflags
& _O_TEMPORARY
)
989 MSVCRT_tempfiles
[fd
] = _strdup(path
);
990 if (ioflag
& MSVCRT__IOAPPEND
)
991 _lseek(fd
, 0, FILE_END
);
997 /*********************************************************************
1000 int MSVCRT__wsopen( const WCHAR
* path
, int oflags
, int shflags
, ... )
1002 const unsigned int len
= strlenW(path
);
1003 char *patha
= MSVCRT_calloc(len
+ 1,1);
1007 va_start(ap
, shflags
);
1008 pmode
= va_arg(ap
, int);
1011 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1013 int retval
= MSVCRT__sopen(patha
,oflags
,shflags
,pmode
);
1018 MSVCRT__set_errno(GetLastError());
1022 /*********************************************************************
1025 int _open( const char *path
, int flags
, ... )
1030 va_start(ap
, flags
);
1031 pmode
= va_arg(ap
, int);
1034 return MSVCRT__sopen( path
, flags
, _SH_DENYNO
, pmode
);
1037 /*********************************************************************
1040 int _wopen(const WCHAR
*path
,int flags
,...)
1042 const unsigned int len
= strlenW(path
);
1043 char *patha
= MSVCRT_calloc(len
+ 1,1);
1047 va_start(ap
, flags
);
1048 pmode
= va_arg(ap
, int);
1051 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1053 int retval
= _open(patha
,flags
,pmode
);
1058 MSVCRT__set_errno(GetLastError());
1062 /*********************************************************************
1065 int _creat(const char *path
, int flags
)
1067 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1068 return _open(path
, usedFlags
);
1071 /*********************************************************************
1072 * _wcreat (MSVCRT.@)
1074 int _wcreat(const WCHAR
*path
, int flags
)
1076 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1077 return _wopen(path
, usedFlags
);
1080 /*********************************************************************
1081 * _open_osfhandle (MSVCRT.@)
1083 int _open_osfhandle(long hand
, int flags
)
1085 /* _O_RDONLY (0) always matches, so set the read flag*/
1086 /* FIXME: handle more flags */
1087 int fd
= msvcrt_alloc_fd((HANDLE
)hand
,flags
|MSVCRT__IOREAD
);
1088 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand
,fd
, flags
|MSVCRT__IOREAD
);
1092 /*********************************************************************
1097 int num_removed
= 0, i
;
1099 for (i
= 3; i
< MSVCRT_fdend
; i
++)
1100 if (MSVCRT_tempfiles
[i
])
1107 TRACE(":removed (%d) temp files\n",num_removed
);
1111 /*********************************************************************
1114 int _read(int fd
, void *buf
, unsigned int count
)
1117 HANDLE hand
= msvcrt_fdtoh(fd
);
1119 /* Dont trace small reads, it gets *very* annoying */
1121 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1122 if (hand
== INVALID_HANDLE_VALUE
)
1125 if (ReadFile(hand
, buf
, count
, &num_read
, NULL
))
1127 if (num_read
!= count
&& MSVCRT_files
[fd
])
1130 MSVCRT_flags
[fd
] |= MSVCRT__IOEOF
;
1132 MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
1137 TRACE(":failed-last error (%ld)\n",GetLastError());
1138 if (MSVCRT_files
[fd
])
1139 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1143 /*********************************************************************
1146 int _getw(MSVCRT_FILE
* file
)
1149 if (_read(file
->_file
, &i
, sizeof(int)) != 1)
1154 /*********************************************************************
1155 * _setmode (MSVCRT.@)
1157 int _setmode(int fd
,int mode
)
1160 FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd
,mode
);
1164 /*********************************************************************
1165 * _stati64 (MSVCRT.@)
1167 int _stati64(const char* path
, struct _stati64
* buf
)
1170 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1171 unsigned short mode
= MSVCRT_S_IREAD
;
1174 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1176 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1178 TRACE("failed (%ld)\n",GetLastError());
1179 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1183 memset(buf
,0,sizeof(struct _stati64
));
1185 /* FIXME: rdev isnt drive num,despite what the docs say-what is it?
1186 Bon 011120: This FIXME seems incorrect
1187 Also a letter as first char isn't enough to be classify
1190 if (isalpha(*path
)&& (*(path
+1)==':'))
1191 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1193 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1195 plen
= strlen(path
);
1197 /* Dir, or regular file? */
1198 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1199 (path
[plen
-1] == '\\'))
1200 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1205 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1207 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1208 (tolower(path
[plen
-3]) << 16);
1209 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1210 mode
|= MSVCRT_S_IEXEC
;
1214 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1215 mode
|= MSVCRT_S_IWRITE
;
1217 buf
->st_mode
= mode
;
1219 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1220 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1222 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1223 buf
->st_mtime
= buf
->st_ctime
= dw
;
1224 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1225 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1226 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1230 /*********************************************************************
1233 int MSVCRT__stat(const char* path
, struct _stat
* buf
)
1235 struct _stati64 bufi64
;
1237 ret
= _stati64( path
, &bufi64
);
1239 msvcrt_cp_from_stati64(&bufi64
, buf
);
1243 /*********************************************************************
1246 int _wstat(const WCHAR
* path
, struct _stat
* buf
)
1249 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1250 unsigned short mode
= MSVCRT_S_IREAD
;
1253 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1255 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1257 TRACE("failed (%ld)\n",GetLastError());
1258 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND
);
1262 memset(buf
,0,sizeof(struct _stat
));
1264 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1265 if (MSVCRT_iswalpha(*path
))
1266 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- (WCHAR
)L
'A'); /* drive num */
1268 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1270 plen
= strlenW(path
);
1272 /* Dir, or regular file? */
1273 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1274 (path
[plen
-1] == (WCHAR
)L
'\\'))
1275 mode
|= (_S_IFDIR
| MSVCRT_S_IEXEC
);
1280 if (plen
> 6 && path
[plen
-4] == (WCHAR
)L
'.') /* shortest exe: "\x.exe" */
1282 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1283 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1284 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1285 mode
|= MSVCRT_S_IEXEC
;
1289 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1290 mode
|= MSVCRT_S_IWRITE
;
1292 buf
->st_mode
= mode
;
1294 buf
->st_size
= hfi
.nFileSizeLow
;
1295 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1297 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1298 buf
->st_mtime
= buf
->st_ctime
= dw
;
1299 TRACE("\n%d %d %d %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,buf
->st_size
,
1300 buf
->st_atime
,buf
->st_mtime
, buf
->st_ctime
);
1304 /*********************************************************************
1309 return _lseek(fd
, 0, SEEK_CUR
);
1312 /*********************************************************************
1313 * _tempnam (MSVCRT.@)
1315 char *_tempnam(const char *dir
, const char *prefix
)
1317 char tmpbuf
[MAX_PATH
];
1319 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
1320 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
1322 TRACE("got name (%s)\n",tmpbuf
);
1323 return _strdup(tmpbuf
);
1325 TRACE("failed (%ld)\n",GetLastError());
1329 /*********************************************************************
1330 * _wtempnam (MSVCRT.@)
1332 WCHAR
*_wtempnam(const WCHAR
*dir
, const WCHAR
*prefix
)
1334 WCHAR tmpbuf
[MAX_PATH
];
1336 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
1337 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
1339 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
1340 return _wcsdup(tmpbuf
);
1342 TRACE("failed (%ld)\n",GetLastError());
1346 /*********************************************************************
1349 int _umask(int umask
)
1351 int old_umask
= MSVCRT_umask
;
1352 TRACE("(%d)\n",umask
);
1353 MSVCRT_umask
= umask
;
1357 /*********************************************************************
1360 int _utime(const char* path
, struct _utimbuf
*t
)
1362 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
1366 int retVal
= _futime(fd
, t
);
1373 /*********************************************************************
1374 * _wutime (MSVCRT.@)
1376 int _wutime(const WCHAR
* path
, struct _utimbuf
*t
)
1378 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
1382 int retVal
= _futime(fd
, t
);
1389 /*********************************************************************
1392 int _write(int fd
, const void* buf
, unsigned int count
)
1395 HANDLE hand
= msvcrt_fdtoh(fd
);
1397 /* Dont trace small writes, it gets *very* annoying */
1400 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1402 if (hand
== INVALID_HANDLE_VALUE
)
1405 /* If appending, go to EOF */
1406 if (MSVCRT_flags
[fd
] & MSVCRT__IOAPPEND
)
1407 _lseek(fd
, 0, FILE_END
);
1409 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
1410 && (num_written
== count
))
1413 TRACE(":failed-last error (%ld)\n",GetLastError());
1414 if (MSVCRT_files
[fd
])
1415 MSVCRT_files
[fd
]->_flag
|= MSVCRT__IOERR
;
1420 /*********************************************************************
1423 int _putw(int val
, MSVCRT_FILE
* file
)
1425 return _write(file
->_file
, &val
, sizeof(val
)) == 1? val
: MSVCRT_EOF
;
1428 /*********************************************************************
1429 * clearerr (MSVCRT.@)
1431 void MSVCRT_clearerr(MSVCRT_FILE
* file
)
1433 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1434 file
->_flag
&= ~(MSVCRT__IOERR
| MSVCRT__IOEOF
);
1437 /*********************************************************************
1440 int MSVCRT_fclose(MSVCRT_FILE
* file
)
1443 r
=_close(file
->_file
);
1444 return ((r
==MSVCRT_EOF
) || (file
->_flag
& MSVCRT__IOERR
) ? MSVCRT_EOF
: 0);
1447 /*********************************************************************
1450 int MSVCRT_feof(MSVCRT_FILE
* file
)
1452 return file
->_flag
& MSVCRT__IOEOF
;
1455 /*********************************************************************
1458 int MSVCRT_ferror(MSVCRT_FILE
* file
)
1460 return file
->_flag
& MSVCRT__IOERR
;
1463 /*********************************************************************
1466 int MSVCRT_fflush(MSVCRT_FILE
* file
)
1472 int res
=msvcrt_flush_buffer(file
);
1477 /*********************************************************************
1480 int MSVCRT_fgetc(MSVCRT_FILE
* file
)
1484 return *(unsigned char *)file
->_ptr
++;
1486 return _filbuf(file
);
1490 /*********************************************************************
1491 * _fgetchar (MSVCRT.@)
1495 return MSVCRT_fgetc(MSVCRT_stdin
);
1498 /*********************************************************************
1499 * _filbuf (MSVCRT.@)
1501 int _filbuf(MSVCRT_FILE
* file
)
1504 /* Allocate buffer if needed */
1505 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
) ) {
1506 msvcrt_alloc_buffer(file
);
1508 if(!(file
->_flag
& MSVCRT__IOREAD
)) {
1509 if(file
->_flag
& MSVCRT__IORW
) {
1510 file
->_flag
|= MSVCRT__IOREAD
;
1515 if(file
->_flag
& MSVCRT__IONBF
) {
1517 if (_read(file
->_file
,&c
,1) != 1) {
1518 file
->_flag
|= MSVCRT__IOEOF
;
1523 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
1524 if(file
->_cnt
<0) file
->_cnt
= 0;
1526 file
->_flag
|= MSVCRT__IOEOF
;
1530 file
->_ptr
= file
->_base
+1;
1531 return *(unsigned char *)file
->_base
;
1535 /*********************************************************************
1536 * fgetpos (MSVCRT.@)
1538 int MSVCRT_fgetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1540 *pos
= MSVCRT_ftell(file
);
1541 return (*pos
== -1? -1 : 0);
1544 /*********************************************************************
1547 char *MSVCRT_fgets(char *s
, int size
, MSVCRT_FILE
* file
)
1550 char * buf_start
= s
;
1552 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1553 file
,file
->_file
,s
,size
);
1555 for(cc
= MSVCRT_fgetc(file
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
1556 cc
= MSVCRT_fgetc(file
))
1559 if (--size
<= 0) break;
1562 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1564 TRACE(":nothing read\n");
1571 TRACE(":got '%s'\n", buf_start
);
1575 /*********************************************************************
1578 MSVCRT_wint_t
MSVCRT_fgetwc(MSVCRT_FILE
* file
)
1581 if (_read(file
->_file
, &wc
, sizeof(wc
)) != sizeof(wc
))
1586 /*********************************************************************
1589 MSVCRT_wint_t
MSVCRT_getwc(MSVCRT_FILE
* file
)
1591 return MSVCRT_fgetwc(file
);
1594 /*********************************************************************
1595 * _fgetwchar (MSVCRT.@)
1597 MSVCRT_wint_t
_fgetwchar(void)
1599 return MSVCRT_fgetwc(MSVCRT_stdin
);
1602 /*********************************************************************
1603 * getwchar (MSVCRT.@)
1605 MSVCRT_wint_t
MSVCRT_getwchar(void)
1607 return _fgetwchar();
1610 /*********************************************************************
1613 WCHAR
*MSVCRT_fgetws(WCHAR
*s
, int size
, MSVCRT_FILE
* file
)
1616 WCHAR
* buf_start
= s
;
1618 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
1619 file
,file
->_file
,s
,size
);
1621 for(cc
= MSVCRT_fgetwc(file
); cc
!= MSVCRT_WEOF
&& cc
!= L
'\n';
1622 cc
= MSVCRT_fgetwc(file
))
1625 if (--size
<= 0) break;
1628 if ((cc
== MSVCRT_EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
1630 TRACE(":nothing read\n");
1637 /* TRACE(":got '%s'\n", buf_start); */
1642 /*********************************************************************
1645 MSVCRT_wint_t
MSVCRT_fputwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
1648 if (MSVCRT_fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
1653 /*********************************************************************
1654 * _fputwchar (MSVCRT.@)
1656 MSVCRT_wint_t
_fputwchar(MSVCRT_wint_t wc
)
1658 return MSVCRT_fputwc(wc
, MSVCRT_stdout
);
1661 /*********************************************************************
1664 MSVCRT_FILE
* MSVCRT_fopen(const char *path
, const char *mode
)
1667 int flags
= 0, plus
= 0, fd
;
1668 const char* search
= mode
;
1670 TRACE("(%s,%s)\n",path
,mode
);
1673 if (*search
++ == '+')
1676 /* map mode string to open() flags. "man fopen" for possibilities. */
1680 flags
= (plus
? _O_RDWR
: _O_RDONLY
);
1683 flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1686 flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1701 flags
&= ~_O_BINARY
;
1706 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1709 fd
= _open(path
, flags
);
1714 file
= msvcrt_alloc_fp(fd
);
1715 TRACE(":got (%p)\n",file
);
1722 /*********************************************************************
1723 * _wfopen (MSVCRT.@)
1725 MSVCRT_FILE
*_wfopen(const WCHAR
*path
, const WCHAR
*mode
)
1727 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
1728 char *patha
= MSVCRT_calloc(plen
+ 1, 1);
1729 char *modea
= MSVCRT_calloc(mlen
+ 1, 1);
1731 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
1733 if (patha
&& modea
&&
1734 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
1735 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1737 MSVCRT_FILE
*retval
= MSVCRT_fopen(patha
,modea
);
1743 MSVCRT__set_errno(GetLastError());
1747 /*********************************************************************
1748 * _fsopen (MSVCRT.@)
1750 MSVCRT_FILE
* _fsopen(const char *path
, const char *mode
, int share
)
1752 FIXME(":(%s,%s,%d),ignoring share mode!\n",path
,mode
,share
);
1753 return MSVCRT_fopen(path
,mode
);
1756 /*********************************************************************
1757 * _wfsopen (MSVCRT.@)
1759 MSVCRT_FILE
* _wfsopen(const WCHAR
*path
, const WCHAR
*mode
, int share
)
1761 FIXME(":(%s,%s,%d),ignoring share mode!\n",
1762 debugstr_w(path
),debugstr_w(mode
),share
);
1763 return _wfopen(path
,mode
);
1766 /*********************************************************************
1769 int MSVCRT_fputc(int c
, MSVCRT_FILE
* file
)
1776 return _flsbuf(c
, file
);
1780 /*********************************************************************
1781 * _flsbuf (MSVCRT.@)
1783 int _flsbuf(int c
, MSVCRT_FILE
* file
)
1785 /* Flush output buffer */
1786 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
1787 msvcrt_alloc_buffer(file
);
1789 if(!(file
->_flag
& MSVCRT__IOWRT
)) {
1790 if(file
->_flag
& MSVCRT__IORW
) {
1791 file
->_flag
|= MSVCRT__IOWRT
;
1797 int res
=msvcrt_flush_buffer(file
);
1798 return res
?res
: MSVCRT_fputc(c
, file
);
1801 return _write(file
->_file
, &cc
, 1) == 1? c
: MSVCRT_EOF
;
1805 /*********************************************************************
1806 * _fputchar (MSVCRT.@)
1808 int _fputchar(int c
)
1810 return MSVCRT_fputc(c
, MSVCRT_stdout
);
1813 /*********************************************************************
1816 MSVCRT_size_t
MSVCRT_fread(void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
1817 { MSVCRT_size_t rcnt
=size
* nmemb
;
1818 MSVCRT_size_t read
=0;
1820 /* first buffered data */
1822 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
1823 memcpy(ptr
, file
->_ptr
, pcnt
);
1828 ptr
= (char*)ptr
+ pcnt
;
1829 } else if(!(file
->_flag
& MSVCRT__IOREAD
)) {
1830 if(file
->_flag
& MSVCRT__IORW
) {
1831 file
->_flag
|= MSVCRT__IOREAD
;
1835 if(rcnt
) pread
= _read(file
->_file
,ptr
, rcnt
);
1836 if (MSVCRT_flags
[file
->_file
] & MSVCRT__IOEOF
)
1837 /* expose feof condition in the flags
1838 MFC tests file->_flag for feof, and doesn't not call feof())
1840 file
->_flag
|= MSVCRT__IOEOF
;
1847 /*********************************************************************
1848 * freopen (MSVCRT.@)
1851 MSVCRT_FILE
* MSVCRT_freopen(const char *path
, const char *mode
,MSVCRT_FILE
* file
)
1853 MSVCRT_FILE
* newfile
;
1856 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
1857 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> MSVCRT_fdend
)
1863 FIXME(":reopen on user file not implemented!\n");
1864 MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED
);
1867 if(MSVCRT_fclose(file
))
1869 return MSVCRT_fopen(path
, mode
);
1872 /* first, create the new file */
1873 if ((newfile
= MSVCRT_fopen(path
,mode
)) == NULL
)
1876 if (fd
< 3 && SetStdHandle(fd
== 0 ? STD_INPUT_HANDLE
:
1877 (fd
== 1? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
),
1878 MSVCRT_handles
[newfile
->_file
]))
1880 /* Redirecting std handle to file , copy over.. */
1881 MSVCRT_handles
[fd
] = MSVCRT_handles
[newfile
->_file
];
1882 MSVCRT_flags
[fd
] = MSVCRT_flags
[newfile
->_file
];
1883 memcpy(&MSVCRT__iob
[fd
], newfile
, sizeof (MSVCRT_FILE
));
1884 MSVCRT__iob
[fd
]._file
= fd
;
1885 /* And free up the resources allocated by fopen, but
1886 * not the HANDLE we copied. */
1887 MSVCRT_free(MSVCRT_files
[fd
]);
1888 msvcrt_free_fd(newfile
->_file
);
1889 return &MSVCRT__iob
[fd
];
1892 WARN(":failed-last error (%ld)\n",GetLastError());
1893 MSVCRT_fclose(newfile
);
1894 MSVCRT__set_errno(GetLastError());
1898 /*********************************************************************
1899 * fsetpos (MSVCRT.@)
1901 int MSVCRT_fsetpos(MSVCRT_FILE
* file
, MSVCRT_fpos_t
*pos
)
1903 return _lseek(file
->_file
,*pos
,SEEK_SET
);
1906 /*********************************************************************
1909 int MSVCRT_fseek(MSVCRT_FILE
* file
, long offset
, int whence
)
1911 /* Flush output if needed */
1912 if(file
->_flag
& MSVCRT__IOWRT
)
1913 msvcrt_flush_buffer(file
);
1915 if(whence
== SEEK_CUR
&& file
->_flag
& MSVCRT__IOREAD
) {
1916 offset
-= file
->_cnt
;
1918 /* Discard buffered input */
1920 file
->_ptr
= file
->_base
;
1921 /* Reset direction of i/o */
1922 if(file
->_flag
& MSVCRT__IORW
) {
1923 file
->_flag
&= ~(MSVCRT__IOREAD
|MSVCRT__IOWRT
);
1925 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
1928 /*********************************************************************
1931 LONG
MSVCRT_ftell(MSVCRT_FILE
* file
)
1936 if( file
->_flag
& MSVCRT__IOWRT
) {
1937 off
= file
->_ptr
- file
->_base
;
1942 pos
= _tell(file
->_file
);
1943 if(pos
== -1) return pos
;
1947 /*********************************************************************
1950 MSVCRT_size_t
MSVCRT_fwrite(const void *ptr
, MSVCRT_size_t size
, MSVCRT_size_t nmemb
, MSVCRT_FILE
* file
)
1952 MSVCRT_size_t wrcnt
=size
* nmemb
;
1957 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
1958 memcpy(file
->_ptr
, ptr
, pcnt
);
1963 ptr
= (char*)ptr
+ pcnt
;
1964 } else if(!(file
->_flag
& MSVCRT__IOWRT
)) {
1965 if(file
->_flag
& MSVCRT__IORW
) {
1966 file
->_flag
|= MSVCRT__IOWRT
;
1972 int res
=msvcrt_flush_buffer(file
);
1974 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
1975 if (pwritten
<= 0) pwritten
=0;
1976 written
+= pwritten
;
1979 return written
/ size
;
1982 /*********************************************************************
1985 int MSVCRT_fputs(const char *s
, MSVCRT_FILE
* file
)
1987 size_t len
= strlen(s
);
1988 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
1991 /*********************************************************************
1994 int MSVCRT_fputws(const WCHAR
*s
, MSVCRT_FILE
* file
)
1996 size_t len
= strlenW(s
);
1997 return MSVCRT_fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : MSVCRT_EOF
;
2000 /*********************************************************************
2001 * getchar (MSVCRT.@)
2003 int MSVCRT_getchar(void)
2005 return MSVCRT_fgetc(MSVCRT_stdin
);
2008 /*********************************************************************
2011 int MSVCRT_getc(MSVCRT_FILE
* file
)
2013 return MSVCRT_fgetc(file
);
2016 /*********************************************************************
2019 char *MSVCRT_gets(char *buf
)
2022 char * buf_start
= buf
;
2024 for(cc
= MSVCRT_fgetc(MSVCRT_stdin
); cc
!= MSVCRT_EOF
&& cc
!= '\n';
2025 cc
= MSVCRT_fgetc(MSVCRT_stdin
))
2026 if(cc
!= '\r') *buf
++ = (char)cc
;
2030 TRACE("got '%s'\n", buf_start
);
2034 /*********************************************************************
2037 WCHAR
* MSVCRT__getws(WCHAR
* buf
)
2042 for (cc
= MSVCRT_fgetwc(MSVCRT_stdin
); cc
!= MSVCRT_WEOF
&& cc
!= '\n';
2043 cc
= MSVCRT_fgetwc(MSVCRT_stdin
))
2050 TRACE("got '%s'\n", debugstr_w(ws
));
2054 /*********************************************************************
2057 int MSVCRT_putc(int c
, MSVCRT_FILE
* file
)
2059 return MSVCRT_fputc(c
, file
);
2062 /*********************************************************************
2063 * putchar (MSVCRT.@)
2065 int MSVCRT_putchar(int c
)
2067 return MSVCRT_fputc(c
, MSVCRT_stdout
);
2070 /*********************************************************************
2073 int MSVCRT_puts(const char *s
)
2075 size_t len
= strlen(s
);
2076 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2077 return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2080 /*********************************************************************
2083 int _putws(const WCHAR
*s
)
2085 static const WCHAR nl
= '\n';
2086 size_t len
= strlenW(s
);
2087 if (MSVCRT_fwrite(s
,sizeof(*s
),len
,MSVCRT_stdout
) != len
) return MSVCRT_EOF
;
2088 return MSVCRT_fwrite(&nl
,sizeof(nl
),1,MSVCRT_stdout
) == 1 ? 0 : MSVCRT_EOF
;
2091 /*********************************************************************
2094 int MSVCRT_remove(const char *path
)
2096 TRACE("(%s)\n",path
);
2097 if (DeleteFileA(path
))
2099 TRACE(":failed (%ld)\n",GetLastError());
2100 MSVCRT__set_errno(GetLastError());
2104 /*********************************************************************
2105 * _wremove (MSVCRT.@)
2107 int _wremove(const WCHAR
*path
)
2109 TRACE("(%s)\n",debugstr_w(path
));
2110 if (DeleteFileW(path
))
2112 TRACE(":failed (%ld)\n",GetLastError());
2113 MSVCRT__set_errno(GetLastError());
2117 /*********************************************************************
2120 int MSVCRT_scanf(const char *format
, ...)
2125 va_start(valist
, format
);
2126 res
= MSVCRT_fscanf(MSVCRT_stdin
, format
, valist
);
2131 /*********************************************************************
2134 int MSVCRT_wscanf(const WCHAR
*format
, ...)
2139 va_start(valist
, format
);
2140 res
= MSVCRT_fwscanf(MSVCRT_stdin
, format
, valist
);
2145 /*********************************************************************
2148 int MSVCRT_rename(const char *oldpath
,const char *newpath
)
2150 TRACE(":from %s to %s\n",oldpath
,newpath
);
2151 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2153 TRACE(":failed (%ld)\n",GetLastError());
2154 MSVCRT__set_errno(GetLastError());
2158 /*********************************************************************
2159 * _wrename (MSVCRT.@)
2161 int _wrename(const WCHAR
*oldpath
,const WCHAR
*newpath
)
2163 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2164 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2166 TRACE(":failed (%ld)\n",GetLastError());
2167 MSVCRT__set_errno(GetLastError());
2171 /*********************************************************************
2172 * setvbuf (MSVCRT.@)
2174 int MSVCRT_setvbuf(MSVCRT_FILE
* file
, char *buf
, int mode
, MSVCRT_size_t size
)
2176 /* TODO: Check if file busy */
2178 MSVCRT_free(file
->_base
);
2182 if(mode
== MSVCRT__IOFBF
) {
2183 file
->_flag
&= ~MSVCRT__IONBF
;
2184 file
->_base
= file
->_ptr
= buf
;
2186 file
->_bufsiz
= size
;
2189 file
->_flag
|= MSVCRT__IONBF
;
2194 /*********************************************************************
2197 void MSVCRT_setbuf(MSVCRT_FILE
* file
, char *buf
)
2199 MSVCRT_setvbuf(file
, buf
, buf
? MSVCRT__IOFBF
: MSVCRT__IONBF
, MSVCRT_BUFSIZ
);
2202 /*********************************************************************
2205 char *MSVCRT_tmpnam(char *s
)
2207 char tmpbuf
[MAX_PATH
];
2208 char* prefix
= "TMP";
2209 if (!GetTempPathA(MAX_PATH
,tmpbuf
) ||
2210 !GetTempFileNameA(tmpbuf
,prefix
,0,MSVCRT_tmpname
))
2212 TRACE(":failed-last error (%ld)\n",GetLastError());
2215 TRACE(":got tmpnam %s\n",MSVCRT_tmpname
);
2220 /*********************************************************************
2221 * tmpfile (MSVCRT.@)
2223 MSVCRT_FILE
* MSVCRT_tmpfile(void)
2225 char *filename
= MSVCRT_tmpnam(NULL
);
2227 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
2229 return msvcrt_alloc_fp(fd
);
2233 /*********************************************************************
2234 * vfprintf (MSVCRT.@)
2236 int MSVCRT_vfprintf(MSVCRT_FILE
* file
, const char *format
, va_list valist
)
2238 char buf
[2048], *mem
= buf
;
2239 int written
, resize
= sizeof(buf
), retval
;
2240 /* There are two conventions for vsnprintf failing:
2241 * Return -1 if we truncated, or
2242 * Return the number of bytes that would have been written
2243 * The code below handles both cases
2245 while ((written
= vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
2248 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
2251 if (!(mem
= (char *)MSVCRT_malloc(resize
)))
2254 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2260 /*********************************************************************
2261 * vfwprintf (MSVCRT.@)
2263 * Is final char included in written (then resize is too big) or not
2264 * (then we must test for equality too)?
2266 int MSVCRT_vfwprintf(MSVCRT_FILE
* file
, const WCHAR
*format
, va_list valist
)
2268 WCHAR buf
[2048], *mem
= buf
;
2269 int written
, resize
= sizeof(buf
) / sizeof(WCHAR
), retval
;
2270 /* See vfprintf comments */
2271 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
2274 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(WCHAR
));
2277 if (!(mem
= (WCHAR
*)MSVCRT_malloc(resize
*sizeof(*mem
))))
2280 retval
= MSVCRT_fwrite(mem
, sizeof(*mem
), written
, file
);
2286 /*********************************************************************
2287 * vprintf (MSVCRT.@)
2289 int MSVCRT_vprintf(const char *format
, va_list valist
)
2291 return MSVCRT_vfprintf(MSVCRT_stdout
,format
,valist
);
2294 /*********************************************************************
2295 * vwprintf (MSVCRT.@)
2297 int MSVCRT_vwprintf(const WCHAR
*format
, va_list valist
)
2299 return MSVCRT_vfwprintf(MSVCRT_stdout
,format
,valist
);
2302 /*********************************************************************
2303 * fprintf (MSVCRT.@)
2305 int MSVCRT_fprintf(MSVCRT_FILE
* file
, const char *format
, ...)
2309 va_start(valist
, format
);
2310 res
= MSVCRT_vfprintf(file
, format
, valist
);
2315 /*********************************************************************
2316 * fwprintf (MSVCRT.@)
2318 int MSVCRT_fwprintf(MSVCRT_FILE
* file
, const WCHAR
*format
, ...)
2322 va_start(valist
, format
);
2323 res
= MSVCRT_vfwprintf(file
, format
, valist
);
2328 /*********************************************************************
2331 int MSVCRT_printf(const char *format
, ...)
2335 va_start(valist
, format
);
2336 res
= MSVCRT_vfprintf(MSVCRT_stdout
, format
, valist
);
2341 /*********************************************************************
2344 int MSVCRT_ungetc(int c
, MSVCRT_FILE
* file
)
2346 if(file
->_bufsiz
== 0 && !(file
->_flag
& MSVCRT__IONBF
)) {
2347 msvcrt_alloc_buffer(file
);
2350 if(file
->_ptr
>file
->_base
) {
2359 /*********************************************************************
2360 * ungetwc (MSVCRT.@)
2362 MSVCRT_wint_t
MSVCRT_ungetwc(MSVCRT_wint_t wc
, MSVCRT_FILE
* file
)
2365 char * pp
= (char *)&mwc
;
2367 for(i
=sizeof(WCHAR
)-1;i
>=0;i
--) {
2368 if(pp
[i
] != MSVCRT_ungetc(pp
[i
],file
))
2374 /*********************************************************************
2375 * wprintf (MSVCRT.@)
2377 int MSVCRT_wprintf(const WCHAR
*format
, ...)
2381 va_start(valist
, format
);
2382 res
= MSVCRT_vwprintf(format
, valist
);