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
|
|
|
*
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2002-04-26 21:05:15 +02:00
|
|
|
|
2020-12-03 19:11:02 +01:00
|
|
|
#include <direct.h>
|
2020-12-04 15:50:17 +01:00
|
|
|
#include <fcntl.h>
|
2020-12-03 19:11:20 +01:00
|
|
|
#include <io.h>
|
2020-12-04 15:50:17 +01:00
|
|
|
#include <share.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2020-12-04 15:50:17 +01:00
|
|
|
#include <sys/locking.h>
|
2004-06-25 03:19:15 +02:00
|
|
|
#include <sys/types.h>
|
2020-12-01 13:51:47 +01:00
|
|
|
#include <sys/utime.h>
|
2020-12-03 19:10:50 +01:00
|
|
|
#include <time.h>
|
2013-03-14 13:13:44 +01:00
|
|
|
#include <limits.h>
|
2001-04-11 01:25:25 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2020-08-05 15:08:06 +02:00
|
|
|
#include "wincon.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2020-07-21 12:21:59 +02:00
|
|
|
#include "winnls.h"
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
2011-05-19 15:31:22 +02:00
|
|
|
#include "mtdll.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
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
#undef _fstat
|
|
|
|
#undef _fstati64
|
|
|
|
#undef _stat
|
|
|
|
#undef _stati64
|
|
|
|
#undef _wstat
|
|
|
|
#undef _wstati64
|
|
|
|
#undef _fstat32
|
|
|
|
#undef _fstat32i64
|
|
|
|
#undef _stat32i64
|
|
|
|
#undef _stat32
|
|
|
|
#undef _wstat32
|
|
|
|
#undef _wstat32i64
|
|
|
|
#undef _fstat64i32
|
|
|
|
#undef _fstat64
|
|
|
|
#undef _stat64
|
|
|
|
#undef _stat64i32
|
|
|
|
#undef _wstat64i32
|
|
|
|
#undef _wstat64
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* for stat mode, permissions apply to all,owner and group */
|
2020-12-04 15:50:17 +01:00
|
|
|
#define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6))
|
|
|
|
#define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
|
|
|
|
#define ALL_S_IEXEC (_S_IEXEC | (_S_IEXEC >> 3) | (_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
|
2010-11-02 22:03:41 +01:00
|
|
|
#define MSVCRT_R_OK 0x04
|
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
|
2013-01-10 11:44:18 +01:00
|
|
|
#define WX_READNL 0x04 /* read started with \n */
|
2013-01-10 11:43:37 +01:00
|
|
|
#define WX_PIPE 0x08
|
2004-05-12 02:12:26 +02:00
|
|
|
#define WX_DONTINHERIT 0x10
|
|
|
|
#define WX_APPEND 0x20
|
2014-07-31 16:51:52 +02:00
|
|
|
#define WX_TTY 0x40
|
2004-05-12 02:12:26 +02:00
|
|
|
#define WX_TEXT 0x80
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2013-01-10 11:42:29 +01:00
|
|
|
/* values for exflag - it's used differently in msvcr90.dll*/
|
|
|
|
#define EF_UTF8 0x01
|
|
|
|
#define EF_UTF16 0x02
|
|
|
|
#define EF_CRIT_INIT 0x04
|
|
|
|
#define EF_UNK_UNICODE 0x08
|
|
|
|
|
|
|
|
static char utf8_bom[3] = { 0xef, 0xbb, 0xbf };
|
|
|
|
static char utf16_bom[2] = { 0xff, 0xfe };
|
|
|
|
|
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
|
2011-10-06 20:13:16 +02:00
|
|
|
#define MSVCRT_FD_BLOCK_SIZE 32
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-06-14 01:23:56 +02:00
|
|
|
#define MSVCRT_INTERNAL_BUFSIZ 4096
|
|
|
|
|
2011-05-19 15:31:12 +02:00
|
|
|
/* ioinfo structure size is different in msvcrXX.dll's */
|
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;
|
2013-01-10 11:43:37 +01:00
|
|
|
char lookahead[3];
|
2013-01-10 11:42:29 +01:00
|
|
|
int exflag;
|
2011-05-19 15:31:12 +02:00
|
|
|
CRITICAL_SECTION crit;
|
2014-05-07 09:27:59 +02:00
|
|
|
#if _MSVCR_VER >= 80
|
|
|
|
char textmode : 7;
|
|
|
|
char unicode : 1;
|
|
|
|
char pipech2[2];
|
|
|
|
__int64 startpos;
|
|
|
|
BOOL utf8translations;
|
|
|
|
#endif
|
|
|
|
#if _MSVCR_VER >= 90
|
|
|
|
char dbcsBuffer;
|
|
|
|
BOOL dbcsBufferUsed;
|
|
|
|
#endif
|
2004-12-10 16:28:25 +01:00
|
|
|
} ioinfo;
|
|
|
|
|
2011-05-19 15:31:12 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* __pioinfo (MSVCRT.@)
|
2011-10-06 20:13:16 +02:00
|
|
|
* array of pointers to ioinfo arrays [32]
|
2011-05-19 15:31:12 +02:00
|
|
|
*/
|
|
|
|
ioinfo * MSVCRT___pioinfo[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 };
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __badioinfo (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
ioinfo MSVCRT___badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT };
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2011-05-19 15:31:22 +02:00
|
|
|
typedef struct {
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE file;
|
2011-05-19 15:31:22 +02:00
|
|
|
CRITICAL_SECTION crit;
|
|
|
|
} file_crit;
|
|
|
|
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE MSVCRT__iob[_IOB_ENTRIES] = { { 0 } };
|
2011-05-19 15:31:22 +02:00
|
|
|
static file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE];
|
|
|
|
static int MSVCRT_max_streams = 512, MSVCRT_stream_idx;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* INTERNAL: process umask */
|
|
|
|
static int MSVCRT_umask = 0;
|
|
|
|
|
2011-05-03 14:26:08 +02:00
|
|
|
/* INTERNAL: static data for tmpnam and _wtmpname functions */
|
|
|
|
static int tmpnam_unique;
|
2015-12-22 17:43:20 +01:00
|
|
|
static int tmpnam_s_unique;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2015-06-10 18:12:53 +02:00
|
|
|
/* This critical section protects the MSVCRT_fstreams table
|
|
|
|
* and MSVCRT_stream_idx from race conditions. It also
|
|
|
|
* protects fd critical sections creation code.
|
2004-12-14 12:59:43 +01:00
|
|
|
*/
|
|
|
|
static CRITICAL_SECTION MSVCRT_file_cs;
|
2011-11-16 16:28:20 +01:00
|
|
|
static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug =
|
|
|
|
{
|
|
|
|
0, 0, &MSVCRT_file_cs,
|
|
|
|
{ &MSVCRT_file_cs_debug.ProcessLocksList, &MSVCRT_file_cs_debug.ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 };
|
2004-12-14 12:59:43 +01:00
|
|
|
#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
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
static void msvcrt_stat64_to_stat(const struct _stat64 *buf64, struct _stat *buf)
|
2007-02-14 11:26:42 +01:00
|
|
|
{
|
|
|
|
buf->st_dev = buf64->st_dev;
|
|
|
|
buf->st_ino = buf64->st_ino;
|
|
|
|
buf->st_mode = buf64->st_mode;
|
|
|
|
buf->st_nlink = buf64->st_nlink;
|
|
|
|
buf->st_uid = buf64->st_uid;
|
|
|
|
buf->st_gid = buf64->st_gid;
|
|
|
|
buf->st_rdev = buf64->st_rdev;
|
|
|
|
buf->st_size = buf64->st_size;
|
|
|
|
buf->st_atime = buf64->st_atime;
|
|
|
|
buf->st_mtime = buf64->st_mtime;
|
|
|
|
buf->st_ctime = buf64->st_ctime;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
static void msvcrt_stat64_to_stati64(const struct _stat64 *buf64, struct _stati64 *buf)
|
2007-02-14 11:26:42 +01:00
|
|
|
{
|
|
|
|
buf->st_dev = buf64->st_dev;
|
|
|
|
buf->st_ino = buf64->st_ino;
|
|
|
|
buf->st_mode = buf64->st_mode;
|
|
|
|
buf->st_nlink = buf64->st_nlink;
|
|
|
|
buf->st_uid = buf64->st_uid;
|
|
|
|
buf->st_gid = buf64->st_gid;
|
|
|
|
buf->st_rdev = buf64->st_rdev;
|
|
|
|
buf->st_size = buf64->st_size;
|
|
|
|
buf->st_atime = buf64->st_atime;
|
|
|
|
buf->st_mtime = buf64->st_mtime;
|
|
|
|
buf->st_ctime = buf64->st_ctime;
|
2002-11-25 03:44:34 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
static void msvcrt_stat64_to_stat32(const struct _stat64 *buf64, struct _stat32 *buf)
|
2013-04-30 14:29:59 +02:00
|
|
|
{
|
|
|
|
buf->st_dev = buf64->st_dev;
|
|
|
|
buf->st_ino = buf64->st_ino;
|
|
|
|
buf->st_mode = buf64->st_mode;
|
|
|
|
buf->st_nlink = buf64->st_nlink;
|
|
|
|
buf->st_uid = buf64->st_uid;
|
|
|
|
buf->st_gid = buf64->st_gid;
|
|
|
|
buf->st_rdev = buf64->st_rdev;
|
|
|
|
buf->st_size = buf64->st_size;
|
|
|
|
buf->st_atime = buf64->st_atime;
|
|
|
|
buf->st_mtime = buf64->st_mtime;
|
|
|
|
buf->st_ctime = buf64->st_ctime;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
static void msvcrt_stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
|
2013-04-30 14:29:59 +02:00
|
|
|
{
|
|
|
|
buf->st_dev = buf64->st_dev;
|
|
|
|
buf->st_ino = buf64->st_ino;
|
|
|
|
buf->st_mode = buf64->st_mode;
|
|
|
|
buf->st_nlink = buf64->st_nlink;
|
|
|
|
buf->st_uid = buf64->st_uid;
|
|
|
|
buf->st_gid = buf64->st_gid;
|
|
|
|
buf->st_rdev = buf64->st_rdev;
|
|
|
|
buf->st_size = buf64->st_size;
|
|
|
|
buf->st_atime = buf64->st_atime;
|
|
|
|
buf->st_mtime = buf64->st_mtime;
|
|
|
|
buf->st_ctime = buf64->st_ctime;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
static void msvcrt_stat64_to_stat32i64(const struct _stat64 *buf64, struct _stat32i64 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
buf->st_dev = buf64->st_dev;
|
|
|
|
buf->st_ino = buf64->st_ino;
|
|
|
|
buf->st_mode = buf64->st_mode;
|
|
|
|
buf->st_nlink = buf64->st_nlink;
|
|
|
|
buf->st_uid = buf64->st_uid;
|
|
|
|
buf->st_gid = buf64->st_gid;
|
|
|
|
buf->st_rdev = buf64->st_rdev;
|
|
|
|
buf->st_size = buf64->st_size;
|
|
|
|
buf->st_atime = buf64->st_atime;
|
|
|
|
buf->st_mtime = buf64->st_mtime;
|
|
|
|
buf->st_ctime = buf64->st_ctime;
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static void time_to_filetime( __time64_t time, FILETIME *ft )
|
2009-05-23 11:10:05 +02:00
|
|
|
{
|
|
|
|
/* 1601 to 1970 is 369 years plus 89 leap days */
|
|
|
|
static const __int64 secs_1601_to_1970 = ((369 * 365 + 89) * (__int64)86400);
|
|
|
|
|
|
|
|
__int64 ticks = (time + secs_1601_to_1970) * 10000000;
|
|
|
|
ft->dwHighDateTime = ticks >> 32;
|
|
|
|
ft->dwLowDateTime = ticks;
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
static inline ioinfo* get_ioinfo_nolock(int fd)
|
2011-05-19 15:31:12 +02:00
|
|
|
{
|
|
|
|
ioinfo *ret = NULL;
|
2014-11-11 15:58:08 +01:00
|
|
|
if(fd>=0 && fd<MSVCRT_MAX_FILES)
|
2011-05-19 15:31:12 +02:00
|
|
|
ret = MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE];
|
|
|
|
if(!ret)
|
|
|
|
return &MSVCRT___badioinfo;
|
|
|
|
|
|
|
|
return ret + (fd%MSVCRT_FD_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:12:53 +02:00
|
|
|
static inline void init_ioinfo_cs(ioinfo *info)
|
2014-11-11 15:58:13 +01:00
|
|
|
{
|
2015-06-10 18:12:53 +02:00
|
|
|
if(!(info->exflag & EF_CRIT_INIT)) {
|
2015-06-03 13:25:10 +02:00
|
|
|
LOCK_FILES();
|
2015-06-10 18:12:53 +02:00
|
|
|
if(!(info->exflag & EF_CRIT_INIT)) {
|
|
|
|
InitializeCriticalSection(&info->crit);
|
|
|
|
info->exflag |= EF_CRIT_INIT;
|
2015-06-03 13:25:10 +02:00
|
|
|
}
|
|
|
|
UNLOCK_FILES();
|
|
|
|
}
|
2015-06-10 18:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline ioinfo* get_ioinfo(int fd)
|
|
|
|
{
|
|
|
|
ioinfo *ret = get_ioinfo_nolock(fd);
|
|
|
|
if(ret == &MSVCRT___badioinfo)
|
|
|
|
return ret;
|
|
|
|
init_ioinfo_cs(ret);
|
2015-06-03 13:25:10 +02:00
|
|
|
EnterCriticalSection(&ret->crit);
|
2014-11-11 15:58:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:48:23 +02:00
|
|
|
static inline BOOL alloc_pioinfo_block(int fd)
|
|
|
|
{
|
|
|
|
ioinfo *block;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(fd<0 || fd>=MSVCRT_MAX_FILES)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENFILE;
|
2015-06-10 17:48:23 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
block = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(ioinfo));
|
2015-06-10 17:48:23 +02:00
|
|
|
if(!block)
|
|
|
|
{
|
|
|
|
WARN(":out of memory!\n");
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOMEM;
|
2015-06-10 17:48:23 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
|
|
|
|
block[i].handle = INVALID_HANDLE_VALUE;
|
|
|
|
if(InterlockedCompareExchangePointer((void**)&MSVCRT___pioinfo[fd/MSVCRT_FD_BLOCK_SIZE], block, NULL))
|
2020-12-01 13:51:32 +01:00
|
|
|
free(block);
|
2015-06-10 17:48:23 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ioinfo* get_ioinfo_alloc_fd(int fd)
|
|
|
|
{
|
2015-06-10 18:12:53 +02:00
|
|
|
ioinfo *ret;
|
2015-06-10 17:48:23 +02:00
|
|
|
|
|
|
|
ret = get_ioinfo(fd);
|
|
|
|
if(ret != &MSVCRT___badioinfo)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if(!alloc_pioinfo_block(fd))
|
|
|
|
return &MSVCRT___badioinfo;
|
|
|
|
|
2015-06-10 18:12:53 +02:00
|
|
|
return get_ioinfo(fd);
|
|
|
|
}
|
2015-06-10 17:48:23 +02:00
|
|
|
|
2015-06-10 18:12:53 +02:00
|
|
|
static inline ioinfo* get_ioinfo_alloc(int *fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*fd = -1;
|
|
|
|
for(i=0; i<MSVCRT_MAX_FILES; i++)
|
2015-06-10 17:48:23 +02:00
|
|
|
{
|
2015-06-10 18:12:53 +02:00
|
|
|
ioinfo *info = get_ioinfo_nolock(i);
|
|
|
|
|
|
|
|
if(info == &MSVCRT___badioinfo)
|
|
|
|
{
|
|
|
|
if(!alloc_pioinfo_block(i))
|
|
|
|
return &MSVCRT___badioinfo;
|
|
|
|
info = get_ioinfo_nolock(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
init_ioinfo_cs(info);
|
|
|
|
if(TryEnterCriticalSection(&info->crit))
|
2015-06-10 17:48:23 +02:00
|
|
|
{
|
2015-06-10 18:12:53 +02:00
|
|
|
if(info->handle == INVALID_HANDLE_VALUE)
|
2015-06-10 17:48:23 +02:00
|
|
|
{
|
2015-06-10 18:12:53 +02:00
|
|
|
*fd = i;
|
|
|
|
return info;
|
2015-06-10 17:48:23 +02:00
|
|
|
}
|
2015-06-10 18:12:53 +02:00
|
|
|
LeaveCriticalSection(&info->crit);
|
2015-06-10 17:48:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:12:53 +02:00
|
|
|
WARN(":files exhausted!\n");
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENFILE;
|
2015-06-10 18:12:53 +02:00
|
|
|
return &MSVCRT___badioinfo;
|
2015-06-10 17:48:23 +02:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:58:13 +01:00
|
|
|
static inline void release_ioinfo(ioinfo *info)
|
|
|
|
{
|
2015-06-04 11:24:52 +02:00
|
|
|
if(info!=&MSVCRT___badioinfo && info->exflag & EF_CRIT_INIT)
|
2014-11-11 15:58:13 +01:00
|
|
|
LeaveCriticalSection(&info->crit);
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:51:39 +01:00
|
|
|
static inline FILE* msvcrt_get_file(int i)
|
2011-05-19 15:31:22 +02:00
|
|
|
{
|
|
|
|
file_crit *ret;
|
|
|
|
|
|
|
|
if(i >= MSVCRT_max_streams)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if(i < _IOB_ENTRIES)
|
|
|
|
return &MSVCRT__iob[i];
|
|
|
|
|
|
|
|
ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE];
|
|
|
|
if(!ret) {
|
2020-12-01 13:51:32 +01:00
|
|
|
MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] = calloc(MSVCRT_FD_BLOCK_SIZE, sizeof(file_crit));
|
2011-05-19 15:31:22 +02:00
|
|
|
if(!MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE]) {
|
2011-05-23 16:20:30 +02:00
|
|
|
ERR("out of memory\n");
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOMEM;
|
2011-05-19 15:31:22 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = MSVCRT_fstream[i/MSVCRT_FD_BLOCK_SIZE] + (i%MSVCRT_FD_BLOCK_SIZE);
|
|
|
|
} else
|
|
|
|
ret += i%MSVCRT_FD_BLOCK_SIZE;
|
|
|
|
|
|
|
|
return &ret->file;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-06-03 13:25:23 +02:00
|
|
|
ioinfo *fdinfo = get_ioinfo(fd);
|
2009-02-18 14:44:17 +01:00
|
|
|
|
2011-05-19 15:31:12 +02:00
|
|
|
if(fdinfo != &MSVCRT___badioinfo)
|
|
|
|
{
|
|
|
|
fdinfo->handle = INVALID_HANDLE_VALUE;
|
|
|
|
fdinfo->wxflag = 0;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE(":fd (%d) freed\n",fd);
|
2013-03-01 15:04:21 +01:00
|
|
|
|
|
|
|
if (fd < 3)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
switch (fd)
|
|
|
|
{
|
2009-02-18 14:44:17 +01:00
|
|
|
case 0:
|
2013-03-01 15:04:21 +01:00
|
|
|
SetStdHandle(STD_INPUT_HANDLE, 0);
|
2009-02-18 14:44:17 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2013-03-01 15:04:21 +01:00
|
|
|
SetStdHandle(STD_OUTPUT_HANDLE, 0);
|
2009-02-18 14:44:17 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2013-03-01 15:04:21 +01:00
|
|
|
SetStdHandle(STD_ERROR_HANDLE, 0);
|
2009-02-18 14:44:17 +01:00
|
|
|
break;
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 13:25:23 +02:00
|
|
|
release_ioinfo(fdinfo);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:48:23 +02:00
|
|
|
static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2011-05-19 15:31:12 +02:00
|
|
|
fdinfo->handle = hand;
|
2014-07-31 16:51:52 +02:00
|
|
|
fdinfo->wxflag = WX_OPEN | (flag & (WX_DONTINHERIT | WX_APPEND | WX_TEXT | WX_PIPE | WX_TTY));
|
2013-01-10 11:43:37 +01:00
|
|
|
fdinfo->lookahead[0] = '\n';
|
|
|
|
fdinfo->lookahead[1] = '\n';
|
|
|
|
fdinfo->lookahead[2] = '\n';
|
2015-06-10 17:48:23 +02:00
|
|
|
fdinfo->exflag &= EF_CRIT_INIT;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2019-04-09 14:06:43 +02:00
|
|
|
if (hand == MSVCRT_NO_CONSOLE) hand = 0;
|
2015-06-10 17:48:23 +02:00
|
|
|
switch (fdinfo-MSVCRT___pioinfo[0])
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-06-10 17:48:23 +02:00
|
|
|
int fd;
|
2015-06-10 18:12:53 +02:00
|
|
|
ioinfo *info = get_ioinfo_alloc(&fd);
|
2004-12-14 12:59:43 +01:00
|
|
|
|
2015-06-10 17:48:23 +02:00
|
|
|
TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
|
|
|
|
|
|
|
|
if(info == &MSVCRT___badioinfo)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
msvcrt_set_fd(info, hand, flag);
|
|
|
|
release_ioinfo(info);
|
|
|
|
return fd;
|
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 */
|
2020-12-01 13:51:39 +01:00
|
|
|
static FILE* msvcrt_alloc_fp(void)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
2013-02-19 23:24:42 +01:00
|
|
|
int i;
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *file;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2011-05-19 15:31:22 +02:00
|
|
|
for (i = 3; i < MSVCRT_max_streams; i++)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
2011-05-19 15:31:22 +02:00
|
|
|
file = msvcrt_get_file(i);
|
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (file->_flag == 0)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
2011-11-23 12:32:45 +01:00
|
|
|
if (i == MSVCRT_stream_idx)
|
|
|
|
{
|
2011-11-24 12:13:03 +01:00
|
|
|
if (file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&((file_crit*)file)->crit);
|
|
|
|
((file_crit*)file)->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": file_crit.crit");
|
|
|
|
}
|
2011-11-23 12:32:45 +01:00
|
|
|
MSVCRT_stream_idx++;
|
|
|
|
}
|
2011-05-19 15:31:22 +02:00
|
|
|
return file;
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-19 15:31:22 +02:00
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: initialize a FILE* from an open fd */
|
2020-12-01 13:51:39 +01:00
|
|
|
static int msvcrt_init_fp(FILE* file, int fd, unsigned stream_flags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":fd (%d) allocating FILE*\n",fd);
|
2015-06-03 13:26:00 +02:00
|
|
|
if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
WARN(":invalid fd %d\n",fd);
|
2020-12-01 13:51:23 +01:00
|
|
|
*__doserrno() = 0;
|
|
|
|
*_errno() = EBADF;
|
2004-05-12 02:12:26 +02:00
|
|
|
return -1;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2014-06-27 10:17:52 +02:00
|
|
|
file->_ptr = file->_base = NULL;
|
|
|
|
file->_cnt = 0;
|
2004-05-12 02:12:26 +02:00
|
|
|
file->_file = fd;
|
|
|
|
file->_flag = stream_flags;
|
2014-06-27 10:17:52 +02:00
|
|
|
file->_tmpfname = NULL;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2007-09-21 15:50:01 +02:00
|
|
|
unsigned msvcrt_create_io_inherit_block(WORD *size, BYTE **block)
|
2004-09-03 03:05:30 +02:00
|
|
|
{
|
2015-06-10 17:48:33 +02:00
|
|
|
int fd, last_fd;
|
2004-09-03 03:05:30 +02:00
|
|
|
char* wxflag_ptr;
|
|
|
|
HANDLE* handle_ptr;
|
2011-05-19 15:31:12 +02:00
|
|
|
ioinfo* fdinfo;
|
2004-09-03 03:05:30 +02:00
|
|
|
|
2015-06-10 17:48:33 +02:00
|
|
|
for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
|
|
|
|
if (get_ioinfo_nolock(last_fd)->handle != INVALID_HANDLE_VALUE)
|
|
|
|
break;
|
|
|
|
last_fd++;
|
|
|
|
|
|
|
|
*size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
|
2020-12-01 13:51:32 +01:00
|
|
|
*block = calloc(1, *size);
|
2007-09-21 15:50:01 +02:00
|
|
|
if (!*block)
|
2004-09-03 03:05:30 +02:00
|
|
|
{
|
2007-09-21 15:50:01 +02:00
|
|
|
*size = 0;
|
2004-09-03 03:05:30 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-09-21 15:50:01 +02:00
|
|
|
wxflag_ptr = (char*)*block + sizeof(unsigned);
|
2015-06-10 17:48:33 +02:00
|
|
|
handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
|
2004-09-03 03:05:30 +02:00
|
|
|
|
2015-06-10 17:48:33 +02:00
|
|
|
*(unsigned*)*block = last_fd;
|
|
|
|
for (fd = 0; fd < last_fd; fd++)
|
2004-09-03 03:05:30 +02:00
|
|
|
{
|
|
|
|
/* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
|
2015-06-03 13:25:52 +02:00
|
|
|
fdinfo = get_ioinfo(fd);
|
2011-05-19 15:31:12 +02:00
|
|
|
if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
|
2004-09-03 03:05:30 +02:00
|
|
|
{
|
2011-05-19 15:31:12 +02:00
|
|
|
*wxflag_ptr = fdinfo->wxflag;
|
|
|
|
*handle_ptr = fdinfo->handle;
|
2004-09-03 03:05:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*wxflag_ptr = 0;
|
|
|
|
*handle_ptr = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2015-06-03 13:25:52 +02:00
|
|
|
release_ioinfo(fdinfo);
|
2004-09-03 03:05:30 +02:00
|
|
|
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;
|
2011-05-19 15:31:12 +02:00
|
|
|
ioinfo *fdinfo;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
GetStartupInfoA(&si);
|
2009-09-10 15:29:04 +02:00
|
|
|
if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
|
2004-09-03 03:05:30 +02:00
|
|
|
{
|
2009-09-10 15:29:04 +02:00
|
|
|
BYTE* wxflag_ptr;
|
2004-09-03 03:05:30 +02:00
|
|
|
HANDLE* handle_ptr;
|
2009-09-10 15:29:04 +02:00
|
|
|
unsigned int count;
|
2004-09-03 03:05:30 +02:00
|
|
|
|
2009-09-10 15:29:04 +02:00
|
|
|
count = *(unsigned*)si.lpReserved2;
|
|
|
|
wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
|
|
|
|
handle_ptr = (HANDLE*)(wxflag_ptr + count);
|
2004-09-03 03:05:30 +02:00
|
|
|
|
2009-09-10 15:29:04 +02:00
|
|
|
count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
|
2011-05-19 15:31:12 +02:00
|
|
|
count = min(count, MSVCRT_MAX_FILES);
|
2009-09-10 15:29:04 +02:00
|
|
|
for (i = 0; i < count; i++)
|
2004-09-03 03:05:30 +02:00
|
|
|
{
|
2021-02-15 15:52:16 +01:00
|
|
|
if ((*wxflag_ptr & WX_OPEN) && GetFileType(*handle_ptr) != FILE_TYPE_UNKNOWN)
|
2015-06-10 17:48:23 +02:00
|
|
|
{
|
|
|
|
fdinfo = get_ioinfo_alloc_fd(i);
|
|
|
|
if(fdinfo != &MSVCRT___badioinfo)
|
|
|
|
msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
|
|
|
|
release_ioinfo(fdinfo);
|
|
|
|
}
|
2011-05-19 15:31:12 +02:00
|
|
|
|
2004-09-03 03:05:30 +02:00
|
|
|
wxflag_ptr++; handle_ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
fdinfo = get_ioinfo_alloc_fd(STDIN_FILENO);
|
2013-08-21 13:52:10 +02:00
|
|
|
if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
|
|
|
|
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
|
2017-11-15 09:47:23 +01:00
|
|
|
DWORD flags = WX_OPEN | WX_TEXT;
|
2013-08-21 13:52:10 +02:00
|
|
|
DWORD type = GetFileType(h);
|
|
|
|
|
2017-11-15 09:47:23 +01:00
|
|
|
if (type == FILE_TYPE_UNKNOWN) {
|
|
|
|
h = MSVCRT_NO_CONSOLE;
|
|
|
|
flags |= WX_TTY;
|
|
|
|
} else if ((type & 0xf) == FILE_TYPE_CHAR) {
|
|
|
|
flags |= WX_TTY;
|
|
|
|
} else if ((type & 0xf) == FILE_TYPE_PIPE) {
|
|
|
|
flags |= WX_PIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
msvcrt_set_fd(fdinfo, h, flags);
|
2013-08-21 13:52:10 +02:00
|
|
|
}
|
2015-06-10 17:48:23 +02:00
|
|
|
release_ioinfo(fdinfo);
|
2011-05-19 15:31:12 +02:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
fdinfo = get_ioinfo_alloc_fd(STDOUT_FILENO);
|
2013-08-21 13:52:10 +02:00
|
|
|
if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
|
|
|
|
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
2017-11-15 09:47:23 +01:00
|
|
|
DWORD flags = WX_OPEN | WX_TEXT;
|
2013-08-21 13:52:10 +02:00
|
|
|
DWORD type = GetFileType(h);
|
|
|
|
|
2017-11-15 09:47:23 +01:00
|
|
|
if (type == FILE_TYPE_UNKNOWN) {
|
|
|
|
h = MSVCRT_NO_CONSOLE;
|
|
|
|
flags |= WX_TTY;
|
|
|
|
} else if ((type & 0xf) == FILE_TYPE_CHAR) {
|
|
|
|
flags |= WX_TTY;
|
|
|
|
} else if ((type & 0xf) == FILE_TYPE_PIPE) {
|
|
|
|
flags |= WX_PIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
msvcrt_set_fd(fdinfo, h, flags);
|
2013-08-21 13:52:10 +02:00
|
|
|
}
|
2015-06-10 17:48:23 +02:00
|
|
|
release_ioinfo(fdinfo);
|
2011-05-19 15:31:12 +02:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
fdinfo = get_ioinfo_alloc_fd(STDERR_FILENO);
|
2013-08-21 13:52:10 +02:00
|
|
|
if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
|
|
|
|
HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
|
2017-11-15 09:47:23 +01:00
|
|
|
DWORD flags = WX_OPEN | WX_TEXT;
|
2013-08-21 13:52:10 +02:00
|
|
|
DWORD type = GetFileType(h);
|
|
|
|
|
2017-11-15 09:47:23 +01:00
|
|
|
if (type == FILE_TYPE_UNKNOWN) {
|
|
|
|
h = MSVCRT_NO_CONSOLE;
|
|
|
|
flags |= WX_TTY;
|
|
|
|
} else if ((type & 0xf) == FILE_TYPE_CHAR) {
|
|
|
|
flags |= WX_TTY;
|
|
|
|
} else if ((type & 0xf) == FILE_TYPE_PIPE) {
|
|
|
|
flags |= WX_PIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
msvcrt_set_fd(fdinfo, h, flags);
|
2013-08-21 13:52:10 +02:00
|
|
|
}
|
2015-06-10 17:48:23 +02:00
|
|
|
release_ioinfo(fdinfo);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO)->handle,
|
|
|
|
get_ioinfo_nolock(STDOUT_FILENO)->handle,
|
|
|
|
get_ioinfo_nolock(STDERR_FILENO)->handle);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2020-12-01 13:51:39 +01:00
|
|
|
memset(MSVCRT__iob,0,3*sizeof(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 */
|
2017-11-15 09:47:23 +01:00
|
|
|
MSVCRT__iob[i]._file = get_ioinfo_nolock(i)->handle == MSVCRT_NO_CONSOLE ?
|
|
|
|
MSVCRT_NO_CONSOLE_FD : i;
|
2004-09-03 03:05:30 +02:00
|
|
|
MSVCRT__iob[i]._tmpfname = NULL;
|
2020-12-02 00:53:28 +01:00
|
|
|
MSVCRT__iob[i]._flag = (i == 0) ? _IOREAD : _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 */
|
2020-12-01 13:51:39 +01:00
|
|
|
static int msvcrt_flush_buffer(FILE* file)
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2020-06-12 12:43:00 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if((file->_flag & (_IOREAD|_IOWRT)) == _IOWRT &&
|
|
|
|
file->_flag & (_IOMYBUF|MSVCRT__USERBUF)) {
|
2002-03-11 06:03:05 +01:00
|
|
|
int cnt=file->_ptr-file->_base;
|
2020-12-03 19:11:20 +01:00
|
|
|
if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOERR;
|
2020-11-30 14:37:09 +01:00
|
|
|
ret = EOF;
|
2020-12-02 00:53:28 +01:00
|
|
|
} else if(file->_flag & _IORW) {
|
|
|
|
file->_flag &= ~_IOWRT;
|
2020-06-12 12:43:00 +02:00
|
|
|
}
|
2015-01-23 17:27:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file->_ptr=file->_base;
|
|
|
|
file->_cnt=0;
|
2020-06-12 12:43:00 +02:00
|
|
|
return ret;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 13:21:08 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _isatty (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _isatty(int fd)
|
2013-01-29 13:21:08 +01:00
|
|
|
{
|
2014-07-31 16:51:52 +02:00
|
|
|
TRACE(":fd (%d)\n",fd);
|
2013-01-29 13:21:08 +01:00
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
return get_ioinfo_nolock(fd)->wxflag & WX_TTY;
|
2013-01-29 13:21:08 +01:00
|
|
|
}
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/* INTERNAL: Allocate stdio file buffer */
|
2020-12-01 13:51:39 +01:00
|
|
|
static BOOL msvcrt_alloc_buffer(FILE* file)
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
if((file->_file==STDOUT_FILENO || file->_file==STDERR_FILENO)
|
2020-12-03 19:11:20 +01:00
|
|
|
&& _isatty(file->_file))
|
2013-01-29 13:21:08 +01:00
|
|
|
return FALSE;
|
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
file->_base = calloc(1, MSVCRT_INTERNAL_BUFSIZ);
|
2013-01-29 13:21:08 +01:00
|
|
|
if(file->_base) {
|
2014-06-14 01:23:56 +02:00
|
|
|
file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOMYBUF;
|
2013-01-29 13:21:08 +01:00
|
|
|
} else {
|
|
|
|
file->_base = (char*)(&file->_charbuf);
|
2014-06-27 10:17:24 +02:00
|
|
|
file->_bufsiz = 2;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IONBF;
|
2013-01-29 13:21:08 +01:00
|
|
|
}
|
|
|
|
file->_ptr = file->_base;
|
|
|
|
file->_cnt = 0;
|
|
|
|
return TRUE;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 13:21:27 +01:00
|
|
|
/* INTERNAL: Allocate temporary buffer for stdout and stderr */
|
2020-12-01 13:51:39 +01:00
|
|
|
static BOOL add_std_buffer(FILE *file)
|
2013-01-29 13:21:27 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
static char buffers[2][BUFSIZ];
|
2013-01-29 13:21:27 +01:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if((file->_file!=STDOUT_FILENO && file->_file!=STDERR_FILENO)
|
|
|
|
|| (file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))
|
2020-12-03 19:11:20 +01:00
|
|
|
|| !_isatty(file->_file))
|
2013-01-29 13:21:27 +01:00
|
|
|
return FALSE;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_ptr = file->_base = buffers[file->_file == STDOUT_FILENO ? 0 : 1];
|
|
|
|
file->_bufsiz = file->_cnt = BUFSIZ;
|
2014-06-27 10:17:24 +02:00
|
|
|
file->_flag |= MSVCRT__USERBUF;
|
2013-01-29 13:21:27 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: Removes temporary buffer from stdout or stderr */
|
|
|
|
/* Only call this function when add_std_buffer returned TRUE */
|
2020-12-01 13:51:39 +01:00
|
|
|
static void remove_std_buffer(FILE *file)
|
2013-01-29 13:21:27 +01:00
|
|
|
{
|
|
|
|
msvcrt_flush_buffer(file);
|
|
|
|
file->_ptr = file->_base = NULL;
|
|
|
|
file->_bufsiz = file->_cnt = 0;
|
2014-06-27 10:17:24 +02:00
|
|
|
file->_flag &= ~MSVCRT__USERBUF;
|
2013-01-29 13:21:27 +01:00
|
|
|
}
|
|
|
|
|
2003-10-24 02:23:51 +02:00
|
|
|
/* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
|
2011-05-03 14:26:08 +02:00
|
|
|
static int msvcrt_int_to_base32(int num, char *str)
|
2003-10-24 02:23:51 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2011-05-03 14:26:08 +02:00
|
|
|
|
|
|
|
return digits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INTERNAL: wide character version of msvcrt_int_to_base32 */
|
2020-11-27 14:16:49 +01:00
|
|
|
static int msvcrt_int_to_base32_w(int num, wchar_t *str)
|
2011-05-03 14:26:08 +02:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *p;
|
2011-05-03 14:26:08 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return digits;
|
2003-10-24 02:23:51 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2019-08-08 18:27:19 +02:00
|
|
|
* __iob_func (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2019-08-08 18:27:19 +02:00
|
|
|
#undef __iob_func
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE * CDECL __iob_func(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return &MSVCRT__iob[0];
|
|
|
|
}
|
|
|
|
|
2019-03-29 09:38:36 +01:00
|
|
|
#if _MSVCR_VER >= 140
|
|
|
|
/*********************************************************************
|
|
|
|
* __acrt_iob_func(UCRTBASE.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE * CDECL __acrt_iob_func(unsigned idx)
|
2019-03-29 09:38:36 +01:00
|
|
|
{
|
|
|
|
return &MSVCRT__iob[idx];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _access (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _access(const char *filename, int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
DWORD attr = GetFileAttributesA(filename);
|
|
|
|
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("(%s,%d) %d\n",filename,mode,attr);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-02 22:03:41 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _access_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _access_s(const char *filename, int mode)
|
2010-11-02 22:03:41 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(filename != NULL)) return *_errno();
|
|
|
|
if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *_errno();
|
2010-11-02 22:03:41 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if (_access(filename, mode) == -1)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2013-08-21 22:13:36 +02:00
|
|
|
return 0;
|
2010-11-02 22:03:41 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _waccess (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _waccess(const wchar_t *filename, int mode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
DWORD attr = GetFileAttributesW(filename);
|
|
|
|
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
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
|
|
|
|
2010-11-02 22:03:41 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _waccess_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _waccess_s(const wchar_t *filename, int mode)
|
2010-11-02 22:03:41 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(filename != NULL)) return *_errno();
|
|
|
|
if (!MSVCRT_CHECK_PMT((mode & ~(MSVCRT_R_OK | MSVCRT_W_OK)) == 0)) return *_errno();
|
2010-11-02 22:03:41 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if (_waccess(filename, mode) == -1)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2013-08-21 22:13:36 +02:00
|
|
|
return 0;
|
2010-11-02 22:03:41 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _chmod (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _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
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
DWORD newFlags = (flags & _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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wchmod(const 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
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
DWORD newFlags = (flags & _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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _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;
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("failed (%d)\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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wunlink(const wchar_t *path)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
TRACE("(%s)\n",debugstr_w(path));
|
|
|
|
if(DeleteFileW(path))
|
|
|
|
return 0;
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("failed (%d)\n",GetLastError());
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-18 11:03:13 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _commit (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _commit(int fd)
|
2013-01-18 11:03:13 +01:00
|
|
|
{
|
2014-11-11 15:58:13 +01:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
|
|
|
int ret;
|
2013-01-18 11:03:13 +01:00
|
|
|
|
2014-11-11 15:58:13 +01:00
|
|
|
TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
|
2013-01-18 11:03:13 +01:00
|
|
|
|
2014-11-11 15:58:13 +01:00
|
|
|
if (info->handle == INVALID_HANDLE_VALUE)
|
|
|
|
ret = -1;
|
|
|
|
else if (!FlushFileBuffers(info->handle))
|
2013-01-18 11:03:13 +01:00
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
/* FlushFileBuffers fails for console handles
|
|
|
|
* so we ignore this error.
|
|
|
|
*/
|
2014-11-11 15:58:13 +01:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE(":failed-last error (%d)\n",GetLastError());
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
ret = -1;
|
2013-01-18 11:03:13 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-11 15:58:13 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE(":ok\n");
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_ioinfo(info);
|
|
|
|
return ret;
|
2013-01-18 11:03:13 +01:00
|
|
|
}
|
|
|
|
|
2012-03-12 13:14:06 +01:00
|
|
|
/* INTERNAL: Flush all stream buffer */
|
|
|
|
static int msvcrt_flush_all_buffers(int mask)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
|
|
|
int i, num_flushed = 0;
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *file;
|
2004-03-16 20:17:11 +01:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2014-01-28 10:08:32 +01:00
|
|
|
for (i = 0; i < MSVCRT_stream_idx; i++) {
|
2011-05-19 15:31:22 +02:00
|
|
|
file = msvcrt_get_file(i);
|
|
|
|
|
|
|
|
if (file->_flag)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
2012-03-12 13:14:06 +01:00
|
|
|
if(file->_flag & mask) {
|
2020-12-03 19:11:20 +01:00
|
|
|
fflush(file);
|
2004-03-16 20:17:11 +01:00
|
|
|
num_flushed++;
|
|
|
|
}
|
|
|
|
}
|
2011-05-19 15:31:22 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-12 13:14:06 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _flushall (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _flushall(void)
|
2012-03-12 13:14:06 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
return msvcrt_flush_all_buffers(_IOWRT | _IOREAD);
|
2012-03-12 13:14:06 +01:00
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fflush (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fflush(FILE* file)
|
2014-10-20 21:03:16 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if(!file) {
|
2020-12-02 00:53:28 +01:00
|
|
|
msvcrt_flush_all_buffers(_IOWRT);
|
2014-10-20 21:03:16 +02:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fflush_nolock(file);
|
|
|
|
_unlock_file(file);
|
2014-10-20 21:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fflush_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fflush_nolock(FILE* file)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
2015-01-23 17:27:23 +01:00
|
|
|
int res;
|
|
|
|
|
2011-05-20 13:21:29 +02:00
|
|
|
if(!file) {
|
2020-12-02 00:53:28 +01:00
|
|
|
msvcrt_flush_all_buffers(_IOWRT);
|
2012-03-12 13:14:04 +01:00
|
|
|
return 0;
|
2011-05-20 13:21:29 +02:00
|
|
|
}
|
2015-01-23 17:27:23 +01:00
|
|
|
|
|
|
|
res = msvcrt_flush_buffer(file);
|
|
|
|
if(!res && (file->_flag & MSVCRT__IOCOMMIT))
|
2020-12-03 19:11:20 +01:00
|
|
|
res = _commit(file->_file) ? EOF : 0;
|
2015-01-23 17:27:23 +01:00
|
|
|
return res;
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _close (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _close(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-11-11 15:58:18 +01:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2004-12-14 12:59:43 +01:00
|
|
|
int ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-11-11 15:58:18 +01:00
|
|
|
TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
|
2017-11-15 09:47:29 +01:00
|
|
|
|
|
|
|
if (fd == MSVCRT_NO_CONSOLE_FD) {
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2017-11-15 09:47:29 +01:00
|
|
|
ret = -1;
|
2020-11-30 14:37:27 +01:00
|
|
|
} else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, EBADF)) {
|
2004-12-14 12:59:43 +01:00
|
|
|
ret = -1;
|
2020-12-02 00:53:28 +01:00
|
|
|
} else if (fd == STDOUT_FILENO &&
|
|
|
|
info->handle == get_ioinfo_nolock(STDERR_FILENO)->handle) {
|
2016-02-19 18:43:20 +01:00
|
|
|
msvcrt_free_fd(fd);
|
|
|
|
ret = 0;
|
2020-12-02 00:53:28 +01:00
|
|
|
} else if (fd == STDERR_FILENO &&
|
|
|
|
info->handle == get_ioinfo_nolock(STDOUT_FILENO)->handle) {
|
2016-02-19 18:43:20 +01:00
|
|
|
msvcrt_free_fd(fd);
|
|
|
|
ret = 0;
|
2011-10-06 20:13:38 +02:00
|
|
|
} else {
|
2014-11-11 15:58:18 +01:00
|
|
|
ret = CloseHandle(info->handle) ? 0 : -1;
|
2004-12-14 12:59:43 +01:00
|
|
|
msvcrt_free_fd(fd);
|
2011-10-06 20:13:38 +02:00
|
|
|
if (ret) {
|
|
|
|
WARN(":failed-last error (%d)\n",GetLastError());
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
}
|
2004-12-14 12:59:43 +01:00
|
|
|
}
|
2014-11-11 15:58:18 +01:00
|
|
|
release_ioinfo(info);
|
2004-12-14 12:59:43 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2004-12-09 17:51:26 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _dup2 (MSVCRT.@)
|
|
|
|
* NOTES
|
2008-03-01 05:29:54 +01:00
|
|
|
* MSDN isn't clear on this point, but the remarks for _pipe
|
2004-12-09 17:51:26 +01:00
|
|
|
* indicate file descriptors duplicated with _dup and _dup2 are always
|
|
|
|
* inheritable.
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _dup2(int od, int nd)
|
2004-12-09 17:51:26 +01:00
|
|
|
{
|
2015-06-03 13:25:10 +02:00
|
|
|
ioinfo *info_od, *info_nd;
|
2004-12-09 17:51:26 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
TRACE("(od=%d, nd=%d)\n", od, nd);
|
2015-06-03 13:25:10 +02:00
|
|
|
|
|
|
|
if (od < nd)
|
|
|
|
{
|
|
|
|
info_od = get_ioinfo(od);
|
2015-06-10 17:48:23 +02:00
|
|
|
info_nd = get_ioinfo_alloc_fd(nd);
|
2015-06-03 13:25:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-10 17:48:23 +02:00
|
|
|
info_nd = get_ioinfo_alloc_fd(nd);
|
2015-06-03 13:25:10 +02:00
|
|
|
info_od = get_ioinfo(od);
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:48:23 +02:00
|
|
|
if (info_nd == &MSVCRT___badioinfo)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (info_od->wxflag & WX_OPEN)
|
2004-12-09 17:51:26 +01:00
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
|
2015-06-03 13:25:10 +02:00
|
|
|
if (DuplicateHandle(GetCurrentProcess(), info_od->handle,
|
2004-12-09 17:51:26 +01:00
|
|
|
GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
int wxflag = info_od->wxflag & ~_O_NOINHERIT;
|
2004-12-09 17:51:26 +01:00
|
|
|
|
2015-06-03 13:25:10 +02:00
|
|
|
if (info_nd->wxflag & WX_OPEN)
|
2020-12-03 19:11:20 +01:00
|
|
|
_close(nd);
|
2015-06-10 17:48:23 +02:00
|
|
|
|
|
|
|
msvcrt_set_fd(info_nd, handle, wxflag);
|
|
|
|
/* _dup2 returns 0, not nd, on success */
|
|
|
|
ret = 0;
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2004-12-09 17:51:26 +01:00
|
|
|
ret = -1;
|
|
|
|
}
|
2015-06-03 13:25:10 +02:00
|
|
|
|
|
|
|
release_ioinfo(info_od);
|
|
|
|
release_ioinfo(info_nd);
|
2004-12-09 17:51:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _dup (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _dup(int od)
|
2004-12-09 17:51:26 +01:00
|
|
|
{
|
2004-12-14 12:59:43 +01:00
|
|
|
int fd, ret;
|
2015-06-10 18:12:53 +02:00
|
|
|
ioinfo *info = get_ioinfo_alloc(&fd);
|
2004-12-14 12:59:43 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if (_dup2(od, fd) == 0)
|
2004-12-14 12:59:43 +01:00
|
|
|
ret = fd;
|
|
|
|
else
|
|
|
|
ret = -1;
|
2015-06-10 18:12:53 +02:00
|
|
|
release_ioinfo(info);
|
2004-12-14 12:59:43 +01:00
|
|
|
return ret;
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _eof (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _eof(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-12-05 14:42:44 +01:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2005-09-02 14:26:21 +02:00
|
|
|
DWORD curpos,endpos;
|
|
|
|
LONG hcurpos,hendpos;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-12-05 14:42:44 +01:00
|
|
|
TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-12-05 14:42:44 +01:00
|
|
|
if (info->handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
2014-12-05 14:42:44 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-12-05 14:42:44 +01:00
|
|
|
if (info->wxflag & WX_ATEOF)
|
|
|
|
{
|
|
|
|
release_ioinfo(info);
|
|
|
|
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;
|
2014-12-05 14:42:44 +01:00
|
|
|
curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
|
|
|
|
endpos = SetFilePointer(info->handle, 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? */
|
2014-12-05 14:42:44 +01:00
|
|
|
release_ioinfo(info);
|
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
|
|
|
|
2014-12-05 14:42:44 +01:00
|
|
|
SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
|
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fcloseall (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fcloseall(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int num_closed = 0, i;
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *file;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2011-05-19 15:31:22 +02:00
|
|
|
for (i = 3; i < MSVCRT_stream_idx; i++) {
|
|
|
|
file = msvcrt_get_file(i);
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if (file->_flag && !fclose(file))
|
2001-01-11 00:59:25 +01:00
|
|
|
num_closed++;
|
2011-05-19 15:31:22 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2013-02-19 23:24:42 +01:00
|
|
|
unsigned int i;
|
|
|
|
int j;
|
2011-05-19 15:31:12 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_flushall();
|
|
|
|
_fcloseall();
|
2011-05-19 15:31:12 +02:00
|
|
|
|
2018-07-26 00:05:52 +02:00
|
|
|
for(i=0; i<ARRAY_SIZE(MSVCRT___pioinfo); i++)
|
2014-11-11 15:58:03 +01:00
|
|
|
{
|
|
|
|
if(!MSVCRT___pioinfo[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
|
|
|
|
{
|
|
|
|
if(MSVCRT___pioinfo[i][j].exflag & EF_CRIT_INIT)
|
|
|
|
DeleteCriticalSection(&MSVCRT___pioinfo[i][j].crit);
|
|
|
|
}
|
2020-12-01 13:51:32 +01:00
|
|
|
free(MSVCRT___pioinfo[i]);
|
2014-11-11 15:58:03 +01:00
|
|
|
}
|
2011-05-19 15:31:12 +02:00
|
|
|
|
2013-02-19 23:24:42 +01:00
|
|
|
for(j=0; j<MSVCRT_stream_idx; j++)
|
2011-11-23 12:32:45 +01:00
|
|
|
{
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *file = msvcrt_get_file(j);
|
2011-11-23 12:32:45 +01:00
|
|
|
if(file<MSVCRT__iob || file>=MSVCRT__iob+_IOB_ENTRIES)
|
|
|
|
{
|
|
|
|
((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
|
|
|
|
DeleteCriticalSection(&((file_crit*)file)->crit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:05:52 +02:00
|
|
|
for(i=0; i<ARRAY_SIZE(MSVCRT_fstream); i++)
|
2020-12-01 13:51:32 +01:00
|
|
|
free(MSVCRT_fstream[i]);
|
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
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-12-05 14:42:50 +01:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2011-04-20 08:17:48 +02:00
|
|
|
LARGE_INTEGER ofs;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-12-05 14:42:50 +01:00
|
|
|
TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
|
|
|
|
|
|
|
|
if (info->handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2014-12-05 14:42:50 +01:00
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
2014-12-05 14:42:50 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (whence < 0 || whence > 2)
|
|
|
|
{
|
2014-12-05 14:42:50 +01:00
|
|
|
release_ioinfo(info);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = 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),
|
2020-12-02 00:53:28 +01:00
|
|
|
(whence==SEEK_SET)?"SEEK_SET":
|
|
|
|
(whence==SEEK_CUR)?"SEEK_CUR":
|
|
|
|
(whence==SEEK_END)?"SEEK_END":"UNKNOWN");
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2011-04-20 08:17:48 +02:00
|
|
|
/* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
|
|
|
|
* so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
|
2005-09-02 14:26:21 +02:00
|
|
|
ofs.QuadPart = offset;
|
2014-12-05 14:42:50 +01:00
|
|
|
if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
|
2011-04-20 08:17:48 +02:00
|
|
|
GetLastError() == ERROR_SUCCESS)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-12-05 14:42:50 +01:00
|
|
|
info->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
|
|
|
|
2014-12-05 14:42:50 +01:00
|
|
|
release_ioinfo(info);
|
2011-04-20 08:17:48 +02:00
|
|
|
return ofs.QuadPart;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2014-12-05 14:42:50 +01:00
|
|
|
release_ioinfo(info);
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE(":error-last error (%d)\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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__msvcrt_long CDECL _lseek(int fd, __msvcrt_long offset, int whence)
|
2002-11-25 21:50:01 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _lseeki64(fd, offset, whence);
|
2002-11-25 21:50:01 +01:00
|
|
|
}
|
|
|
|
|
2010-11-01 22:19:14 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _lock_file (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
void CDECL _lock_file(FILE *file)
|
2010-11-01 22:19:14 +01:00
|
|
|
{
|
2011-05-19 15:31:42 +02:00
|
|
|
if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
|
|
|
|
_lock(_STREAM_LOCKS+(file-MSVCRT__iob));
|
|
|
|
else
|
|
|
|
EnterCriticalSection(&((file_crit*)file)->crit);
|
2010-11-01 22:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _unlock_file (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
void CDECL _unlock_file(FILE *file)
|
2010-11-01 22:19:14 +01:00
|
|
|
{
|
2011-05-19 15:31:42 +02:00
|
|
|
if(file>=MSVCRT__iob && file<MSVCRT__iob+_IOB_ENTRIES)
|
|
|
|
_unlock(_STREAM_LOCKS+(file-MSVCRT__iob));
|
|
|
|
else
|
|
|
|
LeaveCriticalSection(&((file_crit*)file)->crit);
|
2010-11-01 22:19:14 +01:00
|
|
|
}
|
|
|
|
|
2002-03-20 23:50:38 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _locking (MSVCRT.@)
|
|
|
|
*
|
|
|
|
* This is untested; the underlying LockFile doesn't work yet.
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _locking(int fd, int mode, __msvcrt_long nbytes)
|
2002-03-20 23:50:38 +01:00
|
|
|
{
|
2014-12-05 14:42:55 +01:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2002-03-20 23:50:38 +01:00
|
|
|
BOOL ret;
|
|
|
|
DWORD cur_locn;
|
|
|
|
|
2014-12-05 14:42:55 +01:00
|
|
|
TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
|
|
|
|
if (info->handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
release_ioinfo(info);
|
2002-03-20 23:50:38 +01:00
|
|
|
return -1;
|
2014-12-05 14:42:55 +01:00
|
|
|
}
|
2002-03-20 23:50:38 +01:00
|
|
|
|
|
|
|
if (mode < 0 || mode > 4)
|
|
|
|
{
|
2014-12-05 14:42:55 +01:00
|
|
|
release_ioinfo(info);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2002-03-20 23:50:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
TRACE(":fd (%d) by 0x%08Ix mode %s\n",
|
2020-12-04 15:50:17 +01:00
|
|
|
fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
|
|
|
|
(mode==_LK_LOCK)?"_LK_LOCK":
|
|
|
|
(mode==_LK_NBLCK)?"_LK_NBLCK":
|
|
|
|
(mode==_LK_RLCK)?"_LK_RLCK":
|
|
|
|
(mode==_LK_NBRLCK)?"_LK_NBRLCK":
|
2002-03-20 23:50:38 +01:00
|
|
|
"UNKNOWN");
|
|
|
|
|
2015-08-16 12:46:38 +02:00
|
|
|
if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
|
2002-03-20 23:50:38 +01:00
|
|
|
{
|
2014-12-05 14:42:55 +01:00
|
|
|
release_ioinfo(info);
|
2002-03-20 23:50:38 +01:00
|
|
|
FIXME ("Seek failed\n");
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL; /* FIXME */
|
2002-03-20 23:50:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 15:50:17 +01:00
|
|
|
if (mode == _LK_LOCK || mode == _LK_RLCK)
|
2002-03-20 23:50:38 +01:00
|
|
|
{
|
|
|
|
int nretry = 10;
|
|
|
|
ret = 1; /* just to satisfy gcc */
|
|
|
|
while (nretry--)
|
|
|
|
{
|
2014-12-05 14:42:55 +01:00
|
|
|
ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
|
2002-03-20 23:50:38 +01:00
|
|
|
if (ret) break;
|
2004-06-25 03:19:15 +02:00
|
|
|
Sleep(1);
|
2002-03-20 23:50:38 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 15:50:17 +01:00
|
|
|
else if (mode == _LK_UNLCK)
|
2014-12-05 14:42:55 +01:00
|
|
|
ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
|
2002-03-20 23:50:38 +01:00
|
|
|
else
|
2014-12-05 14:42:55 +01:00
|
|
|
ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
|
2002-03-20 23:50:38 +01:00
|
|
|
/* FIXME - what about error settings? */
|
2014-12-05 14:42:55 +01:00
|
|
|
release_ioinfo(info);
|
2002-03-20 23:50:38 +01:00
|
|
|
return ret ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
2010-11-01 21:49:22 +01:00
|
|
|
* _fseeki64 (MSVCRT.@)
|
2004-03-16 20:17:11 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fseeki64(FILE* file, __int64 offset, int whence)
|
2014-10-15 16:47:07 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fseeki64_nolock(file, offset, whence);
|
|
|
|
_unlock_file(file);
|
2014-10-15 16:47:07 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fseeki64_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fseeki64_nolock(FILE* file, __int64 offset, int whence)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
2011-05-20 13:22:39 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
|
|
|
|
whence = SEEK_SET;
|
2020-12-03 19:11:20 +01:00
|
|
|
offset += _ftelli64_nolock(file);
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
2013-01-10 11:44:02 +01:00
|
|
|
|
2015-01-23 17:27:23 +01:00
|
|
|
/* Flush output if needed */
|
|
|
|
msvcrt_flush_buffer(file);
|
2004-03-16 20:17:11 +01:00
|
|
|
/* Reset direction of i/o */
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IORW) {
|
|
|
|
file->_flag &= ~(_IOREAD|_IOWRT);
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
2008-04-08 13:53:53 +02:00
|
|
|
/* Clear end of file flag */
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~_IOEOF;
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = (_lseeki64(file->_file,offset,whence) == -1)?-1:0;
|
2011-05-20 13:22:39 +02:00
|
|
|
|
|
|
|
return ret;
|
2010-11-01 21:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fseek (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fseek(FILE* file, __msvcrt_long offset, int whence)
|
2010-11-01 21:49:22 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _fseeki64( file, offset, whence );
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
|
|
|
|
2014-10-15 16:47:46 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _fseek_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fseek_nolock(FILE* file, __msvcrt_long offset, int whence)
|
2014-10-15 16:47:46 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _fseeki64_nolock( file, offset, whence );
|
2014-10-15 16:47:46 +02:00
|
|
|
}
|
|
|
|
|
2005-03-31 21:06:10 +02:00
|
|
|
/*********************************************************************
|
2013-11-05 10:04:19 +01:00
|
|
|
* _chsize_s (MSVCRT.@)
|
2005-03-31 21:06:10 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _chsize_s(int fd, __int64 size)
|
2005-03-31 21:06:10 +02:00
|
|
|
{
|
2014-12-05 14:43:07 +01:00
|
|
|
ioinfo *info;
|
2013-11-05 10:04:19 +01:00
|
|
|
__int64 cur, pos;
|
2005-03-31 21:06:10 +02:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
2013-11-05 10:04:19 +01:00
|
|
|
TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(size >= 0)) return EINVAL;
|
2005-03-31 21:06:10 +02:00
|
|
|
|
|
|
|
|
2014-12-05 14:43:07 +01:00
|
|
|
info = get_ioinfo(fd);
|
|
|
|
if (info->handle != INVALID_HANDLE_VALUE)
|
2005-03-31 21:06:10 +02:00
|
|
|
{
|
|
|
|
/* save the current file pointer */
|
2020-12-03 19:11:20 +01:00
|
|
|
cur = _lseeki64(fd, 0, SEEK_CUR);
|
2005-03-31 21:06:10 +02:00
|
|
|
if (cur >= 0)
|
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
pos = _lseeki64(fd, size, SEEK_SET);
|
2005-03-31 21:06:10 +02:00
|
|
|
if (pos >= 0)
|
|
|
|
{
|
2014-12-05 14:43:07 +01:00
|
|
|
ret = SetEndOfFile(info->handle);
|
2005-03-31 21:06:10 +02:00
|
|
|
if (!ret) msvcrt_set_errno(GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the file pointer */
|
2020-12-03 19:11:20 +01:00
|
|
|
_lseeki64(fd, cur, SEEK_SET);
|
2005-03-31 21:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 14:43:07 +01:00
|
|
|
release_ioinfo(info);
|
2020-12-01 13:51:23 +01:00
|
|
|
return ret ? 0 : *_errno();
|
2013-11-05 10:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _chsize (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _chsize(int fd, __msvcrt_long size)
|
2013-11-05 10:04:19 +01:00
|
|
|
{
|
|
|
|
/* _chsize_s returns errno on failure but _chsize should return -1 */
|
2020-12-03 19:11:20 +01:00
|
|
|
return _chsize_s( fd, size ) == 0 ? 0 : -1;
|
2005-03-31 21:06:10 +02:00
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* clearerr (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
void CDECL clearerr(FILE* file)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
|
|
|
TRACE(":file (%p) fd (%d)\n",file,file->_file);
|
2011-05-20 13:22:29 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~(_IOERR | _IOEOF);
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:30:12 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* clearerr_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL clearerr_s(FILE* file)
|
2017-09-26 00:30:12 +02:00
|
|
|
{
|
|
|
|
TRACE(":file (%p)\n",file);
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
|
2017-09-26 00:30:12 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~(_IOERR | _IOEOF);
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2017-09-26 00:30:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* rewind (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
void CDECL rewind(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":file (%p) fd (%d)\n",file,file->_file);
|
2011-05-20 13:22:39 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
_fseek_nolock(file, 0L, SEEK_SET);
|
|
|
|
clearerr(file);
|
|
|
|
_unlock_file(file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
static int msvcrt_get_flags(const wchar_t* mode, int *open_flags, int* stream_flags)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
2020-12-03 19:10:54 +01:00
|
|
|
int plus = wcschr(mode, '+') != NULL;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2013-01-10 11:42:15 +01:00
|
|
|
TRACE("%s\n", debugstr_w(mode));
|
|
|
|
|
2013-01-14 10:24:18 +01:00
|
|
|
while(*mode == ' ') mode++;
|
|
|
|
|
2004-05-12 02:12:26 +02:00
|
|
|
switch(*mode++)
|
|
|
|
{
|
|
|
|
case 'R': case 'r':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags = plus ? _O_RDWR : _O_RDONLY;
|
2020-12-02 00:53:28 +01:00
|
|
|
*stream_flags = plus ? _IORW : _IOREAD;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
|
|
|
case 'W': case 'w':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
|
2020-12-02 00:53:28 +01:00
|
|
|
*stream_flags = plus ? _IORW : _IOWRT;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
|
|
|
case 'A': case 'a':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
|
2020-12-02 00:53:28 +01:00
|
|
|
*stream_flags = plus ? _IORW : _IOWRT;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT(0, EINVAL);
|
2004-05-12 02:12:26 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-18 11:03:13 +01:00
|
|
|
*stream_flags |= MSVCRT__commode;
|
|
|
|
|
2013-01-10 11:42:15 +01:00
|
|
|
while (*mode && *mode!=',')
|
2004-05-12 02:12:26 +02:00
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'B': case 'b':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_BINARY;
|
|
|
|
*open_flags &= ~_O_TEXT;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
2012-03-27 14:43:55 +02:00
|
|
|
case 't':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_TEXT;
|
|
|
|
*open_flags &= ~_O_BINARY;
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
2012-03-27 14:43:55 +02:00
|
|
|
case 'D':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_TEMPORARY;
|
2012-03-27 14:43:55 +02:00
|
|
|
break;
|
|
|
|
case 'T':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_SHORT_LIVED;
|
2012-03-27 14:43:55 +02:00
|
|
|
break;
|
2013-01-18 11:03:13 +01:00
|
|
|
case 'c':
|
|
|
|
*stream_flags |= MSVCRT__IOCOMMIT;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*stream_flags &= ~MSVCRT__IOCOMMIT;
|
|
|
|
break;
|
2013-01-25 11:39:05 +01:00
|
|
|
case 'N':
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_NOINHERIT;
|
2013-01-25 11:39:05 +01:00
|
|
|
break;
|
2004-05-12 02:12:26 +02:00
|
|
|
case '+':
|
2010-01-16 17:21:48 +01:00
|
|
|
case ' ':
|
2013-01-14 10:24:18 +01:00
|
|
|
case 'a':
|
|
|
|
case 'w':
|
2004-05-12 02:12:26 +02:00
|
|
|
break;
|
2013-01-25 11:38:48 +01:00
|
|
|
case 'S':
|
2021-08-04 13:29:34 +02:00
|
|
|
if (!(*open_flags & _O_RANDOM))
|
|
|
|
*open_flags |= _O_SEQUENTIAL;
|
|
|
|
break;
|
2013-01-25 11:38:48 +01:00
|
|
|
case 'R':
|
2021-08-04 13:29:34 +02:00
|
|
|
if (!(*open_flags & _O_SEQUENTIAL))
|
|
|
|
*open_flags |= _O_RANDOM;
|
2013-01-25 11:38:48 +01:00
|
|
|
break;
|
2004-05-12 02:12:26 +02:00
|
|
|
default:
|
2013-11-01 12:12:06 +01:00
|
|
|
ERR("incorrect mode flag: %c\n", mode[-1]);
|
|
|
|
break;
|
2013-01-10 11:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(*mode == ',')
|
|
|
|
{
|
|
|
|
mode++;
|
|
|
|
while(*mode == ' ') mode++;
|
2020-12-03 19:10:54 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(!wcsncmp(L"ccs", mode, 3)))
|
2013-01-10 11:42:15 +01:00
|
|
|
return -1;
|
2020-11-27 14:16:53 +01:00
|
|
|
mode += 3;
|
2013-01-10 11:42:15 +01:00
|
|
|
while(*mode == ' ') mode++;
|
|
|
|
if(!MSVCRT_CHECK_PMT(*mode == '='))
|
|
|
|
return -1;
|
|
|
|
mode++;
|
|
|
|
while(*mode == ' ') mode++;
|
|
|
|
|
2020-12-03 19:10:54 +01:00
|
|
|
if(!_wcsnicmp(L"utf-8", mode, 5))
|
2013-01-10 11:42:15 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_U8TEXT;
|
2020-11-27 14:16:53 +01:00
|
|
|
mode += 5;
|
2013-01-10 11:42:15 +01:00
|
|
|
}
|
2020-12-03 19:10:54 +01:00
|
|
|
else if(!_wcsnicmp(L"utf-16le", mode, 8))
|
2013-01-10 11:42:15 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_U16TEXT;
|
2020-11-27 14:16:53 +01:00
|
|
|
mode += 8;
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
2020-12-03 19:10:54 +01:00
|
|
|
else if(!_wcsnicmp(L"unicode", mode, 7))
|
2013-01-10 11:42:15 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
*open_flags |= _O_WTEXT;
|
2020-11-27 14:16:53 +01:00
|
|
|
mode += 7;
|
2013-01-10 11:42:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT(0, EINVAL);
|
2013-01-10 11:42:15 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*mode == ' ') mode++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(*mode == 0))
|
|
|
|
return -1;
|
2004-05-12 02:12:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fdopen (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE* CDECL _fdopen(int fd, const char *mode)
|
2009-01-19 11:41:15 +01:00
|
|
|
{
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *ret;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *modeW = NULL;
|
2009-01-19 11:41:15 +01:00
|
|
|
|
|
|
|
if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wfdopen(fd, modeW);
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
free(modeW);
|
2009-01-19 11:41:15 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wfdopen (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE* CDECL _wfdopen(int fd, const wchar_t *mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
int open_flags, stream_flags;
|
2020-12-01 13:51:39 +01:00
|
|
|
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;
|
|
|
|
}
|
2009-01-19 11:41:15 +01:00
|
|
|
else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
|
2004-12-14 12:59:43 +01:00
|
|
|
UNLOCK_FILES();
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _filelength (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__msvcrt_long CDECL _filelength(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
LONG curPos = _lseek(fd, 0, SEEK_CUR);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (curPos != -1)
|
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
LONG endPos = _lseek(fd, 0, SEEK_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
if (endPos != -1)
|
|
|
|
{
|
|
|
|
if (endPos != curPos)
|
2020-12-03 19:11:20 +01: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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__int64 CDECL _filelengthi64(int fd)
|
2004-01-13 06:45:05 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
__int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
|
2004-01-13 06:45:05 +01:00
|
|
|
if (curPos != -1)
|
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
__int64 endPos = _lseeki64(fd, 0, SEEK_END);
|
2004-01-13 06:45:05 +01:00
|
|
|
if (endPos != -1)
|
|
|
|
{
|
|
|
|
if (endPos != curPos)
|
2020-12-03 19:11:20 +01:00
|
|
|
_lseeki64(fd, curPos, SEEK_SET);
|
2004-01-13 06:45:05 +01:00
|
|
|
return endPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fileno (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fileno(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
|
2011-11-15 15:25:42 +01:00
|
|
|
return file->_file;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2007-02-14 11:26:42 +01:00
|
|
|
* _fstat64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fstat64(int fd, struct _stat64* buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-12-05 14:43:17 +01:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
DWORD dw;
|
2007-02-16 08:19:34 +01:00
|
|
|
DWORD type;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-12-05 14:43:17 +01:00
|
|
|
TRACE(":fd (%d) stat (%p)\n", fd, buf);
|
|
|
|
if (info->handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
2014-12-05 14:43:17 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
WARN(":failed-NULL buf\n");
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(ERROR_INVALID_PARAMETER);
|
2014-12-05 14:43:17 +01:00
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
memset(buf, 0, sizeof(struct _stat64));
|
2014-12-05 14:43:17 +01:00
|
|
|
type = GetFileType(info->handle);
|
2007-02-16 08:19:34 +01:00
|
|
|
if (type == FILE_TYPE_PIPE)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2007-02-16 08:19:34 +01:00
|
|
|
buf->st_dev = buf->st_rdev = fd;
|
2020-12-04 15:50:17 +01:00
|
|
|
buf->st_mode = _S_IFIFO;
|
2007-02-16 08:19:34 +01:00
|
|
|
buf->st_nlink = 1;
|
|
|
|
}
|
|
|
|
else if (type == FILE_TYPE_CHAR)
|
|
|
|
{
|
|
|
|
buf->st_dev = buf->st_rdev = fd;
|
2020-12-04 15:50:17 +01:00
|
|
|
buf->st_mode = _S_IFCHR;
|
2007-02-16 08:19:34 +01:00
|
|
|
buf->st_nlink = 1;
|
|
|
|
}
|
|
|
|
else /* FILE_TYPE_DISK etc. */
|
|
|
|
{
|
2021-03-17 10:29:28 +01:00
|
|
|
FILE_BASIC_INFORMATION basic_info;
|
|
|
|
FILE_STANDARD_INFORMATION std_info;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if ((status = NtQueryInformationFile( info->handle, &io, &basic_info, sizeof(basic_info), FileBasicInformation )) ||
|
|
|
|
(status = NtQueryInformationFile( info->handle, &io, &std_info, sizeof(std_info), FileStandardInformation )))
|
2007-02-16 08:19:34 +01:00
|
|
|
{
|
2021-03-17 10:29:28 +01:00
|
|
|
WARN(":failed-error %x\n",status);
|
2007-02-16 08:19:34 +01:00
|
|
|
msvcrt_set_errno(ERROR_INVALID_PARAMETER);
|
2014-12-05 14:43:17 +01:00
|
|
|
release_ioinfo(info);
|
2007-02-16 08:19:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 15:50:17 +01:00
|
|
|
buf->st_mode = _S_IFREG | 0444;
|
2021-03-17 10:29:28 +01:00
|
|
|
if (!(basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY))
|
2008-12-09 15:19:20 +01:00
|
|
|
buf->st_mode |= 0222;
|
2021-03-17 10:29:28 +01:00
|
|
|
buf->st_size = std_info.EndOfFile.QuadPart;
|
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&basic_info.LastAccessTime, &dw);
|
2007-02-16 08:19:34 +01:00
|
|
|
buf->st_atime = dw;
|
2021-03-17 10:29:28 +01:00
|
|
|
RtlTimeToSecondsSince1970((LARGE_INTEGER *)&basic_info.LastWriteTime, &dw);
|
2007-02-16 08:19:34 +01:00
|
|
|
buf->st_mtime = buf->st_ctime = dw;
|
2021-03-17 10:29:28 +01:00
|
|
|
buf->st_nlink = std_info.NumberOfLinks;
|
|
|
|
TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",basic_info.FileAttributes, buf->st_mode);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2014-12-05 14:43:17 +01:00
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 11:26:42 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fstati64 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fstati64(int fd, struct _stati64* buf)
|
2007-02-14 11:26:42 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2007-02-14 11:26:42 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fstat64(fd, &buf64);
|
2007-02-14 11:26:42 +01:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stati64(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:44:34 +01:00
|
|
|
/*********************************************************************
|
2020-12-03 19:11:20 +01:00
|
|
|
* _fstat (MSVCRT.@)
|
2002-11-25 03:44:34 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fstat(int fd, struct _stat* buf)
|
2002-11-25 03:44:34 +01:00
|
|
|
{ int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2002-11-25 03:44:34 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fstat64(fd, &buf64);
|
2002-11-25 03:44:34 +01:00
|
|
|
if (!ret)
|
2007-02-14 11:26:42 +01:00
|
|
|
msvcrt_stat64_to_stat(&buf64, buf);
|
2002-11-25 03:44:34 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-21 14:32:40 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _fstat32 (MSVCR80.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fstat32(int fd, struct _stat32* buf)
|
2013-04-30 14:29:59 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2013-04-30 14:29:59 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fstat64(fd, &buf64);
|
2013-04-30 14:29:59 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat32(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:37:24 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _fstat32i64 (MSVCR80.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fstat32i64(int fd, struct _stat32i64* buf)
|
2016-05-12 22:37:24 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2016-05-12 22:37:24 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fstat64(fd, &buf64);
|
2016-05-12 22:37:24 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat32i64(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-21 14:32:40 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _fstat64i32 (MSVCR80.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fstat64i32(int fd, struct _stat64i32* buf)
|
2013-04-30 14:29:59 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2013-04-30 14:29:59 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fstat64(fd, &buf64);
|
2013-04-30 14:29:59 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat64i32(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2009-05-23 11:10:05 +02:00
|
|
|
* _futime64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-01 13:51:47 +01:00
|
|
|
int CDECL _futime64(int fd, struct __utimbuf64 *t)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2015-06-03 13:25:40 +02:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
FILETIME at, wt;
|
|
|
|
|
|
|
|
if (!t)
|
|
|
|
{
|
2020-12-03 19:10:50 +01:00
|
|
|
time_to_filetime( _time64(NULL), &at );
|
2009-05-23 11:10:05 +02:00
|
|
|
wt = at;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-23 11:10:05 +02:00
|
|
|
time_to_filetime( t->actime, &at );
|
|
|
|
time_to_filetime( t->modtime, &wt );
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2015-06-03 13:25:40 +02:00
|
|
|
if (!SetFileTime(info->handle, NULL, &at, &wt))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2015-06-03 13:25:40 +02:00
|
|
|
release_ioinfo(info);
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1 ;
|
|
|
|
}
|
2015-06-03 13:25:40 +02:00
|
|
|
release_ioinfo(info);
|
2001-01-11 00:59:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-23 11:10:05 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _futime32 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:47 +01:00
|
|
|
int CDECL _futime32(int fd, struct __utimbuf32 *t)
|
2009-05-23 11:10:05 +02:00
|
|
|
{
|
2011-06-20 06:27:41 +02:00
|
|
|
if (t)
|
|
|
|
{
|
2020-12-01 13:51:47 +01:00
|
|
|
struct __utimbuf64 t64;
|
2011-06-20 06:27:41 +02:00
|
|
|
t64.actime = t->actime;
|
|
|
|
t64.modtime = t->modtime;
|
|
|
|
return _futime64( fd, &t64 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return _futime64( fd, NULL );
|
2009-05-23 11:10:05 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _get_osfhandle (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
intptr_t CDECL _get_osfhandle(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2015-06-03 13:25:47 +02:00
|
|
|
HANDLE hand = get_ioinfo_nolock(fd)->handle;
|
2002-10-25 05:12:01 +02:00
|
|
|
TRACE(":fd (%d) handle (%p)\n",fd,hand);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2015-06-03 13:25:47 +02:00
|
|
|
if(hand == INVALID_HANDLE_VALUE)
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2020-11-27 14:17:05 +01:00
|
|
|
return (intptr_t)hand;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-24 12:20:42 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _mktemp_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _mktemp_s(char *pattern, size_t size)
|
2013-07-24 12:20:42 +02:00
|
|
|
{
|
|
|
|
DWORD len, xno, id;
|
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(pattern!=NULL))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-07-24 12:20:42 +02:00
|
|
|
|
|
|
|
for(len=0; len<size; len++)
|
|
|
|
if(!pattern[len])
|
|
|
|
break;
|
|
|
|
if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
|
|
|
|
if(size)
|
|
|
|
pattern[0] = 0;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-07-24 12:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(xno=1; xno<=6; xno++)
|
|
|
|
if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-07-24 12:20:42 +02:00
|
|
|
|
|
|
|
id = GetCurrentProcessId();
|
|
|
|
for(xno=1; xno<6; xno++) {
|
|
|
|
pattern[len-xno] = id%10 + '0';
|
|
|
|
id /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
|
|
|
|
if(GetFileAttributesA(pattern) == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern[0] = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EEXIST;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EEXIST;
|
2013-07-24 12:20:42 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _mktemp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
char * CDECL _mktemp(char *pattern)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int numX = 0;
|
|
|
|
char *retVal = pattern;
|
|
|
|
int id;
|
|
|
|
char letter = 'a';
|
|
|
|
|
2013-07-24 12:20:29 +02:00
|
|
|
if(!pattern)
|
|
|
|
return NULL;
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
while(*pattern)
|
|
|
|
numX = (*pattern++ == 'X')? numX + 1 : 0;
|
2013-07-24 12:20:29 +02:00
|
|
|
if (numX < 6)
|
2001-01-11 00:59:25 +01:00
|
|
|
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++;
|
2013-07-24 12:20:29 +02:00
|
|
|
if (GetFileAttributesA(retVal) == INVALID_FILE_ATTRIBUTES)
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-24 12:20:52 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _wmktemp_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wmktemp_s(wchar_t *pattern, size_t size)
|
2013-07-24 12:20:52 +02:00
|
|
|
{
|
|
|
|
DWORD len, xno, id;
|
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(pattern!=NULL))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-07-24 12:20:52 +02:00
|
|
|
|
|
|
|
for(len=0; len<size; len++)
|
|
|
|
if(!pattern[len])
|
|
|
|
break;
|
|
|
|
if(!MSVCRT_CHECK_PMT(len!=size && len>=6)) {
|
|
|
|
if(size)
|
|
|
|
pattern[0] = 0;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-07-24 12:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(xno=1; xno<=6; xno++)
|
|
|
|
if(!MSVCRT_CHECK_PMT(pattern[len-xno] == 'X'))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-07-24 12:20:52 +02:00
|
|
|
|
|
|
|
id = GetCurrentProcessId();
|
|
|
|
for(xno=1; xno<6; xno++) {
|
|
|
|
pattern[len-xno] = id%10 + '0';
|
|
|
|
id /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(pattern[len-6]='a'; pattern[len-6]<='z'; pattern[len-6]++) {
|
|
|
|
if(GetFileAttributesW(pattern) == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern[0] = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EEXIST;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EEXIST;
|
2013-07-24 12:20:52 +02:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wmktemp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wchar_t * CDECL _wmktemp(wchar_t *pattern)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
int numX = 0;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *retVal = pattern;
|
2001-01-22 03:21:54 +01:00
|
|
|
int id;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t letter = 'a';
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2013-07-24 12:20:29 +02:00
|
|
|
if(!pattern)
|
|
|
|
return NULL;
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
while(*pattern)
|
2002-12-19 05:21:30 +01:00
|
|
|
numX = (*pattern++ == 'X')? numX + 1 : 0;
|
2013-07-24 12:20:29 +02:00
|
|
|
if (numX < 6)
|
2001-01-22 03:21:54 +01:00
|
|
|
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
|
|
|
|
{
|
2013-07-24 12:20:29 +02:00
|
|
|
if (GetFileAttributesW(retVal) == INVALID_FILE_ATTRIBUTES)
|
2001-01-22 03:21:54 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-12-01 13:52:43 +01:00
|
|
|
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
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_APPEND) wxflags |= WX_APPEND;
|
|
|
|
if (oflags & _O_BINARY) {/* Nothing to do */}
|
|
|
|
else if (oflags & _O_TEXT) wxflags |= WX_TEXT;
|
|
|
|
else if (oflags & _O_WTEXT) wxflags |= WX_TEXT;
|
|
|
|
else if (oflags & _O_U16TEXT) wxflags |= WX_TEXT;
|
|
|
|
else if (oflags & _O_U8TEXT) wxflags |= WX_TEXT;
|
2020-12-01 13:52:43 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int fmode;
|
|
|
|
_get_fmode(&fmode);
|
2020-12-04 15:50:17 +01:00
|
|
|
if (!(fmode & _O_BINARY)) wxflags |= WX_TEXT; /* default to TEXT*/
|
2020-12-01 13:52:43 +01:00
|
|
|
}
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
|
|
|
|
|
|
|
|
if ((unsupp = oflags & ~(_O_BINARY | _O_TEXT | _O_APPEND | _O_TRUNC | _O_EXCL | _O_CREAT |
|
|
|
|
_O_RDWR | _O_WRONLY | _O_TEMPORARY | _O_NOINHERIT | _O_SEQUENTIAL |
|
|
|
|
_O_RANDOM | _O_SHORT_LIVED | _O_WTEXT | _O_U16TEXT | _O_U8TEXT)))
|
2005-01-10 15:28:21 +01:00
|
|
|
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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _pipe(int *pfds, unsigned int psize, int textmode)
|
2004-12-09 17:51:26 +01:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
HANDLE readHandle, writeHandle;
|
|
|
|
|
|
|
|
if (!pfds)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2004-12-09 17:51:26 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
2020-12-04 15:50:17 +01:00
|
|
|
sa.bInheritHandle = !(textmode & _O_NOINHERIT);
|
2004-12-09 17:51:26 +01:00
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
|
|
|
|
{
|
|
|
|
unsigned int wxflags = split_oflags(textmode);
|
|
|
|
int fd;
|
|
|
|
|
2013-01-10 11:43:37 +01:00
|
|
|
fd = msvcrt_alloc_fd(readHandle, wxflags|WX_PIPE);
|
2004-12-09 17:51:26 +01:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
pfds[0] = fd;
|
2013-01-10 11:43:37 +01:00
|
|
|
fd = msvcrt_alloc_fd(writeHandle, wxflags|WX_PIPE);
|
2004-12-09 17:51:26 +01:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
pfds[1] = fd;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
_close(pfds[0]);
|
2004-12-09 17:51:26 +01:00
|
|
|
CloseHandle(writeHandle);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EMFILE;
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CloseHandle(readHandle);
|
|
|
|
CloseHandle(writeHandle);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EMFILE;
|
2004-12-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-10 11:42:29 +01:00
|
|
|
static int check_bom(HANDLE h, int oflags, BOOL seek)
|
|
|
|
{
|
|
|
|
char bom[sizeof(utf8_bom)];
|
|
|
|
DWORD r;
|
|
|
|
|
|
|
|
if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
|
|
|
|
return oflags;
|
|
|
|
|
|
|
|
if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
|
2020-12-04 15:50:17 +01:00
|
|
|
oflags = (oflags & ~(_O_WTEXT | _O_U16TEXT)) | _O_U8TEXT;
|
2013-01-10 11:42:29 +01:00
|
|
|
}else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
|
|
|
|
if (seek && r>2)
|
|
|
|
SetFilePointer(h, 2, NULL, FILE_BEGIN);
|
2020-12-04 15:50:17 +01:00
|
|
|
oflags = (oflags & ~(_O_WTEXT | _O_U8TEXT)) | _O_U16TEXT;
|
2013-01-10 11:42:29 +01:00
|
|
|
}else if (seek) {
|
|
|
|
SetFilePointer(h, 0, NULL, FILE_BEGIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return oflags;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2016-10-29 19:22:27 +02:00
|
|
|
* _wsopen_dispatch (UCRTBASE.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wsopen_dispatch( const wchar_t* path, int oflags, int shflags, int pmode,
|
2016-10-29 19:22:27 +02:00
|
|
|
int *fd, int secure )
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2009-01-19 11:41:15 +01:00
|
|
|
DWORD access = 0, creation = 0, attrib;
|
2011-01-26 23:56:30 +01:00
|
|
|
SECURITY_ATTRIBUTES sa;
|
2014-07-31 16:51:52 +02:00
|
|
|
DWORD sharing, type;
|
2011-01-26 23:56:30 +01:00
|
|
|
int wxflag;
|
2009-01-19 11:41:15 +01:00
|
|
|
HANDLE hand;
|
|
|
|
|
2016-10-29 19:22:27 +02:00
|
|
|
TRACE("path: (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x fd*: %p secure: %d\n",
|
|
|
|
debugstr_w(path), oflags, shflags, pmode, fd, secure);
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2011-01-26 23:56:30 +01:00
|
|
|
*fd = -1;
|
2009-01-19 11:41:15 +01:00
|
|
|
wxflag = split_oflags(oflags);
|
2020-12-04 15:50:17 +01:00
|
|
|
switch (oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
|
2009-01-19 11:41:15 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
case _O_RDONLY: access |= GENERIC_READ; break;
|
|
|
|
case _O_WRONLY: access |= GENERIC_WRITE; break;
|
|
|
|
case _O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
|
2009-01-19 11:41:15 +01:00
|
|
|
}
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_CREAT)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
if (secure && !MSVCRT_CHECK_PMT(!(pmode & ~(_S_IREAD | _S_IWRITE))))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_EXCL)
|
2009-01-19 11:41:15 +01:00
|
|
|
creation = CREATE_NEW;
|
2020-12-04 15:50:17 +01:00
|
|
|
else if (oflags & _O_TRUNC)
|
2009-01-19 11:41:15 +01:00
|
|
|
creation = CREATE_ALWAYS;
|
|
|
|
else
|
|
|
|
creation = OPEN_ALWAYS;
|
|
|
|
}
|
2020-12-04 15:50:17 +01:00
|
|
|
else /* no _O_CREAT */
|
2009-01-19 11:41:15 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_TRUNC)
|
2009-01-19 11:41:15 +01:00
|
|
|
creation = TRUNCATE_EXISTING;
|
|
|
|
else
|
|
|
|
creation = OPEN_EXISTING;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-19 11:41:15 +01:00
|
|
|
switch( shflags )
|
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
case _SH_DENYRW:
|
2009-01-19 11:41:15 +01:00
|
|
|
sharing = 0L;
|
|
|
|
break;
|
2020-12-04 15:50:17 +01:00
|
|
|
case _SH_DENYWR:
|
2009-01-19 11:41:15 +01:00
|
|
|
sharing = FILE_SHARE_READ;
|
|
|
|
break;
|
2020-12-04 15:50:17 +01:00
|
|
|
case _SH_DENYRD:
|
2009-01-19 11:41:15 +01:00
|
|
|
sharing = FILE_SHARE_WRITE;
|
|
|
|
break;
|
2020-12-04 15:50:17 +01:00
|
|
|
case _SH_DENYNO:
|
2009-01-19 11:41:15 +01:00
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR( "Unhandled shflags 0x%x\n", shflags );
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2009-01-19 11:41:15 +01:00
|
|
|
}
|
2015-04-16 17:37:10 +02:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (!(pmode & ~MSVCRT_umask & _S_IWRITE))
|
2015-04-16 17:37:10 +02:00
|
|
|
attrib = FILE_ATTRIBUTE_READONLY;
|
|
|
|
else
|
|
|
|
attrib = FILE_ATTRIBUTE_NORMAL;
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_TEMPORARY)
|
2009-01-19 11:41:15 +01:00
|
|
|
{
|
|
|
|
attrib |= FILE_FLAG_DELETE_ON_CLOSE;
|
|
|
|
access |= DELETE;
|
|
|
|
sharing |= FILE_SHARE_DELETE;
|
|
|
|
}
|
|
|
|
|
2021-08-04 13:29:34 +02:00
|
|
|
if (oflags & _O_RANDOM)
|
|
|
|
attrib |= FILE_FLAG_RANDOM_ACCESS;
|
|
|
|
if (oflags & _O_SEQUENTIAL)
|
|
|
|
attrib |= FILE_FLAG_SEQUENTIAL_SCAN;
|
|
|
|
if (oflags & _O_SHORT_LIVED)
|
|
|
|
attrib |= FILE_ATTRIBUTE_TEMPORARY;
|
|
|
|
|
2009-01-19 11:41:15 +01:00
|
|
|
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
2020-12-04 15:50:17 +01:00
|
|
|
sa.bInheritHandle = !(oflags & _O_NOINHERIT);
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if ((oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
|
2013-01-10 11:42:29 +01:00
|
|
|
&& (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
|
|
|
|
&& !(access&GENERIC_READ))
|
|
|
|
{
|
|
|
|
hand = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
&sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (hand != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
oflags = check_bom(hand, oflags, FALSE);
|
|
|
|
CloseHandle(hand);
|
|
|
|
}
|
|
|
|
}
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2013-01-10 11:42:29 +01:00
|
|
|
hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
|
2009-01-19 11:41:15 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE) {
|
|
|
|
WARN(":failed-last error (%d)\n",GetLastError());
|
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2009-01-19 11:41:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
|
2013-01-10 11:42:29 +01:00
|
|
|
{
|
2020-11-26 15:26:27 +01:00
|
|
|
LARGE_INTEGER size = {{0}};
|
|
|
|
|
|
|
|
if ((access & GENERIC_WRITE) && (creation==OPEN_EXISTING || creation==OPEN_ALWAYS))
|
|
|
|
GetFileSizeEx(hand, &size);
|
|
|
|
|
2013-01-10 11:42:29 +01:00
|
|
|
if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
|
|
|
|
|| creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
|
2020-11-26 15:26:27 +01:00
|
|
|
|| ((creation==OPEN_EXISTING || creation==OPEN_ALWAYS) && !size.QuadPart)))
|
2013-01-10 11:42:29 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_U8TEXT)
|
2013-01-10 11:42:29 +01:00
|
|
|
{
|
|
|
|
DWORD written = 0, tmp;
|
|
|
|
|
|
|
|
while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
|
|
|
|
sizeof(utf8_bom)-written, &tmp, NULL))
|
|
|
|
written += tmp;
|
|
|
|
if (written != sizeof(utf8_bom)) {
|
2013-01-29 07:06:07 +01:00
|
|
|
WARN("error writing BOM\n");
|
2013-01-10 11:42:29 +01:00
|
|
|
CloseHandle(hand);
|
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2013-01-10 11:42:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD written = 0, tmp;
|
|
|
|
|
|
|
|
while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
|
|
|
|
sizeof(utf16_bom)-written, &tmp, NULL))
|
|
|
|
written += tmp;
|
|
|
|
if (written != sizeof(utf16_bom))
|
|
|
|
{
|
2013-01-29 07:06:07 +01:00
|
|
|
WARN("error writing BOM\n");
|
2013-01-10 11:42:29 +01:00
|
|
|
CloseHandle(hand);
|
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2013-01-10 11:42:29 +01:00
|
|
|
}
|
2021-01-27 19:07:26 +01:00
|
|
|
oflags |= _O_U16TEXT;
|
2013-01-10 11:42:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (access & GENERIC_READ)
|
|
|
|
oflags = check_bom(hand, oflags, TRUE);
|
|
|
|
}
|
|
|
|
|
2014-07-31 16:51:52 +02:00
|
|
|
type = GetFileType(hand);
|
|
|
|
if (type == FILE_TYPE_CHAR)
|
|
|
|
wxflag |= WX_TTY;
|
|
|
|
else if (type == FILE_TYPE_PIPE)
|
|
|
|
wxflag |= WX_PIPE;
|
|
|
|
|
2011-01-26 23:56:30 +01:00
|
|
|
*fd = msvcrt_alloc_fd(hand, wxflag);
|
2013-01-10 11:42:29 +01:00
|
|
|
if (*fd == -1)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2013-01-10 11:42:29 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_WTEXT)
|
2021-01-27 19:07:26 +01:00
|
|
|
get_ioinfo_nolock(*fd)->exflag |= EF_UNK_UNICODE;
|
|
|
|
|
|
|
|
if (oflags & _O_U16TEXT)
|
2014-11-11 15:58:08 +01:00
|
|
|
get_ioinfo_nolock(*fd)->exflag |= EF_UTF16;
|
2020-12-04 15:50:17 +01:00
|
|
|
else if (oflags & _O_U8TEXT)
|
2014-11-11 15:58:08 +01:00
|
|
|
get_ioinfo_nolock(*fd)->exflag |= EF_UTF8;
|
2011-01-26 23:56:30 +01:00
|
|
|
|
|
|
|
TRACE(":fd (%d) handle (%p)\n", *fd, hand);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-29 19:22:27 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wsopen_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wsopen_s( int *fd, const wchar_t* path, int oflags, int shflags, int pmode )
|
2016-10-29 19:22:27 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _wsopen_dispatch( path, oflags, shflags, pmode, fd, 1 );
|
2016-10-29 19:22:27 +02:00
|
|
|
}
|
|
|
|
|
2011-01-26 23:56:30 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wsopen (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV _wsopen( const wchar_t *path, int oflags, int shflags, ... )
|
2011-01-26 23:56:30 +01:00
|
|
|
{
|
|
|
|
int pmode;
|
|
|
|
int fd;
|
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_CREAT)
|
2011-01-26 23:56:30 +01:00
|
|
|
{
|
|
|
|
__ms_va_list ap;
|
|
|
|
|
|
|
|
__ms_va_start(ap, shflags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
__ms_va_end(ap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pmode = 0;
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
return _wsopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2016-10-29 19:22:27 +02:00
|
|
|
|
2013-01-10 11:42:43 +01:00
|
|
|
/*********************************************************************
|
2016-10-29 19:22:27 +02:00
|
|
|
* _sopen_dispatch (UCRTBASE.@)
|
2013-01-10 11:42:43 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _sopen_dispatch( const char *path, int oflags, int shflags,
|
2016-10-29 19:22:27 +02:00
|
|
|
int pmode, int *fd, int secure)
|
2013-01-10 11:42:43 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *pathW;
|
2013-01-10 11:42:43 +01:00
|
|
|
int ret;
|
|
|
|
|
2014-07-10 15:12:12 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(fd != NULL))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2014-07-10 15:12:12 +02:00
|
|
|
*fd = -1;
|
2013-01-10 11:42:43 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-01-10 11:42:43 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wsopen_dispatch(pathW, oflags, shflags, pmode, fd, secure);
|
2020-12-01 13:51:32 +01:00
|
|
|
free(pathW);
|
2013-01-10 11:42:43 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-29 19:22:27 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _sopen_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _sopen_s( int *fd, const char *path, int oflags, int shflags, int pmode )
|
2016-10-29 19:22:27 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _sopen_dispatch(path, oflags, shflags, pmode, fd, 1);
|
2016-10-29 19:22:27 +02:00
|
|
|
}
|
|
|
|
|
2013-01-10 11:42:43 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _sopen (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV _sopen( const char *path, int oflags, int shflags, ... )
|
2013-01-10 11:42:43 +01:00
|
|
|
{
|
|
|
|
int pmode;
|
|
|
|
int fd;
|
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (oflags & _O_CREAT)
|
2013-01-10 11:42:43 +01:00
|
|
|
{
|
|
|
|
__ms_va_list ap;
|
|
|
|
|
|
|
|
__ms_va_start(ap, shflags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
__ms_va_end(ap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pmode = 0;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
return _sopen_dispatch(path, oflags, shflags, pmode, &fd, 0) ? -1 : fd;
|
2013-01-10 11:42:43 +01:00
|
|
|
}
|
|
|
|
|
2001-03-19 20:13:40 +01:00
|
|
|
/*********************************************************************
|
2002-02-21 21:22:00 +01:00
|
|
|
* _open (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV _open( const char *path, int flags, ... )
|
2002-02-21 21:22:00 +01:00
|
|
|
{
|
2009-01-02 21:46:24 +01:00
|
|
|
__ms_va_list ap;
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (flags & _O_CREAT)
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
|
|
|
int pmode;
|
2009-01-02 21:46:24 +01:00
|
|
|
__ms_va_start(ap, flags);
|
2003-02-19 23:09:33 +01:00
|
|
|
pmode = va_arg(ap, int);
|
2009-01-02 21:46:24 +01:00
|
|
|
__ms_va_end(ap);
|
2020-12-04 15:50:17 +01:00
|
|
|
return _sopen( path, flags, _SH_DENYNO, pmode );
|
2003-02-19 23:09:33 +01:00
|
|
|
}
|
|
|
|
else
|
2020-12-04 15:50:17 +01:00
|
|
|
return _sopen( path, flags, _SH_DENYNO);
|
2002-02-21 21:22:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wopen (MSVCRT.@)
|
2001-03-19 20:13:40 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV _wopen(const wchar_t *path,int flags,...)
|
2001-03-19 20:13:40 +01:00
|
|
|
{
|
2009-01-02 21:46:24 +01:00
|
|
|
__ms_va_list ap;
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if (flags & _O_CREAT)
|
2002-02-21 21:22:00 +01:00
|
|
|
{
|
2009-01-19 11:41:15 +01:00
|
|
|
int pmode;
|
|
|
|
__ms_va_start(ap, flags);
|
|
|
|
pmode = va_arg(ap, int);
|
|
|
|
__ms_va_end(ap);
|
2020-12-04 15:50:17 +01:00
|
|
|
return _wsopen( path, flags, _SH_DENYNO, pmode );
|
2002-02-21 21:22:00 +01:00
|
|
|
}
|
2009-01-19 11:41:15 +01:00
|
|
|
else
|
2020-12-04 15:50:17 +01:00
|
|
|
return _wsopen( path, flags, _SH_DENYNO);
|
2001-03-19 20:13:40 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _creat (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _creat(const char *path, int pmode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
|
2020-12-03 19:11:20 +01:00
|
|
|
return _open(path, flags, pmode);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wcreat (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wcreat(const wchar_t *path, int pmode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
|
2020-12-03 19:11:20 +01:00
|
|
|
return _wopen(path, flags, pmode);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _open_osfhandle (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _open_osfhandle(intptr_t handle, int oflags)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-02-12 13:33:51 +01:00
|
|
|
DWORD flags;
|
2003-05-08 23:02:31 +02:00
|
|
|
int fd;
|
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
/* _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
|
2020-12-04 15:50:17 +01:00
|
|
|
* file, so set the write flag. It also only sets _O_TEXT if it wants
|
|
|
|
* text - it never sets _O_BINARY.
|
2003-03-30 05:06:30 +02:00
|
|
|
*/
|
2007-01-21 16:33:37 +01:00
|
|
|
/* don't let split_oflags() decide the mode if no mode is passed */
|
2020-12-04 15:50:17 +01:00
|
|
|
if (!(oflags & (_O_BINARY | _O_TEXT)))
|
|
|
|
oflags |= _O_BINARY;
|
2003-05-08 23:02:31 +02:00
|
|
|
|
2014-02-12 13:33:51 +01:00
|
|
|
flags = GetFileType((HANDLE)handle);
|
|
|
|
if (flags==FILE_TYPE_UNKNOWN && GetLastError()!=NO_ERROR)
|
|
|
|
{
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags == FILE_TYPE_CHAR)
|
2014-07-31 16:51:52 +02:00
|
|
|
flags = WX_TTY;
|
2014-02-12 13:33:51 +01:00
|
|
|
else if (flags == FILE_TYPE_PIPE)
|
|
|
|
flags = WX_PIPE;
|
|
|
|
else
|
|
|
|
flags = 0;
|
|
|
|
flags |= split_oflags(oflags);
|
|
|
|
|
|
|
|
fd = msvcrt_alloc_fd((HANDLE)handle, flags);
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE(":handle (%Iu) fd (%d) flags 0x%08x\n", handle, fd, flags);
|
2001-01-11 00:59:25 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _rmtmp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _rmtmp(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int num_removed = 0, i;
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *file;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2011-05-19 15:31:22 +02:00
|
|
|
for (i = 3; i < MSVCRT_stream_idx; i++) {
|
|
|
|
file = msvcrt_get_file(i);
|
|
|
|
|
|
|
|
if (file->_tmpfname)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
fclose(file);
|
2001-01-11 00:59:25 +01:00
|
|
|
num_removed++;
|
|
|
|
}
|
2011-05-19 15:31:22 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-10 11:45:39 +01:00
|
|
|
static inline int get_utf8_char_len(char ch)
|
|
|
|
{
|
|
|
|
if((ch&0xf8) == 0xf0)
|
|
|
|
return 4;
|
|
|
|
else if((ch&0xf0) == 0xe0)
|
|
|
|
return 3;
|
|
|
|
else if((ch&0xe0) == 0xc0)
|
|
|
|
return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* (internal) read_utf8
|
|
|
|
*/
|
2020-11-27 14:16:49 +01:00
|
|
|
static int read_utf8(ioinfo *fdinfo, wchar_t *buf, unsigned int count)
|
2013-01-10 11:45:39 +01:00
|
|
|
{
|
|
|
|
HANDLE hand = fdinfo->handle;
|
|
|
|
char min_buf[4], *readbuf, lookahead;
|
|
|
|
DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
|
|
|
|
|
|
|
|
/* make the buffer big enough to hold at least one character */
|
|
|
|
/* read bytes have to fit to output and lookahead buffers */
|
|
|
|
count /= 2;
|
|
|
|
readbuf_size = count < 4 ? 4 : count;
|
2020-12-01 13:51:32 +01:00
|
|
|
if(readbuf_size<=4 || !(readbuf = malloc(readbuf_size))) {
|
2013-01-10 11:45:39 +01:00
|
|
|
readbuf_size = 4;
|
|
|
|
readbuf = min_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fdinfo->lookahead[0] != '\n') {
|
|
|
|
readbuf[pos++] = fdinfo->lookahead[0];
|
|
|
|
fdinfo->lookahead[0] = '\n';
|
|
|
|
|
|
|
|
if(fdinfo->lookahead[1] != '\n') {
|
|
|
|
readbuf[pos++] = fdinfo->lookahead[1];
|
|
|
|
fdinfo->lookahead[1] = '\n';
|
|
|
|
|
|
|
|
if(fdinfo->lookahead[2] != '\n') {
|
|
|
|
readbuf[pos++] = fdinfo->lookahead[2];
|
|
|
|
fdinfo->lookahead[2] = '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: this case is broken in native dll, reading
|
|
|
|
* sometimes fails when small buffer is passed
|
|
|
|
*/
|
|
|
|
if(count < 4) {
|
|
|
|
if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE) {
|
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
|
|
|
return 0;
|
|
|
|
}else {
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}else if(!num_read) {
|
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
|
|
|
return 0;
|
|
|
|
}else {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
char_len = get_utf8_char_len(readbuf[0]);
|
|
|
|
if(char_len>pos) {
|
|
|
|
if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
|
|
|
|
pos += num_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(readbuf[0] == '\n')
|
|
|
|
fdinfo->wxflag |= WX_READNL;
|
|
|
|
else
|
|
|
|
fdinfo->wxflag &= ~WX_READNL;
|
|
|
|
|
|
|
|
if(readbuf[0] == 0x1a) {
|
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(readbuf[0] == '\r') {
|
|
|
|
if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
|
|
|
|
buf[0] = '\r';
|
|
|
|
else if(lookahead == '\n')
|
|
|
|
buf[0] = '\n';
|
|
|
|
else {
|
|
|
|
buf[0] = '\r';
|
2014-07-31 16:51:52 +02:00
|
|
|
if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
|
2013-01-10 11:45:39 +01:00
|
|
|
fdinfo->lookahead[0] = lookahead;
|
|
|
|
else
|
|
|
|
SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_read*2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
|
|
|
|
if(pos) {
|
|
|
|
num_read = 0;
|
|
|
|
}else if(GetLastError() == ERROR_BROKEN_PIPE) {
|
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
2020-12-01 13:51:32 +01:00
|
|
|
if (readbuf != min_buf) free(readbuf);
|
2013-01-10 11:45:39 +01:00
|
|
|
return 0;
|
|
|
|
}else {
|
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-01 13:51:32 +01:00
|
|
|
if (readbuf != min_buf) free(readbuf);
|
2013-01-10 11:45:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}else if(!pos && !num_read) {
|
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
2020-12-01 13:51:32 +01:00
|
|
|
if (readbuf != min_buf) free(readbuf);
|
2013-01-10 11:45:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += num_read;
|
|
|
|
if(readbuf[0] == '\n')
|
|
|
|
fdinfo->wxflag |= WX_READNL;
|
|
|
|
else
|
|
|
|
fdinfo->wxflag &= ~WX_READNL;
|
|
|
|
|
|
|
|
/* Find first byte of last character (may be incomplete) */
|
|
|
|
for(i=pos-1; i>0 && i>pos-4; i--)
|
|
|
|
if((readbuf[i]&0xc0) != 0x80)
|
|
|
|
break;
|
|
|
|
char_len = get_utf8_char_len(readbuf[i]);
|
|
|
|
if(char_len+i <= pos)
|
|
|
|
i += char_len;
|
|
|
|
|
2014-07-31 16:51:52 +02:00
|
|
|
if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
|
2013-01-10 11:45:39 +01:00
|
|
|
if(i < pos)
|
|
|
|
fdinfo->lookahead[0] = readbuf[i];
|
|
|
|
if(i+1 < pos)
|
|
|
|
fdinfo->lookahead[1] = readbuf[i+1];
|
|
|
|
if(i+2 < pos)
|
|
|
|
fdinfo->lookahead[2] = readbuf[i+2];
|
|
|
|
}else if(i < pos) {
|
|
|
|
SetFilePointer(fdinfo->handle, i-pos, NULL, FILE_CURRENT);
|
|
|
|
}
|
|
|
|
pos = i;
|
|
|
|
|
|
|
|
for(i=0, j=0; i<pos; i++) {
|
|
|
|
if(readbuf[i] == 0x1a) {
|
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* strip '\r' if followed by '\n' */
|
|
|
|
if(readbuf[i] == '\r' && i+1==pos) {
|
|
|
|
if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
|
|
|
|
readbuf[j++] = '\r';
|
|
|
|
}else if(lookahead == '\n' && j==0) {
|
|
|
|
readbuf[j++] = '\n';
|
|
|
|
}else {
|
|
|
|
if(lookahead != '\n')
|
|
|
|
readbuf[j++] = '\r';
|
|
|
|
|
2014-07-31 16:51:52 +02:00
|
|
|
if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
|
2013-01-10 11:45:39 +01:00
|
|
|
fdinfo->lookahead[0] = lookahead;
|
|
|
|
else
|
|
|
|
SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
|
|
|
|
}
|
|
|
|
}else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
|
|
|
|
readbuf[j++] = readbuf[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = j;
|
|
|
|
|
|
|
|
if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
|
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-01 13:51:32 +01:00
|
|
|
if (readbuf != min_buf) free(readbuf);
|
2013-01-10 11:45:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
if (readbuf != min_buf) free(readbuf);
|
2013-01-10 11:45:39 +01:00
|
|
|
return num_read*2;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2006-07-30 00:27:59 +02:00
|
|
|
* (internal) read_i
|
2010-01-16 17:24:19 +01:00
|
|
|
*
|
|
|
|
* When reading \r as last character in text mode, read() positions
|
|
|
|
* the file pointer on the \r character while getc() goes on to
|
|
|
|
* the following \n
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2015-06-03 13:25:35 +02:00
|
|
|
static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2013-01-10 11:45:29 +01:00
|
|
|
DWORD num_read, utf16;
|
2013-01-10 11:43:37 +01:00
|
|
|
char *bufstart = buf;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2013-01-10 11:43:37 +01:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
2010-01-28 12:41:37 +01:00
|
|
|
|
2013-01-10 11:43:37 +01:00
|
|
|
if (fdinfo->wxflag & WX_ATEOF) {
|
|
|
|
TRACE("already at EOF, returning 0\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Don't trace small reads, it gets *very* annoying */
|
|
|
|
if (count > 4)
|
2015-06-03 13:25:35 +02:00
|
|
|
TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
|
|
|
|
if (fdinfo->handle == INVALID_HANDLE_VALUE)
|
2013-01-10 11:45:29 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2013-01-10 11:43:37 +01:00
|
|
|
return -1;
|
2013-01-10 11:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
utf16 = (fdinfo->exflag & EF_UTF16) != 0;
|
|
|
|
if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2013-01-10 11:45:29 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2013-01-10 11:45:39 +01:00
|
|
|
if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
|
2015-06-03 13:25:35 +02:00
|
|
|
return read_utf8(fdinfo, buf, count);
|
2013-01-10 11:45:39 +01:00
|
|
|
|
2015-06-03 13:25:35 +02:00
|
|
|
if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
|
2006-07-31 21:12:02 +02:00
|
|
|
{
|
2013-01-10 11:43:37 +01:00
|
|
|
if (fdinfo->lookahead[0] != '\n')
|
|
|
|
{
|
|
|
|
bufstart[0] = fdinfo->lookahead[0];
|
|
|
|
fdinfo->lookahead[0] = '\n';
|
|
|
|
|
2013-01-10 11:45:29 +01:00
|
|
|
if (utf16)
|
|
|
|
{
|
|
|
|
bufstart[1] = fdinfo->lookahead[1];
|
|
|
|
fdinfo->lookahead[1] = '\n';
|
|
|
|
}
|
|
|
|
|
2015-06-03 13:25:35 +02:00
|
|
|
if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
|
2013-01-10 11:45:29 +01:00
|
|
|
num_read += 1+utf16;
|
2013-01-10 11:43:37 +01:00
|
|
|
else
|
2013-01-10 11:45:29 +01:00
|
|
|
num_read = 1+utf16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(utf16 && (num_read&1))
|
|
|
|
{
|
|
|
|
/* msvcr90 uses uninitialized value from the buffer in this case */
|
|
|
|
/* msvcrt ignores additional data */
|
|
|
|
ERR("got odd number of bytes in UTF16 mode\n");
|
|
|
|
num_read--;
|
2013-01-10 11:43:37 +01:00
|
|
|
}
|
|
|
|
|
2010-01-28 12:41:37 +01:00
|
|
|
if (count != 0 && num_read == 0)
|
|
|
|
{
|
2013-01-10 11:43:37 +01:00
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
2010-01-28 12:41:37 +01:00
|
|
|
TRACE(":EOF %s\n",debugstr_an(buf,num_read));
|
|
|
|
}
|
2011-05-19 15:31:12 +02:00
|
|
|
else if (fdinfo->wxflag & WX_TEXT)
|
2006-08-09 01:08:05 +02:00
|
|
|
{
|
2009-01-19 04:19:46 +01:00
|
|
|
DWORD i, j;
|
2013-01-10 11:44:18 +01:00
|
|
|
|
2013-01-10 11:45:29 +01:00
|
|
|
if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
|
2013-01-10 11:44:18 +01:00
|
|
|
fdinfo->wxflag |= WX_READNL;
|
|
|
|
else
|
|
|
|
fdinfo->wxflag &= ~WX_READNL;
|
|
|
|
|
2013-01-10 11:45:29 +01:00
|
|
|
for (i=0, j=0; i<num_read; i+=1+utf16)
|
2006-08-09 01:08:05 +02:00
|
|
|
{
|
2009-01-19 04:19:46 +01:00
|
|
|
/* in text mode, a ctrl-z signals EOF */
|
2013-01-10 11:45:29 +01:00
|
|
|
if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
|
2006-08-09 01:08:05 +02:00
|
|
|
{
|
2013-01-10 11:43:37 +01:00
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
2008-02-09 17:27:35 +01:00
|
|
|
TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
|
2006-08-09 01:08:05 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-01-10 11:43:37 +01:00
|
|
|
|
|
|
|
/* in text mode, strip \r if followed by \n */
|
2013-01-10 11:45:29 +01:00
|
|
|
if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
|
2013-01-10 11:43:37 +01:00
|
|
|
{
|
2013-01-10 11:45:29 +01:00
|
|
|
char lookahead[2];
|
2013-01-10 11:43:37 +01:00
|
|
|
DWORD len;
|
|
|
|
|
2013-01-10 11:45:29 +01:00
|
|
|
lookahead[1] = '\n';
|
2015-06-03 13:25:35 +02:00
|
|
|
if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
|
2013-01-10 11:43:37 +01:00
|
|
|
{
|
2013-01-10 11:45:29 +01:00
|
|
|
if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
|
|
|
|
{
|
2013-01-10 11:43:37 +01:00
|
|
|
bufstart[j++] = '\n';
|
2013-01-10 11:45:29 +01:00
|
|
|
if(utf16) bufstart[j++] = 0;
|
|
|
|
}
|
2013-01-10 11:43:37 +01:00
|
|
|
else
|
|
|
|
{
|
2013-01-10 11:45:29 +01:00
|
|
|
if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
|
|
|
|
{
|
2013-01-10 11:43:37 +01:00
|
|
|
bufstart[j++] = '\r';
|
2013-01-10 11:45:29 +01:00
|
|
|
if(utf16) bufstart[j++] = 0;
|
|
|
|
}
|
2013-01-10 11:43:37 +01:00
|
|
|
|
2014-07-31 16:51:52 +02:00
|
|
|
if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
|
2013-01-10 11:45:29 +01:00
|
|
|
{
|
2013-08-21 13:52:26 +02:00
|
|
|
if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
|
|
|
|
{
|
|
|
|
bufstart[j++] = '\n';
|
|
|
|
if (utf16) bufstart[j++] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fdinfo->lookahead[0] = lookahead[0];
|
|
|
|
fdinfo->lookahead[1] = lookahead[1];
|
|
|
|
}
|
2013-01-10 11:45:29 +01:00
|
|
|
}
|
2013-01-10 11:43:37 +01:00
|
|
|
else
|
2013-01-10 11:45:29 +01:00
|
|
|
SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
|
2013-01-10 11:43:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-01-10 11:45:29 +01:00
|
|
|
{
|
2013-01-10 11:43:37 +01:00
|
|
|
bufstart[j++] = '\r';
|
2013-01-10 11:45:29 +01:00
|
|
|
if(utf16) bufstart[j++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
|
|
|
|
|| (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
|
|
|
|
{
|
|
|
|
bufstart[j++] = bufstart[i];
|
|
|
|
if(utf16) bufstart[j++] = bufstart[i+1];
|
2013-01-10 11:43:37 +01:00
|
|
|
}
|
2006-08-09 01:08:05 +02:00
|
|
|
}
|
2009-01-19 04:19:46 +01:00
|
|
|
num_read = j;
|
2006-08-09 01:08:05 +02:00
|
|
|
}
|
2006-07-31 21:12:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-05-21 16:04:48 +02:00
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE)
|
|
|
|
{
|
|
|
|
TRACE(":end-of-pipe\n");
|
2013-01-10 11:43:37 +01:00
|
|
|
fdinfo->wxflag |= WX_ATEOF;
|
2007-05-21 16:04:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE(":failed-last error (%d)\n",GetLastError());
|
2017-11-15 09:47:33 +01:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2007-05-21 16:04:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-31 21:12:02 +02:00
|
|
|
}
|
2003-10-07 05:33:58 +02:00
|
|
|
|
2013-01-10 11:43:37 +01:00
|
|
|
if (count > 4)
|
|
|
|
TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
|
|
|
|
return num_read;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 00:27:59 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _read (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _read(int fd, void *buf, unsigned int count)
|
2006-07-30 00:27:59 +02:00
|
|
|
{
|
2017-11-15 09:47:33 +01:00
|
|
|
ioinfo *info;
|
|
|
|
int num_read;
|
|
|
|
|
|
|
|
if(fd == MSVCRT_NO_CONSOLE_FD) {
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2017-11-15 09:47:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = get_ioinfo(fd);
|
|
|
|
num_read = read_i(fd, info, buf, count);
|
2015-06-03 13:25:35 +02:00
|
|
|
release_ioinfo(info);
|
|
|
|
return num_read;
|
2006-07-30 00:27:59 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _setmode (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _setmode(int fd,int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2015-06-04 11:24:44 +02:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2020-12-04 15:50:17 +01:00
|
|
|
int ret = info->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY;
|
|
|
|
if(ret==_O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
|
|
|
|
ret = _O_WTEXT;
|
2013-01-10 11:44:48 +01:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if(mode!=_O_TEXT && mode!=_O_BINARY && mode!=_O_WTEXT
|
|
|
|
&& mode!=_O_U16TEXT && mode!=_O_U8TEXT) {
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2015-06-04 11:24:44 +02:00
|
|
|
release_ioinfo(info);
|
2013-01-10 11:44:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:32:53 +02:00
|
|
|
if(info == &MSVCRT___badioinfo) {
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2015-08-03 21:32:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if(mode == _O_BINARY) {
|
2015-06-04 11:24:44 +02:00
|
|
|
info->wxflag &= ~WX_TEXT;
|
|
|
|
info->exflag &= ~(EF_UTF8|EF_UTF16);
|
|
|
|
release_ioinfo(info);
|
2013-01-10 11:44:48 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:24:44 +02:00
|
|
|
info->wxflag |= WX_TEXT;
|
2020-12-04 15:50:17 +01:00
|
|
|
if(mode == _O_TEXT)
|
2015-06-04 11:24:44 +02:00
|
|
|
info->exflag &= ~(EF_UTF8|EF_UTF16);
|
2020-12-04 15:50:17 +01:00
|
|
|
else if(mode == _O_U8TEXT)
|
2015-06-04 11:24:44 +02:00
|
|
|
info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
|
2013-01-10 11:44:48 +01:00
|
|
|
else
|
2015-06-04 11:24:44 +02:00
|
|
|
info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
|
2013-01-10 11:44:48 +01:00
|
|
|
|
2015-06-04 11:24:44 +02:00
|
|
|
release_ioinfo(info);
|
2013-01-10 11:44:48 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2007-02-14 11:26:42 +01:00
|
|
|
* _stat64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stat64(const char* path, struct _stat64 * 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);
|
|
|
|
|
2012-12-27 12:03:51 +01:00
|
|
|
plen = strlen(path);
|
|
|
|
while (plen && path[plen-1]==' ')
|
|
|
|
plen--;
|
|
|
|
|
2018-07-13 16:49:34 +02:00
|
|
|
if (plen==2 && path[1]==':')
|
2012-12-27 12:03:51 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOENT;
|
2012-12-27 12:03:51 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-13 16:49:34 +02:00
|
|
|
#if _MSVCR_VER<140
|
|
|
|
if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOENT;
|
2018-07-13 16:49:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
|
|
|
|
{
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("failed (%d)\n",GetLastError());
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOENT;
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-04 15:50:15 +01:00
|
|
|
memset(buf,0,sizeof(struct _stat64));
|
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
|
|
|
*/
|
2020-12-01 13:52:34 +01:00
|
|
|
if (isalpha(*path)&& (*(path+1)==':'))
|
|
|
|
buf->st_dev = buf->st_rdev = _toupper_l(*path, NULL) - 'A'; /* drive num */
|
2001-01-11 00:59:25 +01:00
|
|
|
else
|
2020-12-03 19:11:02 +01:00
|
|
|
buf->st_dev = buf->st_rdev = _getdrive() - 1;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Dir, or regular file? */
|
2012-12-27 12:03:51 +01:00
|
|
|
if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
2020-12-04 15:50:17 +01:00
|
|
|
mode |= (_S_IFDIR | ALL_S_IEXEC);
|
2001-01-11 00:59:25 +01:00
|
|
|
else
|
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
mode |= _S_IFREG;
|
2001-01-11 00:59:25 +01:00
|
|
|
/* executable? */
|
|
|
|
if (plen > 6 && path[plen-4] == '.') /* shortest exe: "\x.exe" */
|
|
|
|
{
|
2020-12-01 13:52:34 +01:00
|
|
|
unsigned int ext = _tolower_l(path[plen-1], NULL) |
|
|
|
|
(_tolower_l(path[plen-2], NULL) << 8) |
|
|
|
|
(_tolower_l(path[plen-3], NULL) << 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;
|
2009-05-23 11:49:09 +02:00
|
|
|
TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
|
|
|
|
(int)(buf->st_size >> 32),(int)buf->st_size,
|
|
|
|
(int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
|
2001-01-22 03:21:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 11:26:42 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _stati64 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stati64(const char* path, struct _stati64 * buf)
|
2007-02-14 11:26:42 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2007-02-14 11:26:42 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _stat64(path, &buf64);
|
2007-02-14 11:26:42 +01:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stati64(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:44:34 +01:00
|
|
|
/*********************************************************************
|
2020-12-03 19:11:20 +01:00
|
|
|
* _stat (MSVCRT.@)
|
2002-11-25 03:44:34 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stat(const char* path, struct _stat * buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2002-11-25 03:44:34 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _stat64( path, &buf64);
|
2002-11-25 03:44:34 +01:00
|
|
|
if (!ret)
|
2007-02-14 11:26:42 +01:00
|
|
|
msvcrt_stat64_to_stat(&buf64, buf);
|
2002-11-25 03:44:34 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#if _MSVCR_VER >= 80
|
|
|
|
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _stat32 (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stat32(const char *path, struct _stat32 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _stat64(path, &buf64);
|
2014-04-16 16:15:21 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat32(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _stat32i64 (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stat32i64(const char *path, struct _stat32i64 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _stat64(path, &buf64);
|
2014-04-16 16:15:21 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat32i64(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _stat64i32 (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stat64i32(const char* path, struct _stat64i32 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _stat64(path, &buf64);
|
2014-04-16 16:15:21 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat64i32(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#endif /* _MSVCR_VER >= 80 */
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2007-02-14 11:26:42 +01:00
|
|
|
* _wstat64 (MSVCRT.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wstat64(const wchar_t* path, struct _stat64 * 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);
|
|
|
|
|
2020-12-03 19:10:54 +01:00
|
|
|
plen = wcslen(path);
|
2012-12-27 12:03:51 +01:00
|
|
|
while (plen && path[plen-1]==' ')
|
|
|
|
plen--;
|
|
|
|
|
2018-07-13 16:49:34 +02:00
|
|
|
if (plen==2 && path[1]==':')
|
2012-12-27 12:03:51 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOENT;
|
2012-12-27 12:03:51 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-13 16:49:34 +02:00
|
|
|
#if _MSVCR_VER<140
|
|
|
|
if (plen>=2 && path[plen-2]!=':' && (path[plen-1]=='\\' || path[plen-1]=='/'))
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOENT;
|
2018-07-13 16:49:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
|
|
|
|
{
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("failed (%d)\n",GetLastError());
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOENT;
|
2001-01-22 03:21:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-04 15:50:15 +01:00
|
|
|
memset(buf,0,sizeof(struct _stat64));
|
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? */
|
2020-12-03 19:10:54 +01:00
|
|
|
if (iswalpha(*path) && path[1] == ':')
|
|
|
|
buf->st_dev = buf->st_rdev = towupper(*path) - 'A'; /* drive num */
|
2001-01-22 03:21:54 +01:00
|
|
|
else
|
2020-12-03 19:11:02 +01:00
|
|
|
buf->st_dev = buf->st_rdev = _getdrive() - 1;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
/* Dir, or regular file? */
|
2012-12-27 12:03:51 +01:00
|
|
|
if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
2020-12-04 15:50:17 +01:00
|
|
|
mode |= (_S_IFDIR | ALL_S_IEXEC);
|
2001-01-22 03:21:54 +01:00
|
|
|
else
|
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
mode |= _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
|
|
|
{
|
2020-12-03 19:10:54 +01:00
|
|
|
ULONGLONG ext = towlower(path[plen-1]) | (towlower(path[plen-2]) << 16) |
|
|
|
|
((ULONGLONG)towlower(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;
|
2009-05-23 11:49:09 +02:00
|
|
|
TRACE("%d %d 0x%08x%08x %d %d %d\n", buf->st_mode,buf->st_nlink,
|
|
|
|
(int)(buf->st_size >> 32),(int)buf->st_size,
|
|
|
|
(int)buf->st_atime,(int)buf->st_mtime,(int)buf->st_ctime);
|
2001-01-11 00:59:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 11:26:42 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wstati64 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wstati64(const wchar_t* path, struct _stati64 * buf)
|
2007-02-14 11:26:42 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2007-02-14 11:26:42 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wstat64(path, &buf64);
|
2007-02-14 11:26:42 +01:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stati64(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-01-13 06:45:05 +01:00
|
|
|
/*********************************************************************
|
2020-12-03 19:11:20 +01:00
|
|
|
* _wstat (MSVCRT.@)
|
2004-01-13 06:45:05 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wstat(const wchar_t* path, struct _stat * buf)
|
2004-01-13 06:45:05 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2004-01-13 06:45:05 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wstat64( path, &buf64 );
|
2007-02-14 11:26:42 +01:00
|
|
|
if (!ret) msvcrt_stat64_to_stat(&buf64, buf);
|
2004-01-13 06:45:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#if _MSVCR_VER >= 80
|
|
|
|
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _wstat32 (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wstat32(const wchar_t *path, struct _stat32 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wstat64(path, &buf64);
|
2014-04-16 16:15:21 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat32(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _wstat32i64 (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wstat32i64(const wchar_t *path, struct _stat32i64 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wstat64(path, &buf64);
|
2014-04-16 16:15:21 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat32i64(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _wstat64i32 (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wstat64i32(const wchar_t *path, struct _stat64i32 *buf)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-12-03 19:11:20 +01:00
|
|
|
struct _stat64 buf64;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wstat64(path, &buf64);
|
2014-04-16 16:15:21 +02:00
|
|
|
if (!ret)
|
|
|
|
msvcrt_stat64_to_stat64i32(&buf64, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#endif /* _MSVCR_VER >= 80 */
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _tell (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__msvcrt_long CDECL _tell(int fd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01: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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
__int64 CDECL _telli64(int fd)
|
2004-01-13 06:45:05 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _lseeki64(fd, 0, SEEK_CUR);
|
2004-01-13 06:45:05 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _tempnam (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
char * CDECL _tempnam(const char *dir, const char *prefix)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char tmpbuf[MAX_PATH];
|
2020-12-02 00:53:33 +01:00
|
|
|
const char *tmp_dir = getenv("TMP");
|
2006-01-18 14:22:27 +01:00
|
|
|
|
|
|
|
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);
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strdup(tmpbuf);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("failed (%d)\n",GetLastError());
|
2001-01-22 03:21:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wtempnam (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wchar_t * CDECL _wtempnam(const wchar_t *dir, const wchar_t *prefix)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t tmpbuf[MAX_PATH];
|
2020-12-02 00:53:33 +01:00
|
|
|
const wchar_t *tmp_dir = _wgetenv(L"TMP");
|
2012-01-06 21:24:06 +01:00
|
|
|
|
|
|
|
if (tmp_dir) dir = tmp_dir;
|
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);
|
2020-12-03 19:10:54 +01:00
|
|
|
return _wcsdup(tmpbuf);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE("failed (%d)\n",GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _umask (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2009-05-23 11:10:05 +02:00
|
|
|
* _utime64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-01 13:51:47 +01:00
|
|
|
int CDECL _utime64(const char* path, struct __utimbuf64 *t)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
int fd = _open(path, _O_WRONLY | _O_BINARY);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
2009-05-23 11:10:05 +02:00
|
|
|
int retVal = _futime64(fd, t);
|
2020-12-03 19:11:20 +01:00
|
|
|
_close(fd);
|
2001-01-11 00:59:25 +01:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2009-05-23 11:10:05 +02:00
|
|
|
* _utime32 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:47 +01:00
|
|
|
int CDECL _utime32(const char* path, struct __utimbuf32 *t)
|
2009-05-23 11:10:05 +02:00
|
|
|
{
|
2011-06-20 06:27:41 +02:00
|
|
|
if (t)
|
|
|
|
{
|
2020-12-01 13:51:47 +01:00
|
|
|
struct __utimbuf64 t64;
|
2011-06-20 06:27:41 +02:00
|
|
|
t64.actime = t->actime;
|
|
|
|
t64.modtime = t->modtime;
|
|
|
|
return _utime64( path, &t64 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return _utime64( path, NULL );
|
2009-05-23 11:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wutime64 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:47 +01:00
|
|
|
int CDECL _wutime64(const wchar_t* path, struct __utimbuf64 *t)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
int fd = _wopen(path, _O_WRONLY | _O_BINARY);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
2009-05-23 11:10:05 +02:00
|
|
|
int retVal = _futime64(fd, t);
|
2020-12-03 19:11:20 +01:00
|
|
|
_close(fd);
|
2001-01-22 03:21:54 +01:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-23 11:10:05 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _wutime32 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:47 +01:00
|
|
|
int CDECL _wutime32(const wchar_t* path, struct __utimbuf32 *t)
|
2009-05-23 11:10:05 +02:00
|
|
|
{
|
2011-06-20 06:27:41 +02:00
|
|
|
if (t)
|
|
|
|
{
|
2020-12-01 13:51:47 +01:00
|
|
|
struct __utimbuf64 t64;
|
2011-06-20 06:27:41 +02:00
|
|
|
t64.actime = t->actime;
|
|
|
|
t64.modtime = t->modtime;
|
|
|
|
return _wutime64( path, &t64 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return _wutime64( path, NULL );
|
2009-05-23 11:10:05 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _write (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _write(int fd, const void* buf, unsigned int count)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2015-06-04 11:25:00 +02:00
|
|
|
ioinfo *info = get_ioinfo(fd);
|
2013-01-10 11:43:11 +01:00
|
|
|
HANDLE hand = info->handle;
|
2020-08-05 15:07:27 +02:00
|
|
|
DWORD num_written, i;
|
2020-09-18 17:15:08 +02:00
|
|
|
BOOL console = FALSE;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2017-11-15 09:47:37 +01:00
|
|
|
if (hand == INVALID_HANDLE_VALUE || fd == MSVCRT_NO_CONSOLE_FD)
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2015-06-04 11:25:00 +02:00
|
|
|
release_ioinfo(info);
|
2013-01-10 11:43:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2015-06-04 11:25:00 +02:00
|
|
|
release_ioinfo(info);
|
2013-01-10 11:43:11 +01:00
|
|
|
return -1;
|
2003-02-19 23:09:33 +01:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2013-01-10 11:43:11 +01:00
|
|
|
/* If appending, go to EOF */
|
|
|
|
if (info->wxflag & WX_APPEND)
|
2020-12-03 19:11:20 +01:00
|
|
|
_lseek(fd, 0, FILE_END);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2013-01-10 11:43:11 +01:00
|
|
|
if (!(info->wxflag & WX_TEXT))
|
2003-02-19 23:09:33 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
if (!WriteFile(hand, buf, count, &num_written, NULL)
|
|
|
|
|| num_written != count)
|
2015-06-04 11:25:00 +02:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
|
|
|
|
hand, GetLastError());
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
num_written = -1;
|
2015-06-04 11:25:00 +02:00
|
|
|
}
|
2020-08-05 15:07:27 +02:00
|
|
|
|
|
|
|
release_ioinfo(info);
|
|
|
|
return num_written;
|
2003-02-19 23:09:33 +01:00
|
|
|
}
|
2020-08-05 15:07:27 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if (_isatty(fd)) console = VerifyConsoleIoHandle(hand);
|
2020-08-05 15:07:27 +02:00
|
|
|
for (i = 0; i < count;)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2017-11-15 09:47:37 +01:00
|
|
|
const char *s = buf;
|
2020-08-05 15:07:27 +02:00
|
|
|
char lfbuf[2048];
|
2020-08-05 15:08:06 +02:00
|
|
|
DWORD j = 0;
|
2003-10-07 06:01:28 +02:00
|
|
|
|
2020-08-05 15:08:06 +02:00
|
|
|
if (!(info->exflag & (EF_UTF8|EF_UTF16)) && console)
|
|
|
|
{
|
|
|
|
char conv[sizeof(lfbuf)];
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t len = 0;
|
2020-08-05 15:08:06 +02:00
|
|
|
|
|
|
|
#if _MSVCR_VER >= 90
|
|
|
|
if (info->dbcsBufferUsed)
|
|
|
|
{
|
|
|
|
conv[j++] = info->dbcsBuffer;
|
|
|
|
info->dbcsBufferUsed = FALSE;
|
|
|
|
conv[j++] = s[i++];
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-15 11:55:37 +02:00
|
|
|
for (; i < count && j < sizeof(conv)-1 &&
|
|
|
|
len < (sizeof(lfbuf) - 1) / sizeof(WCHAR); i++, j++, len++)
|
2020-08-05 15:08:06 +02:00
|
|
|
{
|
2020-12-01 13:52:34 +01:00
|
|
|
if (isleadbyte((unsigned char)s[i]))
|
2020-08-05 15:08:06 +02:00
|
|
|
{
|
|
|
|
conv[j++] = s[i++];
|
|
|
|
|
|
|
|
if (i == count)
|
|
|
|
{
|
|
|
|
#if _MSVCR_VER >= 90
|
|
|
|
info->dbcsBuffer = conv[j-1];
|
|
|
|
info->dbcsBufferUsed = TRUE;
|
|
|
|
break;
|
|
|
|
#else
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-08-05 15:08:06 +02:00
|
|
|
release_ioinfo(info);
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (s[i] == '\n')
|
|
|
|
{
|
|
|
|
conv[j++] = '\r';
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
conv[j] = s[i];
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:51 +01:00
|
|
|
len = mbstowcs((WCHAR*)lfbuf, conv, len);
|
2020-08-05 15:08:06 +02:00
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
release_ioinfo(info);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
j = len * 2;
|
|
|
|
}
|
|
|
|
else if (!(info->exflag & (EF_UTF8|EF_UTF16)))
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
for (j = 0; i < count && j < sizeof(lfbuf)-1; i++, j++)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
if (s[i] == '\n')
|
|
|
|
lfbuf[j++] = '\r';
|
|
|
|
lfbuf[j] = s[i];
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 15:08:06 +02:00
|
|
|
else if (info->exflag & EF_UTF16 || console)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
for (j = 0; i < count && j < sizeof(lfbuf)-3; i++, j++)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
if (s[i] == '\n' && !s[i+1])
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
lfbuf[j++] = '\r';
|
|
|
|
lfbuf[j++] = 0;
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
2020-08-05 15:07:27 +02:00
|
|
|
lfbuf[j++] = s[i++];
|
|
|
|
lfbuf[j] = s[i];
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
char conv[sizeof(lfbuf)/4];
|
2013-01-10 11:43:11 +01:00
|
|
|
|
2020-08-05 15:07:27 +02:00
|
|
|
for (j = 0; i < count && j < sizeof(conv)-3; i++, j++)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
if (s[i] == '\n' && !s[i+1])
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
conv[j++] = '\r';
|
|
|
|
conv[j++] = 0;
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
2020-08-05 15:07:27 +02:00
|
|
|
conv[j++] = s[i++];
|
|
|
|
conv[j] = s[i];
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
2020-08-05 15:07:27 +02:00
|
|
|
|
|
|
|
j = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)conv, j/2, lfbuf, sizeof(lfbuf), NULL, NULL);
|
|
|
|
if (!j)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:07:27 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
release_ioinfo(info);
|
|
|
|
return -1;
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:08:06 +02:00
|
|
|
if (console)
|
2013-01-10 11:43:11 +01:00
|
|
|
{
|
2020-08-05 15:08:06 +02:00
|
|
|
j = j/2;
|
|
|
|
if (!WriteConsoleW(hand, lfbuf, j, &num_written, NULL))
|
|
|
|
num_written = -1;
|
|
|
|
}
|
|
|
|
else if (!WriteFile(hand, lfbuf, j, &num_written, NULL))
|
|
|
|
{
|
|
|
|
num_written = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_written != j)
|
|
|
|
{
|
|
|
|
TRACE("WriteFile/WriteConsoleW (fd %d, hand %p) failed-last error (%d)\n", fd,
|
2020-08-05 15:07:27 +02:00
|
|
|
hand, GetLastError());
|
2017-11-15 09:47:37 +01:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-08-05 15:07:27 +02:00
|
|
|
release_ioinfo(info);
|
2017-11-15 09:47:37 +01:00
|
|
|
return -1;
|
2013-01-10 11:43:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:25:00 +02:00
|
|
|
release_ioinfo(info);
|
2020-08-05 15:07:27 +02:00
|
|
|
return count;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _putw (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _putw(int val, FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-05-12 02:12:26 +02:00
|
|
|
int len;
|
2011-05-20 13:22:59 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
len = _write(file->_file, &val, sizeof(val));
|
2011-05-20 13:22:59 +02:00
|
|
|
if (len == sizeof(val)) {
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2011-05-20 13:22:59 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOERR;
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fclose (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fclose(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-10-07 13:35:29 +02:00
|
|
|
int ret;
|
2003-03-30 05:06:30 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fclose_nolock(file);
|
|
|
|
_unlock_file(file);
|
2014-10-07 13:35:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fclose_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fclose_nolock(FILE* file)
|
2014-10-07 13:35:29 +02:00
|
|
|
{
|
|
|
|
int r, flag;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!(file->_flag & (_IOREAD | _IOWRT | _IORW)))
|
2017-11-30 20:28:30 +01:00
|
|
|
{
|
|
|
|
file->_flag = 0;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2017-11-30 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2003-03-30 05:06:30 +02:00
|
|
|
flag = file->_flag;
|
2020-12-01 13:51:32 +01:00
|
|
|
free(file->_tmpfname);
|
2006-08-09 01:17:41 +02:00
|
|
|
file->_tmpfname = NULL;
|
2004-05-12 02:12:26 +02:00
|
|
|
/* flush stdio buffers */
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IOWRT)
|
2020-12-03 19:11:20 +01:00
|
|
|
_fflush_nolock(file);
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IOMYBUF)
|
2020-12-01 13:51:32 +01:00
|
|
|
free(file->_base);
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
r=_close(file->_file);
|
2004-05-12 02:12:26 +02:00
|
|
|
file->_flag = 0;
|
2011-05-19 15:31:22 +02:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
return ((r == -1) || (flag & _IOERR) ? EOF : 0);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* feof (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL feof(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
return file->_flag & _IOEOF;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ferror (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL ferror(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
return file->_flag & _IOERR;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _filbuf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _filbuf(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2011-05-20 13:21:50 +02:00
|
|
|
unsigned char c;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IOSTRG)
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2014-01-28 10:08:17 +01:00
|
|
|
|
2011-05-20 13:21:50 +02:00
|
|
|
/* Allocate buffer if needed */
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF)))
|
2011-05-20 13:21:50 +02:00
|
|
|
msvcrt_alloc_buffer(file);
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!(file->_flag & _IOREAD)) {
|
|
|
|
if(file->_flag & _IORW)
|
|
|
|
file->_flag |= _IOREAD;
|
2014-10-28 14:43:35 +01:00
|
|
|
else
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2011-05-20 13:21:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
|
2004-05-12 02:12:26 +02:00
|
|
|
int r;
|
2020-12-03 19:11:20 +01:00
|
|
|
if ((r = _read(file->_file,&c,1)) != 1) {
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= (r == 0) ? _IOEOF : _IOERR;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2011-05-20 13:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
} else {
|
2020-12-03 19:11:20 +01:00
|
|
|
file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
|
2011-05-20 13:21:50 +02:00
|
|
|
if(file->_cnt<=0) {
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
|
2004-05-12 02:12:26 +02:00
|
|
|
file->_cnt = 0;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2011-05-20 13:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file->_cnt--;
|
|
|
|
file->_ptr = file->_base+1;
|
|
|
|
c = *(unsigned char *)file->_base;
|
|
|
|
return c;
|
|
|
|
}
|
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
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fgetc(FILE* file)
|
2014-10-28 14:43:43 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fgetc_nolock(file);
|
|
|
|
_unlock_file(file);
|
2014-10-28 14:43:43 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetc_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fgetc_nolock(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2006-09-25 23:28:22 +02:00
|
|
|
unsigned char *i;
|
|
|
|
unsigned int j;
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2009-01-19 04:19:46 +01:00
|
|
|
if (file->_cnt>0) {
|
|
|
|
file->_cnt--;
|
|
|
|
i = (unsigned char *)file->_ptr++;
|
|
|
|
j = *i;
|
|
|
|
} else
|
2020-12-03 19:11:20 +01:00
|
|
|
j = _filbuf(file);
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2009-01-19 04:19:46 +01:00
|
|
|
return j;
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fgetchar(void)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fgetc(MSVCRT_stdin);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgets (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
char * CDECL fgets(char *s, int size, FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-30 14:37:09 +01:00
|
|
|
int cc = 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);
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
while ((size >1) && (cc = _fgetc_nolock(file)) != 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
|
|
|
}
|
2020-11-30 14:37:09 +01:00
|
|
|
if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2003-10-07 07:22:04 +02:00
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2020-11-30 14:37:09 +01:00
|
|
|
if ((cc != EOF) && (size > 1))
|
2003-10-07 07:22:04 +02:00
|
|
|
*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));
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2001-01-11 00:59:25 +01:00
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fgetwc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL fgetwc(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wint_t ret;
|
2003-02-19 23:09:33 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fgetwc_nolock(file);
|
|
|
|
_unlock_file(file);
|
2014-10-28 14:43:47 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetwc_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL _fgetwc_nolock(FILE* file)
|
2014-10-28 14:43:47 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wint_t ret;
|
2014-10-28 14:43:47 +01:00
|
|
|
int ch;
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
|
|
|
|
|| !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
|
2013-03-20 10:40:45 +01:00
|
|
|
char *p;
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
for(p=(char*)&ret; (wint_t*)p<&ret+1; p++) {
|
2020-12-03 19:11:20 +01:00
|
|
|
ch = _fgetc_nolock(file);
|
2020-11-30 14:37:09 +01:00
|
|
|
if(ch == EOF) {
|
2020-11-30 14:37:05 +01:00
|
|
|
ret = WEOF;
|
2013-03-20 10:40:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p = (char)ch;
|
|
|
|
}
|
|
|
|
}else {
|
2020-11-27 14:17:01 +01:00
|
|
|
char mbs[MB_LEN_MAX];
|
2013-03-20 10:40:45 +01:00
|
|
|
int len = 0;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ch = _fgetc_nolock(file);
|
2020-11-30 14:37:09 +01:00
|
|
|
if(ch != EOF) {
|
2013-03-20 10:40:45 +01:00
|
|
|
mbs[0] = (char)ch;
|
2020-12-01 13:52:34 +01:00
|
|
|
if(isleadbyte((unsigned char)mbs[0])) {
|
2020-12-03 19:11:20 +01:00
|
|
|
ch = _fgetc_nolock(file);
|
2020-11-30 14:37:09 +01:00
|
|
|
if(ch != EOF) {
|
2013-03-20 10:40:45 +01:00
|
|
|
mbs[1] = (char)ch;
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
len = 1;
|
|
|
|
}
|
2006-07-31 21:15:37 +02:00
|
|
|
}
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2020-12-02 00:53:51 +01:00
|
|
|
if(!len || mbtowc(&ret, mbs, len)==-1)
|
2020-11-30 14:37:05 +01:00
|
|
|
ret = WEOF;
|
2003-02-19 23:09:33 +01:00
|
|
|
}
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2013-03-20 10:40:45 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 00:38:02 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _getw (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _getw(FILE* file)
|
2006-07-30 00:38:02 +02:00
|
|
|
{
|
|
|
|
char *ch;
|
2008-10-21 22:44:27 +02:00
|
|
|
int i, k;
|
|
|
|
unsigned int j;
|
2006-07-30 00:38:02 +02:00
|
|
|
ch = (char *)&i;
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2006-07-30 00:38:02 +02:00
|
|
|
for (j=0; j<sizeof(int); j++) {
|
2020-12-03 19:11:20 +01:00
|
|
|
k = _fgetc_nolock(file);
|
2020-11-30 14:37:09 +01:00
|
|
|
if (k == EOF) {
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOEOF;
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2006-07-30 00:38:02 +02:00
|
|
|
}
|
|
|
|
ch[j] = k;
|
|
|
|
}
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2006-07-30 00:38:02 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* getwc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL getwc(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fgetwc(file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fgetwchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL _fgetwchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fgetwc(MSVCRT_stdin);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getwchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL getwchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _fgetwchar();
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fgetws (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wchar_t * CDECL fgetws(wchar_t *s, int size, FILE* file)
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2020-11-30 14:37:05 +01:00
|
|
|
wint_t cc = WEOF;
|
2020-11-27 14:16:49 +01:00
|
|
|
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);
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2011-05-20 13:22:48 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
while ((size >1) && (cc = _fgetwc_nolock(file)) != WEOF && cc != '\n')
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2013-03-20 10:39:37 +01:00
|
|
|
*s++ = cc;
|
2003-10-07 07:22:04 +02:00
|
|
|
size --;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
2020-11-30 14:37:05 +01:00
|
|
|
if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2003-10-07 07:22:04 +02:00
|
|
|
return NULL;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
2020-11-30 14:37:05 +01:00
|
|
|
if ((cc != WEOF) && (size > 1))
|
2003-10-07 07:22:04 +02:00
|
|
|
*s++ = cc;
|
|
|
|
*s = 0;
|
|
|
|
TRACE(":got %s\n", debugstr_w(buf_start));
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2002-03-11 06:03:05 +01:00
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
2013-10-21 16:11:31 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _flsbuf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _flsbuf(int c, FILE* file)
|
2013-10-21 16:11:31 +02:00
|
|
|
{
|
|
|
|
/* Flush output buffer */
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))) {
|
2013-10-21 16:11:31 +02:00
|
|
|
msvcrt_alloc_buffer(file);
|
|
|
|
}
|
2015-01-21 16:20:48 +01:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!(file->_flag & _IOWRT)) {
|
|
|
|
if(!(file->_flag & _IORW)) {
|
|
|
|
file->_flag |= _IOERR;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EBADF;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2015-01-21 16:20:48 +01:00
|
|
|
}
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOWRT;
|
2015-01-21 16:20:48 +01:00
|
|
|
}
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IOREAD) {
|
|
|
|
if(!(file->_flag & _IOEOF)) {
|
|
|
|
file->_flag |= _IOERR;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2015-01-21 16:20:48 +01:00
|
|
|
}
|
|
|
|
file->_cnt = 0;
|
|
|
|
file->_ptr = file->_base;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~(_IOREAD | _IOEOF);
|
2013-10-21 16:11:31 +02:00
|
|
|
}
|
2015-01-21 16:20:48 +01:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
|
2013-10-21 16:11:31 +02:00
|
|
|
int res = 0;
|
|
|
|
|
2014-06-18 12:55:52 +02:00
|
|
|
if(file->_cnt <= 0) {
|
2013-10-21 16:11:31 +02:00
|
|
|
res = msvcrt_flush_buffer(file);
|
2014-06-18 12:55:52 +02:00
|
|
|
if(res)
|
|
|
|
return res;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOWRT;
|
2014-06-18 12:55:52 +02:00
|
|
|
file->_cnt=file->_bufsiz;
|
2013-10-21 16:11:31 +02:00
|
|
|
}
|
2014-06-18 12:55:52 +02:00
|
|
|
*file->_ptr++ = c;
|
|
|
|
file->_cnt--;
|
|
|
|
return c&0xff;
|
2013-10-21 16:11:31 +02:00
|
|
|
} else {
|
|
|
|
unsigned char cc=c;
|
|
|
|
int len;
|
|
|
|
/* set _cnt to 0 for unbuffered FILEs */
|
|
|
|
file->_cnt = 0;
|
2020-12-03 19:11:20 +01:00
|
|
|
len = _write(file->_file, &cc, 1);
|
2013-10-21 16:11:31 +02:00
|
|
|
if (len == 1)
|
|
|
|
return c & 0xff;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOERR;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2013-10-21 16:11:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fwrite (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file)
|
2014-10-09 19:51:59 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t ret;
|
2014-10-09 19:51:59 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fwrite_nolock(ptr, size, nmemb, file);
|
|
|
|
_unlock_file(file);
|
2014-10-09 19:51:59 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fwrite_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
size_t CDECL _fwrite_nolock(const void *ptr, size_t size, size_t nmemb, FILE* file)
|
2004-03-16 20:17:11 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t wrcnt=size * nmemb;
|
2011-05-20 13:22:59 +02:00
|
|
|
int written = 0;
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
|
2013-10-21 16:11:31 +02:00
|
|
|
while(wrcnt) {
|
2014-06-09 14:24:14 +02:00
|
|
|
if(file->_cnt < 0) {
|
|
|
|
WARN("negative file->_cnt value in %p\n", file);
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOERR;
|
2014-06-09 14:24:14 +02:00
|
|
|
break;
|
|
|
|
} else if(file->_cnt) {
|
2013-10-21 16:11:31 +02:00
|
|
|
int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
|
|
|
|
memcpy(file->_ptr, ptr, pcnt);
|
|
|
|
file->_cnt -= pcnt;
|
|
|
|
file->_ptr += pcnt;
|
|
|
|
written += pcnt;
|
|
|
|
wrcnt -= pcnt;
|
|
|
|
ptr = (const char*)ptr + pcnt;
|
2020-12-02 00:53:28 +01:00
|
|
|
} else if((file->_flag & _IONBF)
|
|
|
|
|| ((file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= file->_bufsiz)
|
|
|
|
|| (!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t pcnt;
|
2014-06-27 10:17:24 +02:00
|
|
|
int bufsiz;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IONBF)
|
2014-06-27 10:17:24 +02:00
|
|
|
bufsiz = 1;
|
2020-12-02 00:53:28 +01:00
|
|
|
else if(!(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)))
|
2014-06-27 10:17:24 +02:00
|
|
|
bufsiz = MSVCRT_INTERNAL_BUFSIZ;
|
|
|
|
else
|
|
|
|
bufsiz = file->_bufsiz;
|
|
|
|
|
|
|
|
pcnt = (wrcnt / bufsiz) * bufsiz;
|
2012-11-02 17:18:34 +01:00
|
|
|
|
2020-11-30 14:37:09 +01:00
|
|
|
if(msvcrt_flush_buffer(file) == EOF)
|
2014-06-18 12:55:53 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if(_write(file->_file, ptr, pcnt) <= 0) {
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOERR;
|
2014-06-18 12:55:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
written += pcnt;
|
|
|
|
wrcnt -= pcnt;
|
|
|
|
ptr = (const char*)ptr + pcnt;
|
2013-10-21 16:11:31 +02:00
|
|
|
} else {
|
2020-12-03 19:11:20 +01:00
|
|
|
if(_flsbuf(*(const char*)ptr, file) == EOF)
|
2013-10-21 16:11:31 +02:00
|
|
|
break;
|
|
|
|
written++;
|
|
|
|
wrcnt--;
|
|
|
|
ptr = (const char*)ptr + 1;
|
2011-05-20 13:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return written / size;
|
2004-03-16 20:17:11 +01:00
|
|
|
}
|
2002-03-11 06:03:05 +01:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fputwc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL fputwc(wint_t wc, FILE* file)
|
2014-11-04 22:51:52 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wint_t ret;
|
2014-11-04 22:51:52 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fputwc_nolock(wc, file);
|
|
|
|
_unlock_file(file);
|
2014-11-04 22:51:52 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fputwc_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL _fputwc_nolock(wint_t wc, FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t mwc=wc;
|
2013-01-11 11:18:44 +01:00
|
|
|
ioinfo *fdinfo;
|
2020-11-27 14:17:05 +01:00
|
|
|
wint_t ret;
|
2013-01-11 11:18:44 +01:00
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
fdinfo = get_ioinfo_nolock(file->_file);
|
2013-01-11 11:18:44 +01:00
|
|
|
|
|
|
|
if((fdinfo->wxflag&WX_TEXT) && !(fdinfo->exflag&(EF_UTF8|EF_UTF16))) {
|
2020-11-27 14:17:01 +01:00
|
|
|
char buf[MB_LEN_MAX];
|
2013-01-11 11:18:44 +01:00
|
|
|
int char_len;
|
|
|
|
|
2020-12-03 19:10:54 +01:00
|
|
|
char_len = wctomb(buf, mwc);
|
2020-12-03 19:11:20 +01:00
|
|
|
if(char_len!=-1 && _fwrite_nolock(buf, char_len, 1, file)==1)
|
2013-01-11 11:18:44 +01:00
|
|
|
ret = wc;
|
|
|
|
else
|
2020-11-30 14:37:05 +01:00
|
|
|
ret = WEOF;
|
2020-12-03 19:11:20 +01:00
|
|
|
}else if(_fwrite_nolock(&mwc, sizeof(mwc), 1, file) == 1) {
|
2013-01-11 11:18:44 +01:00
|
|
|
ret = wc;
|
|
|
|
}else {
|
2020-11-30 14:37:05 +01:00
|
|
|
ret = WEOF;
|
2013-01-11 11:18:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fputwchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL _fputwchar(wint_t wc)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fputwc(wc, MSVCRT_stdout);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2009-01-19 11:41:15 +01:00
|
|
|
* _wfsopen (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE * CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE* file;
|
2004-05-12 02:12:26 +02:00
|
|
|
int open_flags, stream_flags, fd;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2009-01-19 11:41:15 +01:00
|
|
|
TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(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();
|
2020-12-04 15:50:17 +01:00
|
|
|
fd = _wsopen(path, open_flags, share, _S_IREAD | _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)
|
2009-01-19 11:41:15 +01:00
|
|
|
TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
|
2004-05-12 02:12:26 +02:00
|
|
|
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)
|
2020-12-03 19:11:20 +01: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
|
|
|
/*********************************************************************
|
2009-01-19 11:41:15 +01:00
|
|
|
* _fsopen (MSVCRT.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE * CDECL _fsopen(const char *path, const char *mode, int share)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *ret;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *pathW = NULL, *modeW = NULL;
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2010-04-19 10:59:02 +02:00
|
|
|
if (path && !(pathW = msvcrt_wstrdupa(path))) {
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*_errno() = EINVAL;
|
2010-04-19 10:59:02 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-01-19 11:41:15 +01:00
|
|
|
if (mode && !(modeW = msvcrt_wstrdupa(mode)))
|
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
free(pathW);
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*_errno() = EINVAL;
|
2009-01-19 11:41:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wfsopen(pathW, modeW, share);
|
2001-01-22 03:21:54 +01:00
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
free(pathW);
|
|
|
|
free(modeW);
|
2009-01-19 11:41:15 +01:00
|
|
|
return ret;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2007-02-07 13:25:29 +01:00
|
|
|
* fopen (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE * CDECL fopen(const char *path, const char *mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
return _fsopen( path, mode, _SH_DENYNO );
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2010-04-19 10:59:02 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* fopen_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fopen_s(FILE** pFile,
|
2010-04-19 10:59:02 +02:00
|
|
|
const char *filename, const char *mode)
|
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
|
2010-04-19 10:59:02 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
*pFile = fopen(filename, mode);
|
2010-04-19 10:59:02 +02:00
|
|
|
|
|
|
|
if(!*pFile)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2010-04-19 10:59:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2007-02-07 13:25:29 +01:00
|
|
|
* _wfopen (MSVCRT.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE * CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
return _wfsopen( path, mode, _SH_DENYNO );
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2010-04-20 21:38:33 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfopen_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wfopen_s(FILE** pFile, const wchar_t *filename,
|
2020-11-27 14:16:49 +01:00
|
|
|
const wchar_t *mode)
|
2010-04-20 21:38:33 +02:00
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
|
2010-04-20 21:38:33 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
*pFile = _wfopen(filename, mode);
|
2010-04-20 21:38:33 +02:00
|
|
|
|
|
|
|
if(!*pFile)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2010-04-20 21:38:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-16 12:50:50 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fputc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fputc(int c, FILE* file)
|
2014-11-04 22:51:43 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fputc_nolock(c, file);
|
|
|
|
_unlock_file(file);
|
2014-11-04 22:51:43 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fputc_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fputc_nolock(int c, FILE* file)
|
2011-11-16 12:50:50 +01:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if(file->_cnt>0) {
|
|
|
|
*file->_ptr++=c;
|
|
|
|
file->_cnt--;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
res = msvcrt_flush_buffer(file);
|
|
|
|
return res ? res : c;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return c & 0xff;
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-03 19:11:20 +01:00
|
|
|
res = _flsbuf(c, file);
|
2011-11-16 12:50:50 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fputchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _fputchar(int c)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fputc(c, MSVCRT_stdout);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fread (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file)
|
2014-10-09 19:51:55 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t ret;
|
2014-10-09 19:51:55 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fread_nolock(ptr, size, nmemb, file);
|
|
|
|
_unlock_file(file);
|
2014-10-09 19:51:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fread_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
size_t CDECL _fread_nolock(void *ptr, size_t size, size_t nmemb, FILE* file)
|
2011-05-20 13:22:48 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t rcnt=size * nmemb;
|
|
|
|
size_t read=0;
|
|
|
|
size_t 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;
|
2020-12-02 00:53:28 +01:00
|
|
|
} else if(!(file->_flag & _IOREAD )) {
|
|
|
|
if(file->_flag & _IORW) {
|
|
|
|
file->_flag |= _IOREAD;
|
2011-05-20 13:22:48 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
2014-06-27 10:17:24 +02:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(rcnt>0 && !(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF)))
|
2014-06-27 10:17:24 +02:00
|
|
|
msvcrt_alloc_buffer(file);
|
|
|
|
|
2006-07-30 00:35:02 +02:00
|
|
|
while(rcnt>0)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
2006-10-13 00:03:29 +02:00
|
|
|
int i;
|
2020-12-02 00:53:28 +01:00
|
|
|
if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (_IOMYBUF | MSVCRT__USERBUF))) {
|
2020-12-03 19:11:20 +01:00
|
|
|
i = _read(file->_file, file->_base, file->_bufsiz);
|
2006-10-13 00:03:29 +02:00
|
|
|
file->_ptr = file->_base;
|
2017-11-15 09:47:33 +01:00
|
|
|
if (i != -1) {
|
|
|
|
file->_cnt = i;
|
|
|
|
if (i > rcnt) i = rcnt;
|
|
|
|
}
|
2006-10-13 00:03:29 +02:00
|
|
|
/* If the buffer fill reaches eof but fread wouldn't, clear eof. */
|
|
|
|
if (i > 0 && i < file->_cnt) {
|
2014-11-11 15:58:08 +01:00
|
|
|
get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~_IOEOF;
|
2006-10-13 00:03:29 +02:00
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
memcpy(ptr, file->_ptr, i);
|
|
|
|
file->_cnt -= i;
|
|
|
|
file->_ptr += i;
|
|
|
|
}
|
2014-06-27 10:17:24 +02:00
|
|
|
} else if (rcnt > INT_MAX) {
|
2020-12-03 19:11:20 +01:00
|
|
|
i = _read(file->_file, ptr, INT_MAX);
|
2017-11-30 19:55:16 +01:00
|
|
|
} else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
|
2020-12-03 19:11:20 +01:00
|
|
|
i = _read(file->_file, ptr, rcnt);
|
2006-10-13 00:03:29 +02:00
|
|
|
} else {
|
2020-12-03 19:11:20 +01:00
|
|
|
i = _read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
|
2006-10-13 00:03:29 +02:00
|
|
|
}
|
2006-07-30 00:35:02 +02:00
|
|
|
pread += i;
|
|
|
|
rcnt -= i;
|
2006-10-27 01:28:11 +02:00
|
|
|
ptr = (char *)ptr+i;
|
2004-05-12 02:12:26 +02:00
|
|
|
/* expose feof condition in the flags
|
2006-10-27 01:28:11 +02:00
|
|
|
* MFC tests file->_flag for feof, and doesn't call feof())
|
2004-05-12 02:12:26 +02:00
|
|
|
*/
|
2014-11-11 15:58:08 +01:00
|
|
|
if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOEOF;
|
2006-07-30 00:35:02 +02:00
|
|
|
else if (i == -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOERR;
|
2004-05-12 02:12:26 +02:00
|
|
|
pread = 0;
|
2006-07-30 00:35:02 +02:00
|
|
|
rcnt = 0;
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
2006-07-31 21:12:02 +02:00
|
|
|
if (i < 1) break;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
read+=pread;
|
2001-01-11 00:59:25 +01:00
|
|
|
return read / size;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#if _MSVCR_VER >= 80
|
2013-03-28 12:57:14 +01:00
|
|
|
|
2014-04-21 14:32:40 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* fread_s (MSVCR80.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
size_t CDECL fread_s(void *buf, size_t buf_size, size_t elem_size,
|
2020-12-01 13:51:39 +01:00
|
|
|
size_t count, FILE *stream)
|
2014-11-09 20:17:45 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t ret;
|
2014-11-09 20:17:45 +01:00
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(stream != NULL)) {
|
|
|
|
if(buf && buf_size)
|
|
|
|
memset(buf, 0, buf_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!elem_size || !count) return 0;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(stream);
|
|
|
|
ret = _fread_nolock_s(buf, buf_size, elem_size, count, stream);
|
|
|
|
_unlock_file(stream);
|
2014-11-09 20:17:45 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _fread_nolock_s (MSVCR80.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
size_t CDECL _fread_nolock_s(void *buf, size_t buf_size, size_t elem_size,
|
2020-12-01 13:51:39 +01:00
|
|
|
size_t count, FILE *stream)
|
2013-03-28 12:57:14 +01:00
|
|
|
{
|
|
|
|
size_t bytes_left, buf_pos;
|
|
|
|
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p %Iu %Iu %Iu %p)\n", buf, buf_size, elem_size, count, stream);
|
2013-03-28 12:57:14 +01:00
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(stream != NULL)) {
|
|
|
|
if(buf && buf_size)
|
|
|
|
memset(buf, 0, buf_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!elem_size || !count) return 0;
|
|
|
|
if(!MSVCRT_CHECK_PMT(buf != NULL)) return 0;
|
2020-11-27 14:17:01 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(SIZE_MAX/count >= elem_size)) return 0;
|
2013-03-28 12:57:14 +01:00
|
|
|
|
|
|
|
bytes_left = elem_size*count;
|
|
|
|
buf_pos = 0;
|
|
|
|
while(bytes_left) {
|
|
|
|
if(stream->_cnt > 0) {
|
|
|
|
size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT_ERR(size <= buf_size-buf_pos, ERANGE)) {
|
2013-03-28 12:57:14 +01:00
|
|
|
memset(buf, 0, buf_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_fread_nolock((char*)buf+buf_pos, 1, size, stream);
|
2013-03-28 12:57:14 +01:00
|
|
|
buf_pos += size;
|
|
|
|
bytes_left -= size;
|
|
|
|
}else {
|
2020-12-03 19:11:20 +01:00
|
|
|
int c = _filbuf(stream);
|
2013-03-28 12:57:14 +01:00
|
|
|
|
2020-11-30 14:37:09 +01:00
|
|
|
if(c == EOF)
|
2013-03-28 12:57:14 +01:00
|
|
|
break;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT_ERR(buf_size != buf_pos, ERANGE)) {
|
2013-03-28 12:57:14 +01:00
|
|
|
memset(buf, 0, buf_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
((char*)buf)[buf_pos++] = c;
|
|
|
|
bytes_left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf_pos/elem_size;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#endif /* _MSVCR_VER >= 80 */
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2009-01-19 11:41:15 +01:00
|
|
|
* _wfreopen (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2013-06-04 11:16:50 +02:00
|
|
|
int open_flags, stream_flags, fd;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2014-04-29 17:43:26 +02:00
|
|
|
TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2013-06-04 11:16:50 +02:00
|
|
|
LOCK_FILES();
|
2015-06-10 18:12:53 +02:00
|
|
|
if (!file || ((fd = file->_file) < 0))
|
2013-06-04 11:16:50 +02:00
|
|
|
file = NULL;
|
|
|
|
else
|
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
fclose(file);
|
2013-06-04 11:16:50 +02:00
|
|
|
if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
|
|
|
|
file = NULL;
|
2020-12-04 15:50:17 +01:00
|
|
|
else if((fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE)) < 0)
|
2013-06-04 11:16:50 +02:00
|
|
|
file = NULL;
|
|
|
|
else if(msvcrt_init_fp(file, fd, stream_flags) == -1)
|
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UNLOCK_FILES();
|
|
|
|
return file;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2011-11-13 18:33:47 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _wfreopen_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wfreopen_s(FILE** pFile,
|
2020-12-01 13:51:39 +01:00
|
|
|
const wchar_t *path, const wchar_t *mode, FILE* file)
|
2011-11-13 18:33:47 +01:00
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
|
2011-11-13 18:33:47 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
*pFile = _wfreopen(path, mode, file);
|
2011-11-13 18:33:47 +01:00
|
|
|
|
|
|
|
if(!*pFile)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2011-11-13 18:33:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-19 11:41:15 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* freopen (MSVCRT.@)
|
|
|
|
*
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE* CDECL freopen(const char *path, const char *mode, FILE* file)
|
2009-01-19 11:41:15 +01:00
|
|
|
{
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE *ret;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *pathW = NULL, *modeW = NULL;
|
2009-01-19 11:41:15 +01:00
|
|
|
|
|
|
|
if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
|
|
|
|
if (mode && !(modeW = msvcrt_wstrdupa(mode)))
|
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
free(pathW);
|
2009-01-19 11:41:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _wfreopen(pathW, modeW, file);
|
2009-01-19 11:41:15 +01:00
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
free(pathW);
|
|
|
|
free(modeW);
|
2009-01-19 11:41:15 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-13 18:33:00 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* freopen_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL freopen_s(FILE** pFile,
|
2020-12-01 13:51:39 +01:00
|
|
|
const char *path, const char *mode, FILE* file)
|
2011-11-13 18:33:00 +01:00
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(path != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
|
2011-11-13 18:33:00 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
*pFile = freopen(path, mode, file);
|
2011-11-13 18:33:00 +01:00
|
|
|
|
|
|
|
if(!*pFile)
|
2020-12-01 13:51:23 +01:00
|
|
|
return *_errno();
|
2011-11-13 18:33:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fsetpos (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fsetpos(FILE* file, fpos_t *pos)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2011-05-20 13:22:39 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2015-01-23 17:27:23 +01:00
|
|
|
msvcrt_flush_buffer(file);
|
2005-01-04 21:37:54 +01:00
|
|
|
|
|
|
|
/* Reset direction of i/o */
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IORW) {
|
|
|
|
file->_flag &= ~(_IOREAD|_IOWRT);
|
2005-01-04 21:37:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
|
|
|
|
_unlock_file(file);
|
2011-05-20 13:22:39 +02:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2010-11-03 13:33:12 +01:00
|
|
|
* _ftelli64 (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__int64 CDECL _ftelli64(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-10-15 16:46:01 +02:00
|
|
|
__int64 ret;
|
2011-05-20 13:22:39 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _ftelli64_nolock(file);
|
|
|
|
_unlock_file(file);
|
2014-10-15 16:46:01 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _ftelli64_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__int64 CDECL _ftelli64_nolock(FILE* file)
|
2014-10-15 16:46:01 +02:00
|
|
|
{
|
|
|
|
__int64 pos;
|
|
|
|
|
2011-05-19 15:31:12 +02:00
|
|
|
pos = _telli64(file->_file);
|
2014-10-15 16:46:01 +02:00
|
|
|
if(pos == -1)
|
2011-05-20 13:22:39 +02:00
|
|
|
return -1;
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & (_IOMYBUF | MSVCRT__USERBUF)) {
|
|
|
|
if(file->_flag & _IOWRT) {
|
2013-01-10 11:44:18 +01:00
|
|
|
pos += file->_ptr - file->_base;
|
2013-01-11 11:19:44 +01:00
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
|
2013-01-11 11:19:44 +01:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
for(p=file->_base; p<file->_ptr; p++)
|
|
|
|
if(*p == '\n')
|
|
|
|
pos++;
|
|
|
|
}
|
2013-01-10 11:44:18 +01:00
|
|
|
} else if(!file->_cnt) { /* nothing to do */
|
2020-12-03 19:11:20 +01:00
|
|
|
} else if(_lseeki64(file->_file, 0, SEEK_END)==pos) {
|
2013-01-10 11:44:18 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
pos -= file->_cnt;
|
2014-11-11 15:58:08 +01:00
|
|
|
if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
|
2013-01-10 11:44:18 +01:00
|
|
|
for(i=0; i<file->_cnt; i++)
|
|
|
|
if(file->_ptr[i] == '\n')
|
|
|
|
pos--;
|
|
|
|
}
|
2011-05-19 15:31:12 +02:00
|
|
|
} else {
|
2013-01-10 11:44:18 +01:00
|
|
|
char *p;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
if(_lseeki64(file->_file, pos, SEEK_SET) != pos)
|
2013-01-10 11:44:18 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
pos -= file->_bufsiz;
|
|
|
|
pos += file->_ptr - file->_base;
|
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
|
|
|
|
if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
|
2013-01-10 11:44:18 +01:00
|
|
|
pos--;
|
|
|
|
|
|
|
|
for(p=file->_base; p<file->_ptr; p++)
|
|
|
|
if(*p == '\n')
|
|
|
|
pos++;
|
2011-05-19 15:31:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-20 13:22:39 +02:00
|
|
|
|
2013-01-10 11:44:18 +01:00
|
|
|
return pos;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2010-11-03 13:33:12 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* ftell (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__msvcrt_long CDECL ftell(FILE* file)
|
2010-11-03 13:33:12 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _ftelli64(file);
|
2010-11-03 13:33:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#if _MSVCR_VER >= 80
|
2014-10-15 16:46:30 +02:00
|
|
|
/*********************************************************************
|
2018-01-04 05:33:00 +01:00
|
|
|
* _ftell_nolock (MSVCR80.@)
|
2014-10-15 16:46:30 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
__msvcrt_long CDECL _ftell_nolock(FILE* file)
|
2014-10-15 16:46:30 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _ftelli64_nolock(file);
|
2014-10-15 16:46:30 +02:00
|
|
|
}
|
2018-01-04 05:33:00 +01:00
|
|
|
#endif
|
2014-10-15 16:46:30 +02:00
|
|
|
|
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
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fgetpos(FILE* file, fpos_t *pos)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
*pos = _ftelli64(file);
|
2013-01-10 11:43:49 +01:00
|
|
|
if(*pos == -1)
|
2011-05-20 13:22:39 +02:00
|
|
|
return -1;
|
2011-05-19 15:31:12 +02:00
|
|
|
return 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* fputs (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fputs(const char *s, FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t len = strlen(s);
|
2011-05-20 13:22:59 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _fwrite_nolock(s, sizeof(*s), len, file) == len ? 0 : EOF;
|
|
|
|
_unlock_file(file);
|
2012-12-13 13:45:00 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fputws (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL fputws(const wchar_t *s, FILE* file)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-03 19:10:54 +01:00
|
|
|
size_t i, len = wcslen(s);
|
2013-01-29 13:21:27 +01:00
|
|
|
BOOL tmp_buf;
|
2011-05-20 13:22:59 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2014-11-11 15:58:08 +01:00
|
|
|
if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fwrite_nolock(s,sizeof(*s),len,file) == len ? 0 : EOF;
|
|
|
|
_unlock_file(file);
|
2011-05-20 13:22:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2013-01-29 13:21:27 +01:00
|
|
|
|
|
|
|
tmp_buf = add_std_buffer(file);
|
2011-05-20 13:22:59 +02:00
|
|
|
for (i=0; i<len; i++) {
|
2020-12-03 19:11:20 +01:00
|
|
|
if(_fputwc_nolock(s[i], file) == WEOF) {
|
2013-01-29 13:21:27 +01:00
|
|
|
if(tmp_buf) remove_std_buffer(file);
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2020-11-30 14:37:05 +01:00
|
|
|
return WEOF;
|
2011-05-20 13:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 13:21:27 +01:00
|
|
|
if(tmp_buf) remove_std_buffer(file);
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2003-02-19 23:09:33 +01:00
|
|
|
return 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* getchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL getchar(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fgetc(MSVCRT_stdin);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* getc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL getc(FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fgetc(file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2018-07-16 19:29:04 +02:00
|
|
|
* gets_s (MSVCR80.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
char * CDECL gets_s(char *buf, size_t len)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2018-07-16 19:29:04 +02:00
|
|
|
char *buf_start = buf;
|
|
|
|
int cc;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2018-07-16 19:29:04 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(buf != NULL)) return NULL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(len != 0)) return NULL;
|
2018-03-07 05:16:28 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(MSVCRT_stdin);
|
|
|
|
for(cc = _fgetc_nolock(MSVCRT_stdin);
|
2020-11-30 14:37:09 +01:00
|
|
|
len != 0 && cc != EOF && cc != '\n';
|
2020-12-03 19:11:20 +01:00
|
|
|
cc = _fgetc_nolock(MSVCRT_stdin))
|
2018-07-16 19:29:04 +02:00
|
|
|
{
|
|
|
|
if (cc != '\r')
|
|
|
|
{
|
|
|
|
*buf++ = (char)cc;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(MSVCRT_stdin);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2018-07-16 19:29:04 +02:00
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
*buf_start = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
2018-07-16 19:29:04 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:37:09 +01:00
|
|
|
if ((cc == EOF) && (buf_start == buf))
|
2018-07-16 19:29:04 +02:00
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
|
|
|
|
TRACE("got '%s'\n", buf_start);
|
|
|
|
return buf_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* gets (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
char * CDECL gets(char *buf)
|
2018-07-16 19:29:04 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return gets_s(buf, -1);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-05-20 00:21:17 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _getws (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wchar_t* CDECL _getws(wchar_t* buf)
|
2002-05-20 00:21:17 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wint_t cc;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t* ws = buf;
|
2002-05-20 00:21:17 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(MSVCRT_stdin);
|
|
|
|
for (cc = _fgetwc_nolock(MSVCRT_stdin); cc != WEOF && cc != '\n';
|
|
|
|
cc = _fgetwc_nolock(MSVCRT_stdin))
|
2002-05-20 00:21:17 +02:00
|
|
|
{
|
|
|
|
if (cc != '\r')
|
2020-11-27 14:16:49 +01:00
|
|
|
*buf++ = (wchar_t)cc;
|
2002-05-20 00:21:17 +02:00
|
|
|
}
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(MSVCRT_stdin);
|
2018-03-07 05:16:28 +01:00
|
|
|
|
2020-11-30 14:37:05 +01:00
|
|
|
if ((cc == WEOF) && (ws == buf))
|
2018-03-07 05:16:28 +01:00
|
|
|
{
|
|
|
|
TRACE(":nothing read\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-05-20 00:21:17 +02:00
|
|
|
*buf = '\0';
|
|
|
|
|
2007-01-18 11:40:15 +01:00
|
|
|
TRACE("got %s\n", debugstr_w(ws));
|
2002-05-20 00:21:17 +02:00
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* putc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL putc(int c, FILE* file)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fputc(c, file);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* putchar (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL putchar(int c)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fputc(c, MSVCRT_stdout);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* puts (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL puts(const char *s)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t len = strlen(s);
|
2011-05-20 13:22:59 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(MSVCRT_stdout);
|
|
|
|
if(_fwrite_nolock(s, sizeof(*s), len, MSVCRT_stdout) != len) {
|
|
|
|
_unlock_file(MSVCRT_stdout);
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2011-05-20 13:22:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fwrite_nolock("\n",1,1,MSVCRT_stdout) == 1 ? 0 : EOF;
|
|
|
|
_unlock_file(MSVCRT_stdout);
|
2011-05-20 13:22:59 +02:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _putws (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _putws(const wchar_t *s)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2011-05-20 13:22:59 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(MSVCRT_stdout);
|
|
|
|
ret = fputws(s, MSVCRT_stdout);
|
2019-09-30 15:22:15 +02:00
|
|
|
if(ret >= 0)
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fputwc_nolock('\n', MSVCRT_stdout);
|
|
|
|
_unlock_file(MSVCRT_stdout);
|
2020-11-30 14:37:05 +01:00
|
|
|
return ret >= 0 ? 0 : WEOF;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* remove (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL 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;
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE(":failed (%d)\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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wremove(const wchar_t *path)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
|
|
|
TRACE("(%s)\n",debugstr_w(path));
|
|
|
|
if (DeleteFileW(path))
|
|
|
|
return 0;
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE(":failed (%d)\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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL 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;
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE(":failed (%d)\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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wrename(const wchar_t *oldpath,const 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;
|
2006-10-03 20:39:05 +02:00
|
|
|
TRACE(":failed (%d)\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.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL setvbuf(FILE* file, char *buf, int mode, size_t size)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2014-06-26 12:29:59 +02:00
|
|
|
if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
|
2020-12-02 00:53:28 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(mode==_IONBF || mode==_IOFBF || mode==_IOLBF)) return -1;
|
|
|
|
if(!MSVCRT_CHECK_PMT(mode==_IONBF || (size>=2 && size<=INT_MAX))) return -1;
|
2014-06-26 12:29:59 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2014-06-26 12:29:59 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_fflush_nolock(file);
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IOMYBUF)
|
2020-12-01 13:51:32 +01:00
|
|
|
free(file->_base);
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~(_IONBF | _IOMYBUF | MSVCRT__USERBUF);
|
2014-06-26 12:29:59 +02:00
|
|
|
file->_cnt = 0;
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if(mode == _IONBF) {
|
|
|
|
file->_flag |= _IONBF;
|
2014-06-26 12:29:59 +02:00
|
|
|
file->_base = file->_ptr = (char*)&file->_charbuf;
|
|
|
|
file->_bufsiz = 2;
|
|
|
|
}else if(buf) {
|
|
|
|
file->_base = file->_ptr = buf;
|
|
|
|
file->_flag |= MSVCRT__USERBUF;
|
|
|
|
file->_bufsiz = size;
|
|
|
|
}else {
|
2020-12-01 13:51:32 +01:00
|
|
|
file->_base = file->_ptr = malloc(size);
|
2014-06-26 12:29:59 +02:00
|
|
|
if(!file->_base) {
|
|
|
|
file->_bufsiz = 0;
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2014-06-26 12:29:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag |= _IOMYBUF;
|
2014-06-26 12:29:59 +02:00
|
|
|
file->_bufsiz = size;
|
|
|
|
}
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2014-06-26 12:29:59 +02:00
|
|
|
return 0;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* setbuf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
void CDECL setbuf(FILE* file, char *buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static int tmpnam_helper(char *s, size_t size, int *tmpnam_unique, int tmp_max)
|
2015-12-22 17:43:20 +01:00
|
|
|
{
|
|
|
|
char tmpstr[8];
|
|
|
|
char *p = s;
|
|
|
|
int digits;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
|
2015-12-22 17:43:20 +01:00
|
|
|
|
|
|
|
if (size < 3) {
|
|
|
|
if (size) *s = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2015-12-22 17:43:20 +01:00
|
|
|
}
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = 's';
|
|
|
|
size -= 2;
|
|
|
|
digits = msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
|
|
|
|
if (digits+1 > size) {
|
|
|
|
*s = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2015-12-22 17:43:20 +01:00
|
|
|
}
|
|
|
|
memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
|
|
|
|
p += digits;
|
|
|
|
*p++ = '.';
|
|
|
|
size -= digits+1;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
while ((digits = *tmpnam_unique)+1 < tmp_max) {
|
|
|
|
if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
digits = msvcrt_int_to_base32(digits, tmpstr);
|
|
|
|
if (digits+1 > size) {
|
|
|
|
*s = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2015-12-22 17:43:20 +01:00
|
|
|
}
|
|
|
|
memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
|
|
|
|
p[digits] = 0;
|
|
|
|
|
|
|
|
if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
|
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL tmpnam_s(char *s, size_t size)
|
2015-12-22 17:43:20 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
return tmpnam_helper(s, size, &tmpnam_s_unique, TMP_MAX_S);
|
2015-12-22 17:43:20 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* tmpnam (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
char * CDECL tmpnam(char *s)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2011-05-24 17:23:13 +02:00
|
|
|
if (!s) {
|
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
|
|
|
|
|
|
|
if(!data->tmpnam_buffer)
|
2020-12-01 13:51:32 +01:00
|
|
|
data->tmpnam_buffer = malloc(MAX_PATH);
|
2011-05-24 17:23:13 +02:00
|
|
|
|
|
|
|
s = data->tmpnam_buffer;
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
return tmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static int wtmpnam_helper(wchar_t *s, size_t size, int *tmpnam_unique, int tmp_max)
|
2015-12-22 17:47:21 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t tmpstr[8];
|
|
|
|
wchar_t *p = s;
|
2015-12-22 17:47:21 +01:00
|
|
|
int digits;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(s != NULL)) return EINVAL;
|
2015-12-22 17:47:21 +01:00
|
|
|
|
|
|
|
if (size < 3) {
|
|
|
|
if (size) *s = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2015-12-22 17:47:21 +01:00
|
|
|
}
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = 's';
|
|
|
|
size -= 2;
|
|
|
|
digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr);
|
|
|
|
if (digits+1 > size) {
|
|
|
|
*s = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2015-12-22 17:47:21 +01:00
|
|
|
}
|
|
|
|
memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
|
|
|
|
p += digits;
|
|
|
|
*p++ = '.';
|
|
|
|
size -= digits+1;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
while ((digits = *tmpnam_unique)+1 < tmp_max) {
|
|
|
|
if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
digits = msvcrt_int_to_base32_w(digits, tmpstr);
|
|
|
|
if (digits+1 > size) {
|
|
|
|
*s = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2015-12-22 17:47:21 +01:00
|
|
|
}
|
|
|
|
memcpy(p, tmpstr, digits*sizeof(tmpstr[0]));
|
|
|
|
p[digits] = 0;
|
|
|
|
|
|
|
|
if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES &&
|
|
|
|
GetLastError() == ERROR_FILE_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wtmpnam_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _wtmpnam_s(wchar_t *s, size_t size)
|
2015-12-22 17:47:21 +01:00
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
return wtmpnam_helper(s, size, &tmpnam_s_unique, TMP_MAX_S);
|
2015-12-22 17:47:21 +01:00
|
|
|
}
|
|
|
|
|
2011-05-03 14:26:08 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _wtmpnam (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wchar_t * CDECL _wtmpnam(wchar_t *s)
|
2011-05-03 14:26:08 +02:00
|
|
|
{
|
2011-05-24 17:23:13 +02:00
|
|
|
if (!s) {
|
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
|
|
|
|
|
|
|
if(!data->wtmpnam_buffer)
|
2020-12-01 13:51:32 +01:00
|
|
|
data->wtmpnam_buffer = malloc(sizeof(wchar_t[MAX_PATH]));
|
2011-05-24 17:23:13 +02:00
|
|
|
|
|
|
|
s = data->wtmpnam_buffer;
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
return wtmpnam_helper(s, -1, &tmpnam_unique, TMP_MAX) ? NULL : s;
|
2011-05-03 14:26:08 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* tmpfile (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
FILE* CDECL tmpfile(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
char *filename = _tempnam(",", "t");
|
2001-01-11 00:59:25 +01:00
|
|
|
int fd;
|
2020-12-01 13:51:39 +01:00
|
|
|
FILE* file = NULL;
|
2004-05-12 02:12:26 +02:00
|
|
|
|
2004-12-14 12:59:43 +01:00
|
|
|
LOCK_FILES();
|
2020-12-04 15:50:17 +01:00
|
|
|
fd = _open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY,
|
|
|
|
_S_IREAD | _S_IWRITE);
|
2004-05-12 02:12:26 +02:00
|
|
|
if (fd != -1 && (file = msvcrt_alloc_fp()))
|
|
|
|
{
|
2020-12-02 00:53:28 +01:00
|
|
|
if (msvcrt_init_fp(file, fd, _IORW) == -1)
|
2004-05-12 02:12:26 +02:00
|
|
|
{
|
|
|
|
file->_flag = 0;
|
|
|
|
file = NULL;
|
|
|
|
}
|
2020-12-02 00:54:09 +01:00
|
|
|
else file->_tmpfname = _strdup(filename);
|
2004-05-12 02:12:26 +02:00
|
|
|
}
|
2012-10-16 17:23:04 +02:00
|
|
|
|
|
|
|
if(fd != -1 && !file)
|
2020-12-03 19:11:20 +01:00
|
|
|
_close(fd);
|
2020-12-01 13:51:32 +01:00
|
|
|
free(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
|
|
|
}
|
|
|
|
|
2012-07-17 21:49:09 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* tmpfile_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL tmpfile_s(FILE** file)
|
2012-07-17 21:49:09 +02:00
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(file != NULL)) return EINVAL;
|
2012-07-17 21:49:09 +02:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
*file = tmpfile();
|
2012-07-17 21:49:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-20 14:42:36 +02:00
|
|
|
static int puts_clbk_file_a(void *file, int len, const char *str)
|
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return fwrite(str, sizeof(char), len, file);
|
2011-04-20 14:42:36 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
static int puts_clbk_file_w(void *file, int len, const wchar_t *str)
|
2011-04-20 14:42:36 +02:00
|
|
|
{
|
2013-01-11 11:19:08 +01:00
|
|
|
int i, ret;
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2013-01-11 11:19:08 +01:00
|
|
|
|
2020-12-01 13:51:39 +01:00
|
|
|
if(!(get_ioinfo_nolock(((FILE*)file)->_file)->wxflag & WX_TEXT)) {
|
2020-12-03 19:11:20 +01:00
|
|
|
ret = _fwrite_nolock(str, sizeof(wchar_t), len, file);
|
|
|
|
_unlock_file(file);
|
2013-01-11 11:19:08 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<len; i++) {
|
2020-12-03 19:11:20 +01:00
|
|
|
if(_fputwc_nolock(str[i], file) == WEOF) {
|
|
|
|
_unlock_file(file);
|
2013-01-11 11:19:08 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2013-01-11 11:19:08 +01:00
|
|
|
return len;
|
2011-04-20 14:42:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:51:39 +01:00
|
|
|
static int vfprintf_helper(DWORD options, FILE* file, const char *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-01 13:52:05 +01:00
|
|
|
printf_arg args_ctx[_ARGMAX+1];
|
2013-01-29 13:21:27 +01:00
|
|
|
BOOL tmp_buf;
|
2011-05-19 15:31:51 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
|
|
|
|
if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
|
|
|
|
|
2017-10-13 20:23:29 +02:00
|
|
|
if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
|
|
|
|
memset(args_ctx, 0, sizeof(args_ctx));
|
|
|
|
ret = create_positional_ctx_a(args_ctx, format, valist);
|
|
|
|
if(ret < 0) {
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*_errno() = EINVAL;
|
2017-10-13 20:23:29 +02:00
|
|
|
return ret;
|
|
|
|
} else if(!ret)
|
|
|
|
options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2013-01-29 13:21:27 +01:00
|
|
|
tmp_buf = add_std_buffer(file);
|
2017-10-13 20:23:29 +02:00
|
|
|
ret = pf_printf_a(puts_clbk_file_a, file, format, locale, options,
|
|
|
|
options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
|
|
|
|
options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
|
2013-01-29 13:21:27 +01:00
|
|
|
if(tmp_buf) remove_std_buffer(file);
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2011-05-19 15:31:51 +02:00
|
|
|
|
|
|
|
return ret;
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:51:39 +01:00
|
|
|
static int vfwprintf_helper(DWORD options, FILE* file, const wchar_t *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2010-11-24 21:12:35 +01:00
|
|
|
{
|
2020-12-01 13:52:05 +01:00
|
|
|
printf_arg args_ctx[_ARGMAX+1];
|
2013-01-29 13:21:27 +01:00
|
|
|
BOOL tmp_buf;
|
2011-05-19 15:31:51 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
if(!MSVCRT_CHECK_PMT( file != NULL )) return -1;
|
|
|
|
if(!MSVCRT_CHECK_PMT( format != NULL )) return -1;
|
2010-11-24 21:12:35 +01:00
|
|
|
|
2017-10-13 20:23:29 +02:00
|
|
|
if(options & MSVCRT_PRINTF_POSITIONAL_PARAMS) {
|
|
|
|
memset(args_ctx, 0, sizeof(args_ctx));
|
|
|
|
ret = create_positional_ctx_w(args_ctx, format, valist);
|
|
|
|
if(ret < 0) {
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*_errno() = EINVAL;
|
2017-10-13 20:23:29 +02:00
|
|
|
return ret;
|
|
|
|
} else if(!ret)
|
|
|
|
options &= ~MSVCRT_PRINTF_POSITIONAL_PARAMS;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
2013-01-29 13:21:27 +01:00
|
|
|
tmp_buf = add_std_buffer(file);
|
2017-10-13 20:23:29 +02:00
|
|
|
ret = pf_printf_w(puts_clbk_file_w, file, format, locale, options,
|
|
|
|
options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? arg_clbk_positional : arg_clbk_valist,
|
|
|
|
options & MSVCRT_PRINTF_POSITIONAL_PARAMS ? args_ctx : NULL, &valist);
|
2013-01-29 13:21:27 +01:00
|
|
|
if(tmp_buf) remove_std_buffer(file);
|
2020-12-03 19:11:20 +01:00
|
|
|
_unlock_file(file);
|
2011-05-19 15:31:51 +02:00
|
|
|
|
|
|
|
return ret;
|
2010-11-24 21:12:35 +01:00
|
|
|
}
|
|
|
|
|
2017-10-10 16:14:03 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _vfprintf_s_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfprintf_s_l(FILE* file, const char *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-10-10 16:14:03 +02:00
|
|
|
{
|
|
|
|
return vfprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _vfwprintf_s_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfwprintf_s_l(FILE* file, const wchar_t *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-10-10 16:14:03 +02:00
|
|
|
{
|
|
|
|
return vfwprintf_helper(MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
2017-10-07 01:24:14 +02:00
|
|
|
* vfprintf (MSVCRT.@)
|
2001-01-22 03:21:54 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vfprintf(FILE* file, const char *format, __ms_va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfprintf_helper(0, file, format, NULL, valist);
|
|
|
|
}
|
2011-05-19 15:31:51 +02:00
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* vfprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vfprintf_s(FILE* file, const char *format, __ms_va_list valist)
|
2017-10-07 01:24:14 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _vfprintf_s_l(file, format, NULL, valist);
|
2017-10-07 01:24:14 +02:00
|
|
|
}
|
2011-05-19 15:31:51 +02:00
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* vfwprintf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vfwprintf(FILE* file, const wchar_t *format, __ms_va_list valist)
|
2017-10-07 01:24:14 +02:00
|
|
|
{
|
|
|
|
return vfwprintf_helper(0, file, format, NULL, valist);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2010-11-24 21:12:35 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* vfwprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vfwprintf_s(FILE* file, const wchar_t *format, __ms_va_list valist)
|
2010-11-24 21:12:35 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _vfwprintf_s_l(file, format, NULL, valist);
|
2010-11-24 21:12:35 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#if _MSVCR_VER >= 140
|
|
|
|
|
2015-10-27 22:42:15 +01:00
|
|
|
/*********************************************************************
|
2017-09-04 20:06:56 +02:00
|
|
|
* __stdio_common_vfprintf (UCRTBASE.@)
|
2015-10-27 22:42:15 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _stdio_common_vfprintf(unsigned __int64 options, FILE *file, const char *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2015-10-27 22:42:15 +01:00
|
|
|
{
|
2015-11-03 19:40:38 +01:00
|
|
|
if (options & ~UCRTBASE_PRINTF_MASK)
|
|
|
|
FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
|
2015-10-27 22:42:15 +01:00
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
|
2015-10-27 22:42:15 +01:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:42:52 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* __stdio_common_vfprintf_p (UCRTBASE.@)
|
|
|
|
*/
|
|
|
|
int CDECL __stdio_common_vfprintf_p(unsigned __int64 options, FILE *file, const char *format,
|
|
|
|
_locale_t locale, __ms_va_list valist)
|
|
|
|
{
|
|
|
|
if (options & ~UCRTBASE_PRINTF_MASK)
|
|
|
|
FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
|
|
|
|
|
|
|
|
return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_POSITIONAL_PARAMS
|
|
|
|
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-04 20:06:57 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* __stdio_common_vfprintf_s (UCRTBASE.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL __stdio_common_vfprintf_s(unsigned __int64 options, FILE *file, const char *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-09-04 20:06:57 +02:00
|
|
|
{
|
|
|
|
if (options & ~UCRTBASE_PRINTF_MASK)
|
|
|
|
FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
|
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
|
|
|
|
file, format, locale, valist);
|
2017-09-04 20:06:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 00:56:43 +02:00
|
|
|
/*********************************************************************
|
2017-09-04 20:06:56 +02:00
|
|
|
* __stdio_common_vfwprintf (UCRTBASE.@)
|
2016-04-07 00:56:43 +02:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL __stdio_common_vfwprintf(unsigned __int64 options, FILE *file, const wchar_t *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2016-04-07 00:56:43 +02:00
|
|
|
{
|
|
|
|
if (options & ~UCRTBASE_PRINTF_MASK)
|
|
|
|
FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
|
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfwprintf_helper(options & UCRTBASE_PRINTF_MASK, file, format, locale, valist);
|
2016-04-07 00:56:43 +02:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:42:52 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* __stdio_common_vfwprintf_p (UCRTBASE.@)
|
|
|
|
*/
|
|
|
|
int CDECL __stdio_common_vfwprintf_p(unsigned __int64 options, FILE *file, const wchar_t *format,
|
|
|
|
_locale_t locale, __ms_va_list valist)
|
|
|
|
{
|
|
|
|
if (options & ~UCRTBASE_PRINTF_MASK)
|
|
|
|
FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
|
|
|
|
|
|
|
|
return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_POSITIONAL_PARAMS
|
|
|
|
| MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER, file, format, locale, valist);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-04 20:06:58 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* __stdio_common_vfwprintf_s (UCRTBASE.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL __stdio_common_vfwprintf_s(unsigned __int64 options, FILE *file, const wchar_t *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-09-04 20:06:58 +02:00
|
|
|
{
|
|
|
|
if (options & ~UCRTBASE_PRINTF_MASK)
|
|
|
|
FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
|
|
|
|
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfwprintf_helper((options & UCRTBASE_PRINTF_MASK) | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
|
|
|
|
file, format, locale, valist);
|
2017-09-04 20:06:58 +02:00
|
|
|
}
|
2016-04-07 00:56:43 +02:00
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#endif /* _MSVCR_VER >= 140 */
|
|
|
|
|
2017-09-19 01:41:52 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _vfprintf_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfprintf_l(FILE* file, const char *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-09-19 01:41:52 +02:00
|
|
|
{
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfprintf_helper(0, file, format, locale, valist);
|
2017-09-19 01:41:52 +02:00
|
|
|
}
|
|
|
|
|
2012-12-12 11:16:32 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _vfwprintf_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfwprintf_l(FILE* file, const wchar_t *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2012-12-12 11:16:32 +01:00
|
|
|
{
|
2017-10-07 01:24:14 +02:00
|
|
|
return vfwprintf_helper(0, file, format, locale, valist);
|
2012-12-12 11:16:32 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 20:23:29 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _vfprintf_p_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfprintf_p_l(FILE* file, const char *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-10-13 20:23:29 +02:00
|
|
|
{
|
|
|
|
return vfprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
|
|
|
|
file, format, locale, valist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _vfprintf_p (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfprintf_p(FILE* file, const char *format, __ms_va_list valist)
|
2017-10-13 20:23:29 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _vfprintf_p_l(file, format, NULL, valist);
|
2017-10-13 20:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _vfwprintf_p_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfwprintf_p_l(FILE* file, const wchar_t *format,
|
2020-11-26 19:19:18 +01:00
|
|
|
_locale_t locale, __ms_va_list valist)
|
2017-10-13 20:23:29 +02:00
|
|
|
{
|
|
|
|
return vfwprintf_helper(MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
|
|
|
|
file, format, locale, valist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _vfwprintf_p (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _vfwprintf_p(FILE* file, const wchar_t *format, __ms_va_list valist)
|
2017-10-13 20:23:29 +02:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return _vfwprintf_p_l(file, format, NULL, valist);
|
2017-10-13 20:23:29 +02:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* vprintf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vprintf(const char *format, __ms_va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return vfprintf(MSVCRT_stdout,format,valist);
|
2001-01-22 03:21:54 +01:00
|
|
|
}
|
|
|
|
|
2010-11-24 21:25:30 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* vprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vprintf_s(const char *format, __ms_va_list valist)
|
2010-11-24 21:25:30 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return vfprintf_s(MSVCRT_stdout,format,valist);
|
2010-11-24 21:25:30 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* vwprintf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vwprintf(const wchar_t *format, __ms_va_list valist)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return vfwprintf(MSVCRT_stdout,format,valist);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2010-11-24 21:25:30 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* vwprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL vwprintf_s(const wchar_t *format, __ms_va_list valist)
|
2010-11-24 21:25:30 +01:00
|
|
|
{
|
2020-12-03 19:11:20 +01:00
|
|
|
return vfwprintf_s(MSVCRT_stdout,format,valist);
|
2010-11-24 21:25:30 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fprintf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV fprintf(FILE* file, const char *format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_list valist;
|
2001-01-11 00:59:25 +01:00
|
|
|
int res;
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vfprintf(file, format, valist);
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_end(valist);
|
2001-01-11 00:59:25 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-11-24 21:25:49 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV fprintf_s(FILE* file, const char *format, ...)
|
2010-11-24 21:25:49 +01:00
|
|
|
{
|
|
|
|
__ms_va_list valist;
|
|
|
|
int res;
|
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vfprintf_s(file, format, valist);
|
2010-11-24 21:25:49 +01:00
|
|
|
__ms_va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fwprintf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV fwprintf(FILE* file, const wchar_t *format, ...)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_list valist;
|
2001-01-22 03:21:54 +01:00
|
|
|
int res;
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vfwprintf(file, format, valist);
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_end(valist);
|
2001-01-22 03:21:54 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-11-24 21:25:49 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* fwprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV fwprintf_s(FILE* file, const wchar_t *format, ...)
|
2010-11-24 21:25:49 +01:00
|
|
|
{
|
|
|
|
__ms_va_list valist;
|
|
|
|
int res;
|
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vfwprintf_s(file, format, valist);
|
2010-11-24 21:25:49 +01:00
|
|
|
__ms_va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-12-12 11:16:43 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _fwprintf_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV _fwprintf_l(FILE* file, const wchar_t *format, _locale_t locale, ...)
|
2012-12-12 11:16:43 +01:00
|
|
|
{
|
|
|
|
__ms_va_list valist;
|
|
|
|
int res;
|
|
|
|
__ms_va_start(valist, locale);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = _vfwprintf_l(file, format, locale, valist);
|
2012-12-12 11:16:43 +01:00
|
|
|
__ms_va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* printf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV printf(const char *format, ...)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_list valist;
|
2001-01-11 00:59:25 +01:00
|
|
|
int res;
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vfprintf(MSVCRT_stdout, format, valist);
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_end(valist);
|
2001-01-11 00:59:25 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-11-24 21:12:37 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* printf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV printf_s(const char *format, ...)
|
2010-11-24 21:12:37 +01:00
|
|
|
{
|
|
|
|
__ms_va_list valist;
|
|
|
|
int res;
|
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vprintf_s(format, valist);
|
2010-11-24 21:12:37 +01:00
|
|
|
__ms_va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-11 06:03:05 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* ungetc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL ungetc(int c, FILE * file)
|
2014-11-04 22:52:03 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-11-30 14:37:09 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
|
2014-11-04 22:52:03 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _ungetc_nolock(c, file);
|
|
|
|
_unlock_file(file);
|
2014-11-04 22:52:03 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _ungetc_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _ungetc_nolock(int c, FILE * file)
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2020-11-30 14:37:09 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
|
2014-06-26 12:29:54 +02:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if (c == EOF || !(file->_flag&_IOREAD ||
|
|
|
|
(file->_flag&_IORW && !(file->_flag&_IOWRT))))
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2011-05-20 13:22:20 +02:00
|
|
|
|
2020-12-02 00:53:28 +01:00
|
|
|
if((!(file->_flag & (_IONBF | _IOMYBUF | MSVCRT__USERBUF))
|
2014-06-27 10:17:24 +02:00
|
|
|
&& msvcrt_alloc_buffer(file))
|
2014-06-26 12:29:54 +02:00
|
|
|
|| (!file->_cnt && file->_ptr==file->_base))
|
2011-05-20 13:22:20 +02:00
|
|
|
file->_ptr++;
|
2014-06-26 12:29:54 +02:00
|
|
|
|
2011-05-20 13:22:20 +02:00
|
|
|
if(file->_ptr>file->_base) {
|
|
|
|
file->_ptr--;
|
2020-12-02 00:53:28 +01:00
|
|
|
if(file->_flag & _IOSTRG) {
|
2013-12-30 16:55:59 +01:00
|
|
|
if(*file->_ptr != c) {
|
|
|
|
file->_ptr++;
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2013-12-30 16:55:59 +01:00
|
|
|
}
|
|
|
|
}else {
|
|
|
|
*file->_ptr = c;
|
|
|
|
}
|
2011-05-20 13:22:20 +02:00
|
|
|
file->_cnt++;
|
2020-12-02 00:53:28 +01:00
|
|
|
file->_flag &= ~(_IOERR | _IOEOF);
|
|
|
|
file->_flag |= _IOREAD;
|
2011-05-20 13:22:20 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:37:09 +01:00
|
|
|
return EOF;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ungetwc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL ungetwc(wint_t wc, FILE * file)
|
2014-11-04 22:52:11 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
wint_t ret;
|
2014-11-04 22:52:11 +01:00
|
|
|
|
2020-11-30 14:37:05 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
|
2014-11-04 22:52:11 +01:00
|
|
|
|
2020-12-03 19:11:20 +01:00
|
|
|
_lock_file(file);
|
|
|
|
ret = _ungetwc_nolock(wc, file);
|
|
|
|
_unlock_file(file);
|
2014-11-04 22:52:11 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _ungetwc_nolock (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
wint_t CDECL _ungetwc_nolock(wint_t wc, FILE * file)
|
2002-03-11 06:03:05 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t mwc = wc;
|
2011-05-20 13:22:20 +02:00
|
|
|
|
2020-11-30 14:37:05 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(file != NULL)) return WEOF;
|
|
|
|
if (wc == WEOF)
|
|
|
|
return WEOF;
|
2012-10-23 18:09:48 +02:00
|
|
|
|
2014-11-11 15:58:08 +01:00
|
|
|
if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
|
|
|
|
|| !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
|
2013-12-30 16:55:42 +01:00
|
|
|
unsigned char * pp = (unsigned char *)&mwc;
|
|
|
|
int i;
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
for(i=sizeof(wchar_t)-1;i>=0;i--) {
|
2020-12-03 19:11:20 +01:00
|
|
|
if(pp[i] != _ungetc_nolock(pp[i],file))
|
2020-11-30 14:37:05 +01:00
|
|
|
return WEOF;
|
2013-12-30 16:55:42 +01:00
|
|
|
}
|
|
|
|
}else {
|
2020-11-27 14:17:01 +01:00
|
|
|
char mbs[MB_LEN_MAX];
|
2013-12-30 16:55:42 +01:00
|
|
|
int len;
|
|
|
|
|
2020-12-03 19:10:54 +01:00
|
|
|
len = wctomb(mbs, mwc);
|
2014-11-04 22:52:11 +01:00
|
|
|
if(len == -1)
|
2020-11-30 14:37:05 +01:00
|
|
|
return WEOF;
|
2013-12-30 16:55:42 +01:00
|
|
|
|
|
|
|
for(len--; len>=0; len--) {
|
2020-12-03 19:11:20 +01:00
|
|
|
if(mbs[len] != _ungetc_nolock(mbs[len], file))
|
2020-11-30 14:37:05 +01:00
|
|
|
return WEOF;
|
2013-12-30 16:55:42 +01:00
|
|
|
}
|
2011-05-20 13:22:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mwc;
|
2002-03-11 06:03:05 +01:00
|
|
|
}
|
|
|
|
|
2001-01-22 03:21:54 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* wprintf (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV wprintf(const wchar_t *format, ...)
|
2001-01-22 03:21:54 +01:00
|
|
|
{
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_list valist;
|
2001-01-22 03:21:54 +01:00
|
|
|
int res;
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vwprintf(format, valist);
|
2009-01-02 21:43:58 +01:00
|
|
|
__ms_va_end(valist);
|
2001-01-22 03:21:54 +01:00
|
|
|
return res;
|
|
|
|
}
|
2004-12-10 16:28:25 +01:00
|
|
|
|
2010-11-24 21:12:37 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* wprintf_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int WINAPIV wprintf_s(const wchar_t *format, ...)
|
2010-11-24 21:12:37 +01:00
|
|
|
{
|
|
|
|
__ms_va_list valist;
|
|
|
|
int res;
|
|
|
|
__ms_va_start(valist, format);
|
2020-12-03 19:11:20 +01:00
|
|
|
res = vwprintf_s(format, valist);
|
2010-11-24 21:12:37 +01:00
|
|
|
__ms_va_end(valist);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-10-18 00:34:02 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _getmaxstdio (MSVCRT.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _getmaxstdio(void)
|
2006-10-18 00:34:02 +02:00
|
|
|
{
|
2011-05-19 15:31:33 +02:00
|
|
|
return MSVCRT_max_streams;
|
2006-10-18 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
2006-10-30 21:50:32 +01:00
|
|
|
/*********************************************************************
|
2011-05-19 15:31:33 +02:00
|
|
|
* _setmaxstdio (MSVCRT.@)
|
2006-10-30 21:50:32 +01:00
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _setmaxstdio(int newmax)
|
2006-10-30 21:50:32 +01:00
|
|
|
{
|
2011-05-19 15:31:33 +02:00
|
|
|
TRACE("%d\n", newmax);
|
|
|
|
|
|
|
|
if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
MSVCRT_max_streams = newmax;
|
|
|
|
return MSVCRT_max_streams;
|
2006-10-30 21:50:32 +01:00
|
|
|
}
|
2015-11-04 11:25:27 +01:00
|
|
|
|
2018-01-04 05:33:00 +01:00
|
|
|
#if _MSVCR_VER >= 140
|
2015-11-04 11:25:27 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _get_stream_buffer_pointers (UCRTBASE.@)
|
|
|
|
*/
|
2020-12-03 19:11:20 +01:00
|
|
|
int CDECL _get_stream_buffer_pointers(FILE *file, char*** base,
|
2015-11-04 11:25:27 +01:00
|
|
|
char*** ptr, int** count)
|
|
|
|
{
|
|
|
|
if (base)
|
|
|
|
*base = &file->_base;
|
|
|
|
if (ptr)
|
|
|
|
*ptr = &file->_ptr;
|
|
|
|
if (count)
|
|
|
|
*count = &file->_cnt;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-04 05:33:00 +01:00
|
|
|
#endif
|