summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2017-04-06 15:30:56 -0700
committerH. Peter Anvin <hpa@zytor.com>2017-04-06 15:48:51 -0700
commit3118429089d22af93099f7eea64c062842a8e499 (patch)
treeeb9a6ed31f831dabc0c75da8eb0254905a23f6e2
parent0da1549b7386c60669b34819b0c93b700d212b3c (diff)
downloadnasm-3118429089d22af93099f7eea64c062842a8e499.tar.gz
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>
-rw-r--r--config/msvc.h3
-rw-r--r--configure.ac8
-rw-r--r--nasmlib/file.c6
-rw-r--r--nasmlib/file.h69
4 files changed, 59 insertions, 27 deletions
diff --git a/config/msvc.h b/config/msvc.h
index 4ade0273..631652ef 100644
--- a/config/msvc.h
+++ b/config/msvc.h
@@ -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
diff --git a/configure.ac b/configure.ac
index 9e84627e..00774e66 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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__)
diff --git a/nasmlib/file.c b/nasmlib/file.c
index 93d61117..95f3c52a 100644
--- a/nasmlib/file.c
+++ b/nasmlib/file.c
@@ -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;
diff --git a/nasmlib/file.h b/nasmlib/file.h
index df79ee89..4069ec64 100644
--- a/nasmlib/file.h
+++ b/nasmlib/file.h
@@ -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
-# define nasm_ftruncate(fd,size) _chsize_s(fd,size)
-# elif defined(HAVE__CHSIZE)
-# define nasm_ftruncate(fd,size) _chsize(fd,size)
-# elif defined(HAVE_FTRUNCATE)
-# define nasm_ftruncate(fd,size) ftruncate(fd,size)
-# endif
+#ifdef HAVE__CHSIZE_S
+# define nasm_ftruncate(fd,size) _chsize_s(fd,size)
+#elif defined(HAVE__CHSIZE)
+# define nasm_ftruncate(fd,size) _chsize(fd,size)
+#elif defined(HAVE_FTRUNCATE)
+# define nasm_ftruncate(fd,size) ftruncate(fd,size)
#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
-# define nasm_stat _stati64
-# if defined(HAVE_FILENO) && defined(HAVE__FSTATI64)
+#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
+# endif
+# ifdef HAVE__FSTATI64
# define nasm_fstat _fstati64
# endif
-#elif defined(HAVE_STAT)
-# define nasm_stat stat
-# if defined(HAVE_FILENO) && defined(HAVE_FSTAT)
+#elif defined(HAVE_STRUCT_STAT)
+typedef struct stat nasm_struct_stat;
+# ifdef HAVE_STAT
+# define nasm_stat stat
+# 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 */