diff --git a/src/port/crypt.c b/src/port/crypt.c
index 2c71be98a9407704b936356a9058f488ce5dacda..5488fe60ce2d7562b7dd3be5e4ac5540cc137a9d 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 ed8b8b9b577737917e4579743629a9017cb32dad..47fb372cff2dd6f6530dce0765856999286c4d7e 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 05d1900ead452fa8da8238eff6367d0c9aa228ae..fdf8bcfab632ca8402128bd60c8d4c13007e3824 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 b8b471a91b460065f499728de89fbb8b2f2ab482..a168bdd45bd3fbb71bf442b7b6322692553a1013 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 8451a872aa1e4197f961bbd68c1df481e62b7a43..48d10fb9ee41c5eeaa0473d319f0a6bf9e1cf794 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 4c1f65d7767cfbc24f86c0f77abd97a55d2509ba..2bf0c8c9e34c9341b0ce59cdbbf0a08fe8866e96 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 da8fd56b6a1be6e9d20d29ceba8df1765ef05ad1..49d87d4c6f62ad9f4c75a1e2e420f91f19fa6657 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 7643122470e10c7d7adc55abe8556710e160ac85..c6df6c8df60f9f71a596d1caa522fd1f273aa0d6 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 904f40b5df3ec4cb22c360d4c80813e53927b86e..1e5fb3630ed306ea0e742e0f91ef66bc96c51441 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 6e1d37d3cbae9f7e554067b3d945c1ef47c1c5d1..fd7e010c1c9f4871977304ed8edd4228fe4efeb0 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 08b5389044200b4743d7a5f2e5ae7c391c60f2a0..607fcaccb9cda03ad35aadfe98fc6a4552b5b525 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 976ea41fd5c49b57421b75e6cacb5a21b3c87255..227afa7b1a684a554b35ebdf12e25a1c786f8e68 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 04a5eb58b249b88848419c2c4d5db9527c45a373..e7024b038efd5e23be744a1e4f8303b31a5d7fb6 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 167f4722b3fae2157b9a755d9c3d70b3d2759520..e25a6a42cd80b069452e1353f8dde093e91783df 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 a4cb16d4af341e3d6a257058c51dbb574e975519..2854b6601102d244a1e31da3c2d3eba370d99c73 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 00de19b62d5ebcecd0bfdf501d7523ceb8657bc2..eebeb1b858b99d2ebc2a92fc317482cc364ed383 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 3e0a92f06f2139ec58d5d21b126d47e5785a77c5..6bf14c5ff631b532f9b15a82b1ce6fe138b282d3 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 5cb7f7855a6535260f87dfee389465d585e75824..9cfe4fa458f97dd0d47da3509a7c36ca7e3c0f46 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 d61b2b41caa87912a71d2426a791e168fa7d48dc..bfdf1bd2829faa09e6b96f40a662ed6bbe05dba6 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