From 05cc71685fb0e177233655c4d499ad223ffe9a6f Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 1 Sep 2017 16:37:15 -0700
Subject: [PATCH] Merge glob-related changes from Gnulib

* include/scratch_buffer.h (struct scratch_buffer):
Use portable method to align buffer, instead of relying
on GCC's __attribute__ ((aligned (...))), so that this
can be compiled with non-GCC compilers when used as
part of Gnulib.
* malloc/scratch_buffer_grow.c:
* malloc/scratch_buffer_grow_preserve.c:
* malloc/scratch_buffer_set_array_size.c:
Include <libc-config.h> first thing, if !_LIBC.
Remove stray top-level ";" that non-GCC compilers reject.
* misc/sys/cdefs.h: Do not include <bits/wordsize.h> and
<bits/long-double.h> if __WORDSIZE is already defined, so that
Gnulib-using code does not include these nonexistent files.
* posix/glob.c: Minor simplifications due to Gnulib changes:
do not include <config.h>, and do not define __set_errno.
does that via another method for glob.c.
---
 include/scratch_buffer.h               | 3 +--
 malloc/scratch_buffer_grow.c           | 6 +++++-
 malloc/scratch_buffer_grow_preserve.c  | 6 +++++-
 malloc/scratch_buffer_set_array_size.c | 6 +++++-
 misc/sys/cdefs.h                       | 8 ++++++--
 posix/glob.c                           | 8 --------
 6 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/include/scratch_buffer.h b/include/scratch_buffer.h
index dd17a4a7e1..bb04662eb2 100644
--- a/include/scratch_buffer.h
+++ b/include/scratch_buffer.h
@@ -66,8 +66,7 @@
 struct scratch_buffer {
   void *data;    /* Pointer to the beginning of the scratch area.  */
   size_t length; /* Allocated space at the data pointer, in bytes.  */
-  char __space[1024]
-    __attribute__ ((aligned (__alignof__ (max_align_t))));
+  max_align_t __space[(1023 + sizeof (max_align_t)) / sizeof (max_align_t)];
 };
 
 /* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space
diff --git a/malloc/scratch_buffer_grow.c b/malloc/scratch_buffer_grow.c
index 22bae506a1..d2df028654 100644
--- a/malloc/scratch_buffer_grow.c
+++ b/malloc/scratch_buffer_grow.c
@@ -16,6 +16,10 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#ifndef _LIBC
+# include <libc-config.h>
+#endif
+
 #include <scratch_buffer.h>
 #include <errno.h>
 
@@ -49,4 +53,4 @@ __libc_scratch_buffer_grow (struct scratch_buffer *buffer)
   buffer->length = new_length;
   return true;
 }
-libc_hidden_def (__libc_scratch_buffer_grow);
+libc_hidden_def (__libc_scratch_buffer_grow)
diff --git a/malloc/scratch_buffer_grow_preserve.c b/malloc/scratch_buffer_grow_preserve.c
index 18543ef85b..9268615311 100644
--- a/malloc/scratch_buffer_grow_preserve.c
+++ b/malloc/scratch_buffer_grow_preserve.c
@@ -16,6 +16,10 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#ifndef _LIBC
+# include <libc-config.h>
+#endif
+
 #include <scratch_buffer.h>
 #include <errno.h>
 #include <string.h>
@@ -60,4 +64,4 @@ __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
   buffer->length = new_length;
   return true;
 }
-libc_hidden_def (__libc_scratch_buffer_grow_preserve);
+libc_hidden_def (__libc_scratch_buffer_grow_preserve)
diff --git a/malloc/scratch_buffer_set_array_size.c b/malloc/scratch_buffer_set_array_size.c
index 8ab6d9d300..6fcc115340 100644
--- a/malloc/scratch_buffer_set_array_size.c
+++ b/malloc/scratch_buffer_set_array_size.c
@@ -16,6 +16,10 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#ifndef _LIBC
+# include <libc-config.h>
+#endif
+
 #include <scratch_buffer.h>
 #include <errno.h>
 #include <limits.h>
@@ -57,4 +61,4 @@ __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
   buffer->length = new_length;
   return true;
 }
-libc_hidden_def (__libc_scratch_buffer_set_array_size);
+libc_hidden_def (__libc_scratch_buffer_set_array_size)
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index cfd39d5302..d4dd8d0418 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -415,8 +415,12 @@
       [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
 #endif
 
-#include <bits/wordsize.h>
-#include <bits/long-double.h>
+/* The #ifndef lets Gnulib avoid including these on non-glibc
+   platforms, where the includes typically do not exist.  */
+#ifndef __WORDSIZE
+# include <bits/wordsize.h>
+# include <bits/long-double.h>
+#endif
 
 #if defined __LONG_DOUBLE_MATH_OPTIONAL && defined __NO_LONG_DOUBLE_MATH
 # define __LDBL_COMPAT 1
diff --git a/posix/glob.c b/posix/glob.c
index c0ee4dacb5..7ddc2c8d70 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -15,10 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-#ifndef _LIBC
-# include <config.h>
-#endif
-
 #include <glob.h>
 
 #include <errno.h>
@@ -39,10 +35,6 @@
 #endif
 
 #include <errno.h>
-#ifndef __set_errno
-# define __set_errno(val) errno = (val)
-#endif
-
 #include <dirent.h>
 #include <stdlib.h>
 #include <string.h>
-- 
2.13.5

