2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll file functions
|
|
|
|
*
|
|
|
|
* Copyright 1996,1998 Marcus Meissner
|
|
|
|
* Copyright 1996 Jukka Iivonen
|
|
|
|
* Copyright 1997,2000 Uwe Bonnes
|
|
|
|
* Copyright 2000 Jon Griffiths
|
|
|
|
*/
|
|
|
|
#include <time.h>
|
2001-04-11 01:25:25 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "ntddk.h"
|
|
|
|
#include "msvcrt.h"
|
|
|
|
#include "ms_errno.h"
|
|
|
|
|
2001-04-11 01:25:25 +02:00
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "msvcrt/direct.h"
|
|
|
|
#include "msvcrt/fcntl.h"
|
|
|
|
#include "msvcrt/io.h"
|
|
|
|
#include "msvcrt/stdio.h"
|
|
|
|
#include "msvcrt/stdlib.h"
|
|
|
|
#include "msvcrt/string.h"
|
|
|
|
#include "msvcrt/sys/stat.h"
|
|
|
|
#include "msvcrt/sys/utime.h"
|
|
|
|
#include "msvcrt/time.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
#include "msvcrt/share.h"
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* for stat mode, permissions apply to all,owner and group */
|
|
|
|
#define MSVCRT_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
|
|
|
|
#define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
|
|
|
|
#define MSVCRT_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6))
|
|
|
|
|
|
|
|
/* _access() bit flags FIXME: incomplete */
|
2001-09-27 01:04:02 +02:00
|
|
|
#define MSVCRT_W_OK 0x02
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* FIXME: Make this dynamic */
|
|
|
|
#define MSVCRT_MAX_FILES 257
|
|
|
|
|
|
|
|
HANDLE MSVCRT_handles[MSVCRT_MAX_FILES];
|
|
|
|
MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES];
|
|
|
|
int MSVCRT_flags[MSVCRT_MAX_FILES];
|
|
|
|
char *MSVCRT_tempfiles[MSVCRT_MAX_FILES];
|
|
|
|
MSVCRT_FILE MSVCRT__iob[3];
|
2001-04-11 01:25:25 +02:00
|
|
|
#define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO)
|
|
|
|
#define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO)
|
|
|
|
#define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO)
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
static int MSVCRT_fdstart = 3; /* first unallocated fd */
|
|
|
|
static int MSVCRT_fdend = 3; /* highest allocated fd */
|
|
|
|
|
|
|
|
/* INTERNAL: process umask */
|
|
|
|
static int MSVCRT_umask = 0;
|
|
|
|
|
|
|
|
/* INTERNAL: Static buffer for temp file name */
|
|
|
|
static char MSVCRT_tmpname[MAX_PATH];
|
|
|
|
|
|
|
|
static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
|
|
|
|
static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
|
|
|
|
static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
|
|
|
|
static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
#define TOUL(x) (ULONGLONG)((WCHAR)L##x)
|
|
|
|
static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
|
|
|
|
static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
|
|
|
|
static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
|
|
|
|
static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
extern CRITICAL_SECTION MSVCRT_file_cs;
|
|
|
|
#define LOCK_FILES EnterCriticalSection(&MSVCRT_file_cs)
|
|
|
|
#define UNLOCK_FILES LeaveCriticalSection(&MSVCRT_file_cs)
|
|
|
|
|
|
|
|
|
|
|
|
/* INTERNAL: Get the HANDLE for a fd */
|
2001-04-10 23:16:07 +02:00
|
|
|
static HANDLE msvcrt_fdtoh(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= MSVCRT_fdend ||
|
|
|
|
MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
WARN(":fd (%d) - no handle!\n",fd);
|
|
|
|
SET_THREAD_VAR(doserrno,0);
|
|
|
|
SET_THREAD_VAR(errno,MSVCRT_EBADF);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
return MSVCRT_handles[fd];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: free a file entry fd */
|
2001-04-10 23:16:07 +02:00
|
|
|
static void msvcrt_free_fd(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
MSVCRT_handles[fd] = INVALID_HANDLE_VALUE;
|
|
|
|
MSVCRT_files[fd] = 0;
|
|
|
|
MSVCRT_flags[fd] = 0;
|
|
|
|
TRACE(":fd (%d) freed\n",fd);
|
|
|
|
if (fd < 3)
|
|
|
|
return; /* dont use 0,1,2 for user files */
|
|
|
|
if (fd == MSVCRT_fdend - 1)
|
|
|
|
MSVCRT_fdend--;
|
|
|
|
if (fd < MSVCRT_fdstart)
|
|
|
|
MSVCRT_fdstart = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
|
2001-04-10 23:16:07 +02:00
|
|
|
static int msvcrt_alloc_fd(HANDLE hand, int flag)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int fd = MSVCRT_fdstart;
|
|
|
|
|
|
|
|
TRACE(":handle (%d) allocating fd (%d)\n",hand,fd);
|
|
|
|
if (fd >= MSVCRT_MAX_FILES)
|
|
|
|
{
|
|
|
|
WARN(":files exhausted!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
MSVCRT_handles[fd] = hand;
|
|
|
|
MSVCRT_flags[fd] = flag;
|
|
|
|
|
|
|
|
/* locate next free slot */
|
|
|
|
if (fd == MSVCRT_fdend)
|
|
|
|
MSVCRT_fdstart = ++MSVCRT_fdend;
|
|
|
|
else
|
|
|
|
while(MSVCRT_fdstart < MSVCRT_fdend &&
|
|
|
|
MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)
|
|
|
|
MSVCRT_fdstart++;
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: Allocate a FILE* for an fd slot
|
|
|
|
* This is done lazily to avoid memory wastage for low level open/write
|
|
|
|
* usage when a FILE* is not requested (but may be later).
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
static MSVCRT_FILE* msvcrt_alloc_fp(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":fd (%d) allocating FILE*\n",fd);
|
|
|
|
if (fd < 0 || fd >= MSVCRT_fdend ||
|
|
|
|
MSVCRT_handles[fd] == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
WARN(":invalid fd %d\n",fd);
|
|
|
|
SET_THREAD_VAR(doserrno,0);
|
|
|
|
SET_THREAD_VAR(errno,MSVCRT_EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!MSVCRT_files[fd])
|
|
|
|
{
|
|
|
|
if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
|
|
|
|
{
|
|
|
|
MSVCRT_files[fd]->_file = fd;
|
|
|
|
MSVCRT_files[fd]->_flag = MSVCRT_flags[fd];
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]);
|
|
|
|
return MSVCRT_files[fd];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* INTERNAL: Set up stdin, stderr and stdout */
|
2001-04-10 23:16:07 +02:00
|
|
|
void msvcrt_init_io(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
|
|
|
|
MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD;
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT;
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":handles (%d)(%d)(%d)\n",MSVCRT_handles[0],
|
|
|
|
MSVCRT_handles[1],MSVCRT_handles[2]);
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
/* FILE structs for stdin/out/err are static and never deleted */
|
|
|
|
MSVCRT_files[i] = &MSVCRT__iob[i];
|
|
|
|
MSVCRT__iob[i]._file = i;
|
|
|
|
MSVCRT_tempfiles[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __p__iob(MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE *__p__iob(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return &MSVCRT__iob[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _access (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _access(const char *filename, int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD attr = GetFileAttributesA(filename);
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("(%s,%d) %ld\n",filename,mode,attr);
|
|
|
|
|
|
|
|
if (!filename || attr == 0xffffffff)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-27 01:04:02 +02:00
|
|
|
if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
MSVCRT__set_errno(ERROR_ACCESS_DENIED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _waccess (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _waccess(const WCHAR *filename, int mode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
DWORD attr = GetFileAttributesW(filename);
|
|
|
|
|
|
|
|
TRACE("(%s,%d) %ld\n",debugstr_w(filename),mode,attr);
|
|
|
|
|
|
|
|
if (!filename || attr == 0xffffffff)
|
|
|
|
{
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-27 01:04:02 +02:00
|
|
|
if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK))
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
MSVCRT__set_errno(ERROR_ACCESS_DENIED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _chmod (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _chmod(const char *path, int flags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD oldFlags = GetFileAttributesA(path);
|
|
|
|
|
|
|
|
if (oldFlags != 0x0FFFFFFFF)
|
|
|
|
{
|
|
|
|
DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
|
|
|
|
oldFlags | FILE_ATTRIBUTE_READONLY;
|
|
|
|
|
|
|
|
if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wchmod (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wchmod(const WCHAR *path, int flags)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
DWORD oldFlags = GetFileAttributesW(path);
|
|
|
|
|
|
|
|
if (oldFlags != 0x0FFFFFFFF)
|
|
|
|
{
|
|
|
|
DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
|
|
|
|
oldFlags | FILE_ATTRIBUTE_READONLY;
|
|
|
|
|
|
|
|
if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _unlink (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _unlink(const char *path)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("(%s)\n",path);
|
2001-01-11 00:59:25 +01:00
|
|
|
if(DeleteFileA(path))
|
|
|
|
return 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wunlink (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wunlink(const WCHAR *path)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
TRACE("(%s)\n",debugstr_w(path));
|
|
|
|
if(DeleteFileW(path))
|
|
|
|
return 0;
|
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _close (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _close(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd,hand);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Dont free std FILE*'s, they are not dynamic */
|
|
|
|
if (fd > 2 && MSVCRT_files[fd])
|
|
|
|
MSVCRT_free(MSVCRT_files[fd]);
|
|
|
|
|
2001-04-10 23:16:07 +02:00
|
|
|
msvcrt_free_fd(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (!CloseHandle(hand))
|
|
|
|
{
|
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (MSVCRT_tempfiles[fd])
|
|
|
|
{
|
|
|
|
TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]);
|
2001-04-10 23:16:07 +02:00
|
|
|
_unlink(MSVCRT_tempfiles[fd]);
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_free(MSVCRT_tempfiles[fd]);
|
|
|
|
MSVCRT_tempfiles[fd] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(":ok\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _commit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _commit(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd,hand);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!FlushFileBuffers(hand))
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
/* FlushFileBuffers fails for console handles
|
|
|
|
* so we ignore this error.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
TRACE(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
TRACE(":ok\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _eof (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _eof(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD curpos,endpos;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd,hand);
|
|
|
|
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* If we have a FILE* for this file, the EOF flag
|
|
|
|
* will be set by the read()/write() functions.
|
|
|
|
*/
|
|
|
|
if (MSVCRT_files[fd])
|
2001-10-15 19:50:55 +02:00
|
|
|
return MSVCRT_files[fd]->_flag & MSVCRT__IOEOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Otherwise we do it the hard way */
|
|
|
|
curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR);
|
|
|
|
endpos = SetFilePointer(hand, 0, NULL, FILE_END);
|
|
|
|
|
|
|
|
if (curpos == endpos)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
SetFilePointer(hand, curpos, 0, FILE_BEGIN);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fcloseall (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _fcloseall(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int num_closed = 0, i;
|
|
|
|
|
|
|
|
for (i = 3; i < MSVCRT_fdend; i++)
|
|
|
|
if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
_close(i);
|
2001-01-11 00:59:25 +01:00
|
|
|
num_closed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(":closed (%d) handles\n",num_closed);
|
|
|
|
return num_closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _lseek (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
LONG _lseek(int fd, LONG offset, int whence)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD ret;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd,hand);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (whence < 0 || whence > 2)
|
|
|
|
{
|
|
|
|
SET_THREAD_VAR(errno,MSVCRT_EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(":fd (%d) to 0x%08lx pos %s\n",
|
|
|
|
fd,offset,(whence==SEEK_SET)?"SEEK_SET":
|
|
|
|
(whence==SEEK_CUR)?"SEEK_CUR":
|
|
|
|
(whence==SEEK_END)?"SEEK_END":"UNKNOWN");
|
|
|
|
|
|
|
|
if ((ret = SetFilePointer(hand, offset, NULL, whence)) != 0xffffffff)
|
|
|
|
{
|
|
|
|
if (MSVCRT_files[fd])
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* FIXME: What if we seek _to_ EOF - is EOF set? */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
TRACE(":error-last error (%ld)\n",GetLastError());
|
|
|
|
if (MSVCRT_files[fd])
|
|
|
|
switch(GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_NEGATIVE_SEEK:
|
|
|
|
case ERROR_SEEK_ON_DEVICE:
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
|
2001-01-11 00:59:25 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* rewind (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT_rewind(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":file (%p) fd (%d)\n",file,file->_file);
|
2001-04-10 23:16:07 +02:00
|
|
|
_lseek(file->_file,0,SEEK_SET);
|
2001-10-15 19:50:55 +02:00
|
|
|
file->_flag &= ~(MSVCRT__IOEOF | MSVCRT__IOERR);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fdopen (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* _fdopen(int fd, const char *mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
|
|
|
|
if (file)
|
|
|
|
MSVCRT_rewind(file);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfdopen (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* _wfdopen(int fd, const WCHAR *mode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* file = msvcrt_alloc_fp(fd);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
|
|
|
|
if (file)
|
|
|
|
MSVCRT_rewind(file);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _filelength (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
LONG _filelength(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
LONG curPos = _lseek(fd, 0, SEEK_CUR);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (curPos != -1)
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
LONG endPos = _lseek(fd, 0, SEEK_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (endPos != -1)
|
|
|
|
{
|
|
|
|
if (endPos != curPos)
|
2001-04-10 23:16:07 +02:00
|
|
|
_lseek(fd, curPos, SEEK_SET);
|
2001-01-11 00:59:25 +01:00
|
|
|
return endPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fileno (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _fileno(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
|
|
|
|
return file->_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _flushall (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _flushall(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int num_flushed = 0, i = 3;
|
|
|
|
|
|
|
|
while(i < MSVCRT_fdend)
|
|
|
|
if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
if (_commit(i) == -1)
|
2001-01-11 00:59:25 +01:00
|
|
|
if (MSVCRT_files[i])
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[i]->_flag |= MSVCRT__IOERR;
|
2001-01-11 00:59:25 +01:00
|
|
|
num_flushed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(":flushed (%d) handles\n",num_flushed);
|
|
|
|
return num_flushed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fstat (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _fstat(int fd, struct _stat* buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
BY_HANDLE_FILE_INFORMATION hfi;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) stat (%p)\n",fd,buf);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
WARN(":failed-NULL buf\n");
|
|
|
|
MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&hfi, 0, sizeof(hfi));
|
|
|
|
memset(buf, 0, sizeof(struct _stat));
|
|
|
|
if (!GetFileInformationByHandle(hand, &hfi))
|
|
|
|
{
|
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(ERROR_INVALID_PARAMETER);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-12-26 20:50:58 +01:00
|
|
|
FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes);
|
2001-01-11 00:59:25 +01:00
|
|
|
buf->st_nlink = hfi.nNumberOfLinks;
|
|
|
|
buf->st_size = hfi.nFileSizeLow;
|
|
|
|
RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
|
|
|
|
buf->st_atime = dw;
|
|
|
|
RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
|
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _futime (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _futime(int fd, struct _utimbuf *t)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
FILETIME at, wt;
|
|
|
|
|
|
|
|
if (!t)
|
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_time_t currTime;
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_time(&currTime);
|
|
|
|
RtlSecondsSince1970ToTime(currTime, &at);
|
|
|
|
memcpy(&wt, &at, sizeof(wt));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlSecondsSince1970ToTime(t->actime, &at);
|
|
|
|
if (t->actime == t->modtime)
|
|
|
|
memcpy(&wt, &at, sizeof(wt));
|
|
|
|
else
|
|
|
|
RtlSecondsSince1970ToTime(t->modtime, &wt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetFileTime(hand, NULL, &at, &wt))
|
|
|
|
{
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1 ;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _get_osfhandle (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
long _get_osfhandle(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
HANDLE newhand = hand;
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd,hand);
|
|
|
|
|
|
|
|
if (hand != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* FIXME: I'm not convinced that I should be copying the
|
|
|
|
* handle here - it may be leaked if the app doesn't
|
|
|
|
* close it (and the API docs dont say that it should)
|
|
|
|
* Not duplicating it means that it can't be inherited
|
|
|
|
* and so lcc's wedit doesn't cope when it passes it to
|
|
|
|
* child processes. I've an idea that it should either
|
|
|
|
* be copied by CreateProcess, or marked as inheritable
|
|
|
|
* when initialised, or maybe both? JG 21-9-00.
|
|
|
|
*/
|
|
|
|
DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(),
|
|
|
|
&newhand,0,TRUE,DUPLICATE_SAME_ACCESS);
|
|
|
|
}
|
|
|
|
return newhand;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _isatty (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _isatty(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd,hand);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return 0;
|
|
|
|
|
2001-07-23 20:02:18 +02:00
|
|
|
return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _mktemp (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char *_mktemp(char *pattern)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int numX = 0;
|
|
|
|
char *retVal = pattern;
|
|
|
|
int id;
|
|
|
|
char letter = 'a';
|
|
|
|
|
|
|
|
while(*pattern)
|
|
|
|
numX = (*pattern++ == 'X')? numX + 1 : 0;
|
|
|
|
if (numX < 5)
|
|
|
|
return NULL;
|
|
|
|
pattern--;
|
|
|
|
id = GetCurrentProcessId();
|
|
|
|
numX = 6;
|
|
|
|
while(numX--)
|
|
|
|
{
|
|
|
|
int tempNum = id / 10;
|
|
|
|
*pattern-- = id - (tempNum * 10) + '0';
|
|
|
|
id = tempNum;
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (GetFileAttributesA(retVal) == 0xFFFFFFFF &&
|
2001-01-22 03:21:54 +01:00
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
2001-01-11 00:59:25 +01:00
|
|
|
return retVal;
|
|
|
|
*pattern = letter++;
|
|
|
|
} while(letter != '|');
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wmktemp (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
WCHAR *_wmktemp(WCHAR *pattern)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
int numX = 0;
|
|
|
|
WCHAR *retVal = pattern;
|
|
|
|
int id;
|
|
|
|
WCHAR letter = (WCHAR)L'a';
|
|
|
|
|
|
|
|
while(*pattern)
|
|
|
|
numX = (*pattern++ == (WCHAR)L'X')? numX + 1 : 0;
|
|
|
|
if (numX < 5)
|
|
|
|
return NULL;
|
|
|
|
pattern--;
|
|
|
|
id = GetCurrentProcessId();
|
|
|
|
numX = 6;
|
|
|
|
while(numX--)
|
|
|
|
{
|
|
|
|
int tempNum = id / 10;
|
|
|
|
*pattern-- = id - (tempNum * 10) + (WCHAR)L'0';
|
|
|
|
id = tempNum;
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (GetFileAttributesW(retVal) == 0xFFFFFFFF &&
|
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
|
|
|
return retVal;
|
|
|
|
*pattern = letter++;
|
|
|
|
} while(letter != (WCHAR)L'|');
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2002-02-21 21:22:00 +01:00
|
|
|
* _sopen (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2002-02-21 21:22:00 +01:00
|
|
|
int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-02-21 21:22:00 +01:00
|
|
|
va_list ap;
|
|
|
|
int pmode;
|
2001-01-11 00:59:25 +01:00
|
|
|
DWORD access = 0, creation = 0;
|
2002-02-21 21:22:00 +01:00
|
|
|
DWORD sharing;
|
2001-01-11 00:59:25 +01:00
|
|
|
int ioflag = 0, fd;
|
|
|
|
HANDLE hand;
|
2001-07-10 21:03:46 +02:00
|
|
|
SECURITY_ATTRIBUTES sa;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
|
|
|
|
TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n",
|
|
|
|
path, oflags, shflags);
|
|
|
|
|
|
|
|
switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
case _O_RDONLY:
|
|
|
|
access |= GENERIC_READ;
|
2001-10-15 19:50:55 +02:00
|
|
|
ioflag |= MSVCRT__IOREAD;
|
2001-01-11 00:59:25 +01:00
|
|
|
break;
|
|
|
|
case _O_WRONLY:
|
|
|
|
access |= GENERIC_WRITE;
|
2001-10-15 19:50:55 +02:00
|
|
|
ioflag |= MSVCRT__IOWRT;
|
2001-01-11 00:59:25 +01:00
|
|
|
break;
|
|
|
|
case _O_RDWR:
|
|
|
|
access |= GENERIC_WRITE | GENERIC_READ;
|
2001-10-15 19:50:55 +02:00
|
|
|
ioflag |= MSVCRT__IORW;
|
2001-01-11 00:59:25 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
if (oflags & _O_CREAT)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-02-21 21:22:00 +01:00
|
|
|
va_start(ap, shflags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
FIXME(": pmode 0x%04x ignored\n", pmode);
|
|
|
|
|
|
|
|
if (oflags & _O_EXCL)
|
2001-01-11 00:59:25 +01:00
|
|
|
creation = CREATE_NEW;
|
2002-02-21 21:22:00 +01:00
|
|
|
else if (oflags & _O_TRUNC)
|
2001-01-11 00:59:25 +01:00
|
|
|
creation = CREATE_ALWAYS;
|
|
|
|
else
|
|
|
|
creation = OPEN_ALWAYS;
|
|
|
|
}
|
|
|
|
else /* no _O_CREAT */
|
|
|
|
{
|
2002-02-21 21:22:00 +01:00
|
|
|
if (oflags & _O_TRUNC)
|
2001-01-11 00:59:25 +01:00
|
|
|
creation = TRUNCATE_EXISTING;
|
|
|
|
else
|
|
|
|
creation = OPEN_EXISTING;
|
|
|
|
}
|
2002-02-21 21:22:00 +01:00
|
|
|
if (oflags & _O_APPEND)
|
2001-10-15 19:50:55 +02:00
|
|
|
ioflag |= MSVCRT__IOAPPEND;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
oflags |= _O_BINARY; /* FIXME: Default to text */
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
if (oflags & _O_TEXT)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
/* Dont warn when writing */
|
|
|
|
if (ioflag & GENERIC_READ)
|
|
|
|
FIXME(":TEXT node not implemented\n");
|
2002-02-21 21:22:00 +01:00
|
|
|
oflags &= ~_O_TEXT;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
switch( shflags )
|
|
|
|
{
|
|
|
|
case _SH_DENYRW:
|
|
|
|
sharing = 0L;
|
|
|
|
break;
|
|
|
|
case _SH_DENYWR:
|
|
|
|
sharing = FILE_SHARE_READ;
|
|
|
|
break;
|
|
|
|
case _SH_DENYRD:
|
|
|
|
sharing = FILE_SHARE_WRITE;
|
|
|
|
break;
|
|
|
|
case _SH_DENYNO:
|
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR( "Unhandled shflags 0x%x\n", shflags );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL
|
|
|
|
|_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT))
|
|
|
|
TRACE(":unsupported oflags 0x%04x\n",oflags);
|
|
|
|
|
2001-07-10 21:03:46 +02:00
|
|
|
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
2002-02-21 21:22:00 +01:00
|
|
|
sa.bInheritHandle = (oflags & _O_NOINHERIT) ? FALSE : TRUE;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
hand = CreateFileA(path, access, sharing,
|
2001-07-10 21:03:46 +02:00
|
|
|
&sa, creation, FILE_ATTRIBUTE_NORMAL, 0);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE) {
|
2001-01-11 00:59:25 +01:00
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-04-10 23:16:07 +02:00
|
|
|
fd = msvcrt_alloc_fd(hand, ioflag);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%d)\n",fd, hand);
|
|
|
|
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
2002-02-21 21:22:00 +01:00
|
|
|
if (oflags & _O_TEMPORARY)
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_tempfiles[fd] = _strdup(path);
|
2001-10-15 19:50:55 +02:00
|
|
|
if (ioflag & MSVCRT__IOAPPEND)
|
2001-04-10 23:16:07 +02:00
|
|
|
_lseek(fd, 0, FILE_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2002-02-21 21:22:00 +01:00
|
|
|
* _wsopen (MSVCRT.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2002-02-21 21:22:00 +01:00
|
|
|
int MSVCRT__wsopen( const WCHAR* path, int oflags, int shflags, ... )
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
const unsigned int len = strlenW(path);
|
2001-01-22 03:21:54 +01:00
|
|
|
char *patha = MSVCRT_calloc(len + 1,1);
|
2002-02-21 21:22:00 +01:00
|
|
|
va_list ap;
|
|
|
|
int pmode;
|
|
|
|
|
|
|
|
va_start(ap, shflags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
|
|
|
|
{
|
2002-02-21 21:22:00 +01:00
|
|
|
int retval = MSVCRT__sopen(patha,oflags,shflags,pmode);
|
2001-01-22 03:21:54 +01:00
|
|
|
MSVCRT_free(patha);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-03-19 20:13:40 +01:00
|
|
|
/*********************************************************************
|
2002-02-21 21:22:00 +01:00
|
|
|
* _open (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _open( const char *path, int flags, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int pmode;
|
|
|
|
|
|
|
|
va_start(ap, flags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wopen (MSVCRT.@)
|
2001-03-19 20:13:40 +01:00
|
|
|
*/
|
2002-02-21 21:22:00 +01:00
|
|
|
int _wopen(const WCHAR *path,int flags,...)
|
2001-03-19 20:13:40 +01:00
|
|
|
{
|
2002-02-21 21:22:00 +01:00
|
|
|
const unsigned int len = strlenW(path);
|
|
|
|
char *patha = MSVCRT_calloc(len + 1,1);
|
|
|
|
va_list ap;
|
|
|
|
int pmode;
|
|
|
|
|
|
|
|
va_start(ap, flags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL))
|
|
|
|
{
|
|
|
|
int retval = _open(patha,flags,pmode);
|
|
|
|
MSVCRT_free(patha);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
2001-03-19 20:13:40 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _creat (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _creat(const char *path, int flags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
|
2001-04-10 23:16:07 +02:00
|
|
|
return _open(path, usedFlags);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wcreat (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wcreat(const WCHAR *path, int flags)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
|
2001-04-10 23:16:07 +02:00
|
|
|
return _wopen(path, usedFlags);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _open_osfhandle (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
int _open_osfhandle(long hand, int flags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
int fd = msvcrt_alloc_fd(hand,flags);
|
2001-04-11 01:25:25 +02:00
|
|
|
TRACE(":handle (%ld) fd (%d)\n",hand,fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _rmtmp (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _rmtmp(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int num_removed = 0, i;
|
|
|
|
|
|
|
|
for (i = 3; i < MSVCRT_fdend; i++)
|
|
|
|
if (MSVCRT_tempfiles[i])
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
_close(i);
|
2001-01-11 00:59:25 +01:00
|
|
|
num_removed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_removed)
|
|
|
|
TRACE(":removed (%d) temp files\n",num_removed);
|
|
|
|
return num_removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _read (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _read(int fd, void *buf, unsigned int count)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD num_read;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Dont trace small reads, it gets *very* annoying */
|
|
|
|
if (count > 4)
|
|
|
|
TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Set _cnt to 0 so optimised binaries will call our implementation
|
|
|
|
* of putc/getc. See _filbuf/_flsbuf comments.
|
|
|
|
*/
|
|
|
|
if (MSVCRT_files[fd])
|
|
|
|
MSVCRT_files[fd]->_cnt = 0;
|
|
|
|
|
|
|
|
if (ReadFile(hand, buf, count, &num_read, NULL))
|
|
|
|
{
|
|
|
|
if (num_read != count && MSVCRT_files[fd])
|
|
|
|
{
|
|
|
|
TRACE(":EOF\n");
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
return num_read;
|
|
|
|
}
|
|
|
|
TRACE(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
if (MSVCRT_files[fd])
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _getw (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _getw(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int i;
|
2001-04-10 23:16:07 +02:00
|
|
|
if (_read(file->_file, &i, sizeof(int)) != 1)
|
2001-01-11 00:59:25 +01:00
|
|
|
return MSVCRT_EOF;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _setmode (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _setmode(int fd,int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (mode & _O_TEXT)
|
|
|
|
FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _stat (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _stat(const char* path, struct _stat * buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA hfi;
|
|
|
|
unsigned short mode = MSVCRT_S_IREAD;
|
|
|
|
int plen;
|
|
|
|
|
|
|
|
TRACE(":file (%s) buf(%p)\n",path,buf);
|
|
|
|
|
|
|
|
if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
|
|
|
|
{
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf,0,sizeof(struct _stat));
|
|
|
|
|
2002-01-14 19:35:21 +01:00
|
|
|
/* FIXME: rdev isnt drive num,despite what the docs say-what is it?
|
2002-01-29 04:12:19 +01:00
|
|
|
Bon 011120: This FIXME seems incorrect
|
2002-01-14 19:35:21 +01:00
|
|
|
Also a letter as first char isn't enough to be classify
|
|
|
|
as drive letter
|
|
|
|
*/
|
|
|
|
if (isalpha(*path)&& (*(path+1)==':'))
|
|
|
|
buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
|
2001-01-11 00:59:25 +01:00
|
|
|
else
|
2001-04-10 23:16:07 +02:00
|
|
|
buf->st_dev = buf->st_rdev = _getdrive() - 1;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
plen = strlen(path);
|
|
|
|
|
|
|
|
/* Dir, or regular file? */
|
|
|
|
if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
|
|
|
|
(path[plen-1] == '\\'))
|
|
|
|
mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode |= _S_IFREG;
|
|
|
|
/* executable? */
|
|
|
|
if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
|
|
|
|
{
|
2001-01-22 03:21:54 +01:00
|
|
|
unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
|
|
|
|
(tolower(path[plen-3]) << 16);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
|
2001-01-22 03:21:54 +01:00
|
|
|
mode |= MSVCRT_S_IEXEC;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
|
|
|
|
mode |= MSVCRT_S_IWRITE;
|
|
|
|
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_nlink = 1;
|
|
|
|
buf->st_size = hfi.nFileSizeLow;
|
|
|
|
RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
|
|
|
|
buf->st_atime = dw;
|
|
|
|
RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
|
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
2001-04-11 01:25:25 +02:00
|
|
|
TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
|
2001-01-22 03:21:54 +01:00
|
|
|
buf->st_atime,buf->st_mtime, buf->st_ctime);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wstat (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wstat(const WCHAR* path, struct _stat * buf)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA hfi;
|
|
|
|
unsigned short mode = MSVCRT_S_IREAD;
|
|
|
|
int plen;
|
|
|
|
|
|
|
|
TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
|
|
|
|
|
|
|
|
if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
|
|
|
|
{
|
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf,0,sizeof(struct _stat));
|
|
|
|
|
2001-04-11 01:25:25 +02:00
|
|
|
/* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
|
|
|
|
if (MSVCRT_iswalpha(*path))
|
|
|
|
buf->st_dev = buf->st_rdev = toupperW(*path - (WCHAR)L'A'); /* drive num */
|
2001-01-22 03:21:54 +01:00
|
|
|
else
|
2001-04-10 23:16:07 +02:00
|
|
|
buf->st_dev = buf->st_rdev = _getdrive() - 1;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2001-04-11 01:25:25 +02:00
|
|
|
plen = strlenW(path);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
/* Dir, or regular file? */
|
|
|
|
if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
|
|
|
|
(path[plen-1] == (WCHAR)L'\\'))
|
|
|
|
mode |= (_S_IFDIR | MSVCRT_S_IEXEC);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode |= _S_IFREG;
|
|
|
|
/* executable? */
|
|
|
|
if (plen > 6 && path[plen-4] == (WCHAR)L'.') /* shortest exe: "\x.exe" */
|
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
|
|
|
|
((ULONGLONG)tolowerW(path[plen-3]) << 32);
|
2001-01-22 03:21:54 +01:00
|
|
|
if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
|
|
|
|
mode |= MSVCRT_S_IEXEC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
|
|
|
|
mode |= MSVCRT_S_IWRITE;
|
|
|
|
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_nlink = 1;
|
|
|
|
buf->st_size = hfi.nFileSizeLow;
|
|
|
|
RtlTimeToSecondsSince1970(&hfi.ftLastAccessTime, &dw);
|
|
|
|
buf->st_atime = dw;
|
|
|
|
RtlTimeToSecondsSince1970(&hfi.ftLastWriteTime, &dw);
|
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
2001-04-11 01:25:25 +02:00
|
|
|
TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size,
|
2001-01-22 03:21:54 +01:00
|
|
|
buf->st_atime,buf->st_mtime, buf->st_ctime);
|
2001-01-11 00:59:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _tell (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
LONG _tell(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _lseek(fd, 0, SEEK_CUR);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _tempnam (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char *_tempnam(const char *dir, const char *prefix)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char tmpbuf[MAX_PATH];
|
|
|
|
|
|
|
|
TRACE("dir (%s) prefix (%s)\n",dir,prefix);
|
|
|
|
if (GetTempFileNameA(dir,prefix,0,tmpbuf))
|
|
|
|
{
|
|
|
|
TRACE("got name (%s)\n",tmpbuf);
|
2001-04-10 23:16:07 +02:00
|
|
|
return _strdup(tmpbuf);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wtempnam (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
WCHAR *_wtempnam(const WCHAR *dir, const WCHAR *prefix)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
WCHAR tmpbuf[MAX_PATH];
|
|
|
|
|
|
|
|
TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
|
|
|
|
if (GetTempFileNameW(dir,prefix,0,tmpbuf))
|
|
|
|
{
|
|
|
|
TRACE("got name (%s)\n",debugstr_w(tmpbuf));
|
2001-04-10 23:16:07 +02:00
|
|
|
return _wcsdup(tmpbuf);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _umask (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _umask(int umask)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int old_umask = MSVCRT_umask;
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("(%d)\n",umask);
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_umask = umask;
|
|
|
|
return old_umask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _utime (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _utime(const char* path, struct _utimbuf *t)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
int fd = _open(path, _O_WRONLY | _O_BINARY);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
int retVal = _futime(fd, t);
|
|
|
|
_close(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wutime (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wutime(const WCHAR* path, struct _utimbuf *t)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
int fd = _wopen(path, _O_WRONLY | _O_BINARY);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
int retVal = _futime(fd, t);
|
|
|
|
_close(fd);
|
2001-01-22 03:21:54 +01:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _write (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
int _write(int fd, const void* buf, unsigned int count)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD num_written;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Dont trace small writes, it gets *very* annoying */
|
|
|
|
// if (count > 32)
|
|
|
|
// TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* If appending, go to EOF */
|
2001-10-15 19:50:55 +02:00
|
|
|
if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
|
2001-04-10 23:16:07 +02:00
|
|
|
_lseek(fd, 0, FILE_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Set _cnt to 0 so optimised binaries will call our implementation
|
|
|
|
* of putc/getc.
|
|
|
|
*/
|
|
|
|
if (MSVCRT_files[fd])
|
|
|
|
MSVCRT_files[fd]->_cnt = 0;
|
|
|
|
|
|
|
|
if (WriteFile(hand, buf, count, &num_written, NULL)
|
|
|
|
&& (num_written == count))
|
|
|
|
return num_written;
|
|
|
|
|
|
|
|
TRACE(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
if (MSVCRT_files[fd])
|
2001-10-15 19:50:55 +02:00
|
|
|
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putw (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _putw(int val, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* clearerr (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT_clearerr(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":file (%p) fd (%d)\n",file,file->_file);
|
2001-10-15 19:50:55 +02:00
|
|
|
file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fclose (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fclose(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-05-31 23:33:32 +02:00
|
|
|
int r;
|
|
|
|
r=_close(file->_file);
|
2001-10-15 19:50:55 +02:00
|
|
|
return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* feof (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_feof(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-10-15 19:50:55 +02:00
|
|
|
return file->_flag & MSVCRT__IOEOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ferror (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_ferror(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-10-15 19:50:55 +02:00
|
|
|
return file->_flag & MSVCRT__IOERR;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fflush (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fflush(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _commit(file->_file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgetc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fgetc(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char c;
|
2001-04-10 23:16:07 +02:00
|
|
|
if (_read(file->_file,&c,1) != 1)
|
2001-01-11 00:59:25 +01:00
|
|
|
return MSVCRT_EOF;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _fgetchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fgetc(MSVCRT_stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _filbuf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _filbuf(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fgetc(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgetpos (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
*pos = _tell(file->_file);
|
2001-01-11 00:59:25 +01:00
|
|
|
return (*pos == -1? -1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgets (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
char * buf_start = s;
|
|
|
|
|
|
|
|
TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
|
|
|
|
file,file->_file,s,size);
|
|
|
|
|
|
|
|
/* BAD, for the whole WINE process blocks... just done this way to test
|
|
|
|
* windows95's ftp.exe.
|
|
|
|
* JG - Is this true now we use ReadFile() on stdin too?
|
|
|
|
*/
|
|
|
|
for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
|
|
|
|
cc = MSVCRT_fgetc(file))
|
|
|
|
if (cc != '\r')
|
|
|
|
{
|
|
|
|
if (--size <= 0) break;
|
|
|
|
*s++ = (char)cc;
|
|
|
|
}
|
|
|
|
if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
|
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (cc == '\n')
|
|
|
|
if (--size > 0)
|
|
|
|
*s++ = '\n';
|
|
|
|
*s = '\0';
|
|
|
|
TRACE(":got '%s'\n", buf_start);
|
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgetwc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t wc;
|
2001-04-10 23:16:07 +02:00
|
|
|
if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
|
2001-01-11 00:59:25 +01:00
|
|
|
return MSVCRT_WEOF;
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getwc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fgetwc(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetwchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t _fgetwchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fgetwc(MSVCRT_stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getwchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t MSVCRT_getwchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _fgetwchar();
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fputwc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
if (_write(file->_file, &wc, sizeof(wc)) != sizeof(wc))
|
2001-01-11 00:59:25 +01:00
|
|
|
return MSVCRT_WEOF;
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fputwchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fputwc(wc, MSVCRT_stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fopen (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
MSVCRT_FILE* file;
|
|
|
|
int flags = 0, plus = 0, fd;
|
|
|
|
const char* search = mode;
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("(%s,%s)\n",path,mode);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
while (*search)
|
|
|
|
if (*search++ == '+')
|
|
|
|
plus = 1;
|
|
|
|
|
|
|
|
/* map mode string to open() flags. "man fopen" for possibilities. */
|
|
|
|
switch(*mode++)
|
|
|
|
{
|
|
|
|
case 'R': case 'r':
|
|
|
|
flags = (plus ? _O_RDWR : _O_RDONLY);
|
|
|
|
break;
|
|
|
|
case 'W': case 'w':
|
|
|
|
flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
|
|
|
|
break;
|
|
|
|
case 'A': case 'a':
|
|
|
|
flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*mode)
|
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'B': case 'b':
|
|
|
|
flags |= _O_BINARY;
|
|
|
|
flags &= ~_O_TEXT;
|
|
|
|
break;
|
|
|
|
case 'T': case 't':
|
|
|
|
flags |= _O_TEXT;
|
|
|
|
flags &= ~_O_BINARY;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME(":unknown flag %c not supported\n",mode[-1]);
|
|
|
|
}
|
|
|
|
|
2001-04-10 23:16:07 +02:00
|
|
|
fd = _open(path, flags);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2001-04-10 23:16:07 +02:00
|
|
|
file = msvcrt_alloc_fp(fd);
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE(":got (%p)\n",file);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (!file)
|
2001-04-10 23:16:07 +02:00
|
|
|
_close(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfopen (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE *_wfopen(const WCHAR *path, const WCHAR *mode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
const unsigned int plen = strlenW(path), mlen = strlenW(mode);
|
2001-01-22 03:21:54 +01:00
|
|
|
char *patha = MSVCRT_calloc(plen + 1, 1);
|
|
|
|
char *modea = MSVCRT_calloc(mlen + 1, 1);
|
|
|
|
|
|
|
|
TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode));
|
|
|
|
|
|
|
|
if (patha && modea &&
|
|
|
|
WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) &&
|
|
|
|
WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
|
|
|
|
{
|
|
|
|
MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea);
|
|
|
|
MSVCRT_free(patha);
|
|
|
|
MSVCRT_free(modea);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fsopen (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* _fsopen(const char *path, const char *mode, int share)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share);
|
|
|
|
return MSVCRT_fopen(path,mode);
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfsopen (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* _wfsopen(const WCHAR *path, const WCHAR *mode, int share)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
FIXME(":(%s,%s,%d),ignoring share mode!\n",
|
|
|
|
debugstr_w(path),debugstr_w(mode),share);
|
2001-04-10 23:16:07 +02:00
|
|
|
return _wfopen(path,mode);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fputc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fputc(int c, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _write(file->_file, &c, 1) == 1? c : MSVCRT_EOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _flsbuf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _flsbuf(int c, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fputc(c,file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fputchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _fputchar(int c)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fputc(c, MSVCRT_stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fread (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
int read = _read(file->_file,ptr, size * nmemb);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (read <= 0)
|
|
|
|
return 0;
|
|
|
|
return read / size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* freopen (MSVCRT.@)
|
|
|
|
*
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
MSVCRT_FILE* newfile;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
|
|
|
|
if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fd > 2)
|
|
|
|
{
|
|
|
|
FIXME(":reopen on user file not implemented!\n");
|
|
|
|
MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first, create the new file */
|
|
|
|
if ((newfile = MSVCRT_fopen(path,mode)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE :
|
|
|
|
(fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE),
|
|
|
|
MSVCRT_handles[newfile->_file]))
|
|
|
|
{
|
|
|
|
/* Redirecting std handle to file , copy over.. */
|
|
|
|
MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file];
|
|
|
|
MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file];
|
|
|
|
memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE));
|
|
|
|
MSVCRT__iob[fd]._file = fd;
|
|
|
|
/* And free up the resources allocated by fopen, but
|
|
|
|
* not the HANDLE we copied. */
|
|
|
|
MSVCRT_free(MSVCRT_files[fd]);
|
2001-04-10 23:16:07 +02:00
|
|
|
msvcrt_free_fd(newfile->_file);
|
2001-01-11 00:59:25 +01:00
|
|
|
return &MSVCRT__iob[fd];
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
MSVCRT_fclose(newfile);
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fsetpos (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _lseek(file->_file,*pos,SEEK_SET);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-02-08 18:17:11 +01:00
|
|
|
/* helper function for fscanf. Returns the value of character c in the
|
|
|
|
* given base, or -1 if the given character is not a digit of the base.
|
|
|
|
*/
|
|
|
|
static int char2digit(char c, int base) {
|
|
|
|
if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
|
|
|
|
if (base<=10) return -1;
|
|
|
|
if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
|
|
|
|
if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fscanf (MSVCRT.@)
|
2002-02-08 18:17:11 +01:00
|
|
|
* Implemented based on
|
|
|
|
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
|
|
|
|
* Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
|
|
|
|
* more types of format spec.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
/* NOTE: If you extend this function, extend MSVCRT__cscanf in console.c too */
|
2001-01-11 00:59:25 +01:00
|
|
|
int rd = 0;
|
|
|
|
int nch;
|
|
|
|
va_list ap;
|
|
|
|
if (!*format) return 0;
|
|
|
|
WARN("%p (\"%s\"): semi-stub\n", file, format);
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
va_start(ap, format);
|
|
|
|
while (*format) {
|
2002-02-08 18:17:11 +01:00
|
|
|
/* a whitespace character in the format string causes scanf to read,
|
|
|
|
* but not store, all consecutive white-space characters in the input
|
|
|
|
* up to the next non-white-space character. One white space character
|
|
|
|
* in the input matches any number (including zero) and combination of
|
|
|
|
* white-space characters in the input. */
|
|
|
|
if (isspace(*format)) {
|
2001-01-11 00:59:25 +01:00
|
|
|
/* skip whitespace */
|
|
|
|
while ((nch!=MSVCRT_EOF) && isspace(nch))
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* a format specification causes scanf to read and convert characters
|
|
|
|
* in the input into values of a specified type. The value is assigned
|
|
|
|
* to an argument in the argument list. Format specifications have
|
|
|
|
* the form %[*][width][{h | l | I64 | L}]type */
|
|
|
|
/* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
|
2001-01-11 00:59:25 +01:00
|
|
|
else if (*format == '%') {
|
2002-02-08 18:17:11 +01:00
|
|
|
int st = 0; int suppress = 0; int width = 0;
|
|
|
|
int base, number_signed;
|
2001-01-11 00:59:25 +01:00
|
|
|
format++;
|
2002-02-08 18:17:11 +01:00
|
|
|
/* look for leading asterisk, which means 'suppress assignment of
|
|
|
|
* this field'. */
|
|
|
|
if (*format=='*') {
|
|
|
|
format++;
|
|
|
|
suppress=1;
|
|
|
|
}
|
|
|
|
/* look for width specification */
|
|
|
|
while (isdigit(*format)) {
|
|
|
|
width*=10;
|
|
|
|
width+=*format++ - '0';
|
|
|
|
}
|
|
|
|
if (width==0) width=-1; /* no width spec seen */
|
2001-01-11 00:59:25 +01:00
|
|
|
switch(*format) {
|
2002-02-08 18:17:11 +01:00
|
|
|
case '%': /* read a percent symbol */
|
|
|
|
if (nch!='%') break;
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
case 'X': /* hexadecimal integer. */
|
|
|
|
base = 16; number_signed = 0;
|
|
|
|
goto number;
|
|
|
|
case 'o': /* octal integer */
|
|
|
|
base = 8; number_signed = 0;
|
|
|
|
goto number;
|
|
|
|
case 'u': /* unsigned decimal integer */
|
|
|
|
base = 10; number_signed = 0;
|
|
|
|
goto number;
|
|
|
|
case 'd': /* signed decimal integer */
|
|
|
|
base = 10; number_signed = 1;
|
|
|
|
goto number;
|
|
|
|
case 'i': /* generic integer */
|
|
|
|
base = 0; number_signed = 1;
|
|
|
|
number: {
|
|
|
|
/* read an integer */
|
|
|
|
int*val = suppress ? NULL : va_arg(ap, int*);
|
|
|
|
int cur = 0; int negative = 0; int seendigit=0;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* skip initial whitespace */
|
|
|
|
while ((nch!=MSVCRT_EOF) && isspace(nch))
|
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
/* get sign */
|
|
|
|
if (number_signed && (nch == '-' || nch == '+')) {
|
|
|
|
negative = (nch=='-');
|
2001-01-11 00:59:25 +01:00
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
if (width>0) width--;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* look for leading indication of base */
|
|
|
|
if (width!=0 && nch == '0') {
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
seendigit=1;
|
|
|
|
if (width!=0 && (nch=='x' || nch=='X')) {
|
|
|
|
if (base==0)
|
|
|
|
base=16;
|
|
|
|
if (base==16) {
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
seendigit=0;
|
|
|
|
}
|
|
|
|
} else if (base==0)
|
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
if (base==0)
|
|
|
|
base=10;
|
|
|
|
/* throw away leading zeros */
|
|
|
|
while (width!=0 && nch=='0') {
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
seendigit=1;
|
|
|
|
}
|
|
|
|
/* get first digit. Keep working copy negative, as the
|
|
|
|
* range of negative numbers in two's complement notation
|
|
|
|
* is one larger than the range of positive numbers. */
|
|
|
|
if (width!=0 && char2digit(nch, base)!=-1) {
|
|
|
|
cur = -char2digit(nch, base);
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
seendigit=1;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
/* read until no more digits */
|
2002-02-08 18:17:11 +01:00
|
|
|
while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
|
|
|
|
cur = cur*base + char2digit(nch, base);
|
2001-01-11 00:59:25 +01:00
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
if (width>0) width--;
|
|
|
|
seendigit=1;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* negate parsed number if non-negative */
|
|
|
|
if (!negative) cur=-cur;
|
|
|
|
/* okay, done! */
|
|
|
|
if (!seendigit) break; /* not a valid number */
|
2001-01-11 00:59:25 +01:00
|
|
|
st = 1;
|
2002-02-08 18:17:11 +01:00
|
|
|
if (!suppress) *val = cur;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
break;
|
2002-02-08 18:17:11 +01:00
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
case 'G': { /* read a float */
|
|
|
|
float*val = suppress ? NULL : va_arg(ap, float*);
|
2001-01-11 00:59:25 +01:00
|
|
|
float cur = 0;
|
2002-02-08 18:17:11 +01:00
|
|
|
int negative = 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* skip initial whitespace */
|
|
|
|
while ((nch!=MSVCRT_EOF) && isspace(nch))
|
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
/* get sign. */
|
|
|
|
if (nch == '-' || nch == '+') {
|
|
|
|
negative = (nch=='-');
|
|
|
|
if (width>0) width--;
|
|
|
|
if (width==0) break;
|
2001-01-11 00:59:25 +01:00
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* get first digit. */
|
|
|
|
if (!isdigit(nch)) break;
|
|
|
|
cur = (nch - '0') * (negative ? -1 : 1);
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* read until no more digits */
|
2002-02-08 18:17:11 +01:00
|
|
|
while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
|
2001-01-11 00:59:25 +01:00
|
|
|
cur = cur*10 + (nch - '0');
|
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
if (width>0) width--;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* handle decimals */
|
|
|
|
if (width!=0 && nch == '.') {
|
2001-01-11 00:59:25 +01:00
|
|
|
float dec = 1;
|
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
if (width>0) width--;
|
|
|
|
while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
|
2001-01-11 00:59:25 +01:00
|
|
|
dec /= 10;
|
|
|
|
cur += dec * (nch - '0');
|
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
if (width>0) width--;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* handle exponent */
|
|
|
|
if (width!=0 && (nch == 'e' || nch == 'E')) {
|
|
|
|
int exponent = 0, negexp = 0;
|
|
|
|
float expcnt;
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
/* possible sign on the exponent */
|
|
|
|
if (width!=0 && (nch=='+' || nch=='-')) {
|
|
|
|
negexp = (nch=='-');
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
}
|
|
|
|
/* exponent digits */
|
|
|
|
while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
|
|
|
|
exponent *= 10;
|
|
|
|
exponent += (nch - '0');
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
if (width>0) width--;
|
|
|
|
}
|
|
|
|
/* update 'cur' with this exponent. */
|
|
|
|
expcnt = negexp ? .1 : 10;
|
|
|
|
while (exponent!=0) {
|
|
|
|
if (exponent&1)
|
|
|
|
cur*=expcnt;
|
|
|
|
exponent/=2;
|
|
|
|
expcnt=expcnt*expcnt;
|
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
st = 1;
|
2002-02-08 18:17:11 +01:00
|
|
|
if (!suppress) *val = cur;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's': { /* read a word */
|
2002-02-08 18:17:11 +01:00
|
|
|
char*str = suppress ? NULL : va_arg(ap, char*);
|
2001-01-11 00:59:25 +01:00
|
|
|
char*sptr = str;
|
|
|
|
/* skip initial whitespace */
|
|
|
|
while ((nch!=MSVCRT_EOF) && isspace(nch))
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
/* read until whitespace */
|
2002-02-08 18:17:11 +01:00
|
|
|
while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
|
|
|
|
if (!suppress) *sptr++ = nch;
|
|
|
|
st++;
|
2001-01-11 00:59:25 +01:00
|
|
|
nch = MSVCRT_fgetc(file);
|
2002-02-08 18:17:11 +01:00
|
|
|
if (width>0) width--;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
/* terminate */
|
2002-02-08 18:17:11 +01:00
|
|
|
if (!suppress) *sptr = 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE("read word: %s\n", str);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: FIXME("unhandled: %%%c\n", *format);
|
2002-02-08 18:17:11 +01:00
|
|
|
/* From spec: "if a percent sign is followed by a character
|
|
|
|
* that has no meaning as a format-control character, that
|
|
|
|
* character and the following characters are treated as
|
|
|
|
* an ordinary sequence of characters, that is, a sequence
|
|
|
|
* of characters that must match the input. For example,
|
|
|
|
* to specify that a percent-sign character is to be input,
|
|
|
|
* use %%."
|
|
|
|
* LEAVING AS-IS because we catch bugs better that way. */
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
if (st && !suppress) rd++;
|
2001-01-11 00:59:25 +01:00
|
|
|
else break;
|
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
/* a non-white-space character causes scanf to read, but not store,
|
|
|
|
* a matching non-white-space character. */
|
2001-01-11 00:59:25 +01:00
|
|
|
else {
|
|
|
|
/* check for character match */
|
|
|
|
if (nch == *format)
|
|
|
|
nch = MSVCRT_fgetc(file);
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
if (nch!=MSVCRT_EOF) {
|
2002-02-08 18:17:11 +01:00
|
|
|
FIXME("need ungetch\n");
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2002-02-08 18:17:11 +01:00
|
|
|
va_end(ap);
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE("returning %d\n", rd);
|
|
|
|
return rd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fseek (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _lseek(file->_file,offset,whence);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ftell (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
LONG MSVCRT_ftell(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _tell(file->_file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fwrite (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
int written = _write(file->_file, ptr, size * nmemb);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (written <= 0)
|
|
|
|
return 0;
|
|
|
|
return written / size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fputs (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fwrite(s,strlen(s),1,file) == 1 ? 0 : MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fputws (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fputws(const WCHAR *s, MSVCRT_FILE* file)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
return MSVCRT_fwrite(s,strlenW(s),1,file) == 1 ? 0 : MSVCRT_EOF;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* getchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_getchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fgetc(MSVCRT_stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_getc(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fgetc(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* gets (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char *MSVCRT_gets(char *buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
char * buf_start = buf;
|
|
|
|
|
|
|
|
/* BAD, for the whole WINE process blocks... just done this way to test
|
|
|
|
* windows95's ftp.exe.
|
|
|
|
* JG 19/9/00: Is this still true, now we are using ReadFile?
|
|
|
|
*/
|
|
|
|
for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n';
|
|
|
|
cc = MSVCRT_fgetc(MSVCRT_stdin))
|
|
|
|
if(cc != '\r') *buf++ = (char)cc;
|
|
|
|
|
|
|
|
*buf = '\0';
|
|
|
|
|
|
|
|
TRACE("got '%s'\n", buf_start);
|
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* putc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_putc(int c, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_fputc(c, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* putchar (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
int MSVCRT_putchar(int c)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
return MSVCRT_fputc(c, MSVCRT_stdout);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* puts (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_puts(const char *s)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval = MSVCRT_EOF;
|
|
|
|
if (MSVCRT_fwrite(s,strlen(s),1,MSVCRT_stdout) == 1)
|
|
|
|
return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _putws (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _putws(const WCHAR *s)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
static const WCHAR nl = (WCHAR)L'\n';
|
2001-04-11 01:25:25 +02:00
|
|
|
if (MSVCRT_fwrite(s,strlenW(s),1,MSVCRT_stdout) == 1)
|
2001-01-22 03:21:54 +01:00
|
|
|
return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* remove (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_remove(const char *path)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("(%s)\n",path);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (DeleteFileA(path))
|
|
|
|
return 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE(":failed (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wremove (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wremove(const WCHAR *path)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
TRACE("(%s)\n",debugstr_w(path));
|
|
|
|
if (DeleteFileW(path))
|
|
|
|
return 0;
|
|
|
|
TRACE(":failed (%ld)\n",GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* scanf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_scanf(const char *format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
va_start(valist, format);
|
|
|
|
res = MSVCRT_fscanf(MSVCRT_stdin, format, valist);
|
|
|
|
va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* rename (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_rename(const char *oldpath,const char *newpath)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":from %s to %s\n",oldpath,newpath);
|
|
|
|
if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
|
|
|
|
return 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE(":failed (%ld)\n",GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wrename (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _wrename(const WCHAR *oldpath,const WCHAR *newpath)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
|
|
|
|
if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
|
|
|
|
return 0;
|
|
|
|
TRACE(":failed (%ld)\n",GetLastError());
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* setvbuf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
FIXME("(%p,%p,%d,%d)stub\n",file, buf, mode, size);
|
2001-04-11 01:25:25 +02:00
|
|
|
return -1;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* setbuf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, BUFSIZ);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* tmpnam (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char *MSVCRT_tmpnam(char *s)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char tmpbuf[MAX_PATH];
|
|
|
|
char* prefix = "TMP";
|
|
|
|
if (!GetTempPathA(MAX_PATH,tmpbuf) ||
|
|
|
|
!GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname))
|
|
|
|
{
|
|
|
|
TRACE(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
TRACE(":got tmpnam %s\n",MSVCRT_tmpname);
|
|
|
|
s = MSVCRT_tmpname;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* tmpfile (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_FILE* MSVCRT_tmpfile(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char *filename = MSVCRT_tmpnam(NULL);
|
|
|
|
int fd;
|
2001-04-10 23:16:07 +02:00
|
|
|
fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (fd != -1)
|
2001-04-10 23:16:07 +02:00
|
|
|
return msvcrt_alloc_fp(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* vfprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char buf[2048], *mem = buf;
|
2001-01-22 03:21:54 +01:00
|
|
|
int written, resize = sizeof(buf), retval;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* There are two conventions for vsnprintf failing:
|
|
|
|
* Return -1 if we truncated, or
|
|
|
|
* Return the number of bytes that would have been written
|
|
|
|
* The code below handles both cases
|
|
|
|
*/
|
|
|
|
while ((written = vsnprintf(mem, resize, format, valist)) == -1 ||
|
|
|
|
written > resize)
|
|
|
|
{
|
|
|
|
resize = (written == -1 ? resize * 2 : written + 1);
|
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
|
|
|
if (!(mem = (char *)MSVCRT_malloc(resize)))
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
2001-01-22 03:21:54 +01:00
|
|
|
retval = MSVCRT_fwrite(mem, 1, written, file);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
2001-01-22 03:21:54 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* vfwprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_vfwprintf(MSVCRT_FILE* file, const WCHAR *format, va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
WCHAR buf[2048], *mem = buf;
|
|
|
|
int written, resize = sizeof(buf) / sizeof(WCHAR), retval;
|
|
|
|
/* See vfprintf comments */
|
2001-04-10 23:16:07 +02:00
|
|
|
while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
|
2001-01-22 03:21:54 +01:00
|
|
|
written > resize)
|
|
|
|
{
|
|
|
|
resize = (written == -1 ? resize * 2 : written + sizeof(WCHAR));
|
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
|
|
|
if (!(mem = (WCHAR *)MSVCRT_malloc(resize)))
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
retval = MSVCRT_fwrite(mem, 1, written * sizeof (WCHAR), file);
|
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* vprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_vprintf(const char *format, va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_vfprintf(MSVCRT_stdout,format,valist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* vwprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_vwprintf(const WCHAR *format, va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
int res;
|
|
|
|
va_start(valist, format);
|
|
|
|
res = MSVCRT_vfprintf(file, format, valist);
|
|
|
|
va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fwprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_fwprintf(MSVCRT_FILE* file, const WCHAR *format, ...)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
int res;
|
|
|
|
va_start(valist, format);
|
|
|
|
res = MSVCRT_vfwprintf(file, format, valist);
|
|
|
|
va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* printf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_printf(const char *format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
int res;
|
|
|
|
va_start(valist, format);
|
|
|
|
res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist);
|
|
|
|
va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* wprintf (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int MSVCRT_wprintf(const WCHAR *format, ...)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
int res;
|
|
|
|
va_start(valist, format);
|
|
|
|
res = MSVCRT_vwprintf(format, valist);
|
|
|
|
va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|