From 06e1d62689d2d3ceca014b46476f81ef2d9ceeac Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 28 Jul 2005 04:03:14 +0000
Subject: [PATCH] Fix a whole bunch of #includes that were either wrong or
 redundant. The first rule of portability for us is 'thou shalt have no other
 gods before c.h', and a whole lot of these files were either not including
 c.h at all, or including random system headers beforehand, either of which
 sins can mess up largefile support nicely.  Once you have included c.h, there
 is no need to re-include what it includes, either.

---
 src/port/crypt.c       |  3 ---
 src/port/fseeko.c      |  6 ++----
 src/port/getaddrinfo.c |  3 +--
 src/port/gethostname.c |  4 +---
 src/port/getopt.c      |  4 ----
 src/port/getopt_long.c | 11 +++++++----
 src/port/getrusage.c   |  6 ++----
 src/port/memcmp.c      |  5 +++--
 src/port/noblock.c     |  4 ++--
 src/port/open.c        |  6 +++---
 src/port/qsort.c       |  6 ++----
 src/port/random.c      |  5 ++---
 src/port/snprintf.c    |  4 ++--
 src/port/srandom.c     |  5 ++---
 src/port/strdup.c      |  7 ++++---
 src/port/strerror.c    |  7 +++----
 src/port/strtol.c      |  5 +++--
 src/port/strtoul.c     |  5 +++--
 src/port/thread.c      |  5 ++---
 19 files changed, 44 insertions(+), 57 deletions(-)

diff --git a/src/port/crypt.c b/src/port/crypt.c
index 2c71be98a94..5488fe60ce2 100644
--- a/src/port/crypt.c
+++ b/src/port/crypt.c
@@ -43,10 +43,7 @@ __RCSID("$NetBSD: crypt.c,v 1.18 2001/03/01 14:37:35 wiz Exp $");
 
 #include "c.h"
 
-#include <stddef.h>
-#include <sys/types.h>
 #include <limits.h>
-#include <stdlib.h>
 
 #ifndef WIN32
 #include <unistd.h>
diff --git a/src/port/fseeko.c b/src/port/fseeko.c
index ed8b8b9b577..47fb372cff2 100644
--- a/src/port/fseeko.c
+++ b/src/port/fseeko.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/fseeko.c,v 1.17 2004/12/31 22:03:53 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/port/fseeko.c,v 1.18 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -24,10 +24,8 @@
 #ifdef bsdi
 #include <pthread.h>
 #endif
-#include <stdio.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <errno.h>
+
 
 /*
  *	On BSD/OS and NetBSD, off_t and fpos_t are the same.  Standards
diff --git a/src/port/getaddrinfo.c b/src/port/getaddrinfo.c
index 05d1900ead4..fdf8bcfab63 100644
--- a/src/port/getaddrinfo.c
+++ b/src/port/getaddrinfo.c
@@ -12,7 +12,7 @@
  * Copyright (c) 2003-2005, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/getaddrinfo.c,v 1.16 2005/01/01 20:44:33 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/port/getaddrinfo.c,v 1.17 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -21,7 +21,6 @@
 #include "c.h"
 
 #ifndef WIN32_CLIENT_ONLY
-#include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
diff --git a/src/port/gethostname.c b/src/port/gethostname.c
index b8b471a91b4..a168bdd45bd 100644
--- a/src/port/gethostname.c
+++ b/src/port/gethostname.c
@@ -7,15 +7,13 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/gethostname.c,v 1.6 2004/12/31 22:03:53 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/port/gethostname.c,v 1.7 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "c.h"
 
-#include <string.h>
-
 #include <sys/utsname.h>
 
 int
diff --git a/src/port/getopt.c b/src/port/getopt.c
index 8451a872aa1..48d10fb9ee4 100644
--- a/src/port/getopt.c
+++ b/src/port/getopt.c
@@ -34,14 +34,10 @@
 
 #include "c.h"
 
-
 #if defined(LIBC_SCCS) && !defined(lint)
 static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
 #endif   /* LIBC_SCCS and not lint */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
 int			opterr = 1,			/* if error message should be printed */
 			optind = 1,			/* index into parent argv vector */
diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c
index 4c1f65d7767..2bf0c8c9e34 100644
--- a/src/port/getopt_long.c
+++ b/src/port/getopt_long.c
@@ -35,19 +35,22 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $PostgreSQL: pgsql/src/port/getopt_long.c,v 1.3 2003/11/29 19:52:13 pgsql Exp $
+ * $PostgreSQL: pgsql/src/port/getopt_long.c,v 1.4 2005/07/28 04:03:14 tgl Exp $
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "c.h"
 
 #include "getopt_long.h"
 
+#ifndef HAVE_INT_OPTRESET
+int			optreset;
+#endif
+
 #define BADCH	'?'
 #define BADARG	':'
 #define EMSG	""
 
+
 int
 getopt_long(int argc, char *const argv[],
 			const char *optstring,
diff --git a/src/port/getrusage.c b/src/port/getrusage.c
index da8fd56b6a1..49d87d4c6f6 100644
--- a/src/port/getrusage.c
+++ b/src/port/getrusage.c
@@ -8,15 +8,13 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/getrusage.c,v 1.9 2005/05/16 05:52:13 neilc Exp $
+ *	  $PostgreSQL: pgsql/src/port/getrusage.c,v 1.10 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
-#include <stdio.h>
-#include <errno.h>
-
 #include "c.h"
+
 #include "rusagestub.h"
 
 /* This code works on:
diff --git a/src/port/memcmp.c b/src/port/memcmp.c
index 7643122470e..c6df6c8df60 100644
--- a/src/port/memcmp.c
+++ b/src/port/memcmp.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/memcmp.c,v 1.7 2004/12/31 22:03:53 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/port/memcmp.c,v 1.8 2005/07/28 04:03:14 tgl Exp $
  *
  * This file was taken from NetBSD and is used by SunOS because memcmp
  * on that platform does not properly compare negative bytes. The
@@ -46,7 +46,8 @@
  * SUCH DAMAGE.
  */
 
-#include <string.h>
+#include "c.h"
+
 
 /*
  * Compare memory regions.
diff --git a/src/port/noblock.c b/src/port/noblock.c
index 904f40b5df3..1e5fb3630ed 100644
--- a/src/port/noblock.c
+++ b/src/port/noblock.c
@@ -7,16 +7,16 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/noblock.c,v 1.6 2005/03/25 00:34:31 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/port/noblock.c,v 1.7 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "c.h"
 
-#include <sys/types.h>
 #include <fcntl.h>
 
+
 bool
 pg_set_noblock(int sock)
 {
diff --git a/src/port/open.c b/src/port/open.c
index 6e1d37d3cba..fd7e010c1c9 100644
--- a/src/port/open.c
+++ b/src/port/open.c
@@ -6,17 +6,17 @@
  *
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/port/open.c,v 1.9 2005/03/24 04:36:20 momjian Exp $
+ * $PostgreSQL: pgsql/src/port/open.c,v 1.10 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #ifdef WIN32
 
-#include <postgres.h>
+#include "c.h"
+
 #include <windows.h>
 #include <fcntl.h>
-#include <errno.h>
 #include <assert.h>
 
 int win32_open(const char *fileName, int fileFlags, ...);
diff --git a/src/port/qsort.c b/src/port/qsort.c
index 08b53890442..607fcaccb9c 100644
--- a/src/port/qsort.c
+++ b/src/port/qsort.c
@@ -3,7 +3,7 @@
  *	  Add do ... while() macro fix
  *	  Remove __inline, _DIAGASSERTs, __P
  *
- *	$PostgreSQL: pgsql/src/port/qsort.c,v 1.6 2005/05/25 21:40:43 momjian Exp $
+ *	$PostgreSQL: pgsql/src/port/qsort.c,v 1.7 2005/07/28 04:03:14 tgl Exp $
  */
 
 /*	$NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $	*/
@@ -37,9 +37,7 @@
  * SUCH DAMAGE.
  */
 
-#include <stdlib.h>
-#include <errno.h>
-#include <sys/types.h>
+#include "c.h"
 
 
 static char *med3(char *, char *, char *,
diff --git a/src/port/random.c b/src/port/random.c
index 976ea41fd5c..227afa7b1a6 100644
--- a/src/port/random.c
+++ b/src/port/random.c
@@ -8,16 +8,15 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/random.c,v 1.5 2004/12/31 22:03:53 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/port/random.c,v 1.6 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "c.h"
 
-#include <stdlib.h>
 #include <math.h>
-#include <errno.h>
+
 
 long
 random()
diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 04a5eb58b24..e7024b038ef 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -32,7 +32,7 @@
  * SUCH DAMAGE.
  */
 
-#include "postgres.h"
+#include "c.h"
 
 #ifndef WIN32
 #include <sys/ioctl.h>
@@ -62,7 +62,7 @@
  * causing nasty effects.
  **************************************************************/
 
-/*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.27 2005/04/14 20:53:09 tgl Exp $";*/
+/*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.28 2005/07/28 04:03:14 tgl Exp $";*/
 
 static void dopr(char *buffer, const char *format, va_list args, char *end);
 
diff --git a/src/port/srandom.c b/src/port/srandom.c
index 167f4722b3f..e25a6a42cd8 100644
--- a/src/port/srandom.c
+++ b/src/port/srandom.c
@@ -8,16 +8,15 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/srandom.c,v 1.5 2004/12/31 22:03:53 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/port/srandom.c,v 1.6 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "c.h"
 
-#include <stdlib.h>
 #include <math.h>
-#include <errno.h>
+
 
 void
 srandom(unsigned int seed)
diff --git a/src/port/strdup.c b/src/port/strdup.c
index a4cb16d4af3..2854b660110 100644
--- a/src/port/strdup.c
+++ b/src/port/strdup.c
@@ -8,15 +8,16 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/strdup.c,v 1.6 2004/12/31 22:03:53 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/port/strdup.c,v 1.7 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
-#include <string.h>
-#include <stdlib.h>
+#include "c.h"
+
 #include "strdup.h"
 
+
 char *
 strdup(char const * string)
 {
diff --git a/src/port/strerror.c b/src/port/strerror.c
index 00de19b62d5..eebeb1b858b 100644
--- a/src/port/strerror.c
+++ b/src/port/strerror.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/port/strerror.c,v 1.4 2005/02/22 04:43:16 momjian Exp $ */
+/* $PostgreSQL: pgsql/src/port/strerror.c,v 1.5 2005/07/28 04:03:14 tgl Exp $ */
 
 /*
  * strerror - map error number to descriptive string
@@ -9,9 +9,8 @@
  * modified for ANSI by D'Arcy J.M. Cain
  */
 
-#include <string.h>
-#include <stdio.h>
-#include <errno.h>
+#include "c.h"
+
 
 extern const char *const sys_errlist[];
 extern int	sys_nerr;
diff --git a/src/port/strtol.c b/src/port/strtol.c
index 3e0a92f06f2..6bf14c5ff63 100644
--- a/src/port/strtol.c
+++ b/src/port/strtol.c
@@ -36,10 +36,11 @@
 static char sccsid[] = "@(#)strtol.c	5.4 (Berkeley) 2/23/91";
 #endif   /* LIBC_SCCS and not lint */
 
+#include "c.h"
+
 #include <limits.h>
 #include <ctype.h>
-#include <errno.h>
-#include <stdlib.h>
+
 
 #define const
 
diff --git a/src/port/strtoul.c b/src/port/strtoul.c
index 5cb7f7855a6..9cfe4fa458f 100644
--- a/src/port/strtoul.c
+++ b/src/port/strtoul.c
@@ -35,10 +35,11 @@
 static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
 #endif   /* LIBC_SCCS and not lint */
 
+#include "c.h"
+
 #include <limits.h>
 #include <ctype.h>
-#include <errno.h>
-#include <stdlib.h>
+
 
 /*
  * Convert a string to an unsigned long integer.
diff --git a/src/port/thread.c b/src/port/thread.c
index d61b2b41caa..bfdf1bd2829 100644
--- a/src/port/thread.c
+++ b/src/port/thread.c
@@ -7,15 +7,13 @@
  *
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/port/thread.c,v 1.29 2004/12/31 22:03:53 pgsql Exp $
+ * $PostgreSQL: pgsql/src/port/thread.c,v 1.30 2005/07/28 04:03:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "c.h"
 
-#include <sys/types.h>
-#include <errno.h>
 #ifdef WIN32_CLIENT_ONLY
 #undef ERROR
 #else
@@ -25,6 +23,7 @@
 #include <pthread.h>
 #endif
 
+
 /*
  *	Threading sometimes requires specially-named versions of functions
  *	that return data in static buffers, like strerror_r() instead of
-- 
GitLab