2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/stdio/file.c
5 * PURPOSE: File CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
11 * msvcrt.dll file functions
13 * Copyright 1996,1998 Marcus Meissner
14 * Copyright 1996 Jukka Iivonen
15 * Copyright 1997,2000 Uwe Bonnes
16 * Copyright 2000 Jon Griffiths
17 * Copyright 2004 Eric Pouech
18 * Copyright 2004 Juan Lang
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 * Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
39 #include "wine/unicode.h"
41 #include <sys/utime.h>
43 int *__p__fmode(void);
44 int *__p___mb_cur_max(void);
71 /* for stat mode, permissions apply to all,owner and group */
72 #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
73 #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
74 #define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
76 /* _access() bit flags FIXME: incomplete */
77 /* defined in crt/io.h */
79 /* values for wxflag in file descriptor */
82 #define WX_READEOF 0x04 /* like ATEOF, but for underlying file rather than buffer */
83 #define WX_DONTINHERIT 0x10
84 #define WX_APPEND 0x20
87 /* FIXME: this should be allocated dynamically */
88 #define MAX_FILES 2048
90 /* ReactOS: Use _FDINFO instead! */
94 DWORD unkn
[7]; /* critical section and init flag */
97 /*static */ioinfo fdesc
[MAX_FILES
];
101 static int fdstart
= 3; /* first unallocated fd */
102 static int fdend
= 3; /* highest allocated fd */
104 static FILE* fstreams
[2048];
105 static int stream_idx
;
107 /* INTERNAL: process umask */
108 static int MSVCRT_umask
= 0;
110 /* INTERNAL: Static buffer for temp file name */
111 static char tmpname
[MAX_PATH
];
113 static const unsigned int EXE
= 'e' << 16 | 'x' << 8 | 'e';
114 static const unsigned int BAT
= 'b' << 16 | 'a' << 8 | 't';
115 static const unsigned int CMD
= 'c' << 16 | 'm' << 8 | 'd';
116 static const unsigned int COM
= 'c' << 16 | 'o' << 8 | 'm';
118 #define TOUL(x) (ULONGLONG)(x)
119 static const ULONGLONG WCEXE
= TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
120 static const ULONGLONG WCBAT
= TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
121 static const ULONGLONG WCCMD
= TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
122 static const ULONGLONG WCCOM
= TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
124 /* This critical section protects the tables fdesc and fstreams,
125 * and their related indexes, fdstart, fdend,
126 * and stream_idx, from race conditions.
127 * It doesn't protect against race conditions manipulating the underlying files
128 * or flags; doing so would probably be better accomplished with per-file
129 * protection, rather than locking the whole table for every change.
131 static CRITICAL_SECTION FILE_cs
;
132 #define LOCK_FILES() do { EnterCriticalSection(&FILE_cs); } while (0)
133 #define UNLOCK_FILES() do { LeaveCriticalSection(&FILE_cs); } while (0)
135 static void stat64_to_stat(const struct __stat64
*buf64
, struct _stat
*buf
)
137 buf
->st_dev
= buf64
->st_dev
;
138 buf
->st_ino
= buf64
->st_ino
;
139 buf
->st_mode
= buf64
->st_mode
;
140 buf
->st_nlink
= buf64
->st_nlink
;
141 buf
->st_uid
= buf64
->st_uid
;
142 buf
->st_gid
= buf64
->st_gid
;
143 buf
->st_rdev
= buf64
->st_rdev
;
144 buf
->st_size
= buf64
->st_size
;
145 buf
->st_atime
= buf64
->st_atime
;
146 buf
->st_mtime
= buf64
->st_mtime
;
147 buf
->st_ctime
= buf64
->st_ctime
;
150 static void stat64_to_stati64(const struct __stat64
*buf64
, struct _stati64
*buf
)
152 buf
->st_dev
= buf64
->st_dev
;
153 buf
->st_ino
= buf64
->st_ino
;
154 buf
->st_mode
= buf64
->st_mode
;
155 buf
->st_nlink
= buf64
->st_nlink
;
156 buf
->st_uid
= buf64
->st_uid
;
157 buf
->st_gid
= buf64
->st_gid
;
158 buf
->st_rdev
= buf64
->st_rdev
;
159 buf
->st_size
= buf64
->st_size
;
160 buf
->st_atime
= buf64
->st_atime
;
161 buf
->st_mtime
= buf64
->st_mtime
;
162 buf
->st_ctime
= buf64
->st_ctime
;
165 static inline BOOL
is_valid_fd(int fd
)
167 return fd
>= 0 && fd
< fdend
&& (fdesc
[fd
].wxflag
& WX_OPEN
);
170 /* INTERNAL: Get the HANDLE for a fd
171 * This doesn't lock the table, because a failure will result in
172 * INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
173 * it returns a valid handle which is about to be closed, a subsequent call
174 * will fail, most likely in a sane way.
176 static HANDLE
fdtoh(int fd
)
178 if (!is_valid_fd(fd
))
180 WARN(":fd (%d) - no handle!\n",fd
);
183 return INVALID_HANDLE_VALUE
;
185 if (fdesc
[fd
].handle
== INVALID_HANDLE_VALUE
) FIXME("wtf\n");
186 return fdesc
[fd
].handle
;
189 /* INTERNAL: free a file entry fd */
190 static void free_fd(int fd
)
193 fdesc
[fd
].handle
= INVALID_HANDLE_VALUE
;
194 fdesc
[fd
].wxflag
= 0;
195 TRACE(":fd (%d) freed\n",fd
);
196 if (fd
< 3) /* don't use 0,1,2 for user files */
200 case 0: SetStdHandle(STD_INPUT_HANDLE
, NULL
); break;
201 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, NULL
); break;
202 case 2: SetStdHandle(STD_ERROR_HANDLE
, NULL
); break;
215 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
216 /* caller must hold the files lock */
217 static int alloc_fd_from(HANDLE hand
, int flag
, int fd
)
221 WARN(":files exhausted!\n");
224 fdesc
[fd
].handle
= hand
;
225 fdesc
[fd
].wxflag
= WX_OPEN
| (flag
& (WX_DONTINHERIT
| WX_APPEND
| WX_TEXT
));
227 /* locate next free slot */
228 if (fd
== fdstart
&& fd
== fdend
)
231 while (fdstart
< fdend
&&
232 fdesc
[fdstart
].handle
!= INVALID_HANDLE_VALUE
)
234 /* update last fd in use */
237 TRACE("fdstart is %d, fdend is %d\n", fdstart
, fdend
);
241 case 0: SetStdHandle(STD_INPUT_HANDLE
, hand
); break;
242 case 1: SetStdHandle(STD_OUTPUT_HANDLE
, hand
); break;
243 case 2: SetStdHandle(STD_ERROR_HANDLE
, hand
); break;
249 /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
250 /*static */int alloc_fd(HANDLE hand
, int flag
)
255 TRACE(":handle (%p) allocating fd (%d)\n",hand
,fdstart
);
256 ret
= alloc_fd_from(hand
, flag
, fdstart
);
261 /* INTERNAL: Allocate a FILE* for an fd slot */
262 /* caller must hold the files lock */
263 static FILE* alloc_fp(void)
267 for (i
= 3; i
< sizeof(fstreams
) / sizeof(fstreams
[0]); i
++)
269 if (!fstreams
[i
] || fstreams
[i
]->_flag
== 0)
273 if (!(fstreams
[i
] = calloc(sizeof(FILE),1)))
275 if (i
== stream_idx
) stream_idx
++;
283 /* INTERNAL: initialize a FILE* from an open fd */
284 static int init_fp(FILE* file
, int fd
, unsigned stream_flags
)
286 TRACE(":fd (%d) allocating FILE*\n",fd
);
287 if (!is_valid_fd(fd
))
289 WARN(":invalid fd %d\n",fd
);
294 memset(file
, 0, sizeof(*file
));
296 file
->_flag
= stream_flags
;
298 TRACE(":got FILE* (%p)\n",file
);
302 /* INTERNAL: Create an inheritance data block (for spawned process)
303 * The inheritance block is made of:
304 * 00 int nb of file descriptor (NBFD)
305 * 04 char file flags (wxflag): repeated for each fd
306 * 4+NBFD HANDLE file handle: repeated for each fd
308 unsigned create_io_inherit_block(WORD
*size
, BYTE
**block
)
314 *size
= sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE
)) * fdend
;
315 *block
= calloc(*size
, 1);
321 wxflag_ptr
= (char*)*block
+ sizeof(unsigned);
322 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
324 *(unsigned*)*block
= fdend
;
325 for (fd
= 0; fd
< fdend
; fd
++)
327 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
328 if ((fdesc
[fd
].wxflag
& (WX_OPEN
| WX_DONTINHERIT
)) == WX_OPEN
)
330 *wxflag_ptr
= fdesc
[fd
].wxflag
;
331 *handle_ptr
= fdesc
[fd
].handle
;
336 *handle_ptr
= INVALID_HANDLE_VALUE
;
338 wxflag_ptr
++; handle_ptr
++;
343 /* INTERNAL: Set up all file descriptors,
344 * as well as default streams (stdin, stderr and stdout)
346 void msvcrt_init_io(void)
351 InitializeCriticalSection(&FILE_cs
);
352 FILE_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FILE_cs");
353 GetStartupInfoA(&si
);
354 if (si
.cbReserved2
!= 0 && si
.lpReserved2
!= NULL
)
359 fdend
= *(unsigned*)si
.lpReserved2
;
361 wxflag_ptr
= (char*)(si
.lpReserved2
+ sizeof(unsigned));
362 handle_ptr
= (HANDLE
*)(wxflag_ptr
+ fdend
* sizeof(char));
364 fdend
= min(fdend
, sizeof(fdesc
) / sizeof(fdesc
[0]));
365 for (i
= 0; i
< fdend
; i
++)
367 if ((*wxflag_ptr
& WX_OPEN
) && *handle_ptr
!= INVALID_HANDLE_VALUE
)
369 fdesc
[i
].wxflag
= *wxflag_ptr
;
370 fdesc
[i
].handle
= *handle_ptr
;
375 fdesc
[i
].handle
= INVALID_HANDLE_VALUE
;
377 wxflag_ptr
++; handle_ptr
++;
379 for (fdstart
= 3; fdstart
< fdend
; fdstart
++)
380 if (fdesc
[fdstart
].handle
== INVALID_HANDLE_VALUE
) break;
383 if (!(fdesc
[0].wxflag
& WX_OPEN
) || fdesc
[0].handle
== INVALID_HANDLE_VALUE
)
386 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE
),
387 GetCurrentProcess(), &fdesc
[0].handle
, 0, TRUE
,
388 DUPLICATE_SAME_ACCESS
);
390 fdesc
[0].handle
= GetStdHandle(STD_INPUT_HANDLE
);
391 if (fdesc
[0].handle
== NULL
)
392 fdesc
[0].handle
= INVALID_HANDLE_VALUE
;
394 fdesc
[0].wxflag
= WX_OPEN
| WX_TEXT
;
396 if (!(fdesc
[1].wxflag
& WX_OPEN
) || fdesc
[1].handle
== INVALID_HANDLE_VALUE
)
399 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE
),
400 GetCurrentProcess(), &fdesc
[1].handle
, 0, TRUE
,
401 DUPLICATE_SAME_ACCESS
);
403 fdesc
[1].handle
= GetStdHandle(STD_OUTPUT_HANDLE
);
404 if (fdesc
[1].handle
== NULL
)
405 fdesc
[1].handle
= INVALID_HANDLE_VALUE
;
407 fdesc
[1].wxflag
= WX_OPEN
| WX_TEXT
;
409 if (!(fdesc
[2].wxflag
& WX_OPEN
) || fdesc
[2].handle
== INVALID_HANDLE_VALUE
)
412 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE
),
413 GetCurrentProcess(), &fdesc
[2].handle
, 0, TRUE
,
414 DUPLICATE_SAME_ACCESS
);
416 fdesc
[2].handle
= GetStdHandle(STD_ERROR_HANDLE
);
417 if (fdesc
[2].handle
== NULL
)
418 fdesc
[2].handle
= INVALID_HANDLE_VALUE
;
420 fdesc
[2].wxflag
= WX_OPEN
| WX_TEXT
;
423 TRACE(":handles (%p)(%p)(%p)\n",fdesc
[0].handle
,
424 fdesc
[1].handle
,fdesc
[2].handle
);
426 memset(_iob
,0,3*sizeof(FILE));
427 for (i
= 0; i
< 3; i
++)
429 /* FILE structs for stdin/out/err are static and never deleted */
430 fstreams
[i
] = &_iob
[i
];
432 _iob
[i
]._tmpfname
= NULL
;
433 _iob
[i
]._flag
= (i
== 0) ? _IOREAD
: _IOWRT
;
438 /* INTERNAL: Flush stdio file buffer */
439 static int flush_buffer(FILE* file
)
442 int cnt
=file
->_ptr
-file
->_base
;
443 if(cnt
>0 && _write(file
->_file
, file
->_base
, cnt
) != cnt
) {
444 file
->_flag
|= _IOERR
;
447 file
->_ptr
=file
->_base
;
448 file
->_cnt
=file
->_bufsiz
;
453 /* INTERNAL: Allocate stdio file buffer */
454 static void alloc_buffer(FILE* file
)
456 file
->_base
= calloc(BUFSIZ
,1);
458 file
->_bufsiz
= BUFSIZ
;
459 file
->_flag
|= _IOMYBUF
;
461 file
->_base
= (char*)(&file
->_charbuf
);
463 file
->_bufsiz
= sizeof(file
->_charbuf
);
465 file
->_ptr
= file
->_base
;
469 /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
470 static void int_to_base32(int num
, char *str
)
485 *p
= (num
& 31) + '0';
487 *p
+= ('a' - '0' - 10);
492 /*********************************************************************
495 FILE * CDECL
__p__iob(void)
500 /*********************************************************************
503 int CDECL
_access(const char *filename
, int mode
)
505 DWORD attr
= GetFileAttributesA(filename
);
507 TRACE("(%s,%d) %d\n",filename
,mode
,attr
);
509 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
511 __set_errno(GetLastError());
514 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
516 __set_errno(ERROR_ACCESS_DENIED
);
522 /*********************************************************************
523 * _waccess (MSVCRT.@)
525 int CDECL
_waccess(const wchar_t *filename
, int mode
)
527 DWORD attr
= GetFileAttributesW(filename
);
529 TRACE("(%s,%d) %d\n",debugstr_w(filename
),mode
,attr
);
531 if (!filename
|| attr
== INVALID_FILE_ATTRIBUTES
)
533 __set_errno(GetLastError());
536 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
538 __set_errno(ERROR_ACCESS_DENIED
);
544 /*********************************************************************
547 int CDECL
_chmod(const char *path
, int flags
)
549 DWORD oldFlags
= GetFileAttributesA(path
);
551 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
553 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
554 oldFlags
| FILE_ATTRIBUTE_READONLY
;
556 if (newFlags
== oldFlags
|| SetFileAttributesA(path
, newFlags
))
559 __set_errno(GetLastError());
563 /*********************************************************************
566 int CDECL
_wchmod(const wchar_t *path
, int flags
)
568 DWORD oldFlags
= GetFileAttributesW(path
);
570 if (oldFlags
!= INVALID_FILE_ATTRIBUTES
)
572 DWORD newFlags
= (flags
& _S_IWRITE
)? oldFlags
& ~FILE_ATTRIBUTE_READONLY
:
573 oldFlags
| FILE_ATTRIBUTE_READONLY
;
575 if (newFlags
== oldFlags
|| SetFileAttributesW(path
, newFlags
))
578 __set_errno(GetLastError());
582 /*********************************************************************
585 int CDECL
_unlink(const char *path
)
587 TRACE("%s\n",debugstr_a(path
));
588 if(DeleteFileA(path
))
590 TRACE("failed (%d)\n",GetLastError());
591 __set_errno(GetLastError());
595 /*********************************************************************
596 * _wunlink (MSVCRT.@)
598 int CDECL
_wunlink(const wchar_t *path
)
600 TRACE("(%s)\n",debugstr_w(path
));
601 if(DeleteFileW(path
))
603 TRACE("failed (%d)\n",GetLastError());
604 __set_errno(GetLastError());
608 /* _flushall calls fflush which calls _flushall */
609 int CDECL
fflush(FILE* file
);
611 /*********************************************************************
612 * _flushall (MSVCRT.@)
614 int CDECL
_flushall(void)
616 int i
, num_flushed
= 0;
619 for (i
= 3; i
< stream_idx
; i
++)
620 if (fstreams
[i
] && fstreams
[i
]->_flag
)
623 /* FIXME: flush, do not commit */
624 if (_commit(i
) == -1)
626 fstreams
[i
]->_flag
|= _IOERR
;
628 if(fstreams
[i
]->_flag
& _IOWRT
) {
635 TRACE(":flushed (%d) handles\n",num_flushed
);
639 /*********************************************************************
642 int CDECL
fflush(FILE* file
)
646 } else if(file
->_flag
& _IOWRT
) {
647 int res
=flush_buffer(file
);
653 /*********************************************************************
656 int CDECL
_close(int fd
)
663 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
664 if (hand
== INVALID_HANDLE_VALUE
)
666 else if (!CloseHandle(hand
))
668 WARN(":failed-last error (%d)\n",GetLastError());
669 __set_errno(GetLastError());
682 /*********************************************************************
685 int CDECL
_commit(int fd
)
687 HANDLE hand
= fdtoh(fd
);
689 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
690 if (hand
== INVALID_HANDLE_VALUE
)
693 if (!FlushFileBuffers(hand
))
695 if (GetLastError() == ERROR_INVALID_HANDLE
)
697 /* FlushFileBuffers fails for console handles
698 * so we ignore this error.
702 TRACE(":failed-last error (%d)\n",GetLastError());
703 __set_errno(GetLastError());
710 /*********************************************************************
713 * MSDN isn't clear on this point, but the remarks for _pipe
714 * indicate file descriptors duplicated with _dup and _dup2 are always
717 int CDECL
_dup2(int od
, int nd
)
721 TRACE("(od=%d, nd=%d)\n", od
, nd
);
723 if (nd
< MAX_FILES
&& is_valid_fd(od
))
727 if (DuplicateHandle(GetCurrentProcess(), fdesc
[od
].handle
,
728 GetCurrentProcess(), &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
))
730 int wxflag
= fdesc
[od
].wxflag
& ~_O_NOINHERIT
;
734 ret
= alloc_fd_from(handle
, wxflag
, nd
);
742 /* _dup2 returns 0, not nd, on success */
749 __set_errno(GetLastError());
761 /*********************************************************************
764 int CDECL
_dup(int od
)
770 if (_dup2(od
, fd
) == 0)
778 /*********************************************************************
781 int CDECL
_eof(int fd
)
784 LONG hcurpos
,hendpos
;
785 HANDLE hand
= fdtoh(fd
);
787 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
789 if (hand
== INVALID_HANDLE_VALUE
)
792 if (fdesc
[fd
].wxflag
& WX_ATEOF
) return TRUE
;
794 /* Otherwise we do it the hard way */
795 hcurpos
= hendpos
= 0;
796 curpos
= SetFilePointer(hand
, 0, &hcurpos
, FILE_CURRENT
);
797 endpos
= SetFilePointer(hand
, 0, &hendpos
, FILE_END
);
799 if (curpos
== endpos
&& hcurpos
== hendpos
)
801 /* FIXME: shouldn't WX_ATEOF be set here? */
805 SetFilePointer(hand
, curpos
, &hcurpos
, FILE_BEGIN
);
809 /*********************************************************************
810 * _fcloseall (MSVCRT.@)
812 int CDECL
_fcloseall(void)
814 int num_closed
= 0, i
;
817 for (i
= 3; i
< stream_idx
; i
++)
818 if (fstreams
[i
] && fstreams
[i
]->_flag
&&
819 !fclose(fstreams
[i
]))
823 TRACE(":closed (%d) handles\n",num_closed
);
827 /* free everything on process exit */
828 void msvcrt_free_io(void)
831 /* The Win32 _fcloseall() function explicitly doesn't close stdin,
832 * stdout, and stderr (unlike GNU), so we need to fclose() them here
833 * or they won't get flushed.
838 FILE_cs
.DebugInfo
->Spare
[0] = 0;
839 DeleteCriticalSection(&FILE_cs
);
842 /*********************************************************************
843 * _lseeki64 (MSVCRT.@)
845 __int64 CDECL
_lseeki64(int fd
, __int64 offset
, int whence
)
847 HANDLE hand
= fdtoh(fd
);
848 LARGE_INTEGER ofs
, ret
;
850 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
851 if (hand
== INVALID_HANDLE_VALUE
)
854 if (whence
< 0 || whence
> 2)
860 TRACE(":fd (%d) to %s pos %s\n",
861 fd
,wine_dbgstr_longlong(offset
),
862 (whence
==SEEK_SET
)?"SEEK_SET":
863 (whence
==SEEK_CUR
)?"SEEK_CUR":
864 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
866 ofs
.QuadPart
= offset
;
867 if (SetFilePointerEx(hand
, ofs
, &ret
, whence
))
869 fdesc
[fd
].wxflag
&= ~(WX_ATEOF
|WX_READEOF
);
870 /* FIXME: What if we seek _to_ EOF - is EOF set? */
874 TRACE(":error-last error (%d)\n",GetLastError());
875 __set_errno(GetLastError());
879 /*********************************************************************
882 LONG CDECL
_lseek(int fd
, LONG offset
, int whence
)
884 return _lseeki64(fd
, offset
, whence
);
887 /*********************************************************************
888 * _locking (MSVCRT.@)
890 * This is untested; the underlying LockFile doesn't work yet.
892 int CDECL
_locking(int fd
, int mode
, LONG nbytes
)
896 HANDLE hand
= fdtoh(fd
);
898 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
899 if (hand
== INVALID_HANDLE_VALUE
)
902 if (mode
< 0 || mode
> 4)
908 TRACE(":fd (%d) by 0x%08x mode %s\n",
909 fd
,nbytes
,(mode
==_LK_UNLCK
)?"_LK_UNLCK":
910 (mode
==_LK_LOCK
)?"_LK_LOCK":
911 (mode
==_LK_NBLCK
)?"_LK_NBLCK":
912 (mode
==_LK_RLCK
)?"_LK_RLCK":
913 (mode
==_LK_NBRLCK
)?"_LK_NBRLCK":
916 if ((cur_locn
= SetFilePointer(hand
, 0L, NULL
, SEEK_CUR
)) == INVALID_SET_FILE_POINTER
)
918 FIXME ("Seek failed\n");
919 *_errno() = EINVAL
; /* FIXME */
922 if (mode
== _LK_LOCK
|| mode
== _LK_RLCK
)
925 ret
= 1; /* just to satisfy gcc */
928 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
933 else if (mode
== _LK_UNLCK
)
934 ret
= UnlockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
936 ret
= LockFile(hand
, cur_locn
, 0L, nbytes
, 0L);
937 /* FIXME - what about error settings? */
941 /*********************************************************************
944 int CDECL
fseek(FILE* file
, long offset
, int whence
)
946 /* Flush output if needed */
947 if(file
->_flag
& _IOWRT
)
950 if(whence
== SEEK_CUR
&& file
->_flag
& _IOREAD
) {
951 offset
-= file
->_cnt
;
953 /* Discard buffered input */
955 file
->_ptr
= file
->_base
;
956 /* Reset direction of i/o */
957 if(file
->_flag
& _IORW
) {
958 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
960 /* Clear end of file flag */
961 file
->_flag
&= ~_IOEOF
;
962 return (_lseek(file
->_file
,offset
,whence
) == -1)?-1:0;
965 /*********************************************************************
968 int CDECL
_chsize(int fd
, long size
)
974 TRACE("(fd=%d, size=%ld)\n", fd
, size
);
979 if (handle
!= INVALID_HANDLE_VALUE
)
981 /* save the current file pointer */
982 cur
= _lseek(fd
, 0, SEEK_CUR
);
985 pos
= _lseek(fd
, size
, SEEK_SET
);
988 ret
= SetEndOfFile(handle
);
989 if (!ret
) __set_errno(GetLastError());
992 /* restore the file pointer */
993 _lseek(fd
, cur
, SEEK_SET
);
1001 /*********************************************************************
1002 * clearerr (MSVCRT.@)
1004 void CDECL
clearerr(FILE* file
)
1006 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1007 file
->_flag
&= ~(_IOERR
| _IOEOF
);
1010 /*********************************************************************
1013 void CDECL
rewind(FILE* file
)
1015 TRACE(":file (%p) fd (%d)\n",file
,file
->_file
);
1016 fseek(file
, 0L, SEEK_SET
);
1020 static int get_flags(const char* mode
, int *open_flags
, int* stream_flags
)
1022 int plus
= strchr(mode
, '+') != NULL
;
1027 *open_flags
= plus
? _O_RDWR
: _O_RDONLY
;
1028 *stream_flags
= plus
? _IORW
: _IOREAD
;
1031 *open_flags
= _O_CREAT
| _O_TRUNC
| (plus
? _O_RDWR
: _O_WRONLY
);
1032 *stream_flags
= plus
? _IORW
: _IOWRT
;
1035 *open_flags
= _O_CREAT
| _O_APPEND
| (plus
? _O_RDWR
: _O_WRONLY
);
1036 *stream_flags
= plus
? _IORW
: _IOWRT
;
1046 *open_flags
|= _O_BINARY
;
1047 *open_flags
&= ~_O_TEXT
;
1050 *open_flags
|= _O_TEXT
;
1051 *open_flags
&= ~_O_BINARY
;
1056 FIXME(":unknown flag %c not supported\n",mode
[-1]);
1061 /*********************************************************************
1062 * _fdopen (MSVCRT.@)
1064 FILE* CDECL
_fdopen(int fd
, const char *mode
)
1066 int open_flags
, stream_flags
;
1069 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1) return NULL
;
1072 if (!(file
= alloc_fp()))
1074 else if (init_fp(file
, fd
, stream_flags
) == -1)
1079 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
1085 /*********************************************************************
1086 * _wfdopen (MSVCRT.@)
1088 FILE* CDECL
_wfdopen(int fd
, const wchar_t *mode
)
1090 unsigned mlen
= strlenW(mode
);
1091 char *modea
= calloc(mlen
+ 1, 1);
1093 int open_flags
, stream_flags
;
1096 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
1098 if (get_flags(modea
, &open_flags
, &stream_flags
) == -1) return NULL
;
1100 if (!(file
= alloc_fp()))
1102 else if (init_fp(file
, fd
, stream_flags
) == -1)
1110 rewind(file
); /* FIXME: is this needed ??? */
1111 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,debugstr_w(mode
),file
);
1118 /*********************************************************************
1119 * _filelength (MSVCRT.@)
1121 LONG CDECL
_filelength(int fd
)
1123 LONG curPos
= _lseek(fd
, 0, SEEK_CUR
);
1126 LONG endPos
= _lseek(fd
, 0, SEEK_END
);
1129 if (endPos
!= curPos
)
1130 _lseek(fd
, curPos
, SEEK_SET
);
1137 /*********************************************************************
1138 * _filelengthi64 (MSVCRT.@)
1140 __int64 CDECL
_filelengthi64(int fd
)
1142 __int64 curPos
= _lseeki64(fd
, 0, SEEK_CUR
);
1145 __int64 endPos
= _lseeki64(fd
, 0, SEEK_END
);
1148 if (endPos
!= curPos
)
1149 _lseeki64(fd
, curPos
, SEEK_SET
);
1156 /*********************************************************************
1157 * _fileno (MSVCRT.@)
1159 int CDECL
_fileno(FILE* file
)
1161 TRACE(":FILE* (%p) fd (%d)\n",file
,file
->_file
);
1165 /*********************************************************************
1166 * _fstat64 (MSVCRT.@)
1168 int CDECL
_fstat64(int fd
, struct __stat64
* buf
)
1172 BY_HANDLE_FILE_INFORMATION hfi
;
1173 HANDLE hand
= fdtoh(fd
);
1175 TRACE(":fd (%d) stat (%p)\n",fd
,buf
);
1176 if (hand
== INVALID_HANDLE_VALUE
)
1181 WARN(":failed-NULL buf\n");
1182 __set_errno(ERROR_INVALID_PARAMETER
);
1186 memset(&hfi
, 0, sizeof(hfi
));
1187 memset(buf
, 0, sizeof(struct __stat64
));
1188 type
= GetFileType(hand
);
1189 if (type
== FILE_TYPE_PIPE
)
1191 buf
->st_dev
= buf
->st_rdev
= fd
;
1192 buf
->st_mode
= S_IFIFO
;
1195 else if (type
== FILE_TYPE_CHAR
)
1197 buf
->st_dev
= buf
->st_rdev
= fd
;
1198 buf
->st_mode
= S_IFCHR
;
1201 else /* FILE_TYPE_DISK etc. */
1203 if (!GetFileInformationByHandle(hand
, &hfi
))
1205 WARN(":failed-last error (%d)\n",GetLastError());
1206 __set_errno(ERROR_INVALID_PARAMETER
);
1209 buf
->st_mode
= S_IFREG
| S_IREAD
;
1210 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1211 buf
->st_mode
|= S_IWRITE
;
1212 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1213 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1215 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1216 buf
->st_mtime
= buf
->st_ctime
= dw
;
1217 buf
->st_nlink
= hfi
.nNumberOfLinks
;
1219 TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi
.dwFileAttributes
,
1224 /*********************************************************************
1225 * _fstati64 (MSVCRT.@)
1227 int CDECL
_fstati64(int fd
, struct _stati64
* buf
)
1230 struct __stat64 buf64
;
1232 ret
= _fstat64(fd
, &buf64
);
1234 stat64_to_stati64(&buf64
, buf
);
1238 /*********************************************************************
1241 int CDECL
_fstat(int fd
, struct _stat
* buf
)
1243 struct __stat64 buf64
;
1245 ret
= _fstat64(fd
, &buf64
);
1247 stat64_to_stat(&buf64
, buf
);
1251 /*********************************************************************
1252 * _futime (MSVCRT.@)
1254 int CDECL
_futime(int fd
, struct _utimbuf
*t
)
1256 HANDLE hand
= fdtoh(fd
);
1263 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
1268 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
1269 if (t
->actime
== t
->modtime
)
1272 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
1275 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1277 __set_errno(GetLastError());
1283 /*********************************************************************
1284 * _get_osfhandle (MSVCRT.@)
1286 long CDECL
_get_osfhandle(int fd
)
1288 HANDLE hand
= fdtoh(fd
);
1289 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1294 /*********************************************************************
1295 * _isatty (MSVCRT.@)
1297 int CDECL
_isatty(int fd
)
1299 HANDLE hand
= fdtoh(fd
);
1301 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1302 if (hand
== INVALID_HANDLE_VALUE
)
1305 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1308 /*********************************************************************
1309 * _mktemp (MSVCRT.@)
1311 char * CDECL
_mktemp(char *pattern
)
1314 char *retVal
= pattern
;
1319 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1323 id
= GetCurrentProcessId();
1327 int tempNum
= id
/ 10;
1328 *pattern
-- = id
- (tempNum
* 10) + '0';
1334 *pattern
= letter
++;
1335 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1336 GetLastError() == ERROR_FILE_NOT_FOUND
)
1338 } while(letter
<= 'z');
1342 /*********************************************************************
1343 * _wmktemp (MSVCRT.@)
1345 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1348 wchar_t *retVal
= pattern
;
1350 wchar_t letter
= 'a';
1353 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1357 id
= GetCurrentProcessId();
1361 int tempNum
= id
/ 10;
1362 *pattern
-- = id
- (tempNum
* 10) + '0';
1368 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1369 GetLastError() == ERROR_FILE_NOT_FOUND
)
1371 *pattern
= letter
++;
1372 } while(letter
!= '|');
1376 /*static */unsigned split_oflags(unsigned oflags
)
1379 unsigned unsupp
; /* until we support everything */
1381 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1382 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1383 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1384 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1385 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1386 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1388 if ((unsupp
= oflags
& ~(
1389 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1390 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1391 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1393 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1395 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1400 /*********************************************************************
1403 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1406 SECURITY_ATTRIBUTES sa
;
1407 HANDLE readHandle
, writeHandle
;
1415 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1416 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1417 sa
.lpSecurityDescriptor
= NULL
;
1418 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1420 unsigned int wxflags
= split_oflags(textmode
);
1424 fd
= alloc_fd(readHandle
, wxflags
);
1428 fd
= alloc_fd(writeHandle
, wxflags
);
1437 CloseHandle(writeHandle
);
1443 CloseHandle(readHandle
);
1444 CloseHandle(writeHandle
);
1450 __set_errno(GetLastError());
1455 /*********************************************************************
1458 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1462 DWORD access
= 0, creation
= 0, attrib
;
1466 SECURITY_ATTRIBUTES sa
;
1469 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1470 path
, oflags
, shflags
);
1472 wxflag
= split_oflags(oflags
);
1473 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1475 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1476 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1477 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1480 if (oflags
& _O_CREAT
)
1482 va_start(ap
, shflags
);
1483 pmode
= va_arg(ap
, int);
1486 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1487 FIXME(": pmode 0x%04x ignored\n", pmode
);
1489 WARN(": pmode 0x%04x ignored\n", pmode
);
1491 if (oflags
& _O_EXCL
)
1492 creation
= CREATE_NEW
;
1493 else if (oflags
& _O_TRUNC
)
1494 creation
= CREATE_ALWAYS
;
1496 creation
= OPEN_ALWAYS
;
1498 else /* no _O_CREAT */
1500 if (oflags
& _O_TRUNC
)
1501 creation
= TRUNCATE_EXISTING
;
1503 creation
= OPEN_EXISTING
;
1512 sharing
= FILE_SHARE_READ
;
1515 sharing
= FILE_SHARE_WRITE
;
1518 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1521 ERR( "Unhandled shflags 0x%x\n", shflags
);
1524 attrib
= FILE_ATTRIBUTE_NORMAL
;
1526 if (oflags
& _O_TEMPORARY
)
1528 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1530 sharing
|= FILE_SHARE_DELETE
;
1533 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1534 sa
.lpSecurityDescriptor
= NULL
;
1535 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1537 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1539 if (hand
== INVALID_HANDLE_VALUE
) {
1540 WARN(":failed-last error (%d)\n",GetLastError());
1541 __set_errno(GetLastError());
1545 fd
= alloc_fd(hand
, wxflag
);
1547 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1551 /*********************************************************************
1552 * _wsopen (MSVCRT.@)
1554 int CDECL
_wsopen( const wchar_t* path
, int oflags
, int shflags
, ... )
1556 const unsigned int len
= strlenW(path
);
1557 char *patha
= calloc(len
+ 1,1);
1561 va_start(ap
, shflags
);
1562 pmode
= va_arg(ap
, int);
1565 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1567 int retval
= _sopen(patha
,oflags
,shflags
,pmode
);
1572 __set_errno(GetLastError());
1576 /*********************************************************************
1579 int CDECL
_open( const char *path
, int flags
, ... )
1583 if (flags
& _O_CREAT
)
1586 va_start(ap
, flags
);
1587 pmode
= va_arg(ap
, int);
1589 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1592 return _sopen( path
, flags
, _SH_DENYNO
);
1595 /*********************************************************************
1598 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1600 const unsigned int len
= strlenW(path
);
1601 char *patha
= calloc(len
+ 1,1);
1605 va_start(ap
, flags
);
1606 pmode
= va_arg(ap
, int);
1609 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1611 int retval
= _open(patha
,flags
,pmode
);
1616 __set_errno(GetLastError());
1620 /*********************************************************************
1623 int CDECL
_creat(const char *path
, int flags
)
1625 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1626 return _open(path
, usedFlags
);
1629 /*********************************************************************
1630 * _wcreat (MSVCRT.@)
1632 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1634 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1635 return _wopen(path
, usedFlags
);
1638 /*********************************************************************
1639 * _open_osfhandle (MSVCRT.@)
1641 int CDECL
_open_osfhandle(long handle
, int oflags
)
1645 /* _O_RDONLY (0) always matches, so set the read flag
1646 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1647 * file, so set the write flag. It also only sets _O_TEXT if it wants
1648 * text - it never sets _O_BINARY.
1650 /* don't let split_oflags() decide the mode if no mode is passed */
1651 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1652 oflags
|= _O_BINARY
;
1654 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1655 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1659 /*********************************************************************
1662 int CDECL
_rmtmp(void)
1664 int num_removed
= 0, i
;
1667 for (i
= 3; i
< stream_idx
; i
++)
1668 if (fstreams
[i
] && fstreams
[i
]->_tmpfname
)
1670 fclose(fstreams
[i
]);
1676 TRACE(":removed (%d) temp files\n",num_removed
);
1680 /*********************************************************************
1681 * (internal) remove_cr
1683 * Translate all \r\n to \n inplace.
1684 * return the number of \r removed
1685 * Corner cases required by some apps:
1687 * BUG: should save state across calls somehow, so CR LF that
1688 * straddles buffer boundary gets recognized properly?
1690 static unsigned int remove_cr(char *buf
, unsigned int count
)
1694 for (i
=0, j
=0; j
< count
; j
++)
1695 if ((buf
[j
] != '\r') || ((j
+1) < count
&& buf
[j
+1] != '\n'))
1701 /*********************************************************************
1704 static int read_i(int fd
, void *buf
, unsigned int count
)
1707 char *bufstart
= buf
;
1708 HANDLE hand
= fdtoh(fd
);
1710 if (fdesc
[fd
].wxflag
& WX_READEOF
) {
1711 fdesc
[fd
].wxflag
|= WX_ATEOF
;
1712 TRACE("already at EOF, returning 0\n");
1715 /* Don't trace small reads, it gets *very* annoying */
1717 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1718 if (hand
== INVALID_HANDLE_VALUE
)
1721 /* Reading single bytes in O_TEXT mode makes things slow
1722 * So read big chunks
1724 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1726 if (fdesc
[fd
].wxflag
& WX_TEXT
)
1729 /* in text mode, a ctrl-z signals EOF */
1730 for (i
=0; i
<num_read
; i
++)
1732 if (bufstart
[i
] == 0x1a)
1735 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1736 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1741 if (count
!= 0 && num_read
== 0)
1743 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1744 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1749 if (GetLastError() == ERROR_BROKEN_PIPE
)
1751 TRACE(":end-of-pipe\n");
1752 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1757 TRACE(":failed-last error (%d)\n",GetLastError());
1763 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1767 /*********************************************************************
1770 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1773 num_read
= read_i(fd
, buf
, count
);
1774 if (num_read
>0 && fdesc
[fd
].wxflag
& WX_TEXT
)
1776 num_read
-= remove_cr(buf
,num_read
);
1781 /*********************************************************************
1782 * _setmode (MSVCRT.@)
1784 int CDECL
_setmode(int fd
,int mode
)
1786 int ret
= fdesc
[fd
].wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1787 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1788 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1789 if ((mode
& _O_TEXT
) == _O_TEXT
)
1790 fdesc
[fd
].wxflag
|= WX_TEXT
;
1792 fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1796 /*********************************************************************
1797 * _stat64 (MSVCRT.@)
1799 int CDECL
_stat64(const char* path
, struct __stat64
* buf
)
1802 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1803 unsigned short mode
= ALL_S_IREAD
;
1806 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1808 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1810 TRACE("failed (%d)\n",GetLastError());
1811 __set_errno(ERROR_FILE_NOT_FOUND
);
1815 memset(buf
,0,sizeof(struct __stat64
));
1817 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1818 Bon 011120: This FIXME seems incorrect
1819 Also a letter as first char isn't enough to be classified
1822 if (isalpha(*path
)&& (*(path
+1)==':'))
1823 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1825 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1827 plen
= strlen(path
);
1829 /* Dir, or regular file? */
1830 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1831 (path
[plen
-1] == '\\'))
1832 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
1837 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1839 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1840 (tolower(path
[plen
-3]) << 16);
1841 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1842 mode
|= ALL_S_IEXEC
;
1846 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1847 mode
|= ALL_S_IWRITE
;
1849 buf
->st_mode
= mode
;
1851 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1852 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1854 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1855 buf
->st_mtime
= buf
->st_ctime
= dw
;
1856 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1857 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1858 (long)buf
->st_atime
,(long)buf
->st_mtime
,(long)buf
->st_ctime
);
1862 /*********************************************************************
1863 * _stati64 (MSVCRT.@)
1865 int CDECL
_stati64(const char* path
, struct _stati64
* buf
)
1868 struct __stat64 buf64
;
1870 ret
= _stat64(path
, &buf64
);
1872 stat64_to_stati64(&buf64
, buf
);
1876 /*********************************************************************
1879 int CDECL
_stat(const char* path
, struct _stat
* buf
)
1881 struct __stat64 buf64
;
1883 ret
= _stat64( path
, &buf64
);
1885 stat64_to_stat(&buf64
, buf
);
1889 /*********************************************************************
1890 * _wstat64 (MSVCRT.@)
1892 int CDECL
_wstat64(const wchar_t* path
, struct __stat64
* buf
)
1895 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1896 unsigned short mode
= ALL_S_IREAD
;
1899 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1901 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1903 TRACE("failed (%d)\n",GetLastError());
1904 __set_errno(ERROR_FILE_NOT_FOUND
);
1908 memset(buf
,0,sizeof(struct __stat64
));
1910 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1911 if (iswalpha(*path
))
1912 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1914 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1916 plen
= strlenW(path
);
1918 /* Dir, or regular file? */
1919 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1920 (path
[plen
-1] == '\\'))
1921 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
1926 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1928 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1929 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1930 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1931 mode
|= ALL_S_IEXEC
;
1935 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1936 mode
|= ALL_S_IWRITE
;
1938 buf
->st_mode
= mode
;
1940 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1941 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1943 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1944 buf
->st_mtime
= buf
->st_ctime
= dw
;
1945 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1946 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1947 (long)buf
->st_atime
,(long)buf
->st_mtime
,(long)buf
->st_ctime
);
1951 /*********************************************************************
1952 * _wstati64 (MSVCRT.@)
1954 int CDECL
_wstati64(const wchar_t* path
, struct _stati64
* buf
)
1957 struct __stat64 buf64
;
1959 ret
= _wstat64(path
, &buf64
);
1961 stat64_to_stati64(&buf64
, buf
);
1965 /*********************************************************************
1968 int CDECL
_wstat(const wchar_t* path
, struct _stat
* buf
)
1971 struct __stat64 buf64
;
1973 ret
= _wstat64( path
, &buf64
);
1974 if (!ret
) stat64_to_stat(&buf64
, buf
);
1978 /*********************************************************************
1981 long CDECL
_tell(int fd
)
1983 return _lseek(fd
, 0, SEEK_CUR
);
1986 /*********************************************************************
1987 * _telli64 (MSVCRT.@)
1989 __int64 CDECL
_telli64(int fd
)
1991 return _lseeki64(fd
, 0, SEEK_CUR
);
1994 /*********************************************************************
1995 * _tempnam (MSVCRT.@)
1997 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
1999 char tmpbuf
[MAX_PATH
];
2000 const char *tmp_dir
= getenv("TMP");
2002 if (tmp_dir
) dir
= tmp_dir
;
2004 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2005 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2007 TRACE("got name (%s)\n",tmpbuf
);
2008 DeleteFileA(tmpbuf
);
2009 return _strdup(tmpbuf
);
2011 TRACE("failed (%d)\n",GetLastError());
2015 /*********************************************************************
2016 * _wtempnam (MSVCRT.@)
2018 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2020 wchar_t tmpbuf
[MAX_PATH
];
2022 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2023 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2025 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2026 DeleteFileW(tmpbuf
);
2027 return _wcsdup(tmpbuf
);
2029 TRACE("failed (%d)\n",GetLastError());
2033 /*********************************************************************
2036 int CDECL
_umask(int umask
)
2038 int old_umask
= umask
;
2039 TRACE("(%d)\n",umask
);
2040 MSVCRT_umask
= umask
;
2044 /*********************************************************************
2047 int CDECL
_utime(const char* path
, struct _utimbuf
*t
)
2049 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
2053 int retVal
= _futime(fd
, t
);
2060 /*********************************************************************
2061 * _wutime (MSVCRT.@)
2063 int CDECL
_wutime(const wchar_t* path
, struct _utimbuf
*t
)
2065 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
2069 int retVal
= _futime(fd
, t
);
2076 /*********************************************************************
2079 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2082 HANDLE hand
= fdtoh(fd
);
2084 /* Don't trace small writes, it gets *very* annoying */
2087 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2089 if (hand
== INVALID_HANDLE_VALUE
)
2095 /* If appending, go to EOF */
2096 if (fdesc
[fd
].wxflag
& WX_APPEND
)
2097 _lseek(fd
, 0, FILE_END
);
2099 if (!(fdesc
[fd
].wxflag
& WX_TEXT
))
2101 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2102 && (num_written
== count
))
2104 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2105 hand
, GetLastError());
2110 unsigned int i
, j
, nr_lf
;
2113 const char *s
= (const char *)buf
, *buf_start
= (const char *)buf
;
2114 /* find number of \n ( without preceding \r ) */
2115 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2120 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2125 if ((q
= p
= malloc(count
+ nr_lf
)))
2127 for (s
= (const char *)buf
, i
= 0, j
= 0; i
< count
; i
++)
2132 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2139 FIXME("Malloc failed\n");
2147 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2149 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2150 fd
, hand
, GetLastError(), num_written
);
2154 return s
- buf_start
;
2166 /*********************************************************************
2169 int CDECL
_putw(int val
, FILE* file
)
2172 len
= _write(file
->_file
, &val
, sizeof(val
));
2173 if (len
== sizeof(val
)) return val
;
2174 file
->_flag
|= _IOERR
;
2178 /*********************************************************************
2181 int CDECL
fclose(FILE* file
)
2186 free(file
->_tmpfname
);
2187 file
->_tmpfname
= NULL
;
2188 /* flush stdio buffers */
2189 if(file
->_flag
& _IOWRT
)
2191 if(file
->_flag
& _IOMYBUF
)
2194 r
=_close(file
->_file
);
2198 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2201 /*********************************************************************
2204 int CDECL
feof(FILE* file
)
2206 return file
->_flag
& _IOEOF
;
2209 /*********************************************************************
2212 int CDECL
ferror(FILE* file
)
2214 return file
->_flag
& _IOERR
;
2217 /*********************************************************************
2218 * _filbuf (MSVCRT.@)
2220 int CDECL
_filbuf(FILE* file
)
2222 /* Allocate buffer if needed */
2223 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
) ) {
2226 if(!(file
->_flag
& _IOREAD
)) {
2227 if(file
->_flag
& _IORW
) {
2228 file
->_flag
|= _IOREAD
;
2233 if(file
->_flag
& _IONBF
) {
2236 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2237 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2242 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2244 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2249 file
->_ptr
= file
->_base
+1;
2250 return *(unsigned char *)file
->_base
;
2254 /*********************************************************************
2257 int CDECL
fgetc(FILE* file
)
2264 i
= (unsigned char *)file
->_ptr
++;
2268 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
)
2269 || ((j
!= '\r') || (file
->_cnt
&& file
->_ptr
[0] != '\n')))
2274 /*********************************************************************
2275 * _fgetchar (MSVCRT.@)
2277 int CDECL
_fgetchar(void)
2279 return fgetc(stdin
);
2282 /*********************************************************************
2285 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2288 char * buf_start
= s
;
2290 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2291 file
,file
->_file
,s
,size
);
2293 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2298 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2300 TRACE(":nothing read\n");
2303 if ((cc
!= EOF
) && (size
> 1))
2306 TRACE(":got %s\n", debugstr_a(buf_start
));
2310 /*********************************************************************
2313 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2314 * the CR from CR/LF combinations
2316 wint_t CDECL
fgetwc(FILE* file
)
2320 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2326 for(i
=0; i
<sizeof(wc
); i
++)
2339 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2350 if ((*__p___mb_cur_max() > 1) && isleadbyte(c
))
2352 FIXME("Treat Multibyte characters\n");
2360 /*********************************************************************
2363 int CDECL
_getw(FILE* file
)
2368 for (j
=0; j
<sizeof(int); j
++) {
2371 file
->_flag
|= _IOEOF
;
2379 /*********************************************************************
2382 wint_t CDECL
getwc(FILE* file
)
2384 return fgetwc(file
);
2387 /*********************************************************************
2388 * _fgetwchar (MSVCRT.@)
2390 wint_t CDECL
_fgetwchar(void)
2392 return fgetwc(stdin
);
2395 /*********************************************************************
2396 * getwchar (MSVCRT.@)
2398 wint_t CDECL
getwchar(void)
2400 return _fgetwchar();
2403 /*********************************************************************
2406 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2409 wchar_t * buf_start
= s
;
2411 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2412 file
,file
->_file
,s
,size
);
2414 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2419 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2421 TRACE(":nothing read\n");
2424 if ((cc
!= WEOF
) && (size
> 1))
2427 TRACE(":got %s\n", debugstr_w(buf_start
));
2431 /*********************************************************************
2434 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2436 size_t wrcnt
=size
* nmemb
;
2441 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2442 memcpy(file
->_ptr
, ptr
, pcnt
);
2447 ptr
= (const char*)ptr
+ pcnt
;
2448 } else if(!(file
->_flag
& _IOWRT
)) {
2449 if(file
->_flag
& _IORW
) {
2450 file
->_flag
|= _IOWRT
;
2456 int res
=flush_buffer(file
);
2458 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2461 file
->_flag
|= _IOERR
;
2464 written
+= pwritten
;
2467 return written
/ size
;
2470 /*********************************************************************
2473 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
2476 char mbchar
[10]; // MB_CUR_MAX_CONST
2479 if (file
->_flag
& _IOBINARY
)
2481 if (fwrite( &mwc
, sizeof(mwc
), 1, file
) != 1)
2486 /* Convert to multibyte in text mode */
2487 mb_return
= wctomb(mbchar
, mwc
);
2488 if (mb_return
== -1) return WEOF
;
2490 /* Output all characters */
2491 if (fwrite( mbchar
, 1, mb_return
, file
) != 1)
2498 /*********************************************************************
2499 * _fputwchar (MSVCRT.@)
2501 wint_t CDECL
_fputwchar(wint_t wc
)
2503 return fputwc(wc
, stdout
);
2506 /*********************************************************************
2507 * _fsopen (MSVCRT.@)
2509 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2512 int open_flags
, stream_flags
, fd
;
2514 TRACE("(%s,%s)\n",path
,mode
);
2516 /* map mode string to open() flags. "man fopen" for possibilities. */
2517 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2521 fd
= _sopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2524 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2526 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2533 TRACE(":got (%p)\n",file
);
2534 if (fd
>= 0 && !file
)
2540 /*********************************************************************
2541 * _wfsopen (MSVCRT.@)
2543 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2545 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
2546 char *patha
= calloc(plen
+ 1, 1);
2547 char *modea
= calloc(mlen
+ 1, 1);
2549 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2551 if (patha
&& modea
&&
2552 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2553 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2555 FILE *retval
= _fsopen(patha
,modea
,share
);
2561 __set_errno(GetLastError());
2565 /*********************************************************************
2568 FILE * CDECL
fopen(const char *path
, const char *mode
)
2570 return _fsopen( path
, mode
, _SH_DENYNO
);
2573 /*********************************************************************
2574 * _wfopen (MSVCRT.@)
2576 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2578 return _wfsopen( path
, mode
, _SH_DENYNO
);
2581 /* fputc calls _flsbuf which calls fputc */
2582 int CDECL
_flsbuf(int c
, FILE* file
);
2584 /*********************************************************************
2587 int CDECL
fputc(int c
, FILE* file
)
2594 int res
= flush_buffer(file
);
2595 return res
? res
: c
;
2600 return _flsbuf(c
, file
);
2604 /*********************************************************************
2605 * _flsbuf (MSVCRT.@)
2607 int CDECL
_flsbuf(int c
, FILE* file
)
2609 /* Flush output buffer */
2610 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
2613 if(!(file
->_flag
& _IOWRT
)) {
2614 if(file
->_flag
& _IORW
) {
2615 file
->_flag
|= _IOWRT
;
2621 int res
=flush_buffer(file
);
2622 return res
?res
: fputc(c
, file
);
2626 len
= _write(file
->_file
, &cc
, 1);
2627 if (len
== 1) return c
;
2628 file
->_flag
|= _IOERR
;
2633 /*********************************************************************
2634 * _fputchar (MSVCRT.@)
2636 int CDECL
_fputchar(int c
)
2638 return fputc(c
, stdout
);
2641 /*********************************************************************
2644 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2645 { size_t rcnt
=size
* nmemb
;
2652 /* first buffered data */
2654 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2655 memcpy(ptr
, file
->_ptr
, pcnt
);
2658 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
)
2659 pcnt
-= remove_cr(ptr
,pcnt
);
2662 ptr
= (char*)ptr
+ pcnt
;
2663 } else if(!(file
->_flag
& _IOREAD
)) {
2664 if(file
->_flag
& _IORW
) {
2665 file
->_flag
|= _IOREAD
;
2672 /* Fill the buffer on small reads.
2673 * TODO: Use a better buffering strategy.
2675 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2676 if (file
->_bufsiz
== 0) {
2679 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2680 file
->_ptr
= file
->_base
;
2681 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2682 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2683 if (i
> 0 && i
< file
->_cnt
) {
2684 fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2685 file
->_flag
&= ~_IOEOF
;
2688 memcpy(ptr
, file
->_ptr
, i
);
2693 i
= _read(file
->_file
,ptr
, rcnt
);
2697 ptr
= (char *)ptr
+i
;
2698 /* expose feof condition in the flags
2699 * MFC tests file->_flag for feof, and doesn't call feof())
2701 if ( fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2702 file
->_flag
|= _IOEOF
;
2705 file
->_flag
|= _IOERR
;
2715 /*********************************************************************
2716 * freopen (MSVCRT.@)
2719 FILE* CDECL
freopen(const char *path
, const char *mode
,FILE* file
)
2721 int open_flags
, stream_flags
, fd
;
2723 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2726 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2731 /* map mode string to open() flags. "man fopen" for possibilities. */
2732 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2736 fd
= _open(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2739 else if (init_fp(file
, fd
, stream_flags
) == -1)
2742 WARN(":failed-last error (%d)\n",GetLastError());
2743 __set_errno(GetLastError());
2752 /*********************************************************************
2753 * wfreopen (MSVCRT.@)
2756 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
,FILE* file
)
2758 FIXME("UNIMPLEMENTED stub!\n");
2762 /*********************************************************************
2763 * fsetpos (MSVCRT.@)
2765 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2767 /* Note that all this has been lifted 'as is' from fseek */
2768 if(file
->_flag
& _IOWRT
)
2771 /* Discard buffered input */
2773 file
->_ptr
= file
->_base
;
2775 /* Reset direction of i/o */
2776 if(file
->_flag
& _IORW
) {
2777 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2780 return (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2783 /*********************************************************************
2786 LONG CDECL
ftell(FILE* file
)
2791 if( file
->_flag
& _IOWRT
) {
2792 off
= file
->_ptr
- file
->_base
;
2797 pos
= _tell(file
->_file
);
2798 if(pos
== -1) return pos
;
2802 /*********************************************************************
2803 * fgetpos (MSVCRT.@)
2805 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2807 /* This code has been lifted form the ftell function */
2810 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2812 if (*pos
== -1) return -1;
2815 if( file
->_flag
& _IOWRT
) {
2816 off
= file
->_ptr
- file
->_base
;
2826 /*********************************************************************
2829 int CDECL
fputs(const char *s
, FILE* file
)
2831 size_t i
, len
= strlen(s
);
2832 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2833 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2834 for (i
=0; i
<len
; i
++)
2835 if (fputc(s
[i
], file
) == EOF
)
2840 /*********************************************************************
2843 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2845 size_t i
, len
= strlenW(s
);
2846 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2847 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2848 for (i
=0; i
<len
; i
++)
2850 if ((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2852 if (fputwc(s
[i
], file
) == WEOF
)
2858 /*********************************************************************
2859 * getchar (MSVCRT.@)
2861 int CDECL
getchar(void)
2863 return fgetc(stdin
);
2866 /*********************************************************************
2869 int CDECL
getc(FILE* file
)
2874 /*********************************************************************
2877 char * CDECL
gets(char *buf
)
2880 char * buf_start
= buf
;
2882 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
2884 if(cc
!= '\r') *buf
++ = (char)cc
;
2888 TRACE("got '%s'\n", buf_start
);
2892 /*********************************************************************
2895 wchar_t* CDECL
_getws(wchar_t* buf
)
2900 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
2904 *buf
++ = (wchar_t)cc
;
2908 TRACE("got %s\n", debugstr_w(ws
));
2912 /*********************************************************************
2915 int CDECL
putc(int c
, FILE* file
)
2917 return fputc(c
, file
);
2920 /*********************************************************************
2921 * putchar (MSVCRT.@)
2923 int CDECL
putchar(int c
)
2925 return fputc(c
, stdout
);
2928 /*********************************************************************
2931 int CDECL
puts(const char *s
)
2933 size_t len
= strlen(s
);
2934 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2935 return fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
2938 /*********************************************************************
2941 int CDECL
_putws(const wchar_t *s
)
2943 static const wchar_t nl
= '\n';
2944 size_t len
= strlenW(s
);
2945 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2946 return fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
2949 /*********************************************************************
2952 int CDECL
remove(const char *path
)
2954 TRACE("(%s)\n",path
);
2955 if (DeleteFileA(path
))
2957 TRACE(":failed (%d)\n",GetLastError());
2958 __set_errno(GetLastError());
2962 /*********************************************************************
2963 * _wremove (MSVCRT.@)
2965 int CDECL
_wremove(const wchar_t *path
)
2967 TRACE("(%s)\n",debugstr_w(path
));
2968 if (DeleteFileW(path
))
2970 TRACE(":failed (%d)\n",GetLastError());
2971 __set_errno(GetLastError());
2975 /*********************************************************************
2978 int CDECL
rename(const char *oldpath
,const char *newpath
)
2980 TRACE(":from %s to %s\n",oldpath
,newpath
);
2981 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2983 TRACE(":failed (%d)\n",GetLastError());
2984 __set_errno(GetLastError());
2988 /*********************************************************************
2989 * _wrename (MSVCRT.@)
2991 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
2993 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2994 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2996 TRACE(":failed (%d)\n",GetLastError());
2997 __set_errno(GetLastError());
3001 /*********************************************************************
3002 * setvbuf (MSVCRT.@)
3004 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3006 /* TODO: Check if file busy */
3012 if(mode
== _IOFBF
) {
3013 file
->_flag
&= ~_IONBF
;
3014 file
->_base
= file
->_ptr
= buf
;
3016 file
->_bufsiz
= size
;
3019 file
->_flag
|= _IONBF
;
3024 /*********************************************************************
3027 void CDECL
setbuf(FILE* file
, char *buf
)
3029 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3032 /*********************************************************************
3035 char * CDECL
tmpnam(char *s
)
3043 int_to_base32(GetCurrentProcessId(), tmpstr
);
3044 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3045 for (count
= 0; count
< TMP_MAX
; count
++)
3047 int_to_base32(unique
++, tmpstr
);
3049 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3050 GetLastError() == ERROR_FILE_NOT_FOUND
)
3056 /*********************************************************************
3057 * wtmpnam (MSVCRT.@)
3059 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3061 ERR("UNIMPLEMENTED!\n");
3065 /*********************************************************************
3066 * tmpfile (MSVCRT.@)
3068 FILE* CDECL
tmpfile(void)
3070 char *filename
= tmpnam(NULL
);
3075 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3076 if (fd
!= -1 && (file
= alloc_fp()))
3078 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3083 else file
->_tmpfname
= _strdup(filename
);
3089 /*********************************************************************
3090 * vfprintf (MSVCRT.@)
3092 int CDECL
vfprintf(FILE* file
, const char *format
, va_list valist
)
3094 char buf
[2048], *mem
= buf
;
3095 int written
, resize
= sizeof(buf
), retval
;
3096 /* There are two conventions for vsnprintf failing:
3097 * Return -1 if we truncated, or
3098 * Return the number of bytes that would have been written
3099 * The code below handles both cases
3101 while ((written
= _vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3104 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3107 if (!(mem
= malloc(resize
)))
3110 retval
= fwrite(mem
, sizeof(*mem
), written
, file
);
3116 /*********************************************************************
3117 * vfwprintf (MSVCRT.@)
3119 * Is final char included in written (then resize is too big) or not
3120 * (then we must test for equality too)?
3122 int CDECL
vfwprintf(FILE* file
, const wchar_t *format
, va_list valist
)
3124 wchar_t buf
[2048], *mem
= buf
;
3125 char mbbuf
[2048], *mbmem
= mbbuf
;
3126 int written
, resize
= sizeof(buf
) / sizeof(wchar_t), retval
;
3127 /* See vfprintf comments */
3128 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3131 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(wchar_t));
3134 if (!(mem
= malloc(resize
*sizeof(*mem
))))
3138 /* Check if outputting to a text-file */
3139 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
)
3141 /* Convert to multibyte then */
3142 written
= wcstombs(NULL
, mem
, 0);
3144 if (written
>= sizeof(mbbuf
) && (written
!= (int)-1))
3145 mbmem
= malloc(written
+ 1);
3147 wcstombs(mbmem
, mem
, written
);
3148 retval
= fwrite(mbmem
, 1, written
, file
);
3155 retval
= fwrite(mem
, sizeof(*mem
), written
, file
);
3164 /*********************************************************************
3165 * vprintf (MSVCRT.@)
3167 int CDECL
vprintf(const char *format
, va_list valist
)
3169 return vfprintf(stdout
,format
,valist
);
3172 /*********************************************************************
3173 * vwprintf (MSVCRT.@)
3175 int CDECL
vwprintf(const wchar_t *format
, va_list valist
)
3177 return vfwprintf(stdout
,format
,valist
);
3180 /*********************************************************************
3181 * fprintf (MSVCRT.@)
3183 int CDECL
fprintf(FILE* file
, const char *format
, ...)
3187 va_start(valist
, format
);
3188 res
= vfprintf(file
, format
, valist
);
3193 /*********************************************************************
3194 * fwprintf (MSVCRT.@)
3196 int CDECL
fwprintf(FILE* file
, const wchar_t *format
, ...)
3200 va_start(valist
, format
);
3201 res
= vfwprintf(file
, format
, valist
);
3206 /*********************************************************************
3209 int CDECL
printf(const char *format
, ...)
3213 va_start(valist
, format
);
3214 res
= vfprintf(stdout
, format
, valist
);
3219 /*********************************************************************
3222 int CDECL
ungetc(int c
, FILE * file
)
3226 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
3230 if(file
->_ptr
>file
->_base
) {
3240 /*********************************************************************
3241 * ungetwc (MSVCRT.@)
3243 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3246 char * pp
= (char *)&mwc
;
3248 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3249 if(pp
[i
] != ungetc(pp
[i
],file
))
3255 /*********************************************************************
3256 * wprintf (MSVCRT.@)
3258 int CDECL
wprintf(const wchar_t *format
, ...)
3262 va_start(valist
, format
);
3263 res
= vwprintf(format
, valist
);
3268 /*********************************************************************
3269 * _getmaxstdio (MSVCRT.@)
3271 int CDECL
_getmaxstdio(void)
3273 FIXME("stub, always returns 512\n");
3277 /*********************************************************************
3278 * _setmaxstdio_ (MSVCRT.@)
3280 int CDECL
_setmaxstdio(int newmax
)
3287 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3291 /*********************************************************************
3292 * __pioinfo (MSVCRT.@)
3293 * FIXME: see MAX_FILES define.
3295 ioinfo
* __pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3296 &fdesc
[0 * 64], &fdesc
[1 * 64], &fdesc
[2 * 64],
3297 &fdesc
[3 * 64], &fdesc
[4 * 64], &fdesc
[5 * 64],
3298 &fdesc
[6 * 64], &fdesc
[7 * 64], &fdesc
[8 * 64],
3299 &fdesc
[9 * 64], &fdesc
[10 * 64], &fdesc
[11 * 64],
3300 &fdesc
[12 * 64], &fdesc
[13 * 64], &fdesc
[14 * 64],
3301 &fdesc
[15 * 64], &fdesc
[16 * 64], &fdesc
[17 * 64],
3302 &fdesc
[18 * 64], &fdesc
[19 * 64], &fdesc
[20 * 64],
3303 &fdesc
[21 * 64], &fdesc
[22 * 64], &fdesc
[23 * 64],
3304 &fdesc
[24 * 64], &fdesc
[25 * 64], &fdesc
[26 * 64],
3305 &fdesc
[27 * 64], &fdesc
[28 * 64], &fdesc
[29 * 64],
3306 &fdesc
[30 * 64], &fdesc
[31 * 64]
3309 /*********************************************************************
3310 * __badioinfo (MSVCRT.@)
3312 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};