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
|
2004-05-12 02:12:26 +02:00
|
|
|
* Copyright 2004 Eric Pouech
|
2004-12-14 12:59:43 +01:00
|
|
|
* Copyright 2004 Juan Lang
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-01-10 15:28:21 +01:00
|
|
|
*
|
|
|
|
* TODO
|
|
|
|
* Use the file flag hints O_SEQUENTIAL, O_RANDOM, O_SHORT_LIVED
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2002-04-26 21:05:15 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
#include <time.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2001-04-11 01:25:25 +02:00
|
|
|
#include <stdio.h>
|
2002-08-17 02:43:16 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2004-06-25 03:19:15 +02:00
|
|
|
#include <sys/types.h>
|
2001-04-11 01:25:25 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winreg.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
|
|
|
|
2001-04-11 01:25:25 +02:00
|
|
|
#include "wine/unicode.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 */
|
2004-06-25 03:19:15 +02:00
|
|
|
#define ALL_S_IREAD (MSVCRT__S_IREAD | (MSVCRT__S_IREAD >> 3) | (MSVCRT__S_IREAD >> 6))
|
|
|
|
#define ALL_S_IWRITE (MSVCRT__S_IWRITE | (MSVCRT__S_IWRITE >> 3) | (MSVCRT__S_IWRITE >> 6))
|
|
|
|
#define ALL_S_IEXEC (MSVCRT__S_IEXEC | (MSVCRT__S_IEXEC >> 3) | (MSVCRT__S_IEXEC >> 6))
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* _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
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
/* values for wxflag in file descriptor */
|
2004-05-12 02:12:26 +02:00
|
|
|
#define WX_OPEN 0x01
|
|
|
|
#define WX_ATEOF 0x02
|
|
|
|
#define WX_DONTINHERIT 0x10
|
|
|
|
#define WX_APPEND 0x20
|
|
|
|
#define WX_TEXT 0x80
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-10 16:28:25 +01:00
|
|
|
/* FIXME: this should be allocated dynamically */
|
2004-12-09 17:51:26 +01:00
|
|
|
#define MSVCRT_MAX_FILES 2048
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-10 16:28:25 +01:00
|
|
|
typedef struct {
|
2004-05-12 02:12:26 +02:00
|
|
|
HANDLE handle;
|
2004-09-03 03:05:30 +02:00
|
|
|
unsigned char wxflag;
|
2004-12-10 16:28:25 +01:00
|
|
|
DWORD unkn[7]; /* critical section and init flag */
|
|
|
|
} ioinfo;
|
|
|
|
|
|
|
|
static ioinfo MSVCRT_fdesc[MSVCRT_MAX_FILES];
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_FILE MSVCRT__iob[3];
|
|
|
|
|
|
|
|
static int MSVCRT_fdstart = 3; /* first unallocated fd */
|
|
|
|
static int MSVCRT_fdend = 3; /* highest allocated fd */
|
|
|
|
|
2004-12-09 17:51:26 +01:00
|
|
|
static MSVCRT_FILE* MSVCRT_fstreams[2048];
|
2004-05-12 02:12:26 +02:00
|
|
|
static int MSVCRT_stream_idx;
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* 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';
|
|
|
|
|
2002-12-19 05:21:30 +01:00
|
|
|
#define TOUL(x) (ULONGLONG)(x)
|
2001-01-22 03:21:54 +01:00
|
|
|
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');
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
/* This critical section protects the tables MSVCRT_fdesc and MSVCRT_fstreams,
|
|
|
|
* and their related indexes, MSVCRT_fdstart, MSVCRT_fdend,
|
|
|
|
* and MSVCRT_stream_idx, from race conditions.
|
|
|
|
* It doesn't protect against race conditions manipulating the underlying files
|
|
|
|
* or flags; doing so would probably be better accomplished with per-file
|
|
|
|
* protection, rather than locking the whole table for every change.
|
|
|
|
*/
|
|
|
|
static CRITICAL_SECTION MSVCRT_file_cs;
|
|
|
|
#define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
|
|
|
|
#define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
static void msvcrt_cp_from_stati64(const struct MSVCRT__stati64 *bufi64, struct MSVCRT__stat *buf)
|
2002-11-25 03:44:34 +01:00
|
|
|
{
|
|
|
|
buf->st_dev = bufi64->st_dev;
|
|
|
|
buf->st_ino = bufi64->st_ino;
|
|
|
|
buf->st_mode = bufi64->st_mode;
|
|
|
|
buf->st_nlink = bufi64->st_nlink;
|
|
|
|
buf->st_uid = bufi64->st_uid;
|
|
|
|
buf->st_gid = bufi64->st_gid;
|
|
|
|
buf->st_rdev = bufi64->st_rdev;
|
|
|
|
buf->st_size = bufi64->st_size;
|
|
|
|
buf->st_atime = bufi64->st_atime;
|
|
|
|
buf->st_mtime = bufi64->st_mtime;
|
|
|
|
buf->st_ctime = bufi64->st_ctime;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
static inline BOOL msvcrt_is_valid_fd(int fd)
|
|
|
|
{
|
2004-09-03 03:05:30 +02:00
|
|
|
return fd >= 0 && fd < MSVCRT_fdend && (MSVCRT_fdesc[fd].wxflag & WX_OPEN);
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
/* INTERNAL: Get the HANDLE for a fd
|
|
|
|
* This doesn't lock the table, because a failure will result in
|
|
|
|
* INVALID_HANDLE_VALUE being returned, which should be handled correctly. If
|
|
|
|
* it returns a valid handle which is about to be closed, a subsequent call
|
|
|
|
* will fail, most likely in a sane way.
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
static HANDLE msvcrt_fdtoh(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
if (!msvcrt_is_valid_fd(fd))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
WARN(":fd (%d) - no handle!\n",fd);
|
2004-03-16 20:17:11 +01:00
|
|
|
*MSVCRT___doserrno() = 0;
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EBADF;
|
2004-12-14 12:59:43 +01:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2004-05-12 02:12:26 +02:00
|
|
|
if (MSVCRT_fdesc[fd].handle == INVALID_HANDLE_VALUE) FIXME("wtf\n");
|
|
|
|
return MSVCRT_fdesc[fd].handle;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
MSVCRT_fdesc[fd].handle = INVALID_HANDLE_VALUE;
|
2004-09-03 03:05:30 +02:00
|
|
|
MSVCRT_fdesc[fd].wxflag = 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE(":fd (%d) freed\n",fd);
|
2004-05-12 02:12:26 +02:00
|
|
|
if (fd < 3) /* don't use 0,1,2 for user files */
|
|
|
|
{
|
|
|
|
switch (fd)
|
|
|
|
{
|
|
|
|
case 0: SetStdHandle(STD_INPUT_HANDLE, NULL); break;
|
|
|
|
case 1: SetStdHandle(STD_OUTPUT_HANDLE, NULL); break;
|
|
|
|
case 2: SetStdHandle(STD_ERROR_HANDLE, NULL); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fd == MSVCRT_fdend - 1)
|
|
|
|
MSVCRT_fdend--;
|
|
|
|
if (fd < MSVCRT_fdstart)
|
|
|
|
MSVCRT_fdstart = fd;
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2004-12-09 17:51:26 +01:00
|
|
|
/* INTERNAL: Allocate an fd slot from a Win32 HANDLE, starting from fd */
|
2004-12-14 12:59:43 +01:00
|
|
|
/* caller must hold the files lock */
|
2004-12-09 17:51:26 +01:00
|
|
|
static int msvcrt_alloc_fd_from(HANDLE hand, int flag, int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (fd >= MSVCRT_MAX_FILES)
|
|
|
|
{
|
|
|
|
WARN(":files exhausted!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-12 02:12:26 +02:00
|
|
|
MSVCRT_fdesc[fd].handle = hand;
|
2004-09-03 03:05:30 +02:00
|
|
|
MSVCRT_fdesc[fd].wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT));
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* locate next free slot */
|
2004-12-09 17:51:26 +01:00
|
|
|
if (fd == MSVCRT_fdstart && fd == MSVCRT_fdend)
|
|
|
|
MSVCRT_fdstart = MSVCRT_fdend + 1;
|
2001-01-11 00:59:25 +01:00
|
|
|
else
|
2004-09-03 03:05:30 +02:00
|
|
|
while (MSVCRT_fdstart < MSVCRT_fdend &&
|
2004-12-09 17:51:26 +01:00
|
|
|
MSVCRT_fdesc[MSVCRT_fdstart].handle != INVALID_HANDLE_VALUE)
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_fdstart++;
|
2004-12-09 17:51:26 +01:00
|
|
|
/* update last fd in use */
|
|
|
|
if (fd >= MSVCRT_fdend)
|
|
|
|
MSVCRT_fdend = fd + 1;
|
|
|
|
TRACE("fdstart is %d, fdend is %d\n", MSVCRT_fdstart, MSVCRT_fdend);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
switch (fd)
|
|
|
|
{
|
|
|
|
case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
|
|
|
|
case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
|
|
|
|
case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2004-12-09 17:51:26 +01:00
|
|
|
/* INTERNAL: Allocate an fd slot from a Win32 HANDLE */
|
|
|
|
static int msvcrt_alloc_fd(HANDLE hand, int flag)
|
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
LOCK_FILES();
|
2004-12-09 17:51:26 +01:00
|
|
|
TRACE(":handle (%p) allocating fd (%d)\n",hand,MSVCRT_fdstart);
|
2004-12-14 12:59:43 +01:00
|
|
|
ret = msvcrt_alloc_fd_from(hand, flag, MSVCRT_fdstart);
|
|
|
|
UNLOCK_FILES();
|
|
|
|
return ret;
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
/* INTERNAL: Allocate a FILE* for an fd slot */
|
|
|
|
/* caller must hold the files lock */
|
2004-05-12 02:12:26 +02:00
|
|
|
static MSVCRT_FILE* msvcrt_alloc_fp(void)
|
|
|
|
{
|
2004-09-08 03:23:57 +02:00
|
|
|
unsigned int i;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
|
|
|
for (i = 3; i < sizeof(MSVCRT_fstreams) / sizeof(MSVCRT_fstreams[0]); i++)
|
|
|
|
{
|
|
|
|
if (!MSVCRT_fstreams[i] || MSVCRT_fstreams[i]->_flag == 0)
|
|
|
|
{
|
|
|
|
if (!MSVCRT_fstreams[i])
|
|
|
|
{
|
|
|
|
if (!(MSVCRT_fstreams[i] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1)))
|
|
|
|
return NULL;
|
|
|
|
if (i == MSVCRT_stream_idx) MSVCRT_stream_idx++;
|
|
|
|
}
|
|
|
|
return MSVCRT_fstreams[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: initialize a FILE* from an open fd */
|
|
|
|
static int msvcrt_init_fp(MSVCRT_FILE* file, int fd, unsigned stream_flags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":fd (%d) allocating FILE*\n",fd);
|
2004-05-12 02:12:26 +02:00
|
|
|
if (!msvcrt_is_valid_fd(fd))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
WARN(":invalid fd %d\n",fd);
|
2004-03-16 20:17:11 +01:00
|
|
|
*MSVCRT___doserrno() = 0;
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EBADF;
|
2004-05-12 02:12:26 +02:00
|
|
|
return -1;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2004-05-12 02:12:26 +02:00
|
|
|
memset(file, 0, sizeof(*file));
|
|
|
|
file->_file = fd;
|
|
|
|
file->_flag = stream_flags;
|
|
|
|
|
|
|
|
TRACE(":got FILE* (%p)\n",file);
|
|
|
|
return 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
/* INTERNAL: Create an inheritance data block (for spawned process)
|
|
|
|
* The inheritance block is made of:
|
|
|
|
* 00 int nb of file descriptor (NBFD)
|
|
|
|
* 04 char file flags (wxflag): repeated for each fd
|
|
|
|
* 4+NBFD HANDLE file handle: repeated for each fd
|
|
|
|
*/
|
|
|
|
unsigned msvcrt_create_io_inherit_block(STARTUPINFOA* si)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char* wxflag_ptr;
|
|
|
|
HANDLE* handle_ptr;
|
|
|
|
|
|
|
|
si->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * MSVCRT_fdend;
|
|
|
|
si->lpReserved2 = MSVCRT_calloc(si->cbReserved2, 1);
|
|
|
|
if (!si->lpReserved2)
|
|
|
|
{
|
|
|
|
si->cbReserved2 = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
wxflag_ptr = (char*)si->lpReserved2 + sizeof(unsigned);
|
|
|
|
handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
|
|
|
|
|
|
|
|
*(unsigned*)si->lpReserved2 = MSVCRT_fdend;
|
|
|
|
for (fd = 0; fd < MSVCRT_fdend; fd++)
|
|
|
|
{
|
|
|
|
/* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
|
|
|
|
if ((MSVCRT_fdesc[fd].wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
|
|
|
|
{
|
|
|
|
*wxflag_ptr = MSVCRT_fdesc[fd].wxflag;
|
|
|
|
*handle_ptr = MSVCRT_fdesc[fd].handle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*wxflag_ptr = 0;
|
|
|
|
*handle_ptr = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
wxflag_ptr++; handle_ptr++;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
/* INTERNAL: Set up all file descriptors,
|
|
|
|
* as well as default streams (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
|
|
|
{
|
2004-09-03 03:05:30 +02:00
|
|
|
STARTUPINFOA si;
|
|
|
|
int i;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
InitializeCriticalSection(&MSVCRT_file_cs);
|
2004-09-03 03:05:30 +02:00
|
|
|
GetStartupInfoA(&si);
|
|
|
|
if (si.cbReserved2 != 0 && si.lpReserved2 != NULL)
|
|
|
|
{
|
|
|
|
char* wxflag_ptr;
|
|
|
|
HANDLE* handle_ptr;
|
|
|
|
|
|
|
|
MSVCRT_fdend = *(unsigned*)si.lpReserved2;
|
|
|
|
|
|
|
|
wxflag_ptr = (char*)(si.lpReserved2 + sizeof(unsigned));
|
|
|
|
handle_ptr = (HANDLE*)(wxflag_ptr + MSVCRT_fdend * sizeof(char));
|
|
|
|
|
|
|
|
MSVCRT_fdend = min(MSVCRT_fdend, sizeof(MSVCRT_fdesc) / sizeof(MSVCRT_fdesc[0]));
|
|
|
|
for (i = 0; i < MSVCRT_fdend; i++)
|
|
|
|
{
|
|
|
|
if ((*wxflag_ptr & WX_OPEN) && *handle_ptr != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
MSVCRT_fdesc[i].wxflag = *wxflag_ptr;
|
|
|
|
MSVCRT_fdesc[i].handle = *handle_ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MSVCRT_fdesc[i].wxflag = 0;
|
|
|
|
MSVCRT_fdesc[i].handle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
wxflag_ptr++; handle_ptr++;
|
|
|
|
}
|
|
|
|
for (MSVCRT_fdstart = 3; MSVCRT_fdstart < MSVCRT_fdend; MSVCRT_fdstart++)
|
|
|
|
if (MSVCRT_fdesc[MSVCRT_fdstart].handle == INVALID_HANDLE_VALUE) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(MSVCRT_fdesc[0].wxflag & WX_OPEN) || MSVCRT_fdesc[0].handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
|
2006-04-08 11:59:14 +02:00
|
|
|
GetCurrentProcess(), &MSVCRT_fdesc[0].handle, 0, TRUE,
|
2004-09-03 03:05:30 +02:00
|
|
|
DUPLICATE_SAME_ACCESS);
|
|
|
|
MSVCRT_fdesc[0].wxflag = WX_OPEN | WX_TEXT;
|
|
|
|
}
|
|
|
|
if (!(MSVCRT_fdesc[1].wxflag & WX_OPEN) || MSVCRT_fdesc[1].handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
|
2006-04-08 11:59:14 +02:00
|
|
|
GetCurrentProcess(), &MSVCRT_fdesc[1].handle, 0, TRUE,
|
2004-09-03 03:05:30 +02:00
|
|
|
DUPLICATE_SAME_ACCESS);
|
|
|
|
MSVCRT_fdesc[1].wxflag = WX_OPEN | WX_TEXT;
|
|
|
|
}
|
|
|
|
if (!(MSVCRT_fdesc[2].wxflag & WX_OPEN) || MSVCRT_fdesc[2].handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
|
2006-04-08 11:59:14 +02:00
|
|
|
GetCurrentProcess(), &MSVCRT_fdesc[2].handle, 0, TRUE,
|
2004-09-03 03:05:30 +02:00
|
|
|
DUPLICATE_SAME_ACCESS);
|
|
|
|
MSVCRT_fdesc[2].wxflag = WX_OPEN | WX_TEXT;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_fdesc[0].handle,
|
|
|
|
MSVCRT_fdesc[1].handle,MSVCRT_fdesc[2].handle);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE));
|
2001-01-11 00:59:25 +01:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
/* FILE structs for stdin/out/err are static and never deleted */
|
2004-05-12 02:12:26 +02:00
|
|
|
MSVCRT_fstreams[i] = &MSVCRT__iob[i];
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__iob[i]._file = i;
|
2004-09-03 03:05:30 +02:00
|
|
|
MSVCRT__iob[i]._tmpfname = NULL;
|
|
|
|
MSVCRT__iob[i]._flag = (i == 0) ? MSVCRT__IOREAD : MSVCRT__IOWRT;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2004-05-12 02:12:26 +02:00
|
|
|
MSVCRT_stream_idx = 3;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/* INTERNAL: Flush stdio file buffer */
|
|
|
|
static int msvcrt_flush_buffer(MSVCRT_FILE* file)
|
|
|
|
{
|
|
|
|
if(file->_bufsiz) {
|
|
|
|
int cnt=file->_ptr-file->_base;
|
|
|
|
if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
|
2004-05-12 02:12:26 +02:00
|
|
|
file->_flag |= MSVCRT__IOERR;
|
|
|
|
return MSVCRT_EOF;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
file->_ptr=file->_base;
|
|
|
|
file->_cnt=file->_bufsiz;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: Allocate stdio file buffer */
|
|
|
|
static void msvcrt_alloc_buffer(MSVCRT_FILE* file)
|
|
|
|
{
|
|
|
|
file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1);
|
|
|
|
if(file->_base) {
|
|
|
|
file->_bufsiz = MSVCRT_BUFSIZ;
|
|
|
|
file->_flag |= MSVCRT__IOMYBUF;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2005-09-02 14:26:21 +02:00
|
|
|
file->_base = (char*)(&file->_charbuf);
|
2002-03-11 06:03:05 +01:00
|
|
|
/* put here 2 ??? */
|
|
|
|
file->_bufsiz = sizeof(file->_charbuf);
|
|
|
|
}
|
|
|
|
file->_ptr = file->_base;
|
|
|
|
file->_cnt = 0;
|
|
|
|
}
|
|
|
|
|
2003-10-24 02:23:51 +02:00
|
|
|
/* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
|
|
|
|
static void msvcrt_int_to_base32(int num, char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int n = num;
|
|
|
|
int digits = 0;
|
|
|
|
|
|
|
|
while (n != 0)
|
|
|
|
{
|
|
|
|
n >>= 5;
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
p = str + digits;
|
|
|
|
*p = 0;
|
|
|
|
while (--p >= str)
|
|
|
|
{
|
|
|
|
*p = (num & 31) + '0';
|
|
|
|
if (*p > '9')
|
|
|
|
*p += ('a' - '0' - 10);
|
|
|
|
num >>= 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* __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);
|
|
|
|
|
2003-10-16 21:12:49 +02:00
|
|
|
if (!filename || attr == INVALID_FILE_ATTRIBUTES)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
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
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_ACCESS_DENIED);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _waccess (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _waccess(const MSVCRT_wchar_t *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);
|
|
|
|
|
2003-10-16 21:12:49 +02:00
|
|
|
if (!filename || attr == INVALID_FILE_ATTRIBUTES)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
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
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_ACCESS_DENIED);
|
2001-01-22 03:21:54 +01:00
|
|
|
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);
|
|
|
|
|
2003-10-16 21:12:49 +02:00
|
|
|
if (oldFlags != INVALID_FILE_ATTRIBUTES)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
|
2001-01-11 00:59:25 +01:00
|
|
|
oldFlags | FILE_ATTRIBUTE_READONLY;
|
|
|
|
|
|
|
|
if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wchmod (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wchmod(const MSVCRT_wchar_t *path, int flags)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
DWORD oldFlags = GetFileAttributesW(path);
|
|
|
|
|
2003-10-16 21:12:49 +02:00
|
|
|
if (oldFlags != INVALID_FILE_ATTRIBUTES)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
DWORD newFlags = (flags & MSVCRT__S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
|
2001-01-22 03:21:54 +01:00
|
|
|
oldFlags | FILE_ATTRIBUTE_READONLY;
|
|
|
|
|
|
|
|
if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
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
|
|
|
{
|
2005-11-16 12:47:51 +01:00
|
|
|
TRACE("%s\n",debugstr_a(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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wunlink (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wunlink(const MSVCRT_wchar_t *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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/* _flushall calls MSVCRT_fflush which calls _flushall */
|
|
|
|
int MSVCRT_fflush(MSVCRT_FILE* file);
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _flushall (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _flushall(void)
|
|
|
|
{
|
|
|
|
int i, num_flushed = 0;
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
for (i = 3; i < MSVCRT_stream_idx; i++)
|
|
|
|
if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/* FIXME: flush, do not commit */
|
|
|
|
if (_commit(i) == -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
if (MSVCRT_fstreams[i])
|
|
|
|
MSVCRT_fstreams[i]->_flag |= MSVCRT__IOERR;
|
2004-03-16 20:17:11 +01:00
|
|
|
#endif
|
2004-05-12 02:12:26 +02:00
|
|
|
if(MSVCRT_fstreams[i]->_flag & MSVCRT__IOWRT) {
|
|
|
|
MSVCRT_fflush(MSVCRT_fstreams[i]);
|
2004-03-16 20:17:11 +01:00
|
|
|
num_flushed++;
|
|
|
|
}
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2004-03-16 20:17:11 +01:00
|
|
|
|
|
|
|
TRACE(":flushed (%d) handles\n",num_flushed);
|
|
|
|
return num_flushed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fflush (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int MSVCRT_fflush(MSVCRT_FILE* file)
|
|
|
|
{
|
|
|
|
if(!file) {
|
|
|
|
_flushall();
|
2006-03-24 17:53:12 +01:00
|
|
|
} else if(file->_flag & MSVCRT__IOWRT) {
|
2004-03-16 20:17:11 +01:00
|
|
|
int res=msvcrt_flush_buffer(file);
|
|
|
|
return res;
|
|
|
|
}
|
2006-03-24 17:53:12 +01:00
|
|
|
return 0;
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _close (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _close(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
HANDLE hand;
|
|
|
|
int ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
|
|
|
hand = msvcrt_fdtoh(fd);
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
2004-12-14 12:59:43 +01:00
|
|
|
ret = -1;
|
|
|
|
else if (!CloseHandle(hand))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2004-12-14 12:59:43 +01:00
|
|
|
ret = -1;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
msvcrt_free_fd(fd);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE(":ok\n");
|
2004-12-14 12:59:43 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _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
|
|
|
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
TRACE(":ok\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-09 17:51:26 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _dup2 (MSVCRT.@)
|
|
|
|
* NOTES
|
|
|
|
* MSDN isn't clear on this point, but the remarks for _pipe,
|
|
|
|
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__pipe.asp
|
|
|
|
* indicate file descriptors duplicated with _dup and _dup2 are always
|
|
|
|
* inheritable.
|
|
|
|
*/
|
|
|
|
int _dup2(int od, int nd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
TRACE("(od=%d, nd=%d)\n", od, nd);
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-12-09 17:51:26 +01:00
|
|
|
if (nd < MSVCRT_MAX_FILES && msvcrt_is_valid_fd(od))
|
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
|
|
|
|
if (DuplicateHandle(GetCurrentProcess(), MSVCRT_fdesc[od].handle,
|
|
|
|
GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
int wxflag = MSVCRT_fdesc[od].wxflag & ~MSVCRT__O_NOINHERIT;
|
|
|
|
|
|
|
|
if (msvcrt_is_valid_fd(nd))
|
|
|
|
_close(nd);
|
|
|
|
ret = msvcrt_alloc_fd_from(handle, wxflag, nd);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
CloseHandle(handle);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* _dup2 returns 0, not nd, on success */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*MSVCRT__errno() = MSVCRT_EBADF;
|
|
|
|
ret = -1;
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2004-12-09 17:51:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _dup (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _dup(int od)
|
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
LOCK_FILES();
|
|
|
|
fd = MSVCRT_fdstart;
|
2004-12-09 17:51:26 +01:00
|
|
|
if (_dup2(od, fd) == 0)
|
2004-12-14 12:59:43 +01:00
|
|
|
ret = fd;
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
UNLOCK_FILES();
|
|
|
|
return ret;
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _eof (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _eof(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2005-09-02 14:26:21 +02:00
|
|
|
DWORD curpos,endpos;
|
|
|
|
LONG hcurpos,hendpos;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
if (MSVCRT_fdesc[fd].wxflag & WX_ATEOF) return TRUE;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Otherwise we do it the hard way */
|
2004-05-12 02:12:26 +02:00
|
|
|
hcurpos = hendpos = 0;
|
2005-09-02 14:26:21 +02:00
|
|
|
curpos = SetFilePointer(hand, 0, &hcurpos, FILE_CURRENT);
|
2004-05-12 02:12:26 +02:00
|
|
|
endpos = SetFilePointer(hand, 0, &hendpos, FILE_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
if (curpos == endpos && hcurpos == hendpos)
|
2004-12-14 12:59:43 +01:00
|
|
|
{
|
|
|
|
/* FIXME: shouldn't WX_ATEOF be set here? */
|
2001-01-11 00:59:25 +01:00
|
|
|
return TRUE;
|
2004-12-14 12:59:43 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
SetFilePointer(hand, curpos, &hcurpos, FILE_BEGIN);
|
2001-01-11 00:59:25 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fcloseall (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__fcloseall(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int num_closed = 0, i;
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
for (i = 3; i < MSVCRT_stream_idx; i++)
|
|
|
|
if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_flag &&
|
2005-11-16 12:47:51 +01:00
|
|
|
!MSVCRT_fclose(MSVCRT_fstreams[i]))
|
2001-01-11 00:59:25 +01:00
|
|
|
num_closed++;
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":closed (%d) handles\n",num_closed);
|
|
|
|
return num_closed;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/* free everything on process exit */
|
|
|
|
void msvcrt_free_io(void)
|
|
|
|
{
|
|
|
|
MSVCRT__fcloseall();
|
2005-05-12 11:55:02 +02:00
|
|
|
/* The Win32 _fcloseall() function explicitly doesn't close stdin,
|
|
|
|
* stdout, and stderr (unlike GNU), so we need to fclose() them here
|
|
|
|
* or they won't get flushed.
|
|
|
|
*/
|
|
|
|
MSVCRT_fclose(&MSVCRT__iob[0]);
|
|
|
|
MSVCRT_fclose(&MSVCRT__iob[1]);
|
|
|
|
MSVCRT_fclose(&MSVCRT__iob[2]);
|
2004-12-14 12:59:43 +01:00
|
|
|
DeleteCriticalSection(&MSVCRT_file_cs);
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2004-01-13 06:45:05 +01:00
|
|
|
* _lseeki64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2002-11-25 21:50:01 +01:00
|
|
|
__int64 _lseeki64(int fd, __int64 offset, int whence)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2005-09-02 14:26:21 +02:00
|
|
|
LARGE_INTEGER ofs, ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (whence < 0 || whence > 2)
|
|
|
|
{
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-02 14:26:21 +02:00
|
|
|
TRACE(":fd (%d) to %s pos %s\n",
|
2006-02-17 17:37:14 +01:00
|
|
|
fd,wine_dbgstr_longlong(offset),
|
2002-11-25 21:50:01 +01:00
|
|
|
(whence==SEEK_SET)?"SEEK_SET":
|
2001-01-11 00:59:25 +01:00
|
|
|
(whence==SEEK_CUR)?"SEEK_CUR":
|
|
|
|
(whence==SEEK_END)?"SEEK_END":"UNKNOWN");
|
|
|
|
|
2005-09-02 14:26:21 +02:00
|
|
|
ofs.QuadPart = offset;
|
|
|
|
if (SetFilePointerEx(hand, ofs, &ret, whence))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-09-03 03:05:30 +02:00
|
|
|
MSVCRT_fdesc[fd].wxflag &= ~WX_ATEOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* FIXME: What if we seek _to_ EOF - is EOF set? */
|
2002-11-25 21:50:01 +01:00
|
|
|
|
2005-09-02 14:26:21 +02:00
|
|
|
return ret.QuadPart;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
TRACE(":error-last error (%ld)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-11-25 21:50:01 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _lseek (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
LONG _lseek(int fd, LONG offset, int whence)
|
|
|
|
{
|
|
|
|
return _lseeki64(fd, offset, whence);
|
|
|
|
}
|
|
|
|
|
2002-03-20 23:50:38 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _locking (MSVCRT.@)
|
|
|
|
*
|
|
|
|
* This is untested; the underlying LockFile doesn't work yet.
|
|
|
|
*/
|
|
|
|
int _locking(int fd, int mode, LONG nbytes)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD cur_locn;
|
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
|
|
|
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2002-03-20 23:50:38 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (mode < 0 || mode > 4)
|
|
|
|
{
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
2002-03-20 23:50:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(":fd (%d) by 0x%08lx mode %s\n",
|
2004-06-25 03:19:15 +02:00
|
|
|
fd,nbytes,(mode==MSVCRT__LK_UNLCK)?"_LK_UNLCK":
|
|
|
|
(mode==MSVCRT__LK_LOCK)?"_LK_LOCK":
|
|
|
|
(mode==MSVCRT__LK_NBLCK)?"_LK_NBLCK":
|
|
|
|
(mode==MSVCRT__LK_RLCK)?"_LK_RLCK":
|
|
|
|
(mode==MSVCRT__LK_NBRLCK)?"_LK_NBRLCK":
|
2002-03-20 23:50:38 +01:00
|
|
|
"UNKNOWN");
|
|
|
|
|
2003-04-14 23:33:49 +02:00
|
|
|
if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == INVALID_SET_FILE_POINTER)
|
2002-03-20 23:50:38 +01:00
|
|
|
{
|
|
|
|
FIXME ("Seek failed\n");
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */
|
2002-03-20 23:50:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-06-25 03:19:15 +02:00
|
|
|
if (mode == MSVCRT__LK_LOCK || mode == MSVCRT__LK_RLCK)
|
2002-03-20 23:50:38 +01:00
|
|
|
{
|
|
|
|
int nretry = 10;
|
|
|
|
ret = 1; /* just to satisfy gcc */
|
|
|
|
while (nretry--)
|
|
|
|
{
|
|
|
|
ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
|
|
|
|
if (ret) break;
|
2004-06-25 03:19:15 +02:00
|
|
|
Sleep(1);
|
2002-03-20 23:50:38 +01:00
|
|
|
}
|
|
|
|
}
|
2004-06-25 03:19:15 +02:00
|
|
|
else if (mode == MSVCRT__LK_UNLCK)
|
2002-03-20 23:50:38 +01:00
|
|
|
ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L);
|
|
|
|
else
|
|
|
|
ret = LockFile(hand, cur_locn, 0L, nbytes, 0L);
|
|
|
|
/* FIXME - what about error settings? */
|
|
|
|
return ret ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fseek (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
|
|
|
|
{
|
|
|
|
/* Flush output if needed */
|
|
|
|
if(file->_flag & MSVCRT__IOWRT)
|
|
|
|
msvcrt_flush_buffer(file);
|
|
|
|
|
|
|
|
if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
|
|
|
|
offset -= file->_cnt;
|
|
|
|
}
|
|
|
|
/* Discard buffered input */
|
|
|
|
file->_cnt = 0;
|
|
|
|
file->_ptr = file->_base;
|
|
|
|
/* Reset direction of i/o */
|
|
|
|
if(file->_flag & MSVCRT__IORW) {
|
|
|
|
file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
|
|
|
|
}
|
|
|
|
return (_lseek(file->_file,offset,whence) == -1)?-1:0;
|
|
|
|
}
|
|
|
|
|
2005-03-31 21:06:10 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _chsize (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _chsize(int fd, long size)
|
|
|
|
{
|
|
|
|
LONG cur, pos;
|
|
|
|
HANDLE handle;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(fd=%d, size=%ld)\n", fd, size);
|
|
|
|
|
|
|
|
LOCK_FILES();
|
|
|
|
|
|
|
|
handle = msvcrt_fdtoh(fd);
|
|
|
|
if (handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* save the current file pointer */
|
|
|
|
cur = _lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (cur >= 0)
|
|
|
|
{
|
|
|
|
pos = _lseek(fd, size, SEEK_SET);
|
|
|
|
if (pos >= 0)
|
|
|
|
{
|
|
|
|
ret = SetEndOfFile(handle);
|
|
|
|
if (!ret) msvcrt_set_errno(GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the file pointer */
|
|
|
|
_lseek(fd, cur, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK_FILES();
|
|
|
|
return ret ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* clearerr (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void MSVCRT_clearerr(MSVCRT_FILE* file)
|
|
|
|
{
|
|
|
|
TRACE(":file (%p) fd (%d)\n",file,file->_file);
|
|
|
|
file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF);
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* 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);
|
2002-03-11 06:03:05 +01:00
|
|
|
MSVCRT_fseek(file, 0L, SEEK_SET);
|
|
|
|
MSVCRT_clearerr(file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
static int msvcrt_get_flags(const char* mode, int *open_flags, int* stream_flags)
|
|
|
|
{
|
|
|
|
int plus = strchr(mode, '+') != NULL;
|
|
|
|
|
|
|
|
switch(*mode++)
|
|
|
|
{
|
|
|
|
case 'R': case 'r':
|
2004-06-25 03:19:15 +02:00
|
|
|
*open_flags = plus ? MSVCRT__O_RDWR : MSVCRT__O_RDONLY;
|
2004-05-12 02:12:26 +02:00
|
|
|
*stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOREAD;
|
|
|
|
break;
|
|
|
|
case 'W': case 'w':
|
2004-06-25 03:19:15 +02:00
|
|
|
*open_flags = MSVCRT__O_CREAT | MSVCRT__O_TRUNC | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
|
2004-05-12 02:12:26 +02:00
|
|
|
*stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
|
|
|
|
break;
|
|
|
|
case 'A': case 'a':
|
2004-06-25 03:19:15 +02:00
|
|
|
*open_flags = MSVCRT__O_CREAT | MSVCRT__O_APPEND | (plus ? MSVCRT__O_RDWR : MSVCRT__O_WRONLY);
|
2004-05-12 02:12:26 +02:00
|
|
|
*stream_flags = plus ? MSVCRT__IORW : MSVCRT__IOWRT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*mode)
|
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'B': case 'b':
|
2004-06-25 03:19:15 +02:00
|
|
|
*open_flags |= MSVCRT__O_BINARY;
|
|
|
|
*open_flags &= ~MSVCRT__O_TEXT;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
|
|
|
case 'T': case 't':
|
2004-06-25 03:19:15 +02:00
|
|
|
*open_flags |= MSVCRT__O_TEXT;
|
|
|
|
*open_flags &= ~MSVCRT__O_BINARY;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME(":unknown flag %c not supported\n",mode[-1]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fdopen (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
int open_flags, stream_flags;
|
|
|
|
MSVCRT_FILE* file;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
|
|
|
if (!(file = msvcrt_alloc_fp()))
|
|
|
|
file = NULL;
|
|
|
|
else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
else TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfdopen (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT_FILE* MSVCRT__wfdopen(int fd, const MSVCRT_wchar_t *mode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
unsigned mlen = strlenW(mode);
|
|
|
|
char *modea = MSVCRT_calloc(mlen + 1, 1);
|
|
|
|
MSVCRT_FILE* file = NULL;
|
|
|
|
int open_flags, stream_flags;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
if (modea &&
|
|
|
|
WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL))
|
|
|
|
{
|
|
|
|
if (msvcrt_get_flags(modea, &open_flags, &stream_flags) == -1) return NULL;
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
|
|
|
if (!(file = msvcrt_alloc_fp()))
|
|
|
|
file = NULL;
|
|
|
|
else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (file)
|
|
|
|
MSVCRT_rewind(file); /* FIXME: is this needed ??? */
|
|
|
|
TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file);
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
2001-01-22 03:21:54 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-01-13 06:45:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _filelengthi64 (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
__int64 _filelengthi64(int fd)
|
|
|
|
{
|
|
|
|
__int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
|
|
|
|
if (curPos != -1)
|
|
|
|
{
|
|
|
|
__int64 endPos = _lseeki64(fd, 0, SEEK_END);
|
|
|
|
if (endPos != -1)
|
|
|
|
{
|
|
|
|
if (endPos != curPos)
|
|
|
|
_lseeki64(fd, curPos, SEEK_SET);
|
|
|
|
return endPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fileno (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__fileno(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
|
|
|
|
return file->_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2002-11-25 03:44:34 +01:00
|
|
|
* _fstati64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__fstati64(int fd, struct MSVCRT__stati64* 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");
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_INVALID_PARAMETER);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&hfi, 0, sizeof(hfi));
|
2004-03-16 20:17:11 +01:00
|
|
|
memset(buf, 0, sizeof(struct MSVCRT__stati64));
|
2001-01-11 00:59:25 +01:00
|
|
|
if (!GetFileInformationByHandle(hand, &hfi))
|
|
|
|
{
|
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_INVALID_PARAMETER);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-12-06 17:14:09 +01:00
|
|
|
dw = GetFileType(hand);
|
|
|
|
buf->st_mode = S_IREAD;
|
|
|
|
if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
|
|
|
|
buf->st_mode |= S_IWRITE;
|
|
|
|
/* interestingly, Windows never seems to set S_IFDIR */
|
|
|
|
if (dw == FILE_TYPE_CHAR)
|
|
|
|
buf->st_mode |= S_IFCHR;
|
|
|
|
else if (dw == FILE_TYPE_PIPE)
|
|
|
|
buf->st_mode |= S_IFIFO;
|
|
|
|
else
|
|
|
|
buf->st_mode |= S_IFREG;
|
|
|
|
TRACE(":dwFileAttributes = 0x%lx, mode set to 0x%x\n",hfi.dwFileAttributes,
|
|
|
|
buf->st_mode);
|
2001-01-11 00:59:25 +01:00
|
|
|
buf->st_nlink = hfi.nNumberOfLinks;
|
2002-11-25 03:44:34 +01:00
|
|
|
buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
|
2001-01-11 00:59:25 +01:00
|
|
|
buf->st_atime = dw;
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
|
2001-01-11 00:59:25 +01:00
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:44:34 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fstat (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__fstat(int fd, struct MSVCRT__stat* buf)
|
2002-11-25 03:44:34 +01:00
|
|
|
{ int ret;
|
2004-03-16 20:17:11 +01:00
|
|
|
struct MSVCRT__stati64 bufi64;
|
2002-11-25 03:44:34 +01:00
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
ret = MSVCRT__fstati64(fd, &bufi64);
|
2002-11-25 03:44:34 +01:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_cp_from_stati64(&bufi64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _futime (MSVCRT.@)
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
int _futime(int fd, struct MSVCRT__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);
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
|
2001-01-11 00:59:25 +01:00
|
|
|
memcpy(&wt, &at, sizeof(wt));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (t->actime == t->modtime)
|
|
|
|
memcpy(&wt, &at, sizeof(wt));
|
|
|
|
else
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetFileTime(hand, NULL, &at, &wt))
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
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);
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2006-04-25 04:36:02 +02:00
|
|
|
return (long)hand;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _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
|
|
|
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
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
|
|
|
|
{
|
2006-02-10 15:52:47 +01:00
|
|
|
*pattern = letter++;
|
2003-10-16 21:12:49 +02:00
|
|
|
if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES &&
|
2001-01-22 03:21:54 +01:00
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
2001-01-11 00:59:25 +01:00
|
|
|
return retVal;
|
2006-02-10 15:52:47 +01:00
|
|
|
} while(letter <= 'z');
|
2001-01-11 00:59:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wmktemp (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
int numX = 0;
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t *retVal = pattern;
|
2001-01-22 03:21:54 +01:00
|
|
|
int id;
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t letter = 'a';
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
while(*pattern)
|
2002-12-19 05:21:30 +01:00
|
|
|
numX = (*pattern++ == 'X')? numX + 1 : 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
if (numX < 5)
|
|
|
|
return NULL;
|
|
|
|
pattern--;
|
|
|
|
id = GetCurrentProcessId();
|
|
|
|
numX = 6;
|
|
|
|
while(numX--)
|
|
|
|
{
|
|
|
|
int tempNum = id / 10;
|
2002-12-19 05:21:30 +01:00
|
|
|
*pattern-- = id - (tempNum * 10) + '0';
|
2001-01-22 03:21:54 +01:00
|
|
|
id = tempNum;
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
do
|
|
|
|
{
|
2003-10-16 21:12:49 +02:00
|
|
|
if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES &&
|
2001-01-22 03:21:54 +01:00
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
|
|
|
return retVal;
|
|
|
|
*pattern = letter++;
|
2002-12-19 05:21:30 +01:00
|
|
|
} while(letter != '|');
|
2001-01-22 03:21:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
static unsigned split_oflags(unsigned oflags)
|
|
|
|
{
|
|
|
|
int wxflags = 0;
|
2005-01-10 15:28:21 +01:00
|
|
|
unsigned unsupp; /* until we support everything */
|
2004-09-03 03:05:30 +02:00
|
|
|
|
|
|
|
if (oflags & MSVCRT__O_APPEND) wxflags |= WX_APPEND;
|
2005-09-26 11:54:47 +02:00
|
|
|
if (oflags & MSVCRT__O_BINARY) {/* Nothing to do */}
|
2004-09-03 03:05:30 +02:00
|
|
|
else if (oflags & MSVCRT__O_TEXT) wxflags |= WX_TEXT;
|
2005-09-26 11:54:47 +02:00
|
|
|
else if (*__p__fmode() & MSVCRT__O_BINARY) {/* Nothing to do */}
|
2004-09-03 03:05:30 +02:00
|
|
|
else wxflags |= WX_TEXT; /* default to TEXT*/
|
|
|
|
if (oflags & MSVCRT__O_NOINHERIT) wxflags |= WX_DONTINHERIT;
|
|
|
|
|
2005-01-10 15:28:21 +01:00
|
|
|
if ((unsupp = oflags & ~(
|
|
|
|
MSVCRT__O_BINARY|MSVCRT__O_TEXT|MSVCRT__O_APPEND|
|
|
|
|
MSVCRT__O_TRUNC|MSVCRT__O_EXCL|MSVCRT__O_CREAT|
|
|
|
|
MSVCRT__O_RDWR|MSVCRT__O_WRONLY|MSVCRT__O_TEMPORARY|
|
|
|
|
MSVCRT__O_NOINHERIT|
|
|
|
|
MSVCRT__O_SEQUENTIAL|MSVCRT__O_RANDOM|MSVCRT__O_SHORT_LIVED
|
|
|
|
)))
|
|
|
|
ERR(":unsupported oflags 0x%04x\n",unsupp);
|
2004-09-03 03:05:30 +02:00
|
|
|
|
|
|
|
return wxflags;
|
|
|
|
}
|
|
|
|
|
2004-12-09 17:51:26 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _pipe (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _pipe(int *pfds, unsigned int psize, int textmode)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
HANDLE readHandle, writeHandle;
|
|
|
|
|
|
|
|
if (!pfds)
|
|
|
|
{
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.bInheritHandle = !(textmode & MSVCRT__O_NOINHERIT);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
|
|
|
|
{
|
|
|
|
unsigned int wxflags = split_oflags(textmode);
|
|
|
|
int fd;
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-12-09 17:51:26 +01:00
|
|
|
fd = msvcrt_alloc_fd(readHandle, wxflags);
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
pfds[0] = fd;
|
|
|
|
fd = msvcrt_alloc_fd(writeHandle, wxflags);
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
pfds[1] = fd;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_close(pfds[0]);
|
|
|
|
CloseHandle(writeHandle);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EMFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CloseHandle(readHandle);
|
|
|
|
CloseHandle(writeHandle);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EMFILE;
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
2004-05-12 02:12:26 +02:00
|
|
|
DWORD access = 0, creation = 0, attrib;
|
2002-02-21 21:22:00 +01:00
|
|
|
DWORD sharing;
|
2004-09-03 03:05:30 +02:00
|
|
|
int wxflag = 0, fd;
|
2001-01-11 00:59:25 +01:00
|
|
|
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);
|
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
wxflag = split_oflags(oflags);
|
|
|
|
switch (oflags & (MSVCRT__O_RDONLY | MSVCRT__O_WRONLY | MSVCRT__O_RDWR))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-09-03 03:05:30 +02:00
|
|
|
case MSVCRT__O_RDONLY: access |= GENERIC_READ; break;
|
|
|
|
case MSVCRT__O_WRONLY: access |= GENERIC_WRITE; break;
|
|
|
|
case MSVCRT__O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if (oflags & MSVCRT__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);
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if(pmode & ~(MSVCRT__S_IREAD | MSVCRT__S_IWRITE))
|
2003-02-19 23:09:33 +01:00
|
|
|
FIXME(": pmode 0x%04x ignored\n", pmode);
|
|
|
|
else
|
|
|
|
WARN(": pmode 0x%04x ignored\n", pmode);
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if (oflags & MSVCRT__O_EXCL)
|
2001-01-11 00:59:25 +01:00
|
|
|
creation = CREATE_NEW;
|
2004-06-25 03:19:15 +02:00
|
|
|
else if (oflags & MSVCRT__O_TRUNC)
|
2001-01-11 00:59:25 +01:00
|
|
|
creation = CREATE_ALWAYS;
|
|
|
|
else
|
|
|
|
creation = OPEN_ALWAYS;
|
|
|
|
}
|
2004-06-25 03:19:15 +02:00
|
|
|
else /* no MSVCRT__O_CREAT */
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
if (oflags & MSVCRT__O_TRUNC)
|
2001-01-11 00:59:25 +01:00
|
|
|
creation = TRUNCATE_EXISTING;
|
|
|
|
else
|
|
|
|
creation = OPEN_EXISTING;
|
|
|
|
}
|
2003-02-19 23:09:33 +01:00
|
|
|
|
2002-02-21 21:22:00 +01:00
|
|
|
switch( shflags )
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__SH_DENYRW:
|
2002-02-21 21:22:00 +01:00
|
|
|
sharing = 0L;
|
|
|
|
break;
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__SH_DENYWR:
|
2002-02-21 21:22:00 +01:00
|
|
|
sharing = FILE_SHARE_READ;
|
|
|
|
break;
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__SH_DENYRD:
|
2002-02-21 21:22:00 +01:00
|
|
|
sharing = FILE_SHARE_WRITE;
|
|
|
|
break;
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__SH_DENYNO:
|
2002-02-21 21:22:00 +01:00
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR( "Unhandled shflags 0x%x\n", shflags );
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-12 02:12:26 +02:00
|
|
|
attrib = FILE_ATTRIBUTE_NORMAL;
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if (oflags & MSVCRT__O_TEMPORARY)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
attrib |= FILE_FLAG_DELETE_ON_CLOSE;
|
|
|
|
access |= DELETE;
|
|
|
|
sharing |= FILE_SHARE_DELETE;
|
|
|
|
}
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2001-07-10 21:03:46 +02:00
|
|
|
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
2004-06-25 03:19:15 +02:00
|
|
|
sa.bInheritHandle = (oflags & MSVCRT__O_NOINHERIT) ? FALSE : TRUE;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
hand = CreateFileA(path, access, sharing, &sa, creation, attrib, 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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
fd = msvcrt_alloc_fd(hand, wxflag);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd, hand);
|
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-12-19 05:21:30 +01:00
|
|
|
int MSVCRT__wsopen( const MSVCRT_wchar_t* 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;
|
|
|
|
}
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
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;
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if (flags & MSVCRT__O_CREAT)
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
|
|
|
int pmode;
|
|
|
|
va_start(ap, flags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
2004-06-25 03:19:15 +02:00
|
|
|
return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO, pmode );
|
2003-02-19 23:09:33 +01:00
|
|
|
}
|
|
|
|
else
|
2004-06-25 03:19:15 +02:00
|
|
|
return MSVCRT__sopen( path, flags, MSVCRT__SH_DENYNO);
|
2002-02-21 21:22:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wopen (MSVCRT.@)
|
2001-03-19 20:13:40 +01:00
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wopen(const MSVCRT_wchar_t *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;
|
|
|
|
}
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2002-02-21 21:22:00 +01:00
|
|
|
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
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__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.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wcreat(const MSVCRT_wchar_t *path, int flags)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
int usedFlags = (flags & MSVCRT__O_TEXT)| MSVCRT__O_CREAT| MSVCRT__O_WRONLY| MSVCRT__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.@)
|
|
|
|
*/
|
2004-09-03 03:05:30 +02:00
|
|
|
int _open_osfhandle(long handle, int oflags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-05-08 23:02:31 +02:00
|
|
|
int fd;
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
/* MSVCRT__O_RDONLY (0) always matches, so set the read flag
|
2003-03-30 05:06:30 +02:00
|
|
|
* MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
|
2004-06-25 03:19:15 +02:00
|
|
|
* file, so set the write flag. It also only sets MSVCRT__O_TEXT if it wants
|
|
|
|
* text - it never sets MSVCRT__O_BINARY.
|
2003-03-30 05:06:30 +02:00
|
|
|
*/
|
2002-07-09 03:58:27 +02:00
|
|
|
/* FIXME: handle more flags */
|
2004-09-03 03:05:30 +02:00
|
|
|
if (!(oflags & (MSVCRT__O_BINARY | MSVCRT__O_TEXT)) && (*__p__fmode() & MSVCRT__O_BINARY))
|
|
|
|
oflags |= MSVCRT__O_BINARY;
|
2004-05-12 02:12:26 +02:00
|
|
|
else
|
2004-09-03 03:05:30 +02:00
|
|
|
oflags |= MSVCRT__O_TEXT;
|
2003-05-08 23:02:31 +02:00
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
fd = msvcrt_alloc_fd((HANDLE)handle, split_oflags(oflags));
|
|
|
|
TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
|
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;
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
for (i = 3; i < MSVCRT_stream_idx; i++)
|
|
|
|
if (MSVCRT_fstreams[i] && MSVCRT_fstreams[i]->_tmpfname)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
MSVCRT_fclose(MSVCRT_fstreams[i]);
|
2001-01-11 00:59:25 +01:00
|
|
|
num_removed++;
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (num_removed)
|
|
|
|
TRACE(":removed (%d) temp files\n",num_removed);
|
|
|
|
return num_removed;
|
|
|
|
}
|
|
|
|
|
2003-10-07 05:33:58 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* (internal) remove_cr
|
|
|
|
*
|
|
|
|
* Remove all \r inplace.
|
|
|
|
* return the number of \r removed
|
|
|
|
*/
|
|
|
|
static unsigned int remove_cr(char *buf, unsigned int count)
|
|
|
|
{
|
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) if (buf[i] == '\r') break;
|
|
|
|
for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
|
|
|
|
return count - i;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _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
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
DWORD num_read, all_read = 0;
|
2003-10-07 05:33:58 +02:00
|
|
|
char *bufstart = buf;
|
2001-04-10 23:16:07 +02:00
|
|
|
HANDLE hand = msvcrt_fdtoh(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-01-06 23:08:33 +01:00
|
|
|
/* Don't trace small reads, it gets *very* annoying */
|
2001-01-11 00:59:25 +01:00
|
|
|
if (count > 4)
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
2003-10-07 05:33:58 +02:00
|
|
|
/* Reading single bytes in O_TEXT mode makes things slow
|
|
|
|
* So read big chunks, then remove the \r in memory and try reading
|
|
|
|
* the rest until the request is satisfied or EOF is met
|
|
|
|
*/
|
2004-05-12 02:12:26 +02:00
|
|
|
while (all_read < count)
|
2003-10-07 05:33:58 +02:00
|
|
|
{
|
|
|
|
if (ReadFile(hand, bufstart+all_read, count - all_read, &num_read, NULL))
|
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
if (num_read != (count - all_read))
|
2003-10-07 05:33:58 +02:00
|
|
|
{
|
|
|
|
TRACE(":EOF\n");
|
2004-09-03 03:05:30 +02:00
|
|
|
MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
|
|
|
|
if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
|
2003-10-07 05:33:58 +02:00
|
|
|
num_read -= remove_cr(bufstart+all_read,num_read);
|
|
|
|
all_read += num_read;
|
|
|
|
if (count > 4)
|
|
|
|
TRACE("%s\n",debugstr_an(buf,all_read));
|
|
|
|
return all_read;
|
|
|
|
}
|
2004-09-03 03:05:30 +02:00
|
|
|
if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
|
2003-10-07 05:33:58 +02:00
|
|
|
{
|
|
|
|
num_read -= remove_cr(bufstart+all_read,num_read);
|
|
|
|
}
|
|
|
|
all_read += num_read;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 4)
|
2004-05-12 02:12:26 +02:00
|
|
|
TRACE("(%lu), %s\n",all_read,debugstr_an(buf, all_read));
|
2003-10-07 05:33:58 +02:00
|
|
|
return all_read;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _getw (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__getw(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
int i;
|
2004-05-12 02:12:26 +02:00
|
|
|
switch (_read(file->_file, &i, sizeof(int)))
|
|
|
|
{
|
|
|
|
case 1: return i;
|
|
|
|
case 0: file->_flag |= MSVCRT__IOEOF; break;
|
|
|
|
default: file->_flag |= MSVCRT__IOERR; break;
|
|
|
|
}
|
|
|
|
return EOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _setmode (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _setmode(int fd,int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-09-03 03:05:30 +02:00
|
|
|
int ret = MSVCRT_fdesc[fd].wxflag & WX_TEXT ? MSVCRT__O_TEXT : MSVCRT__O_BINARY;
|
2004-06-25 03:19:15 +02:00
|
|
|
if (mode & (~(MSVCRT__O_TEXT|MSVCRT__O_BINARY)))
|
2003-02-19 23:09:33 +01:00
|
|
|
FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
|
2004-06-25 03:19:15 +02:00
|
|
|
if ((mode & MSVCRT__O_TEXT) == MSVCRT__O_TEXT)
|
2004-12-14 12:59:43 +01:00
|
|
|
MSVCRT_fdesc[fd].wxflag |= WX_TEXT;
|
2004-05-12 02:12:26 +02:00
|
|
|
else
|
2004-12-14 12:59:43 +01:00
|
|
|
MSVCRT_fdesc[fd].wxflag &= ~WX_TEXT;
|
2003-02-19 23:09:33 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2002-11-25 03:44:34 +01:00
|
|
|
* _stati64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__stati64(const char* path, struct MSVCRT__stati64 * buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA hfi;
|
2004-06-25 03:19:15 +02:00
|
|
|
unsigned short mode = ALL_S_IREAD;
|
2001-01-11 00:59:25 +01:00
|
|
|
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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
memset(buf,0,sizeof(struct MSVCRT__stati64));
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-01-06 23:08:33 +01:00
|
|
|
/* FIXME: rdev isn't drive num, despite what the docs say-what is it?
|
2002-06-01 01:06:46 +02:00
|
|
|
Bon 011120: This FIXME seems incorrect
|
2004-01-06 23:08:33 +01:00
|
|
|
Also a letter as first char isn't enough to be classified
|
|
|
|
as a drive letter
|
2002-01-14 19:35:21 +01:00
|
|
|
*/
|
|
|
|
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] == '\\'))
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
|
2001-01-11 00:59:25 +01:00
|
|
|
else
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= MSVCRT__S_IFREG;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* 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)
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= ALL_S_IEXEC;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= ALL_S_IWRITE;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_nlink = 1;
|
2002-11-25 03:44:34 +01:00
|
|
|
buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
|
2001-01-11 00:59:25 +01:00
|
|
|
buf->st_atime = dw;
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
|
2001-01-11 00:59:25 +01:00
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
2002-11-25 03:44:34 +01:00
|
|
|
TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
|
|
|
|
(long)(buf->st_size >> 32),(long)buf->st_size,
|
|
|
|
buf->st_atime,buf->st_mtime, buf->st_ctime);
|
2001-01-22 03:21:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:44:34 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _stat (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__stat(const char* path, struct MSVCRT__stat * buf)
|
2002-11-25 03:44:34 +01:00
|
|
|
{ int ret;
|
2004-03-16 20:17:11 +01:00
|
|
|
struct MSVCRT__stati64 bufi64;
|
2002-11-25 03:44:34 +01:00
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
ret = MSVCRT__stati64( path, &bufi64);
|
2002-11-25 03:44:34 +01:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_cp_from_stati64(&bufi64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2004-01-13 06:45:05 +01:00
|
|
|
* _wstati64 (MSVCRT.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__wstati64(const MSVCRT_wchar_t* path, struct MSVCRT__stati64 * buf)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA hfi;
|
2004-06-25 03:19:15 +02:00
|
|
|
unsigned short mode = ALL_S_IREAD;
|
2001-01-22 03:21:54 +01:00
|
|
|
int plen;
|
|
|
|
|
|
|
|
TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
|
|
|
|
|
|
|
|
if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
|
|
|
|
{
|
|
|
|
TRACE("failed (%ld)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
|
2001-01-22 03:21:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
memset(buf,0,sizeof(struct MSVCRT__stat));
|
2001-01-22 03:21:54 +01:00
|
|
|
|
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))
|
2002-12-19 05:21:30 +01:00
|
|
|
buf->st_dev = buf->st_rdev = toupperW(*path - '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) ||
|
2002-12-19 05:21:30 +01:00
|
|
|
(path[plen-1] == '\\'))
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC);
|
2001-01-22 03:21:54 +01:00
|
|
|
else
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= MSVCRT__S_IFREG;
|
2001-01-22 03:21:54 +01:00
|
|
|
/* executable? */
|
2002-12-19 05:21:30 +01:00
|
|
|
if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
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)
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= ALL_S_IEXEC;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
|
2004-06-25 03:19:15 +02:00
|
|
|
mode |= ALL_S_IWRITE;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_nlink = 1;
|
2004-01-13 06:45:05 +01:00
|
|
|
buf->st_size = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
|
2001-01-22 03:21:54 +01:00
|
|
|
buf->st_atime = dw;
|
2002-09-13 00:28:01 +02:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
|
2001-01-22 03:21:54 +01:00
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
2004-01-13 06:45:05 +01:00
|
|
|
TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
|
|
|
|
(long)(buf->st_size >> 32),(long)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;
|
|
|
|
}
|
|
|
|
|
2004-01-13 06:45:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wstat (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__wstat(const MSVCRT_wchar_t* path, struct MSVCRT__stat * buf)
|
2004-01-13 06:45:05 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2004-03-16 20:17:11 +01:00
|
|
|
struct MSVCRT__stati64 bufi64;
|
2004-01-13 06:45:05 +01:00
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
ret = MSVCRT__wstati64( path, &bufi64 );
|
2004-01-13 06:45:05 +01:00
|
|
|
if (!ret) msvcrt_cp_from_stati64(&bufi64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _tell (MSVCRT.@)
|
|
|
|
*/
|
2004-01-13 06:45:05 +01: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
|
|
|
}
|
|
|
|
|
2004-01-13 06:45:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _telli64 (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
__int64 _telli64(int fd)
|
|
|
|
{
|
|
|
|
return _lseeki64(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];
|
2006-01-18 14:22:27 +01:00
|
|
|
const char *tmp_dir = MSVCRT_getenv("TMP");
|
|
|
|
|
|
|
|
if (tmp_dir) dir = tmp_dir;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE("dir (%s) prefix (%s)\n",dir,prefix);
|
|
|
|
if (GetTempFileNameA(dir,prefix,0,tmpbuf))
|
|
|
|
{
|
|
|
|
TRACE("got name (%s)\n",tmpbuf);
|
2003-09-27 04:45:39 +02:00
|
|
|
DeleteFileA(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.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t tmpbuf[MAX_PATH];
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
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));
|
2003-09-27 04:45:39 +02:00
|
|
|
DeleteFileW(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.@)
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
int _utime(const char* path, struct MSVCRT__utimbuf *t)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
int fd = _open(path, MSVCRT__O_WRONLY | MSVCRT__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.@)
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
int _wutime(const MSVCRT_wchar_t* path, struct MSVCRT__utimbuf *t)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
int fd = _wopen(path, MSVCRT__O_WRONLY | MSVCRT__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
|
|
|
|
2004-01-06 23:08:33 +01:00
|
|
|
/* Don't trace small writes, it gets *very* annoying */
|
2002-04-29 20:48:56 +02:00
|
|
|
#if 0
|
|
|
|
if (count > 32)
|
|
|
|
TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
|
|
|
|
#endif
|
2001-01-11 00:59:25 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE)
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
|
|
|
*MSVCRT__errno() = MSVCRT_EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* If appending, go to EOF */
|
2004-09-03 03:05:30 +02:00
|
|
|
if (MSVCRT_fdesc[fd].wxflag & WX_APPEND)
|
2001-04-10 23:16:07 +02:00
|
|
|
_lseek(fd, 0, FILE_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
if (!(MSVCRT_fdesc[fd].wxflag & WX_TEXT))
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
|
|
|
if (WriteFile(hand, buf, count, &num_written, NULL)
|
|
|
|
&& (num_written == count))
|
|
|
|
return num_written;
|
2004-12-14 12:59:43 +01:00
|
|
|
TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld)\n", fd,
|
|
|
|
hand, GetLastError());
|
2004-05-12 02:12:26 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_ENOSPC;
|
2003-02-19 23:09:33 +01:00
|
|
|
}
|
|
|
|
else
|
2003-10-07 06:01:28 +02:00
|
|
|
{
|
|
|
|
unsigned int i, j, nr_lf;
|
2004-09-03 03:05:30 +02:00
|
|
|
char *s =(char*)buf, *buf_start=(char*)buf, *p;
|
2005-03-02 14:53:50 +01:00
|
|
|
/* find number of \n ( without preceding \r ) */
|
2003-10-07 06:01:28 +02:00
|
|
|
for ( nr_lf=0,i = 0; i <count; i++)
|
|
|
|
{
|
|
|
|
if (s[i]== '\n')
|
|
|
|
{
|
|
|
|
nr_lf++;
|
|
|
|
/*if ((i >1) && (s[i-1] == '\r')) nr_lf--; */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nr_lf)
|
|
|
|
{
|
|
|
|
if ((p = MSVCRT_malloc(count + nr_lf)))
|
|
|
|
{
|
|
|
|
for(s=(char*)buf, i=0, j=0; i<count; i++)
|
|
|
|
{
|
|
|
|
if (s[i]== '\n')
|
|
|
|
{
|
|
|
|
p[j++] = '\r';
|
|
|
|
/*if ((i >1) && (s[i-1] == '\r'))j--;*/
|
|
|
|
}
|
|
|
|
p[j++] = s[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("Malloc failed\n");
|
|
|
|
nr_lf =0;
|
|
|
|
p = (char*)buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p = (char*)buf;
|
|
|
|
|
|
|
|
if ((WriteFile(hand, p, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf))
|
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
TRACE("WriteFile (fd %d, hand %p) failed-last error (%ld), num_written %ld\n",
|
|
|
|
fd, hand, GetLastError(), num_written);
|
2004-05-12 02:12:26 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_ENOSPC;
|
|
|
|
if(nr_lf)
|
|
|
|
MSVCRT_free(p);
|
|
|
|
return s - buf_start;
|
2003-10-07 06:01:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(nr_lf)
|
|
|
|
MSVCRT_free(p);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putw (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__putw(int val, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
int len;
|
|
|
|
len = _write(file->_file, &val, sizeof(val));
|
|
|
|
if (len == sizeof(val)) return val;
|
|
|
|
file->_flag |= MSVCRT__IOERR;
|
|
|
|
return MSVCRT_EOF;
|
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
|
|
|
{
|
2003-03-30 05:06:30 +02:00
|
|
|
int r, flag;
|
|
|
|
|
|
|
|
flag = file->_flag;
|
2004-05-12 02:12:26 +02:00
|
|
|
if (file->_tmpfname)
|
|
|
|
{
|
|
|
|
MSVCRT_free(file->_tmpfname);
|
|
|
|
file->_tmpfname = NULL;
|
|
|
|
}
|
|
|
|
/* flush stdio buffers */
|
|
|
|
if(file->_flag & MSVCRT__IOWRT)
|
|
|
|
MSVCRT_fflush(file);
|
|
|
|
if(file->_flag & MSVCRT__IOMYBUF)
|
|
|
|
MSVCRT_free(file->_base);
|
|
|
|
|
2001-05-31 23:33:32 +02:00
|
|
|
r=_close(file->_file);
|
2004-05-12 02:12:26 +02:00
|
|
|
|
|
|
|
file->_flag = 0;
|
|
|
|
|
2005-11-16 12:47:51 +01:00
|
|
|
return ((r == -1) || (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
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _filbuf (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__filbuf(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-03-11 06:03:05 +01:00
|
|
|
/* Allocate buffer if needed */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) {
|
2002-03-11 06:03:05 +01:00
|
|
|
msvcrt_alloc_buffer(file);
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if(!(file->_flag & MSVCRT__IOREAD)) {
|
|
|
|
if(file->_flag & MSVCRT__IORW) {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_flag |= MSVCRT__IOREAD;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2002-03-11 06:03:05 +01:00
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_flag & MSVCRT__IONBF) {
|
2002-03-11 06:03:05 +01:00
|
|
|
unsigned char c;
|
2004-05-12 02:12:26 +02:00
|
|
|
int r;
|
|
|
|
if ((r = _read(file->_file,&c,1)) != 1) {
|
|
|
|
file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
|
|
|
|
return MSVCRT_EOF;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
return c;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
|
2004-05-12 02:12:26 +02:00
|
|
|
if(file->_cnt<=0) {
|
|
|
|
file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
|
|
|
|
file->_cnt = 0;
|
|
|
|
return MSVCRT_EOF;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
file->_cnt--;
|
|
|
|
file->_ptr = file->_base+1;
|
|
|
|
return *(unsigned char *)file->_base;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2004-03-16 20:17:11 +01:00
|
|
|
* fgetc (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT_fgetc(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-16 20:17:11 +01:00
|
|
|
if (file->_cnt>0) {
|
|
|
|
file->_cnt--;
|
|
|
|
return *(unsigned char *)file->_ptr++;
|
|
|
|
} else {
|
|
|
|
return MSVCRT__filbuf(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetchar (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _fgetchar(void)
|
|
|
|
{
|
|
|
|
return MSVCRT_fgetc(MSVCRT_stdin);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* 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
|
|
|
{
|
2003-10-07 07:22:04 +02:00
|
|
|
int cc = MSVCRT_EOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
char * buf_start = s;
|
|
|
|
|
|
|
|
TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
|
|
|
|
file,file->_file,s,size);
|
|
|
|
|
2003-10-07 07:22:04 +02:00
|
|
|
while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
*s++ = (char)cc;
|
2003-10-07 07:22:04 +02:00
|
|
|
size --;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
|
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
2003-10-07 07:22:04 +02:00
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2003-10-07 07:22:04 +02:00
|
|
|
if ((cc != MSVCRT_EOF) && (size > 1))
|
|
|
|
*s++ = cc;
|
2001-01-11 00:59:25 +01:00
|
|
|
*s = '\0';
|
2005-06-25 19:58:19 +02:00
|
|
|
TRACE(":got %s\n", debugstr_a(buf_start));
|
2001-01-11 00:59:25 +01:00
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgetwc (MSVCRT.@)
|
2003-02-19 23:09:33 +01:00
|
|
|
*
|
2004-06-25 03:19:15 +02:00
|
|
|
* In MSVCRT__O_TEXT mode, multibyte characters are read from the file, dropping
|
2003-02-19 23:09:33 +01:00
|
|
|
* the CR from CR/LF combinations
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-02-19 23:09:33 +01:00
|
|
|
char c;
|
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
|
|
|
MSVCRT_wchar_t wc;
|
2004-05-12 02:12:26 +02:00
|
|
|
int r;
|
|
|
|
if ((r = _read(file->_file, &wc, sizeof(wc))) != sizeof(wc))
|
|
|
|
{
|
|
|
|
file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
|
|
|
|
return MSVCRT_WEOF;
|
|
|
|
}
|
2003-02-19 23:09:33 +01:00
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
c = MSVCRT_fgetc(file);
|
|
|
|
if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
|
|
|
|
{
|
|
|
|
FIXME("Treat Multibyte characters\n");
|
|
|
|
}
|
|
|
|
if (c == MSVCRT_EOF)
|
2001-01-11 00:59:25 +01:00
|
|
|
return MSVCRT_WEOF;
|
2003-02-19 23:09:33 +01:00
|
|
|
else
|
|
|
|
return (MSVCRT_wint_t)c;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fgetws (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2003-10-07 07:22:04 +02:00
|
|
|
int cc = MSVCRT_WEOF;
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t * buf_start = s;
|
2002-03-11 06:03:05 +01:00
|
|
|
|
|
|
|
TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
|
|
|
|
file,file->_file,s,size);
|
|
|
|
|
2003-10-07 07:22:04 +02:00
|
|
|
while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2003-10-07 07:22:04 +02:00
|
|
|
*s++ = (char)cc;
|
|
|
|
size --;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
2003-10-07 07:22:04 +02:00
|
|
|
if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
2003-10-07 07:22:04 +02:00
|
|
|
return NULL;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
2003-10-07 07:22:04 +02:00
|
|
|
if ((cc != MSVCRT_WEOF) && (size > 1))
|
|
|
|
*s++ = cc;
|
|
|
|
*s = 0;
|
|
|
|
TRACE(":got %s\n", debugstr_w(buf_start));
|
2002-03-11 06:03:05 +01:00
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fwrite (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file)
|
|
|
|
{
|
|
|
|
MSVCRT_size_t wrcnt=size * nmemb;
|
|
|
|
int written = 0;
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
if(file->_cnt) {
|
|
|
|
int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
|
|
|
|
memcpy(file->_ptr, ptr, pcnt);
|
|
|
|
file->_cnt -= pcnt;
|
|
|
|
file->_ptr += pcnt;
|
|
|
|
written = pcnt;
|
|
|
|
wrcnt -= pcnt;
|
2004-09-03 03:05:30 +02:00
|
|
|
ptr = (const char*)ptr + pcnt;
|
2004-03-16 20:17:11 +01:00
|
|
|
} else if(!(file->_flag & MSVCRT__IOWRT)) {
|
|
|
|
if(file->_flag & MSVCRT__IORW) {
|
|
|
|
file->_flag |= MSVCRT__IOWRT;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(wrcnt) {
|
|
|
|
/* Flush buffer */
|
|
|
|
int res=msvcrt_flush_buffer(file);
|
|
|
|
if(!res) {
|
|
|
|
int pwritten = _write(file->_file, ptr, wrcnt);
|
2004-05-12 02:12:26 +02:00
|
|
|
if (pwritten <= 0)
|
|
|
|
{
|
|
|
|
file->_flag |= MSVCRT__IOERR;
|
|
|
|
pwritten=0;
|
|
|
|
}
|
2004-03-16 20:17:11 +01:00
|
|
|
written += pwritten;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written / size;
|
|
|
|
}
|
2002-03-11 06:03:05 +01:00
|
|
|
|
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
|
|
|
{
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t mwc=wc;
|
2002-03-21 02:23:32 +01:00
|
|
|
if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1)
|
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;
|
2004-05-12 02:12:26 +02:00
|
|
|
int open_flags, stream_flags, fd;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE("(%s,%s)\n",path,mode);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* map mode string to open() flags. "man fopen" for possibilities. */
|
2004-05-12 02:12:26 +02:00
|
|
|
if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
|
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-06-25 03:19:15 +02:00
|
|
|
fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (fd < 0)
|
2004-12-14 12:59:43 +01:00
|
|
|
file = NULL;
|
|
|
|
else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
|
|
|
|
!= -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
TRACE(":fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file);
|
|
|
|
else if (file)
|
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE(":got (%p)\n",file);
|
2005-05-05 18:48:28 +02:00
|
|
|
if (fd >= 0 && !file)
|
2001-04-10 23:16:07 +02:00
|
|
|
_close(fd);
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfopen (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT_FILE *MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *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;
|
|
|
|
}
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fsopen (MSVCRT.@)
|
|
|
|
*/
|
2004-10-14 02:26:39 +02:00
|
|
|
MSVCRT_FILE* MSVCRT__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.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT_FILE* MSVCRT__wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *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);
|
2004-03-16 20:17:11 +01:00
|
|
|
return MSVCRT__wfopen(path,mode);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/* MSVCRT_fputc calls MSVCRT__flsbuf which calls MSVCRT_fputc */
|
|
|
|
int MSVCRT__flsbuf(int c, MSVCRT_FILE* file);
|
|
|
|
|
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
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_cnt>0) {
|
2004-12-07 18:05:08 +01:00
|
|
|
*file->_ptr++=c;
|
|
|
|
file->_cnt--;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
int res = msvcrt_flush_buffer(file);
|
|
|
|
return res ? res : c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return c;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2004-12-07 18:05:08 +01:00
|
|
|
return MSVCRT__flsbuf(c, file);
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _flsbuf (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-03-11 06:03:05 +01:00
|
|
|
/* Flush output buffer */
|
|
|
|
if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
|
|
|
|
msvcrt_alloc_buffer(file);
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if(!(file->_flag & MSVCRT__IOWRT)) {
|
|
|
|
if(file->_flag & MSVCRT__IORW) {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_flag |= MSVCRT__IOWRT;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2002-03-11 06:03:05 +01:00
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(file->_bufsiz) {
|
|
|
|
int res=msvcrt_flush_buffer(file);
|
|
|
|
return res?res : MSVCRT_fputc(c, file);
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2002-03-11 06:03:05 +01:00
|
|
|
unsigned char cc=c;
|
2004-05-12 02:12:26 +02:00
|
|
|
int len;
|
|
|
|
len = _write(file->_file, &cc, 1);
|
|
|
|
if (len == 1) return c;
|
|
|
|
file->_flag |= MSVCRT__IOERR;
|
|
|
|
return MSVCRT_EOF;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _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)
|
2002-03-11 06:03:05 +01:00
|
|
|
{ MSVCRT_size_t rcnt=size * nmemb;
|
|
|
|
MSVCRT_size_t read=0;
|
|
|
|
int pread=0;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2005-07-10 19:46:08 +02:00
|
|
|
if(!rcnt)
|
|
|
|
return 0;
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/* first buffered data */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_cnt>0) {
|
2002-03-11 06:03:05 +01:00
|
|
|
int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
|
|
|
|
memcpy(ptr, file->_ptr, pcnt);
|
|
|
|
file->_cnt -= pcnt;
|
|
|
|
file->_ptr += pcnt;
|
|
|
|
read += pcnt ;
|
|
|
|
rcnt -= pcnt ;
|
2004-05-12 02:12:26 +02:00
|
|
|
ptr = (char*)ptr + pcnt;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else if(!(file->_flag & MSVCRT__IOREAD )) {
|
|
|
|
if(file->_flag & MSVCRT__IORW) {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_flag |= MSVCRT__IOREAD;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else
|
2004-05-12 02:12:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(rcnt)
|
|
|
|
{
|
|
|
|
pread = _read(file->_file,ptr, rcnt);
|
|
|
|
/* expose feof condition in the flags
|
|
|
|
* MFC tests file->_flag for feof, and doesn't not call feof())
|
|
|
|
*/
|
2006-02-17 17:38:03 +01:00
|
|
|
if ( MSVCRT_fdesc[file->_file].wxflag & WX_ATEOF)
|
2004-05-12 02:12:26 +02:00
|
|
|
file->_flag |= MSVCRT__IOEOF;
|
|
|
|
else if (pread == -1)
|
|
|
|
{
|
|
|
|
file->_flag |= MSVCRT__IOERR;
|
|
|
|
pread = 0;
|
|
|
|
}
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
read+=pread;
|
2001-01-11 00:59:25 +01:00
|
|
|
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
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
int open_flags, stream_flags, fd;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file);
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
|
|
|
if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend)
|
|
|
|
file = NULL;
|
|
|
|
else
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
MSVCRT_fclose(file);
|
|
|
|
/* map mode string to open() flags. "man fopen" for possibilities. */
|
|
|
|
if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
|
|
|
|
file = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fd = _open(path, open_flags, MSVCRT__S_IREAD | MSVCRT__S_IWRITE);
|
|
|
|
if (fd < 0)
|
|
|
|
file = NULL;
|
|
|
|
else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
|
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
WARN(":failed-last error (%ld)\n",GetLastError());
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
return file;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* 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
|
|
|
{
|
2005-01-04 21:37:54 +01:00
|
|
|
/* Note that all this has been lifted 'as is' from fseek */
|
|
|
|
if(file->_flag & MSVCRT__IOWRT)
|
|
|
|
msvcrt_flush_buffer(file);
|
|
|
|
|
|
|
|
/* Discard buffered input */
|
|
|
|
file->_cnt = 0;
|
|
|
|
file->_ptr = file->_base;
|
|
|
|
|
|
|
|
/* Reset direction of i/o */
|
|
|
|
if(file->_flag & MSVCRT__IORW) {
|
|
|
|
file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
|
|
|
|
}
|
|
|
|
|
2005-05-16 10:50:28 +02:00
|
|
|
return (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
|
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
|
|
|
{
|
2002-03-11 06:03:05 +01:00
|
|
|
int off=0;
|
|
|
|
long pos;
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_bufsiz) {
|
2002-03-11 06:03:05 +01:00
|
|
|
if( file->_flag & MSVCRT__IOWRT ) {
|
|
|
|
off = file->_ptr - file->_base;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
2002-03-11 06:03:05 +01:00
|
|
|
off = -file->_cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = _tell(file->_file);
|
|
|
|
if(pos == -1) return pos;
|
|
|
|
return off + pos;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2004-03-16 20:17:11 +01:00
|
|
|
* fgetpos (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2005-01-04 21:37:54 +01:00
|
|
|
/* This code has been lifted form the MSVCRT_ftell function */
|
|
|
|
int off=0;
|
|
|
|
|
|
|
|
*pos = _lseeki64(file->_file,0,SEEK_CUR);
|
|
|
|
|
|
|
|
if (*pos == -1) return -1;
|
|
|
|
|
|
|
|
if(file->_bufsiz) {
|
|
|
|
if( file->_flag & MSVCRT__IOWRT ) {
|
|
|
|
off = file->_ptr - file->_base;
|
|
|
|
} else {
|
|
|
|
off = -file->_cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pos += off;
|
|
|
|
|
|
|
|
return 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* 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
|
|
|
{
|
2003-02-19 23:09:33 +01:00
|
|
|
size_t i, len = strlen(s);
|
2004-09-03 03:05:30 +02:00
|
|
|
if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
|
2003-02-19 23:09:33 +01:00
|
|
|
return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
|
|
|
|
for (i=0; i<len; i++)
|
|
|
|
if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
return 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fputws (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2003-02-19 23:09:33 +01:00
|
|
|
size_t i, len = strlenW(s);
|
2004-09-03 03:05:30 +02:00
|
|
|
if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT))
|
2003-02-19 23:09:33 +01:00
|
|
|
return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
|
|
|
|
for (i=0; i<len; i++)
|
|
|
|
{
|
|
|
|
if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
|
|
|
|
return MSVCRT_WEOF;
|
|
|
|
if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
|
|
|
|
return MSVCRT_WEOF;
|
|
|
|
}
|
|
|
|
return 0;
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-05-20 00:21:17 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _getws (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf)
|
2002-05-20 00:21:17 +02:00
|
|
|
{
|
|
|
|
MSVCRT_wint_t cc;
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t* ws = buf;
|
2002-05-20 00:21:17 +02:00
|
|
|
|
|
|
|
for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n';
|
|
|
|
cc = MSVCRT_fgetwc(MSVCRT_stdin))
|
|
|
|
{
|
|
|
|
if (cc != '\r')
|
2002-12-19 05:21:30 +01:00
|
|
|
*buf++ = (MSVCRT_wchar_t)cc;
|
2002-05-20 00:21:17 +02:00
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
|
|
|
|
TRACE("got '%s'\n", debugstr_w(ws));
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* 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
|
|
|
{
|
2002-03-21 02:23:32 +01:00
|
|
|
size_t len = strlen(s);
|
|
|
|
if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _putws (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _putws(const MSVCRT_wchar_t *s)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2002-12-19 05:21:30 +01:00
|
|
|
static const MSVCRT_wchar_t nl = '\n';
|
2002-03-21 02:23:32 +01:00
|
|
|
size_t len = strlenW(s);
|
|
|
|
if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF;
|
2001-01-22 03:21:54 +01:00
|
|
|
return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : 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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wremove (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wremove(const MSVCRT_wchar_t *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());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* 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);
|
2002-09-17 00:39:50 +02:00
|
|
|
if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
|
2001-01-11 00:59:25 +01:00
|
|
|
return 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
TRACE(":failed (%ld)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wrename (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
|
2002-09-17 00:39:50 +02:00
|
|
|
if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
|
2001-01-22 03:21:54 +01:00
|
|
|
return 0;
|
|
|
|
TRACE(":failed (%ld)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
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
|
|
|
{
|
2002-03-11 06:03:05 +01:00
|
|
|
/* TODO: Check if file busy */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_bufsiz) {
|
2002-03-11 06:03:05 +01:00
|
|
|
MSVCRT_free(file->_base);
|
|
|
|
file->_bufsiz = 0;
|
|
|
|
file->_cnt = 0;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if(mode == MSVCRT__IOFBF) {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_flag &= ~MSVCRT__IONBF;
|
|
|
|
file->_base = file->_ptr = buf;
|
2002-06-01 01:06:46 +02:00
|
|
|
if(buf) {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_bufsiz = size;
|
2002-06-01 01:06:46 +02:00
|
|
|
}
|
2002-03-11 06:03:05 +01:00
|
|
|
} else {
|
|
|
|
file->_flag |= MSVCRT__IONBF;
|
|
|
|
}
|
|
|
|
return 0;
|
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
|
|
|
{
|
2002-03-11 06:03:05 +01:00
|
|
|
MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_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
|
|
|
{
|
2003-10-24 02:23:51 +02:00
|
|
|
static int unique;
|
|
|
|
char tmpstr[16];
|
|
|
|
char *p;
|
|
|
|
int count;
|
|
|
|
if (s == 0)
|
|
|
|
s = MSVCRT_tmpname;
|
|
|
|
msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
|
|
|
|
p = s + sprintf(s, "\\s%s.", tmpstr);
|
|
|
|
for (count = 0; count < MSVCRT_TMP_MAX; count++)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-10-24 02:23:51 +02:00
|
|
|
msvcrt_int_to_base32(unique++, tmpstr);
|
|
|
|
strcpy(p, tmpstr);
|
|
|
|
if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
|
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
|
|
|
break;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
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;
|
2004-05-12 02:12:26 +02:00
|
|
|
MSVCRT_FILE* file = NULL;
|
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2004-06-25 03:19:15 +02:00
|
|
|
fd = _open(filename, MSVCRT__O_CREAT | MSVCRT__O_BINARY | MSVCRT__O_RDWR | MSVCRT__O_TEMPORARY);
|
2004-05-12 02:12:26 +02:00
|
|
|
if (fd != -1 && (file = msvcrt_alloc_fp()))
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
if (msvcrt_init_fp(file, fd, MSVCRT__O_RDWR) == -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
else file->_tmpfname = _strdup(filename);
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2004-05-12 02:12:26 +02:00
|
|
|
return file;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* 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
|
|
|
|
*/
|
2005-03-07 12:00:46 +01:00
|
|
|
while ((written = MSVCRT_vsnprintf(mem, resize, format, valist)) == -1 ||
|
2001-01-11 00:59:25 +01:00
|
|
|
written > resize)
|
|
|
|
{
|
|
|
|
resize = (written == -1 ? resize * 2 : written + 1);
|
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
|
|
|
if (!(mem = (char *)MSVCRT_malloc(resize)))
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
2002-03-21 02:23:32 +01:00
|
|
|
retval = MSVCRT_fwrite(mem, sizeof(*mem), 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.@)
|
2002-06-01 01:06:46 +02:00
|
|
|
* FIXME:
|
2002-03-11 06:03:05 +01:00
|
|
|
* Is final char included in written (then resize is too big) or not
|
|
|
|
* (then we must test for equality too)?
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t buf[2048], *mem = buf;
|
|
|
|
int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval;
|
2001-01-22 03:21:54 +01:00
|
|
|
/* See vfprintf comments */
|
2006-01-24 12:21:58 +01:00
|
|
|
while ((written = MSVCRT_vsnwprintf(mem, resize, format, valist)) == -1 ||
|
2001-01-22 03:21:54 +01:00
|
|
|
written > resize)
|
|
|
|
{
|
2002-12-19 05:21:30 +01:00
|
|
|
resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t));
|
2001-01-22 03:21:54 +01:00
|
|
|
if (mem != buf)
|
|
|
|
MSVCRT_free (mem);
|
2002-12-19 05:21:30 +01:00
|
|
|
if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem))))
|
2001-01-22 03:21:54 +01:00
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
2002-03-21 02:23:32 +01:00
|
|
|
retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file);
|
2001-01-22 03:21:54 +01:00
|
|
|
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.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int MSVCRT_vwprintf(const MSVCRT_wchar_t *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.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *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;
|
|
|
|
}
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* ungetc (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int MSVCRT_ungetc(int c, MSVCRT_FILE * file)
|
|
|
|
{
|
2005-06-25 19:58:19 +02:00
|
|
|
if (c == MSVCRT_EOF)
|
|
|
|
return MSVCRT_EOF;
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) {
|
2002-03-11 06:03:05 +01:00
|
|
|
msvcrt_alloc_buffer(file);
|
|
|
|
file->_ptr++;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
if(file->_ptr>file->_base) {
|
2002-03-11 06:03:05 +01:00
|
|
|
file->_ptr--;
|
|
|
|
*file->_ptr=c;
|
|
|
|
file->_cnt++;
|
2005-06-25 19:58:19 +02:00
|
|
|
MSVCRT_clearerr(file);
|
2002-03-11 06:03:05 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return MSVCRT_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ungetwc (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file)
|
|
|
|
{
|
2002-12-19 05:21:30 +01:00
|
|
|
MSVCRT_wchar_t mwc = wc;
|
2002-03-11 06:03:05 +01:00
|
|
|
char * pp = (char *)&mwc;
|
|
|
|
int i;
|
2002-12-19 05:21:30 +01:00
|
|
|
for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) {
|
2002-03-11 06:03:05 +01:00
|
|
|
if(pp[i] != MSVCRT_ungetc(pp[i],file))
|
|
|
|
return MSVCRT_WEOF;
|
|
|
|
}
|
|
|
|
return mwc;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* wprintf (MSVCRT.@)
|
|
|
|
*/
|
2002-12-19 05:21:30 +01:00
|
|
|
int MSVCRT_wprintf(const MSVCRT_wchar_t *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;
|
|
|
|
}
|
2004-12-10 16:28:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __pioinfo (MSVCRT.@)
|
|
|
|
* FIXME: see MSVCRT_MAX_FILES define.
|
|
|
|
*/
|
|
|
|
ioinfo * MSVCRT___pioinfo[] = { /* array of pointers to ioinfo arrays [64] */
|
|
|
|
&MSVCRT_fdesc[0 * 64], &MSVCRT_fdesc[1 * 64], &MSVCRT_fdesc[2 * 64],
|
|
|
|
&MSVCRT_fdesc[3 * 64], &MSVCRT_fdesc[4 * 64], &MSVCRT_fdesc[5 * 64],
|
|
|
|
&MSVCRT_fdesc[6 * 64], &MSVCRT_fdesc[7 * 64], &MSVCRT_fdesc[8 * 64],
|
|
|
|
&MSVCRT_fdesc[9 * 64], &MSVCRT_fdesc[10 * 64], &MSVCRT_fdesc[11 * 64],
|
|
|
|
&MSVCRT_fdesc[12 * 64], &MSVCRT_fdesc[13 * 64], &MSVCRT_fdesc[14 * 64],
|
|
|
|
&MSVCRT_fdesc[15 * 64], &MSVCRT_fdesc[16 * 64], &MSVCRT_fdesc[17 * 64],
|
|
|
|
&MSVCRT_fdesc[18 * 64], &MSVCRT_fdesc[19 * 64], &MSVCRT_fdesc[20 * 64],
|
|
|
|
&MSVCRT_fdesc[21 * 64], &MSVCRT_fdesc[22 * 64], &MSVCRT_fdesc[23 * 64],
|
|
|
|
&MSVCRT_fdesc[24 * 64], &MSVCRT_fdesc[25 * 64], &MSVCRT_fdesc[26 * 64],
|
|
|
|
&MSVCRT_fdesc[27 * 64], &MSVCRT_fdesc[28 * 64], &MSVCRT_fdesc[29 * 64],
|
|
|
|
&MSVCRT_fdesc[30 * 64], &MSVCRT_fdesc[31 * 64]
|
|
|
|
} ;
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __badioinfo (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
|