Windows: clean up the handling of stat on Windows

[f]stat on Windows is messy: we need to use _stati64 for maximum
compatibility, but because there is a bunch of stuff wrapped in
macros, autoconf sometimes gets the wrong answers.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2017-04-06 15:30:56 -07:00
parent 0da1549b73
commit 3118429089
4 changed files with 59 additions and 27 deletions

View file

@ -113,6 +113,9 @@
/* Define to 1 if you have the `_fullpath' function. */
#define HAVE__FULLPATH 1
/* Define to 1 if the system has the type `struct _stati64'. */
#define HAVE_STRUCT__STATI64
/* Define to 1 if you have the `_stati64' function. */
#define HAVE__STATI64 1

View file

@ -116,8 +116,6 @@ AC_CHECK_FUNCS([ftruncate _chsize _chsize_s])
AC_CHECK_FUNCS([fileno _fileno])
AC_CHECK_FUNCS(_filelengthi64)
AC_CHECK_FUNCS([stat _stati64])
AC_CHECK_FUNCS([fstat _fstati64])
AC_FUNC_MMAP
AC_CHECK_FUNCS(getpagesize)
AC_CHECK_FUNCS(sysconf)
@ -134,6 +132,12 @@ AC_CHECK_FUNCS([vsnprintf _vsnprintf])
AC_CHECK_FUNCS([snprintf _snprintf])
AC_CHECK_FUNCS([strlcpy])
dnl These types are POSIX-specific, and Windows does it differently...
AC_CHECK_TYPES([struct _stati64])
AC_CHECK_TYPES([struct stat])
AC_CHECK_FUNCS([stat _stati64])
AC_CHECK_FUNCS([fstat _fstati64])
dnl Check for functions that might not be declared in the headers for
dnl various idiotic reasons (mostly because of library authors
dnl abusing the meaning of __STRICT_ANSI__)

View file

@ -186,10 +186,10 @@ bool nasm_file_exists(const char *filename)
*/
off_t nasm_file_size(FILE *f)
{
#if defined(HAVE_FILENO) && defined(HAVE__FILELENGTHI64)
#ifdef HAVE__FILELENGTHI64
return _filelengthi64(fileno(f));
#elif defined(nasm_fstat)
struct nasm_stat st;
nasm_struct_stat st;
if (nasm_fstat(fileno(f), &st))
return (off_t)-1;
@ -209,7 +209,7 @@ off_t nasm_file_size(FILE *f)
off_t nasm_file_size_by_path(const char *pathname)
{
#ifdef nasm_stat
struct nasm_stat st;
nasm_struct_stat st;
if (nasm_stat(pathname, &st))
return (off_t)-1;

View file

@ -59,11 +59,6 @@
# include <sys/mman.h>
#endif
#if !defined(HAVE_FILENO) && defined(HAVE__FILENO)
# define HAVE_FILENO 1
# define fileno _fileno
#endif
#if !defined(HAVE_ACCESS) && defined(HAVE__ACCESS)
# define HAVE_ACCESS 1
# define access _access
@ -73,31 +68,61 @@
#endif
/* Can we adjust the file size without actually writing all the bytes? */
#ifdef HAVE_FILENO /* Useless without fileno() */
# ifdef HAVE__CHSIZE_S
#ifdef HAVE__CHSIZE_S
# define nasm_ftruncate(fd,size) _chsize_s(fd,size)
# elif defined(HAVE__CHSIZE)
#elif defined(HAVE__CHSIZE)
# define nasm_ftruncate(fd,size) _chsize(fd,size)
# elif defined(HAVE_FTRUNCATE)
#elif defined(HAVE_FTRUNCATE)
# define nasm_ftruncate(fd,size) ftruncate(fd,size)
# endif
#endif
/*
* On Win32, stat has a 32-bit file size but _stati64 has a 64-bit file
* size. However, as "stat" is already a macro, don't confuse the situation
* further by redefining it, instead we create our own.
* On Win32/64, stat has a 32-bit file size but _stati64 has a 64-bit file
* size. Things get complicated because some of these may be macros,
* which autoconf won't pick up on as the standard autoconf tests do
* #undef.
*/
#ifdef HAVE__STATI64
#ifdef _stati64
# define HAVE_STRUCT__STATI64 1
# define HAVE__STATI64 1
#endif
#ifdef _fstati64
# define HAVE__FSTATI64 1
#endif
#ifdef HAVE_STRUCT__STATI64
typedef struct _stati64 nasm_struct_stat;
# ifdef HAVE__STATI64
# define nasm_stat _stati64
# if defined(HAVE_FILENO) && defined(HAVE__FSTATI64)
# endif
# ifdef HAVE__FSTATI64
# define nasm_fstat _fstati64
# endif
#elif defined(HAVE_STAT)
#elif defined(HAVE_STRUCT_STAT)
typedef struct stat nasm_struct_stat;
# ifdef HAVE_STAT
# define nasm_stat stat
# if defined(HAVE_FILENO) && defined(HAVE_FSTAT)
# endif
# ifdef HAVE_FSTAT
# define nasm_fstat fstat
# endif
#endif
#ifndef HAVE_FILENO
# ifdef fileno /* autoconf doesn't always pick up macros */
# define HAVE_FILENO 1
# elif defined(HAVE__FILENO)
# define HAVE_FILENO 1
# define fileno _fileno
# endif
#endif
/* These functions are utterly useless without fileno() */
#ifndef HAVE_FILENO
# undef nasm_fstat
# undef nasm_ftruncate
# undef HAVE_MMAP
# undef HAVE__FILELENGTHI64
#endif
#endif /* NASMLIB_FILE_H */