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
];
99 FILE _iob
[3] = { { 0 } };
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
);
1259 if (hand
== INVALID_HANDLE_VALUE
)
1266 RtlSecondsSince1970ToTime(currTime
, (LARGE_INTEGER
*)&at
);
1271 RtlSecondsSince1970ToTime(t
->actime
, (LARGE_INTEGER
*)&at
);
1272 if (t
->actime
== t
->modtime
)
1275 RtlSecondsSince1970ToTime(t
->modtime
, (LARGE_INTEGER
*)&wt
);
1278 if (!SetFileTime(hand
, NULL
, &at
, &wt
))
1280 __set_errno(GetLastError());
1286 /*********************************************************************
1287 * _get_osfhandle (MSVCRT.@)
1289 long CDECL
_get_osfhandle(int fd
)
1291 HANDLE hand
= fdtoh(fd
);
1292 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1297 /*********************************************************************
1298 * _isatty (MSVCRT.@)
1300 int CDECL
_isatty(int fd
)
1302 HANDLE hand
= fdtoh(fd
);
1304 TRACE(":fd (%d) handle (%p)\n",fd
,hand
);
1305 if (hand
== INVALID_HANDLE_VALUE
)
1308 return GetFileType(hand
) == FILE_TYPE_CHAR
? 1 : 0;
1311 /*********************************************************************
1312 * _mktemp (MSVCRT.@)
1314 char * CDECL
_mktemp(char *pattern
)
1317 char *retVal
= pattern
;
1322 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1326 id
= GetCurrentProcessId();
1330 int tempNum
= id
/ 10;
1331 *pattern
-- = id
- (tempNum
* 10) + '0';
1337 *pattern
= letter
++;
1338 if (GetFileAttributesA(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1339 GetLastError() == ERROR_FILE_NOT_FOUND
)
1341 } while(letter
<= 'z');
1345 /*********************************************************************
1346 * _wmktemp (MSVCRT.@)
1348 wchar_t * CDECL
_wmktemp(wchar_t *pattern
)
1351 wchar_t *retVal
= pattern
;
1353 wchar_t letter
= 'a';
1356 numX
= (*pattern
++ == 'X')? numX
+ 1 : 0;
1360 id
= GetCurrentProcessId();
1364 int tempNum
= id
/ 10;
1365 *pattern
-- = id
- (tempNum
* 10) + '0';
1371 if (GetFileAttributesW(retVal
) == INVALID_FILE_ATTRIBUTES
&&
1372 GetLastError() == ERROR_FILE_NOT_FOUND
)
1374 *pattern
= letter
++;
1375 } while(letter
!= '|');
1379 /*static */unsigned split_oflags(unsigned oflags
)
1382 unsigned unsupp
; /* until we support everything */
1384 if (oflags
& _O_APPEND
) wxflags
|= WX_APPEND
;
1385 if (oflags
& _O_BINARY
) {/* Nothing to do */}
1386 else if (oflags
& _O_TEXT
) wxflags
|= WX_TEXT
;
1387 else if (*__p__fmode() & _O_BINARY
) {/* Nothing to do */}
1388 else wxflags
|= WX_TEXT
; /* default to TEXT*/
1389 if (oflags
& _O_NOINHERIT
) wxflags
|= WX_DONTINHERIT
;
1391 if ((unsupp
= oflags
& ~(
1392 _O_BINARY
|_O_TEXT
|_O_APPEND
|
1393 _O_TRUNC
|_O_EXCL
|_O_CREAT
|
1394 _O_RDWR
|_O_WRONLY
|_O_TEMPORARY
|
1396 _O_SEQUENTIAL
|_O_RANDOM
|_O_SHORT_LIVED
1398 ERR(":unsupported oflags 0x%04x\n",unsupp
);
1403 /*********************************************************************
1406 int CDECL
_pipe(int *pfds
, unsigned int psize
, int textmode
)
1409 SECURITY_ATTRIBUTES sa
;
1410 HANDLE readHandle
, writeHandle
;
1418 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1419 sa
.bInheritHandle
= !(textmode
& _O_NOINHERIT
);
1420 sa
.lpSecurityDescriptor
= NULL
;
1421 if (CreatePipe(&readHandle
, &writeHandle
, &sa
, psize
))
1423 unsigned int wxflags
= split_oflags(textmode
);
1427 fd
= alloc_fd(readHandle
, wxflags
);
1431 fd
= alloc_fd(writeHandle
, wxflags
);
1440 CloseHandle(writeHandle
);
1446 CloseHandle(readHandle
);
1447 CloseHandle(writeHandle
);
1453 __set_errno(GetLastError());
1458 /*********************************************************************
1461 int CDECL
_sopen( const char *path
, int oflags
, int shflags
, ... )
1465 DWORD access
= 0, creation
= 0, attrib
;
1469 SECURITY_ATTRIBUTES sa
;
1472 TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
1473 path
, oflags
, shflags
);
1475 wxflag
= split_oflags(oflags
);
1476 switch (oflags
& (_O_RDONLY
| _O_WRONLY
| _O_RDWR
))
1478 case _O_RDONLY
: access
|= GENERIC_READ
; break;
1479 case _O_WRONLY
: access
|= GENERIC_WRITE
; break;
1480 case _O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1483 if (oflags
& _O_CREAT
)
1485 va_start(ap
, shflags
);
1486 pmode
= va_arg(ap
, int);
1489 if(pmode
& ~(_S_IREAD
| _S_IWRITE
))
1490 FIXME(": pmode 0x%04x ignored\n", pmode
);
1492 WARN(": pmode 0x%04x ignored\n", pmode
);
1494 if (oflags
& _O_EXCL
)
1495 creation
= CREATE_NEW
;
1496 else if (oflags
& _O_TRUNC
)
1497 creation
= CREATE_ALWAYS
;
1499 creation
= OPEN_ALWAYS
;
1501 else /* no _O_CREAT */
1503 if (oflags
& _O_TRUNC
)
1504 creation
= TRUNCATE_EXISTING
;
1506 creation
= OPEN_EXISTING
;
1515 sharing
= FILE_SHARE_READ
;
1518 sharing
= FILE_SHARE_WRITE
;
1521 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1524 ERR( "Unhandled shflags 0x%x\n", shflags
);
1527 attrib
= FILE_ATTRIBUTE_NORMAL
;
1529 if (oflags
& _O_TEMPORARY
)
1531 attrib
|= FILE_FLAG_DELETE_ON_CLOSE
;
1533 sharing
|= FILE_SHARE_DELETE
;
1536 sa
.nLength
= sizeof( SECURITY_ATTRIBUTES
);
1537 sa
.lpSecurityDescriptor
= NULL
;
1538 sa
.bInheritHandle
= (oflags
& _O_NOINHERIT
) ? FALSE
: TRUE
;
1540 hand
= CreateFileA(path
, access
, sharing
, &sa
, creation
, attrib
, 0);
1542 if (hand
== INVALID_HANDLE_VALUE
) {
1543 WARN(":failed-last error (%d)\n",GetLastError());
1544 __set_errno(GetLastError());
1548 fd
= alloc_fd(hand
, wxflag
);
1550 TRACE(":fd (%d) handle (%p)\n",fd
, hand
);
1554 /*********************************************************************
1555 * _wsopen (MSVCRT.@)
1557 int CDECL
_wsopen( const wchar_t* path
, int oflags
, int shflags
, ... )
1559 const unsigned int len
= strlenW(path
);
1560 char *patha
= calloc(len
+ 1,1);
1564 va_start(ap
, shflags
);
1565 pmode
= va_arg(ap
, int);
1568 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1570 int retval
= _sopen(patha
,oflags
,shflags
,pmode
);
1575 __set_errno(GetLastError());
1579 /*********************************************************************
1582 int CDECL
_open( const char *path
, int flags
, ... )
1586 if (flags
& _O_CREAT
)
1589 va_start(ap
, flags
);
1590 pmode
= va_arg(ap
, int);
1592 return _sopen( path
, flags
, _SH_DENYNO
, pmode
);
1595 return _sopen( path
, flags
, _SH_DENYNO
);
1598 /*********************************************************************
1601 int CDECL
_wopen(const wchar_t *path
,int flags
,...)
1603 const unsigned int len
= strlenW(path
);
1604 char *patha
= calloc(len
+ 1,1);
1608 va_start(ap
, flags
);
1609 pmode
= va_arg(ap
, int);
1612 if (patha
&& WideCharToMultiByte(CP_ACP
,0,path
,len
,patha
,len
,NULL
,NULL
))
1614 int retval
= _open(patha
,flags
,pmode
);
1619 __set_errno(GetLastError());
1623 /*********************************************************************
1626 int CDECL
_creat(const char *path
, int flags
)
1628 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1629 return _open(path
, usedFlags
);
1632 /*********************************************************************
1633 * _wcreat (MSVCRT.@)
1635 int CDECL
_wcreat(const wchar_t *path
, int flags
)
1637 int usedFlags
= (flags
& _O_TEXT
)| _O_CREAT
| _O_WRONLY
| _O_TRUNC
;
1638 return _wopen(path
, usedFlags
);
1641 /*********************************************************************
1642 * _open_osfhandle (MSVCRT.@)
1644 int CDECL
_open_osfhandle(long handle
, int oflags
)
1648 /* _O_RDONLY (0) always matches, so set the read flag
1649 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
1650 * file, so set the write flag. It also only sets _O_TEXT if it wants
1651 * text - it never sets _O_BINARY.
1653 /* don't let split_oflags() decide the mode if no mode is passed */
1654 if (!(oflags
& (_O_BINARY
| _O_TEXT
)))
1655 oflags
|= _O_BINARY
;
1657 fd
= alloc_fd((HANDLE
)handle
, split_oflags(oflags
));
1658 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle
, fd
, oflags
);
1662 /*********************************************************************
1665 int CDECL
_rmtmp(void)
1667 int num_removed
= 0, i
;
1670 for (i
= 3; i
< stream_idx
; i
++)
1671 if (fstreams
[i
] && fstreams
[i
]->_tmpfname
)
1673 fclose(fstreams
[i
]);
1679 TRACE(":removed (%d) temp files\n",num_removed
);
1683 /*********************************************************************
1684 * (internal) remove_cr
1686 * Translate all \r\n to \n inplace.
1687 * return the number of \r removed
1688 * Corner cases required by some apps:
1690 * BUG: should save state across calls somehow, so CR LF that
1691 * straddles buffer boundary gets recognized properly?
1693 static unsigned int remove_cr(char *buf
, unsigned int count
)
1697 for (i
=0, j
=0; j
< count
; j
++)
1698 if ((buf
[j
] != '\r') || ((j
+1) < count
&& buf
[j
+1] != '\n'))
1704 /*********************************************************************
1707 static int read_i(int fd
, void *buf
, unsigned int count
)
1710 char *bufstart
= buf
;
1711 HANDLE hand
= fdtoh(fd
);
1713 if (fdesc
[fd
].wxflag
& WX_READEOF
) {
1714 fdesc
[fd
].wxflag
|= WX_ATEOF
;
1715 TRACE("already at EOF, returning 0\n");
1718 /* Don't trace small reads, it gets *very* annoying */
1720 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
1721 if (hand
== INVALID_HANDLE_VALUE
)
1724 /* Reading single bytes in O_TEXT mode makes things slow
1725 * So read big chunks
1727 if (ReadFile(hand
, bufstart
, count
, &num_read
, NULL
))
1729 if (fdesc
[fd
].wxflag
& WX_TEXT
)
1732 /* in text mode, a ctrl-z signals EOF */
1733 for (i
=0; i
<num_read
; i
++)
1735 if (bufstart
[i
] == 0x1a)
1738 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1739 TRACE(":^Z EOF %s\n",debugstr_an(buf
,num_read
));
1744 if (count
!= 0 && num_read
== 0)
1746 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1747 TRACE(":EOF %s\n",debugstr_an(buf
,num_read
));
1752 if (GetLastError() == ERROR_BROKEN_PIPE
)
1754 TRACE(":end-of-pipe\n");
1755 fdesc
[fd
].wxflag
|= (WX_ATEOF
|WX_READEOF
);
1760 TRACE(":failed-last error (%d)\n",GetLastError());
1766 TRACE("(%u), %s\n",num_read
,debugstr_an(buf
, num_read
));
1770 /*********************************************************************
1773 int CDECL
_read(int fd
, void *buf
, unsigned int count
)
1776 num_read
= read_i(fd
, buf
, count
);
1777 if (num_read
>0 && fdesc
[fd
].wxflag
& WX_TEXT
)
1779 num_read
-= remove_cr(buf
,num_read
);
1784 /*********************************************************************
1785 * _setmode (MSVCRT.@)
1787 int CDECL
_setmode(int fd
,int mode
)
1789 int ret
= fdesc
[fd
].wxflag
& WX_TEXT
? _O_TEXT
: _O_BINARY
;
1790 if (mode
& (~(_O_TEXT
|_O_BINARY
)))
1791 FIXME("fd (%d) mode (0x%08x) unknown\n",fd
,mode
);
1792 if ((mode
& _O_TEXT
) == _O_TEXT
)
1793 fdesc
[fd
].wxflag
|= WX_TEXT
;
1795 fdesc
[fd
].wxflag
&= ~WX_TEXT
;
1799 /*********************************************************************
1800 * _stat64 (MSVCRT.@)
1802 int CDECL
_stat64(const char* path
, struct __stat64
* buf
)
1805 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1806 unsigned short mode
= ALL_S_IREAD
;
1809 TRACE(":file (%s) buf(%p)\n",path
,buf
);
1811 if (!GetFileAttributesExA(path
, GetFileExInfoStandard
, &hfi
))
1813 TRACE("failed (%d)\n",GetLastError());
1814 __set_errno(ERROR_FILE_NOT_FOUND
);
1818 memset(buf
,0,sizeof(struct __stat64
));
1820 /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
1821 Bon 011120: This FIXME seems incorrect
1822 Also a letter as first char isn't enough to be classified
1825 if (isalpha(*path
)&& (*(path
+1)==':'))
1826 buf
->st_dev
= buf
->st_rdev
= toupper(*path
) - 'A'; /* drive num */
1828 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1830 plen
= strlen(path
);
1832 /* Dir, or regular file? */
1833 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1834 (path
[plen
-1] == '\\'))
1835 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
1840 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1842 unsigned int ext
= tolower(path
[plen
-1]) | (tolower(path
[plen
-2]) << 8) |
1843 (tolower(path
[plen
-3]) << 16);
1844 if (ext
== EXE
|| ext
== BAT
|| ext
== CMD
|| ext
== COM
)
1845 mode
|= ALL_S_IEXEC
;
1849 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1850 mode
|= ALL_S_IWRITE
;
1852 buf
->st_mode
= mode
;
1854 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1855 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1857 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1858 buf
->st_mtime
= buf
->st_ctime
= dw
;
1859 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1860 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1861 (long)buf
->st_atime
,(long)buf
->st_mtime
,(long)buf
->st_ctime
);
1865 /*********************************************************************
1866 * _stati64 (MSVCRT.@)
1868 int CDECL
_stati64(const char* path
, struct _stati64
* buf
)
1871 struct __stat64 buf64
;
1873 ret
= _stat64(path
, &buf64
);
1875 stat64_to_stati64(&buf64
, buf
);
1879 /*********************************************************************
1882 int CDECL
_stat(const char* path
, struct _stat
* buf
)
1884 struct __stat64 buf64
;
1886 ret
= _stat64( path
, &buf64
);
1888 stat64_to_stat(&buf64
, buf
);
1892 /*********************************************************************
1893 * _wstat64 (MSVCRT.@)
1895 int CDECL
_wstat64(const wchar_t* path
, struct __stat64
* buf
)
1898 WIN32_FILE_ATTRIBUTE_DATA hfi
;
1899 unsigned short mode
= ALL_S_IREAD
;
1902 TRACE(":file (%s) buf(%p)\n",debugstr_w(path
),buf
);
1904 if (!GetFileAttributesExW(path
, GetFileExInfoStandard
, &hfi
))
1906 TRACE("failed (%d)\n",GetLastError());
1907 __set_errno(ERROR_FILE_NOT_FOUND
);
1911 memset(buf
,0,sizeof(struct __stat64
));
1913 /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
1914 if (iswalpha(*path
))
1915 buf
->st_dev
= buf
->st_rdev
= toupperW(*path
- 'A'); /* drive num */
1917 buf
->st_dev
= buf
->st_rdev
= _getdrive() - 1;
1919 plen
= strlenW(path
);
1921 /* Dir, or regular file? */
1922 if ((hfi
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
1923 (path
[plen
-1] == '\\'))
1924 mode
|= (_S_IFDIR
| ALL_S_IEXEC
);
1929 if (plen
> 6 && path
[plen
-4] == '.') /* shortest exe: "\x.exe" */
1931 ULONGLONG ext
= tolowerW(path
[plen
-1]) | (tolowerW(path
[plen
-2]) << 16) |
1932 ((ULONGLONG
)tolowerW(path
[plen
-3]) << 32);
1933 if (ext
== WCEXE
|| ext
== WCBAT
|| ext
== WCCMD
|| ext
== WCCOM
)
1934 mode
|= ALL_S_IEXEC
;
1938 if (!(hfi
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
))
1939 mode
|= ALL_S_IWRITE
;
1941 buf
->st_mode
= mode
;
1943 buf
->st_size
= ((__int64
)hfi
.nFileSizeHigh
<< 32) + hfi
.nFileSizeLow
;
1944 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastAccessTime
, &dw
);
1946 RtlTimeToSecondsSince1970((LARGE_INTEGER
*)&hfi
.ftLastWriteTime
, &dw
);
1947 buf
->st_mtime
= buf
->st_ctime
= dw
;
1948 TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf
->st_mode
,buf
->st_nlink
,
1949 (long)(buf
->st_size
>> 32),(long)buf
->st_size
,
1950 (long)buf
->st_atime
,(long)buf
->st_mtime
,(long)buf
->st_ctime
);
1954 /*********************************************************************
1955 * _wstati64 (MSVCRT.@)
1957 int CDECL
_wstati64(const wchar_t* path
, struct _stati64
* buf
)
1960 struct __stat64 buf64
;
1962 ret
= _wstat64(path
, &buf64
);
1964 stat64_to_stati64(&buf64
, buf
);
1968 /*********************************************************************
1971 int CDECL
_wstat(const wchar_t* path
, struct _stat
* buf
)
1974 struct __stat64 buf64
;
1976 ret
= _wstat64( path
, &buf64
);
1977 if (!ret
) stat64_to_stat(&buf64
, buf
);
1981 /*********************************************************************
1984 long CDECL
_tell(int fd
)
1986 return _lseek(fd
, 0, SEEK_CUR
);
1989 /*********************************************************************
1990 * _telli64 (MSVCRT.@)
1992 __int64 CDECL
_telli64(int fd
)
1994 return _lseeki64(fd
, 0, SEEK_CUR
);
1997 /*********************************************************************
1998 * _tempnam (MSVCRT.@)
2000 char * CDECL
_tempnam(const char *dir
, const char *prefix
)
2002 char tmpbuf
[MAX_PATH
];
2003 const char *tmp_dir
= getenv("TMP");
2005 if (tmp_dir
) dir
= tmp_dir
;
2007 TRACE("dir (%s) prefix (%s)\n",dir
,prefix
);
2008 if (GetTempFileNameA(dir
,prefix
,0,tmpbuf
))
2010 TRACE("got name (%s)\n",tmpbuf
);
2011 DeleteFileA(tmpbuf
);
2012 return _strdup(tmpbuf
);
2014 TRACE("failed (%d)\n",GetLastError());
2018 /*********************************************************************
2019 * _wtempnam (MSVCRT.@)
2021 wchar_t * CDECL
_wtempnam(const wchar_t *dir
, const wchar_t *prefix
)
2023 wchar_t tmpbuf
[MAX_PATH
];
2025 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir
),debugstr_w(prefix
));
2026 if (GetTempFileNameW(dir
,prefix
,0,tmpbuf
))
2028 TRACE("got name (%s)\n",debugstr_w(tmpbuf
));
2029 DeleteFileW(tmpbuf
);
2030 return _wcsdup(tmpbuf
);
2032 TRACE("failed (%d)\n",GetLastError());
2036 /*********************************************************************
2039 int CDECL
_umask(int umask
)
2041 int old_umask
= umask
;
2042 TRACE("(%d)\n",umask
);
2043 MSVCRT_umask
= umask
;
2047 /*********************************************************************
2050 int CDECL
_utime(const char* path
, struct _utimbuf
*t
)
2052 int fd
= _open(path
, _O_WRONLY
| _O_BINARY
);
2056 int retVal
= _futime(fd
, t
);
2063 /*********************************************************************
2064 * _wutime (MSVCRT.@)
2066 int CDECL
_wutime(const wchar_t* path
, struct _utimbuf
*t
)
2068 int fd
= _wopen(path
, _O_WRONLY
| _O_BINARY
);
2072 int retVal
= _futime(fd
, t
);
2079 /*********************************************************************
2082 int CDECL
_write(int fd
, const void* buf
, unsigned int count
)
2085 HANDLE hand
= fdtoh(fd
);
2087 /* Don't trace small writes, it gets *very* annoying */
2090 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd
,hand
,buf
,count
);
2092 if (hand
== INVALID_HANDLE_VALUE
)
2098 /* If appending, go to EOF */
2099 if (fdesc
[fd
].wxflag
& WX_APPEND
)
2100 _lseek(fd
, 0, FILE_END
);
2102 if (!(fdesc
[fd
].wxflag
& WX_TEXT
))
2104 if (WriteFile(hand
, buf
, count
, &num_written
, NULL
)
2105 && (num_written
== count
))
2107 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd
,
2108 hand
, GetLastError());
2113 unsigned int i
, j
, nr_lf
;
2116 const char *s
= (const char *)buf
, *buf_start
= (const char *)buf
;
2117 /* find number of \n ( without preceding \r ) */
2118 for ( nr_lf
=0,i
= 0; i
<count
; i
++)
2123 /*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
2128 if ((q
= p
= malloc(count
+ nr_lf
)))
2130 for (s
= (const char *)buf
, i
= 0, j
= 0; i
< count
; i
++)
2135 /*if ((i >1) && (s[i-1] == '\r'))j--;*/
2142 FIXME("Malloc failed\n");
2150 if ((WriteFile(hand
, q
, count
+nr_lf
, &num_written
, NULL
) == 0 ) || (num_written
!= count
+nr_lf
))
2152 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2153 fd
, hand
, GetLastError(), num_written
);
2157 return s
- buf_start
;
2169 /*********************************************************************
2172 int CDECL
_putw(int val
, FILE* file
)
2175 len
= _write(file
->_file
, &val
, sizeof(val
));
2176 if (len
== sizeof(val
)) return val
;
2177 file
->_flag
|= _IOERR
;
2181 /*********************************************************************
2184 int CDECL
fclose(FILE* file
)
2189 free(file
->_tmpfname
);
2190 file
->_tmpfname
= NULL
;
2191 /* flush stdio buffers */
2192 if(file
->_flag
& _IOWRT
)
2194 if(file
->_flag
& _IOMYBUF
)
2197 r
=_close(file
->_file
);
2201 return ((r
== -1) || (flag
& _IOERR
) ? EOF
: 0);
2204 /*********************************************************************
2207 int CDECL
feof(FILE* file
)
2209 return file
->_flag
& _IOEOF
;
2212 /*********************************************************************
2215 int CDECL
ferror(FILE* file
)
2217 return file
->_flag
& _IOERR
;
2220 /*********************************************************************
2221 * _filbuf (MSVCRT.@)
2223 int CDECL
_filbuf(FILE* file
)
2225 /* Allocate buffer if needed */
2226 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
) ) {
2229 if(!(file
->_flag
& _IOREAD
)) {
2230 if(file
->_flag
& _IORW
) {
2231 file
->_flag
|= _IOREAD
;
2236 if(file
->_flag
& _IONBF
) {
2239 if ((r
= read_i(file
->_file
,&c
,1)) != 1) {
2240 file
->_flag
|= (r
== 0) ? _IOEOF
: _IOERR
;
2245 file
->_cnt
= read_i(file
->_file
, file
->_base
, file
->_bufsiz
);
2247 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2252 file
->_ptr
= file
->_base
+1;
2253 return *(unsigned char *)file
->_base
;
2257 /*********************************************************************
2260 int CDECL
fgetc(FILE* file
)
2267 i
= (unsigned char *)file
->_ptr
++;
2271 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
)
2272 || ((j
!= '\r') || (file
->_cnt
&& file
->_ptr
[0] != '\n')))
2277 /*********************************************************************
2278 * _fgetchar (MSVCRT.@)
2280 int CDECL
_fgetchar(void)
2282 return fgetc(stdin
);
2285 /*********************************************************************
2288 char * CDECL
fgets(char *s
, int size
, FILE* file
)
2291 char * buf_start
= s
;
2293 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2294 file
,file
->_file
,s
,size
);
2296 while ((size
>1) && (cc
= fgetc(file
)) != EOF
&& cc
!= '\n')
2301 if ((cc
== EOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2303 TRACE(":nothing read\n");
2306 if ((cc
!= EOF
) && (size
> 1))
2309 TRACE(":got %s\n", debugstr_a(buf_start
));
2313 /*********************************************************************
2316 * In _O_TEXT mode, multibyte characters are read from the file, dropping
2317 * the CR from CR/LF combinations
2319 wint_t CDECL
fgetwc(FILE* file
)
2323 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2329 for(i
=0; i
<sizeof(wc
); i
++)
2342 file
->_flag
|= (file
->_cnt
== 0) ? _IOEOF
: _IOERR
;
2353 if ((*__p___mb_cur_max() > 1) && isleadbyte(c
))
2355 FIXME("Treat Multibyte characters\n");
2363 /*********************************************************************
2366 int CDECL
_getw(FILE* file
)
2371 for (j
=0; j
<sizeof(int); j
++) {
2374 file
->_flag
|= _IOEOF
;
2382 /*********************************************************************
2385 wint_t CDECL
getwc(FILE* file
)
2387 return fgetwc(file
);
2390 /*********************************************************************
2391 * _fgetwchar (MSVCRT.@)
2393 wint_t CDECL
_fgetwchar(void)
2395 return fgetwc(stdin
);
2398 /*********************************************************************
2399 * getwchar (MSVCRT.@)
2401 wint_t CDECL
getwchar(void)
2403 return _fgetwchar();
2406 /*********************************************************************
2409 wchar_t * CDECL
fgetws(wchar_t *s
, int size
, FILE* file
)
2412 wchar_t * buf_start
= s
;
2414 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2415 file
,file
->_file
,s
,size
);
2417 while ((size
>1) && (cc
= fgetwc(file
)) != WEOF
&& cc
!= '\n')
2422 if ((cc
== WEOF
) && (s
== buf_start
)) /* If nothing read, return 0*/
2424 TRACE(":nothing read\n");
2427 if ((cc
!= WEOF
) && (size
> 1))
2430 TRACE(":got %s\n", debugstr_w(buf_start
));
2434 /*********************************************************************
2437 size_t CDECL
fwrite(const void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2439 size_t wrcnt
=size
* nmemb
;
2444 int pcnt
=(file
->_cnt
>wrcnt
)? wrcnt
: file
->_cnt
;
2445 memcpy(file
->_ptr
, ptr
, pcnt
);
2450 ptr
= (const char*)ptr
+ pcnt
;
2451 } else if(!(file
->_flag
& _IOWRT
)) {
2452 if(file
->_flag
& _IORW
) {
2453 file
->_flag
|= _IOWRT
;
2459 int res
=flush_buffer(file
);
2461 int pwritten
= _write(file
->_file
, ptr
, wrcnt
);
2464 file
->_flag
|= _IOERR
;
2467 written
+= pwritten
;
2470 return written
/ size
;
2473 /*********************************************************************
2476 wint_t CDECL
fputwc(wint_t wc
, FILE* file
)
2478 if (file
->_flag
& _IOBINARY
)
2480 if (fwrite(&wc
, sizeof(wc
), 1, file
) != 1)
2485 /* Convert to multibyte in text mode */
2486 char mbc
[MB_LEN_MAX
];
2489 mb_return
= wctomb(mbc
, wc
);
2494 /* Output all characters */
2495 if (fwrite(mbc
, mb_return
, 1, file
) != 1)
2502 /*********************************************************************
2503 * _fputwchar (MSVCRT.@)
2505 wint_t CDECL
_fputwchar(wint_t wc
)
2507 return fputwc(wc
, stdout
);
2510 /*********************************************************************
2511 * _fsopen (MSVCRT.@)
2513 FILE * CDECL
_fsopen(const char *path
, const char *mode
, int share
)
2516 int open_flags
, stream_flags
, fd
;
2518 TRACE("(%s,%s)\n",path
,mode
);
2520 /* map mode string to open() flags. "man fopen" for possibilities. */
2521 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2525 fd
= _sopen(path
, open_flags
, share
, _S_IREAD
| _S_IWRITE
);
2528 else if ((file
= alloc_fp()) && init_fp(file
, fd
, stream_flags
)
2530 TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd
,mode
,file
);
2537 TRACE(":got (%p)\n",file
);
2538 if (fd
>= 0 && !file
)
2544 /*********************************************************************
2545 * _wfsopen (MSVCRT.@)
2547 FILE * CDECL
_wfsopen(const wchar_t *path
, const wchar_t *mode
, int share
)
2549 const unsigned int plen
= strlenW(path
), mlen
= strlenW(mode
);
2550 char *patha
= calloc(plen
+ 1, 1);
2551 char *modea
= calloc(mlen
+ 1, 1);
2553 TRACE("(%s,%s)\n",debugstr_w(path
),debugstr_w(mode
));
2555 if (patha
&& modea
&&
2556 WideCharToMultiByte(CP_ACP
,0,path
,plen
,patha
,plen
,NULL
,NULL
) &&
2557 WideCharToMultiByte(CP_ACP
,0,mode
,mlen
,modea
,mlen
,NULL
,NULL
))
2559 FILE *retval
= _fsopen(patha
,modea
,share
);
2565 __set_errno(GetLastError());
2569 /*********************************************************************
2572 FILE * CDECL
fopen(const char *path
, const char *mode
)
2574 return _fsopen( path
, mode
, _SH_DENYNO
);
2577 /*********************************************************************
2578 * _wfopen (MSVCRT.@)
2580 FILE * CDECL
_wfopen(const wchar_t *path
, const wchar_t *mode
)
2582 return _wfsopen( path
, mode
, _SH_DENYNO
);
2585 /* fputc calls _flsbuf which calls fputc */
2586 int CDECL
_flsbuf(int c
, FILE* file
);
2588 /*********************************************************************
2591 int CDECL
fputc(int c
, FILE* file
)
2598 int res
= flush_buffer(file
);
2599 return res
? res
: c
;
2604 return _flsbuf(c
, file
);
2608 /*********************************************************************
2609 * _flsbuf (MSVCRT.@)
2611 int CDECL
_flsbuf(int c
, FILE* file
)
2613 /* Flush output buffer */
2614 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
2617 if(!(file
->_flag
& _IOWRT
)) {
2618 if(file
->_flag
& _IORW
) {
2619 file
->_flag
|= _IOWRT
;
2625 int res
=flush_buffer(file
);
2626 return res
?res
: fputc(c
, file
);
2630 len
= _write(file
->_file
, &cc
, 1);
2631 if (len
== 1) return c
;
2632 file
->_flag
|= _IOERR
;
2637 /*********************************************************************
2638 * _fputchar (MSVCRT.@)
2640 int CDECL
_fputchar(int c
)
2642 return fputc(c
, stdout
);
2645 /*********************************************************************
2648 size_t CDECL
fread(void *ptr
, size_t size
, size_t nmemb
, FILE* file
)
2649 { size_t rcnt
=size
* nmemb
;
2656 /* first buffered data */
2658 int pcnt
= (rcnt
>file
->_cnt
)? file
->_cnt
:rcnt
;
2659 memcpy(ptr
, file
->_ptr
, pcnt
);
2662 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
)
2663 pcnt
-= remove_cr(ptr
,pcnt
);
2666 ptr
= (char*)ptr
+ pcnt
;
2667 } else if(!(file
->_flag
& _IOREAD
)) {
2668 if(file
->_flag
& _IORW
) {
2669 file
->_flag
|= _IOREAD
;
2676 /* Fill the buffer on small reads.
2677 * TODO: Use a better buffering strategy.
2679 if (!file
->_cnt
&& size
*nmemb
<= BUFSIZ
/2 && !(file
->_flag
& _IONBF
)) {
2680 if (file
->_bufsiz
== 0) {
2683 file
->_cnt
= _read(file
->_file
, file
->_base
, file
->_bufsiz
);
2684 file
->_ptr
= file
->_base
;
2685 i
= (file
->_cnt
<rcnt
) ? file
->_cnt
: rcnt
;
2686 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
2687 if (i
> 0 && i
< file
->_cnt
) {
2688 fdesc
[file
->_file
].wxflag
&= ~WX_ATEOF
;
2689 file
->_flag
&= ~_IOEOF
;
2692 memcpy(ptr
, file
->_ptr
, i
);
2697 i
= _read(file
->_file
,ptr
, rcnt
);
2701 ptr
= (char *)ptr
+i
;
2702 /* expose feof condition in the flags
2703 * MFC tests file->_flag for feof, and doesn't call feof())
2705 if ( fdesc
[file
->_file
].wxflag
& WX_ATEOF
)
2706 file
->_flag
|= _IOEOF
;
2709 file
->_flag
|= _IOERR
;
2719 /*********************************************************************
2720 * freopen (MSVCRT.@)
2723 FILE* CDECL
freopen(const char *path
, const char *mode
,FILE* file
)
2725 int open_flags
, stream_flags
, fd
;
2727 TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path
,mode
,file
,file
->_file
);
2730 if (!file
|| ((fd
= file
->_file
) < 0) || fd
> fdend
)
2735 /* map mode string to open() flags. "man fopen" for possibilities. */
2736 if (get_flags(mode
, &open_flags
, &stream_flags
) == -1)
2740 fd
= _open(path
, open_flags
, _S_IREAD
| _S_IWRITE
);
2743 else if (init_fp(file
, fd
, stream_flags
) == -1)
2746 WARN(":failed-last error (%d)\n",GetLastError());
2747 __set_errno(GetLastError());
2756 /*********************************************************************
2757 * wfreopen (MSVCRT.@)
2760 FILE* CDECL
_wfreopen(const wchar_t *path
, const wchar_t *mode
,FILE* file
)
2762 FIXME("UNIMPLEMENTED stub!\n");
2766 /*********************************************************************
2767 * fsetpos (MSVCRT.@)
2769 int CDECL
fsetpos(FILE* file
, const fpos_t *pos
)
2771 /* Note that all this has been lifted 'as is' from fseek */
2772 if(file
->_flag
& _IOWRT
)
2775 /* Discard buffered input */
2777 file
->_ptr
= file
->_base
;
2779 /* Reset direction of i/o */
2780 if(file
->_flag
& _IORW
) {
2781 file
->_flag
&= ~(_IOREAD
|_IOWRT
);
2784 return (_lseeki64(file
->_file
,*pos
,SEEK_SET
) == -1) ? -1 : 0;
2787 /*********************************************************************
2790 LONG CDECL
ftell(FILE* file
)
2795 if( file
->_flag
& _IOWRT
) {
2796 off
= file
->_ptr
- file
->_base
;
2801 pos
= _tell(file
->_file
);
2802 if(pos
== -1) return pos
;
2806 /*********************************************************************
2807 * fgetpos (MSVCRT.@)
2809 int CDECL
fgetpos(FILE* file
, fpos_t *pos
)
2811 /* This code has been lifted form the ftell function */
2814 *pos
= _lseeki64(file
->_file
,0,SEEK_CUR
);
2816 if (*pos
== -1) return -1;
2819 if( file
->_flag
& _IOWRT
) {
2820 off
= file
->_ptr
- file
->_base
;
2830 /*********************************************************************
2833 int CDECL
fputs(const char *s
, FILE* file
)
2835 size_t i
, len
= strlen(s
);
2836 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2837 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2838 for (i
=0; i
<len
; i
++)
2839 if (fputc(s
[i
], file
) == EOF
)
2844 /*********************************************************************
2847 int CDECL
fputws(const wchar_t *s
, FILE* file
)
2849 size_t i
, len
= strlenW(s
);
2850 if (!(fdesc
[file
->_file
].wxflag
& WX_TEXT
))
2851 return fwrite(s
,sizeof(*s
),len
,file
) == len
? 0 : EOF
;
2852 for (i
=0; i
<len
; i
++)
2854 if ((s
[i
] == '\n') && (fputc('\r', file
) == EOF
))
2856 if (fputwc(s
[i
], file
) == WEOF
)
2862 /*********************************************************************
2863 * getchar (MSVCRT.@)
2865 int CDECL
getchar(void)
2867 return fgetc(stdin
);
2870 /*********************************************************************
2873 int CDECL
getc(FILE* file
)
2878 /*********************************************************************
2881 char * CDECL
gets(char *buf
)
2884 char * buf_start
= buf
;
2886 for(cc
= fgetc(stdin
); cc
!= EOF
&& cc
!= '\n';
2888 if(cc
!= '\r') *buf
++ = (char)cc
;
2892 TRACE("got '%s'\n", buf_start
);
2896 /*********************************************************************
2899 wchar_t* CDECL
_getws(wchar_t* buf
)
2904 for (cc
= fgetwc(stdin
); cc
!= WEOF
&& cc
!= '\n';
2908 *buf
++ = (wchar_t)cc
;
2912 TRACE("got %s\n", debugstr_w(ws
));
2916 /*********************************************************************
2919 int CDECL
putc(int c
, FILE* file
)
2921 return fputc(c
, file
);
2924 /*********************************************************************
2925 * putchar (MSVCRT.@)
2927 int CDECL
putchar(int c
)
2929 return fputc(c
, stdout
);
2932 /*********************************************************************
2935 int CDECL
puts(const char *s
)
2937 size_t len
= strlen(s
);
2938 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2939 return fwrite("\n",1,1,stdout
) == 1 ? 0 : EOF
;
2942 /*********************************************************************
2945 int CDECL
_putws(const wchar_t *s
)
2947 static const wchar_t nl
= '\n';
2948 size_t len
= strlenW(s
);
2949 if (fwrite(s
,sizeof(*s
),len
,stdout
) != len
) return EOF
;
2950 return fwrite(&nl
,sizeof(nl
),1,stdout
) == 1 ? 0 : EOF
;
2953 /*********************************************************************
2956 int CDECL
remove(const char *path
)
2958 TRACE("(%s)\n",path
);
2959 if (DeleteFileA(path
))
2961 TRACE(":failed (%d)\n",GetLastError());
2962 __set_errno(GetLastError());
2966 /*********************************************************************
2967 * _wremove (MSVCRT.@)
2969 int CDECL
_wremove(const wchar_t *path
)
2971 TRACE("(%s)\n",debugstr_w(path
));
2972 if (DeleteFileW(path
))
2974 TRACE(":failed (%d)\n",GetLastError());
2975 __set_errno(GetLastError());
2979 /*********************************************************************
2982 int CDECL
rename(const char *oldpath
,const char *newpath
)
2984 TRACE(":from %s to %s\n",oldpath
,newpath
);
2985 if (MoveFileExA(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
2987 TRACE(":failed (%d)\n",GetLastError());
2988 __set_errno(GetLastError());
2992 /*********************************************************************
2993 * _wrename (MSVCRT.@)
2995 int CDECL
_wrename(const wchar_t *oldpath
,const wchar_t *newpath
)
2997 TRACE(":from %s to %s\n",debugstr_w(oldpath
),debugstr_w(newpath
));
2998 if (MoveFileExW(oldpath
, newpath
, MOVEFILE_COPY_ALLOWED
))
3000 TRACE(":failed (%d)\n",GetLastError());
3001 __set_errno(GetLastError());
3005 /*********************************************************************
3006 * setvbuf (MSVCRT.@)
3008 int CDECL
setvbuf(FILE* file
, char *buf
, int mode
, size_t size
)
3010 /* TODO: Check if file busy */
3016 if(mode
== _IOFBF
) {
3017 file
->_flag
&= ~_IONBF
;
3018 file
->_base
= file
->_ptr
= buf
;
3020 file
->_bufsiz
= size
;
3023 file
->_flag
|= _IONBF
;
3028 /*********************************************************************
3031 void CDECL
setbuf(FILE* file
, char *buf
)
3033 setvbuf(file
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
3036 /*********************************************************************
3039 char * CDECL
tmpnam(char *s
)
3047 int_to_base32(GetCurrentProcessId(), tmpstr
);
3048 p
= s
+ sprintf(s
, "\\s%s.", tmpstr
);
3049 for (count
= 0; count
< TMP_MAX
; count
++)
3051 int_to_base32(unique
++, tmpstr
);
3053 if (GetFileAttributesA(s
) == INVALID_FILE_ATTRIBUTES
&&
3054 GetLastError() == ERROR_FILE_NOT_FOUND
)
3060 /*********************************************************************
3061 * wtmpnam (MSVCRT.@)
3063 wchar_t * CDECL
_wtmpnam(wchar_t *s
)
3065 ERR("UNIMPLEMENTED!\n");
3069 /*********************************************************************
3070 * tmpfile (MSVCRT.@)
3072 FILE* CDECL
tmpfile(void)
3074 char *filename
= tmpnam(NULL
);
3079 fd
= _open(filename
, _O_CREAT
| _O_BINARY
| _O_RDWR
| _O_TEMPORARY
);
3080 if (fd
!= -1 && (file
= alloc_fp()))
3082 if (init_fp(file
, fd
, _O_RDWR
) == -1)
3087 else file
->_tmpfname
= _strdup(filename
);
3093 /*********************************************************************
3094 * vfprintf (MSVCRT.@)
3096 int CDECL
vfprintf(FILE* file
, const char *format
, va_list valist
)
3098 char buf
[2048], *mem
= buf
;
3099 int written
, resize
= sizeof(buf
), retval
;
3100 /* There are two conventions for vsnprintf failing:
3101 * Return -1 if we truncated, or
3102 * Return the number of bytes that would have been written
3103 * The code below handles both cases
3105 while ((written
= _vsnprintf(mem
, resize
, format
, valist
)) == -1 ||
3108 resize
= (written
== -1 ? resize
* 2 : written
+ 1);
3111 if (!(mem
= malloc(resize
)))
3114 retval
= fwrite(mem
, sizeof(*mem
), written
, file
);
3120 /*********************************************************************
3121 * vfwprintf (MSVCRT.@)
3123 * Is final char included in written (then resize is too big) or not
3124 * (then we must test for equality too)?
3126 int CDECL
vfwprintf(FILE* file
, const wchar_t *format
, va_list valist
)
3128 wchar_t buf
[2048], *mem
= buf
;
3129 int written
, resize
= sizeof(buf
) / sizeof(wchar_t), retval
;
3130 /* See vfprintf comments */
3131 while ((written
= _vsnwprintf(mem
, resize
, format
, valist
)) == -1 ||
3134 resize
= (written
== -1 ? resize
* 2 : written
+ sizeof(wchar_t));
3137 if (!(mem
= malloc(resize
*sizeof(*mem
))))
3141 /* Check if outputting to a text-file */
3142 if (fdesc
[file
->_file
].wxflag
& WX_TEXT
)
3144 /* Convert each character and stop at the first invalid character. Behavior verified by tests under WinXP SP2 */
3145 char chMultiByte
[MB_LEN_MAX
];
3151 for (p
= mem
; *p
; p
++)
3153 nReturn
= wctomb(chMultiByte
, *p
);
3158 retval
+= fwrite(chMultiByte
, 1, nReturn
, file
);
3163 retval
= fwrite(mem
, sizeof(*mem
), written
, file
);
3172 /*********************************************************************
3173 * vprintf (MSVCRT.@)
3175 int CDECL
vprintf(const char *format
, va_list valist
)
3177 return vfprintf(stdout
,format
,valist
);
3180 /*********************************************************************
3181 * vwprintf (MSVCRT.@)
3183 int CDECL
vwprintf(const wchar_t *format
, va_list valist
)
3185 return vfwprintf(stdout
,format
,valist
);
3188 /*********************************************************************
3189 * fprintf (MSVCRT.@)
3191 int CDECL
fprintf(FILE* file
, const char *format
, ...)
3195 va_start(valist
, format
);
3196 res
= vfprintf(file
, format
, valist
);
3201 /*********************************************************************
3202 * fwprintf (MSVCRT.@)
3204 int CDECL
fwprintf(FILE* file
, const wchar_t *format
, ...)
3208 va_start(valist
, format
);
3209 res
= vfwprintf(file
, format
, valist
);
3214 /*********************************************************************
3217 int CDECL
printf(const char *format
, ...)
3221 va_start(valist
, format
);
3222 res
= vfprintf(stdout
, format
, valist
);
3227 /*********************************************************************
3230 int CDECL
ungetc(int c
, FILE * file
)
3234 if(file
->_bufsiz
== 0 && !(file
->_flag
& _IONBF
)) {
3238 if(file
->_ptr
>file
->_base
) {
3248 /*********************************************************************
3249 * ungetwc (MSVCRT.@)
3251 wint_t CDECL
ungetwc(wint_t wc
, FILE * file
)
3254 char * pp
= (char *)&mwc
;
3256 for(i
=sizeof(wchar_t)-1;i
>=0;i
--) {
3257 if(pp
[i
] != ungetc(pp
[i
],file
))
3263 /*********************************************************************
3264 * wprintf (MSVCRT.@)
3266 int CDECL
wprintf(const wchar_t *format
, ...)
3270 va_start(valist
, format
);
3271 res
= vwprintf(format
, valist
);
3276 /*********************************************************************
3277 * _getmaxstdio (MSVCRT.@)
3279 int CDECL
_getmaxstdio(void)
3281 FIXME("stub, always returns 512\n");
3285 /*********************************************************************
3286 * _setmaxstdio_ (MSVCRT.@)
3288 int CDECL
_setmaxstdio(int newmax
)
3295 FIXME("stub: setting new maximum for number of simultaneously open files not implemented,returning %d\n",res
);
3299 /*********************************************************************
3300 * __pioinfo (MSVCRT.@)
3301 * FIXME: see MAX_FILES define.
3303 ioinfo
* __pioinfo
[] = { /* array of pointers to ioinfo arrays [64] */
3304 &fdesc
[0 * 64], &fdesc
[1 * 64], &fdesc
[2 * 64],
3305 &fdesc
[3 * 64], &fdesc
[4 * 64], &fdesc
[5 * 64],
3306 &fdesc
[6 * 64], &fdesc
[7 * 64], &fdesc
[8 * 64],
3307 &fdesc
[9 * 64], &fdesc
[10 * 64], &fdesc
[11 * 64],
3308 &fdesc
[12 * 64], &fdesc
[13 * 64], &fdesc
[14 * 64],
3309 &fdesc
[15 * 64], &fdesc
[16 * 64], &fdesc
[17 * 64],
3310 &fdesc
[18 * 64], &fdesc
[19 * 64], &fdesc
[20 * 64],
3311 &fdesc
[21 * 64], &fdesc
[22 * 64], &fdesc
[23 * 64],
3312 &fdesc
[24 * 64], &fdesc
[25 * 64], &fdesc
[26 * 64],
3313 &fdesc
[27 * 64], &fdesc
[28 * 64], &fdesc
[29 * 64],
3314 &fdesc
[30 * 64], &fdesc
[31 * 64]
3317 /*********************************************************************
3318 * __badioinfo (MSVCRT.@)
3320 ioinfo __badioinfo
= { INVALID_HANDLE_VALUE
, WX_TEXT
};