diff --git a/clients/fax500/fax500.h b/clients/fax500/fax500.h
index 41d08495ec7e44418533e569cd1955854064039c..74c8809678b578e0ab728b0cf92b131b6519129a 100644
--- a/clients/fax500/fax500.h
+++ b/clients/fax500/fax500.h
@@ -1,10 +1,10 @@
-#include <stdio.h>
-#include <ac/ctype.h>
-#include <ac/string.h>
-extern char *strdup (const char *);
+#ifndef FAX500_H
+#define FAX500_H 1
 
 /* in faxtotpc.c */
 void  strip_nonnum ( char *str );
 char *remove_parens( char *ibuf, char *obuf );
 char *munge_phone  ( char *ibuf, char *obuf );
 char *faxtotpc     ( char *phone, char *userinfo );
+
+#endif
diff --git a/clients/fax500/faxtotpc.c b/clients/fax500/faxtotpc.c
index 73cadf2f16e767ff313339b905780969b295e959..b5b677b5a02290226416043cc813a34f532be56f 100644
--- a/clients/fax500/faxtotpc.c
+++ b/clients/fax500/faxtotpc.c
@@ -19,9 +19,15 @@
  */
 
 #include "portable.h"
-#include "fax500.h"
+
+#include <stdio.h>
 #include <sys/types.h>
 
+#include <ac/ctype.h>
+#include <ac/string.h>
+
+#include "fax500.h"
+
 #define	TPCDOMAIN	"tpc.int"
 
 /*
diff --git a/clients/fax500/main.c b/clients/fax500/main.c
index bc6d6a869ef0b86f1b9b7390eb254baa909fb1e8..cf5fb5e7fd07cfbaffaf116a6562e31810c3dc7c 100644
--- a/clients/fax500/main.c
+++ b/clients/fax500/main.c
@@ -11,29 +11,32 @@
  */
 
 #include "portable.h"
-#include "fax500.h"
 
+#include <stdio.h>
 #include <stdlib.h>
 
 #include <ac/socket.h>
+#include <ac/string.h>
 #include <ac/syslog.h>
 #include <ac/time.h>
 #include <ac/wait.h>
 #include <ac/unistd.h>
 #include <ac/errno.h>
-extern int	optind, errno;
-extern char	*optarg;
 
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
 #endif
-
+#ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
+#endif
+
 #include <sysexits.h>
 
 #include "lber.h"
 #include "ldap.h"
 
+#include "fax500.h"
+
 #include <ldapconfig.h>
 
 #define USER		0
diff --git a/clients/fax500/rp500.c b/clients/fax500/rp500.c
index eb54335dc42bd869dd407abb843b2e2619496f18..6d5d955e060e855ee1cd4a0d8327675824623a34 100644
--- a/clients/fax500/rp500.c
+++ b/clients/fax500/rp500.c
@@ -11,21 +11,26 @@
  */
 
 #include "portable.h"
-#include "fax500.h"
 
 #include <stdlib.h>
-#include <signal.h>
 
+#include <ac/signal.h>
 #include <ac/socket.h>
+#include <ac/string.h>
 #include <ac/syslog.h>
 #include <ac/time.h>
+#include <ac/unistd.h>
 #include <ac/wait.h>
 
+#ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
+#endif
 
 #include <lber.h>
 #include <ldap.h>
 
+#include "fax500.h"
+
 #include <ldapconfig.h>
 
 #define DEFAULT_PORT		79
@@ -61,8 +66,6 @@ main( int argc, char **argv )
 	static char	*attrs[] = { "title", "o", "ou", "postalAddress",
 					"telephoneNumber", "mail",
 					"facsimileTelephoneNumber", NULL };
-	extern char	*optarg;
-	extern int	optind;
 
 	while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
 		switch( i ) {
diff --git a/clients/finger/main.c b/clients/finger/main.c
index a617ae9b56b46097c11583e4761789b7612c9a69..0a53a63efba33c49e6039f58064256a0a3084a10 100644
--- a/clients/finger/main.c
+++ b/clients/finger/main.c
@@ -14,19 +14,19 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
-#include <signal.h>
 
+#include <ac/ctype.h>
+#include <ac/signal.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/syslog.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
 #include <ac/wait.h>
-extern char *strdup (const char *);
-extern int strcasecmp(const char *, const char *);
 
+#ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
+#endif
 
 #include "lber.h"
 #include "ldap.h"
@@ -66,7 +66,6 @@ main( int argc, char **argv )
 	struct sockaddr_in	peername;
 	int			peernamelen;
 	int			interactive = 0;
-	extern char		*optarg;
 
 	deref = FINGER_DEREF;
 	while ( (i = getopt( argc, argv, "f:ilp:t:x:p:c:" )) != EOF ) {
diff --git a/clients/gopher/go500.c b/clients/gopher/go500.c
index 3f21c9cbbcff2f02b05f262e0940ba60f0d84b12..de0059380ab5bdd4d3da3c35e7f4044bbfed8a91 100644
--- a/clients/gopher/go500.c
+++ b/clients/gopher/go500.c
@@ -23,9 +23,6 @@
 #include <ac/time.h>
 #include <ac/unistd.h>
 #include <ac/wait.h>
-extern char *strdup (const char *);
-extern int strcasecmp(const char *, const char *);
-extern int gethostname (char *, int);
 
 #include <ac/setproctitle.h>
 
@@ -33,7 +30,9 @@ extern int gethostname (char *, int);
 #include <sys/param.h>
 #endif
 
+#ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
+#endif
 
 #include "ldapconfig.h"
 #include "lber.h"
@@ -83,7 +82,6 @@ main( int argc, char **argv )
 	struct hostent		*hp;
 	struct sockaddr_in	from;
 	int			fromlen;
-	extern char		*optarg;
 
 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
 	/* for setproctitle */
@@ -559,7 +557,7 @@ do_search( LDAP *ld, FILE *fp, char *buf )
 static int
 entry2textwrite( void *fp, char *buf, int len )
 {
-        return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
+	return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
 }
 
 static void
diff --git a/clients/gopher/go500gw.c b/clients/gopher/go500gw.c
index 5e903beb65cfa23c375b8c170232a62f47848e34..ce37e796ecbab9840b178aec49640fb633c182a6 100644
--- a/clients/gopher/go500gw.c
+++ b/clients/gopher/go500gw.c
@@ -23,16 +23,18 @@
 #include <ac/time.h>
 #include <ac/unistd.h>
 #include <ac/wait.h>
-extern int strcasecmp(const char *, const char *);
 
 #include <ac/setproctitle.h>
 
-#include <sys/resource.h>
-
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
 #endif
 
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+
+
 #include "lber.h"
 #include "ldap.h"
 #include "ldap_log.h"
@@ -94,7 +96,6 @@ main (int  argc, char **argv )
 	struct hostent		*hp;
 	struct sockaddr_in	from;
 	int			fromlen;
-	extern char		*optarg;
 
 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
 	/* for setproctitle */
diff --git a/clients/mail500/main.c b/clients/mail500/main.c
index 4efa295f4c925be5e2a42c2f7ab99c1b43a45da4..7acc1d74d5e4aade61dc0ba3504c037c06d87f0f 100644
--- a/clients/mail500/main.c
+++ b/clients/mail500/main.c
@@ -14,22 +14,21 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
 
+#include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/syslog.h>
 #include <ac/time.h>
 #include <ac/wait.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
-extern int	optind, errno;
-extern char	*optarg;
 
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
 #endif
 
+#ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
+#endif
 
 #include <sysexits.h>
 
@@ -145,10 +144,10 @@ static char **get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
 static char *canonical( char *s );
 static int  connect_to_x500( void );
 
-void do_group_errors( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
-void do_group_request( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
-void do_group_owner( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
-void add_member( char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, char **suppress );
+static void do_group_errors( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
+static void do_group_request( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
+static void do_group_owner( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
+static void add_member( char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, char **suppress );
 
 int
 main ( int argc, char **argv )
@@ -915,7 +914,7 @@ do_group_members(
 	}
 }
 
-void
+static void
 add_member(
 	char	*gdn,
 	char	*dn,
@@ -985,7 +984,7 @@ add_member(
 	free( ndn );
 }
 
-void
+static void
 do_group_request(
 	LDAPMessage *e,
 	char	*dn,
@@ -1007,7 +1006,7 @@ do_group_request(
 	}
 }
 
-void
+static void
 do_group_errors(
 	LDAPMessage *e,
 	char	*dn,
@@ -1029,7 +1028,7 @@ do_group_errors(
 	}
 }
 
-void
+static void
 do_group_owner(
 	LDAPMessage *e,
 	char	*dn,
diff --git a/clients/rcpt500/main.c b/clients/rcpt500/main.c
index 4a1b637a41b79db6912c574ac61ec6366537dd66..346c5fc10d3e4e22a8ddd5885d92c93cf9564de3 100644
--- a/clients/rcpt500/main.c
+++ b/clients/rcpt500/main.c
@@ -14,8 +14,7 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/syslog.h>
-extern char *strdup (const char *);
-extern char *strstr (const char *, const char *);
+#include <ac/unistd.h>
 
 #include "ldapconfig.h"
 #include "rcpt500.h"
@@ -57,9 +56,6 @@ main( int argc, char **argv )
     int			c, errflg;
     char		*replytext;
 
-    extern int		optind;
-    extern char		*optarg;
-
     *reply = '\0';
 
     if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
diff --git a/clients/rcpt500/query.c b/clients/rcpt500/query.c
index f1dc4a25afdce1186c2d1173bd57dfa79ac53ce6..e70a224ec085252614b37d16ce331bd64adfcca0 100644
--- a/clients/rcpt500/query.c
+++ b/clients/rcpt500/query.c
@@ -10,12 +10,11 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
 
+#include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/syslog.h>
 #include <ac/time.h>
-extern int strcasecmp(const char *, const char *);
 
 #include "lber.h"
 #include "ldap.h"
diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c
index 3a24aff5a3a5bb197ddc46a3217221d0f425c5c0..6638fee62956983114884a51d81e07a3e4f95c01 100644
--- a/clients/tools/ldapdelete.c
+++ b/clients/tools/ldapdelete.c
@@ -8,7 +8,6 @@
 
 #include <ac/string.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #include <lber.h>
 #include <ldap.h>
@@ -36,9 +35,6 @@ main( int argc, char **argv )
     FILE		*fp;
     int			i, rc, kerberos, authmethod;
 
-    extern char	*optarg;
-    extern int	optind;
-
     kerberos = not = verbose = contoper = 0;
     fp = NULL;
 
diff --git a/clients/tools/ldapmodify.c b/clients/tools/ldapmodify.c
index 821a8120d368161077ac3ba3e039ba51acb29d0a..f30815a7063276ba05484ea3a3f4870b811dd9ac 100644
--- a/clients/tools/ldapmodify.c
+++ b/clients/tools/ldapmodify.c
@@ -8,7 +8,6 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #include <sys/stat.h>
 
@@ -72,9 +71,6 @@ main( int argc, char **argv )
     int			rc, i, kerberos, use_ldif, authmethod;
     char		*usage = "usage: %s [-abcknrvF] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n";
 
-    extern char	*optarg;
-    extern int	optind;
-
     if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
 	prog = argv[ 0 ];
     } else {
diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c
index 436d8c9978f672d42ff9e9b71fbfa459e6a1aa1f..4697b283cd15bc4e402fe1a49dd0906dd7e66b18 100644
--- a/clients/tools/ldapmodrdn.c
+++ b/clients/tools/ldapmodrdn.c
@@ -8,7 +8,6 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #include <lber.h>
 #include <ldap.h>
@@ -38,9 +37,6 @@ main(int argc, char **argv)
     FILE		*fp;
     int			rc, i, kerberos, remove, havedn, authmethod;
 
-    extern char	*optarg;
-    extern int	optind;
-
     infile = NULL;
     kerberos = not = contoper = verbose = remove = 0;
 
diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c
index bc6f1f6d670c3158b81047d1ad36b56b535ac543..7ae964cd763c3c8b2f93fd39a4081af876a605b3 100644
--- a/clients/tools/ldapsearch.c
+++ b/clients/tools/ldapsearch.c
@@ -6,9 +6,6 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
-extern int strcasecmp(const char *, const char *);
-extern char *mktemp(char *);
 
 #include <lber.h>
 #include <ldap.h>
@@ -91,8 +88,6 @@ main( int argc, char **argv )
     int			rc, i, first, scope, kerberos, deref, attrsonly;
     int			referrals, timelimit, sizelimit, authmethod;
     LDAP		*ld;
-    extern char		*optarg;
-    extern int		optind;
 
     infile = NULL;
     deref = verbose = allow_binary = not = kerberos = vals2tmp =
diff --git a/clients/ud/auth.c b/clients/ud/auth.c
index ccb44282e0dccceed8ec7fc8b145dc2b71b62212..56891cebfd9acd86b496d2cc15da3b9e157e6576 100644
--- a/clients/ud/auth.c
+++ b/clients/ud/auth.c
@@ -20,7 +20,6 @@
 #include <ac/string.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #ifdef HAVE_PWD_H
 #include <pwd.h>
diff --git a/clients/ud/edit.c b/clients/ud/edit.c
index cce199024fd1f82573846daa678ef6f9b7a8f86d..b34790ca92f4d64ad7e0a0cad28951084d6b7fea 100644
--- a/clients/ud/edit.c
+++ b/clients/ud/edit.c
@@ -21,8 +21,6 @@
 #include <ac/time.h>
 #include <ac/wait.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
-extern char * mktemp(char *);
 
 #ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
diff --git a/clients/ud/find.c b/clients/ud/find.c
index 3a8b72efde53e3fbc231020305423e6db5930f2b..ce4f1558b0cc9294fecb06a6bb08911b1201b208 100644
--- a/clients/ud/find.c
+++ b/clients/ud/find.c
@@ -18,7 +18,6 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include <lber.h>
 #include <ldap.h>
diff --git a/clients/ud/group.c b/clients/ud/group.c
index 5ce609bb96ee192099ccbf305377ee054a8bdf15..e9c9c49cde10ea597474ef2269202ee5c4632394 100644
--- a/clients/ud/group.c
+++ b/clients/ud/group.c
@@ -19,7 +19,6 @@
 #include <ac/ctype.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #include <lber.h>
 #include <ldap.h>
diff --git a/clients/ud/main.c b/clients/ud/main.c
index d1d61964e5ddcf18b5b7b3eb4086ab89d159002f..46fe1de720d53cacec02c85536a7577a49ce4670 100644
--- a/clients/ud/main.c
+++ b/clients/ud/main.c
@@ -21,8 +21,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <setjmp.h>
-extern char *strdup (const char *);
-extern char *getenv(const char *);
 
 #ifdef HAVE_PWD_H
 #include <pwd.h>
@@ -88,7 +86,6 @@ int debug;			/* debug flag */
 int
 main( int argc, char **argv )
 {
-	extern char *optarg;			/* for parsing argv */
 	register int c;				/* for parsing argv */
 	register char *cp;			/* for parsing Version */
 
diff --git a/clients/ud/print.c b/clients/ud/print.c
index ce097720912388765dcb1b4cdaec1a83b5e35d2a..ba665222b19b15c59a0c7b7d97d7dae5f39aabc0 100644
--- a/clients/ud/print.c
+++ b/clients/ud/print.c
@@ -17,7 +17,6 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include <lber.h>
 #include <ldap.h>
diff --git a/include/ac/string.h b/include/ac/string.h
index 7283de2d8fb5d9436300d3a84d36ca502e491428..8df4116cff56846cfe70acc946c353016d09ed35 100644
--- a/include/ac/string.h
+++ b/include/ac/string.h
@@ -31,6 +31,8 @@
 	int		strcmp(), strncmp();
 	int		strcasecmp(), strncasecmp();
 	char	*strdup();
+	char	*strtok();
+	char	*strpbrk();
 	int		memcmp();
 #	endif
 
diff --git a/include/ac/unistd.h b/include/ac/unistd.h
index 5e4501f5893fbf6644244412111f4ac078a48369..a52b288417f42b88cb73da13cb9aaaeeb37753cd 100644
--- a/include/ac/unistd.h
+++ b/include/ac/unistd.h
@@ -9,16 +9,34 @@
 
 #if HAVE_UNISTD_H
 # include <unistd.h>
+#else
+	/* we really should test for these */
+	char *crypt();
+	char *gethostname();
+	char *getenv();
+	long *random();
+	int flock();
 #endif
 
 /* getopt() defines may be in separate include file */
 #if HAVE_GETOPT_H
-# include <getopt.h>
+#	include <getopt.h>
+
+#elif !defined(HAVE_GETOPT)
+	/* no getopt, assume we need getopt-compat.h */
+#	include <getopt-compat.h>
+
+#else
+	/* assume we need to declare these externs */
+	extern char *optarg;
+	extern int optind, opterr, optopt;
 #endif
 
-#ifndef HAVE_GETOPT
-/* no getopt, assume we need getopt-compat.h */
-# include <getopt-compat.h>
+#ifndef HAVE_TEMPNAM
+	extern char *tempnam(const char *tmpdir, const char *prefix);
+#endif
+#ifndef HAVE_MKTEMP
+	extern char *mktemp(char *);
 #endif
 
 /* use _POSIX_VERSION for POSIX.1 code */
diff --git a/include/lutil.h b/include/lutil.h
index 3f3a09d392fe32179e9c5ad14a5b4cc54138cc07..be0fd47599850b75e2973527c79f5624d6d377f7 100644
--- a/include/lutil.h
+++ b/include/lutil.h
@@ -17,16 +17,6 @@ LDAP_F void lutil_detach LDAP_P((int debug, int do_close));
 /* passwd.c */
 LDAP_F int lutil_passwd LDAP_P((const char *cred, const char *passwd));
 
-/* strdup.c */
-#ifndef HAVE_STRDUP
-char *strdup ();	/* No prototype, might conflict with someone else''s */
-#endif
-
-/* tempnam.c */
-#ifndef HAVE_TEMPNAM
-LDAP_F char *tempnam (); /* No prototype, might conflict with someone else''s */
-#endif
-
 LDAP_END_DECL
 
 #endif /* _LUTIL_H */
diff --git a/libraries/libavl/testavl.c b/libraries/libavl/testavl.c
index 56e3121ce228d40fcdbadd3cc092dd4f13f663f5..b551c8ba692d94f95919f212e8ee209b1d95d4b9 100644
--- a/libraries/libavl/testavl.c
+++ b/libraries/libavl/testavl.c
@@ -7,7 +7,6 @@
 
 #include <ac/string.h>
 #include <sys/types.h>
-extern char *strdup (const char *);
 
 #include "avl.h"
 
diff --git a/libraries/liblber/dtest.c b/libraries/liblber/dtest.c
index 57763213214d91f5e7d7532358ee7a2572e29bd6..d2f72154b46b526c9773a11efae4f7e8f1502085 100644
--- a/libraries/liblber/dtest.c
+++ b/libraries/liblber/dtest.c
@@ -18,6 +18,7 @@
 
 #include <ac/string.h>
 #include <ac/socket.h>
+#include <ac/unistd.h>
 
 #ifdef HAVE_CONSOLE_H
 #include <console.h>
@@ -38,7 +39,6 @@ main( int argc, char **argv )
 	int		tag;
 	BerElement	ber;
 	Sockbuf		sb;
-	extern char	*optarg;
 
 #ifdef HAVE_CONSOLE_H
 	ccommand( &argv );
diff --git a/libraries/liblber/etest.c b/libraries/liblber/etest.c
index ae1321b568a39891e72c3376b4ca6085f2bec80f..ca1acc742bcacc81422543ad60389b856cd0e72f 100644
--- a/libraries/liblber/etest.c
+++ b/libraries/liblber/etest.c
@@ -10,6 +10,7 @@
 
 #include <ac/socket.h>
 #include <ac/string.h>
+#include <ac/unistd.h>
 
 #ifdef HAVE_CONSOLE_H
 #include <console.h>
@@ -32,7 +33,6 @@ main( int argc, char **argv )
 	Seqorset	*sos = NULLSEQORSET;
 	BerElement	*ber;
 	Sockbuf		sb;
-	extern char	*optarg;
 
 	if ( argc < 2 ) {
 		usage( argv[0] );
diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c
index 0a6954996cde76d175909b7046c13ac9deac9053..e1dfdda505998ecab8cb095ed5a5036c8d9fad0f 100644
--- a/libraries/liblber/io.c
+++ b/libraries/liblber/io.c
@@ -473,7 +473,8 @@ ber_get_next( Sockbuf *sb, unsigned long *len, BerElement *ber )
 	unsigned long	tag = 0, netlen, toread;
 	unsigned char	lc;
 	long		rc;
-	unsigned int	noctets, diff;
+	int			noctets;
+	unsigned int	diff;
 
 #ifdef LDAP_DEBUG
 	if ( lber_debug )
diff --git a/libraries/libldap/disptmpl.c b/libraries/libldap/disptmpl.c
index 426681eb96c5340f2c78c88b219bf5b759c365eb..54b493e7c7cb51891553ae98dd06cbe3faaeca11 100644
--- a/libraries/libldap/disptmpl.c
+++ b/libraries/libldap/disptmpl.c
@@ -22,7 +22,6 @@
 #include <ac/string.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
diff --git a/libraries/libldap/dsparse.c b/libraries/libldap/dsparse.c
index e0a40daa1df4195968ba28fca9874f165916fde9..394e61ec34dbfc214bcf300774a33f4c90242e20 100644
--- a/libraries/libldap/dsparse.c
+++ b/libraries/libldap/dsparse.c
@@ -23,7 +23,6 @@
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
diff --git a/libraries/libldap/friendly.c b/libraries/libldap/friendly.c
index a18a2456d2a143e7a22a04440f0ae6d843238e9a..12896aad8370b9d75aa497100f91de6ad0a6a313 100644
--- a/libraries/libldap/friendly.c
+++ b/libraries/libldap/friendly.c
@@ -19,7 +19,6 @@ static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 
diff --git a/libraries/libldap/getdn.c b/libraries/libldap/getdn.c
index 1abe7668d8fc7218ba8e4b7089ede3831fcf48d0..70c09c7933c4c534a50a634345727a604ceb2761 100644
--- a/libraries/libldap/getdn.c
+++ b/libraries/libldap/getdn.c
@@ -18,8 +18,6 @@ static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
-extern char *strtok (char *, const char *);
 
 #include "ldap-int.h"
 
diff --git a/libraries/libldap/init.c b/libraries/libldap/init.c
index 9836277748145557cf9b12267f824fb1b694f6e3..049274d54a090c8bade0efb490cd3c9341827d7e 100644
--- a/libraries/libldap/init.c
+++ b/libraries/libldap/init.c
@@ -7,7 +7,6 @@
 #include <ac/string.h>
 #include <ac/ctype.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 #include "ldapconfig.h"
diff --git a/libraries/libldap/options.c b/libraries/libldap/options.c
index 5c5580cb272944d119aa5a9734afc668b10d693b..182e7df67d0a45a9e45eeb42525e58a584557147 100644
--- a/libraries/libldap/options.c
+++ b/libraries/libldap/options.c
@@ -5,7 +5,6 @@
 
 #include <ac/socket.h>
 #include <ac/string.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 
diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c
index 846d1989b11c935fd0407afe1f6fefda11a94f2c..f9b724ef8a2b76bea686ab86d5fd14b1cc4160d0 100644
--- a/libraries/libldap/request.c
+++ b/libraries/libldap/request.c
@@ -19,7 +19,6 @@ static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of
 #include <ac/string.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 
@@ -213,8 +212,6 @@ ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
 
 	if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
 #ifdef notyet
-		extern int	errno;
-
 		if ( errno == EWOULDBLOCK ) {
 			/* need to continue write later */
 			lr->lr_status = LDAP_REQST_WRITING;
diff --git a/libraries/libldap/search.c b/libraries/libldap/search.c
index 3ac8de74895a0198844cce6935ab4325a7aeac18..2a45f1ce6d6fec74ff93db8a3cb369be285de35a 100644
--- a/libraries/libldap/search.c
+++ b/libraries/libldap/search.c
@@ -18,7 +18,6 @@ static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 
diff --git a/libraries/libldap/test.c b/libraries/libldap/test.c
index d37556a5cfb2a82d0f5a869d3f4133bd64afe359..66e86efb85d0f159be6b70819956226299f71297 100644
--- a/libraries/libldap/test.c
+++ b/libraries/libldap/test.c
@@ -8,7 +8,6 @@
 #include <ac/string.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern char *strdup (const char *);
 
 #include <sys/stat.h>
 
@@ -278,9 +277,6 @@ main( int argc, char **argv )
 	int		copyoptions = 0;
 	LDAPURLDesc	*ludp;
 
-	extern char	*optarg;
-	extern int	optind;
-
 	host = NULL;
 	port = LDAP_PORT;
 	dnsuffix = "";
diff --git a/libraries/libldap/ufn.c b/libraries/libldap/ufn.c
index 546481b2e49d1bdadbe5a5f91b5a0205645218b9..6a2ef9ec8360c02aa275c2a908c3c8e9dfb28390 100644
--- a/libraries/libldap/ufn.c
+++ b/libraries/libldap/ufn.c
@@ -18,7 +18,6 @@ static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of
 
 #include <ac/socket.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 #include "ldapconfig.h"
diff --git a/libraries/libldap/url.c b/libraries/libldap/url.c
index 39826e117e80d4fd838a93588698f0f6e8fc17fa..4de1a914e0562e47e09f8e0308f7f6e853030d99 100644
--- a/libraries/libldap/url.c
+++ b/libraries/libldap/url.c
@@ -30,7 +30,6 @@ static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern char *strdup (const char *);
 
 #include "ldap-int.h"
 
diff --git a/libraries/liblutil/passwd.c b/libraries/liblutil/passwd.c
index 3abbc3041b2d0a53ec690b1b062105d261537aa5..89b45b92c309796d565aa9b2f12e3eed2feb9e14 100644
--- a/libraries/liblutil/passwd.c
+++ b/libraries/liblutil/passwd.c
@@ -16,7 +16,6 @@
 #include "lutil_md5.h"
 #include "lutil_sha1.h"
 #include "lutil.h"
-extern char *crypt (const char *, const char *);
 
 /*
  */
diff --git a/libraries/liblutil/tempnam.c b/libraries/liblutil/tempnam.c
index cdd5385a2e3fc3d57410a1bc2263a6cefb6a1eeb..d4aa9cc2390a3bdea8c31960b1451d591c29e108 100644
--- a/libraries/liblutil/tempnam.c
+++ b/libraries/liblutil/tempnam.c
@@ -2,15 +2,15 @@
 
 #ifndef HAVE_TEMPNAM
 
-#include <stdlib.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <ac/string.h>
-extern char *mktemp (char *);
+#include <ac/unistd.h>
 
 #include "lutil.h"
 
 char *
-tempnam( char *dir, char *pfx )
+tempnam( const char *dir, const char *pfx )
 {
     char	*s;
 
@@ -41,4 +41,4 @@ tempnam( char *dir, char *pfx )
     return( s );
 }
 
-#endif /* nextstep */
+#endif /* TEMPNAM */
diff --git a/servers/ldapd/association.c b/servers/ldapd/association.c
index a1c77ac14a37f0b8c716af03574a6e90b471dee2..3ca61c9e47a98a1323b83e124841b8abc6e68533 100644
--- a/servers/ldapd/association.c
+++ b/servers/ldapd/association.c
@@ -18,7 +18,6 @@
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-extern int errno;
 
 #include <quipu/commonarg.h>
 #include <quipu/ds_error.h>
diff --git a/servers/ldapd/main.c b/servers/ldapd/main.c
index f4e179a5ff94c047d5996a89ccc3f826dcb32dca..354214609139d198513aca0adfdd1252c0d287c4 100644
--- a/servers/ldapd/main.c
+++ b/servers/ldapd/main.c
@@ -117,8 +117,6 @@ main( int argc, char **argv )
 #ifdef LDAP_PROCTITLE
 	char			title[80];
 #endif
-	extern char		*optarg;
-	extern int		optind;
 
 #ifdef VMS
 	/* Pick up socket from inetd-type server on VMS */
diff --git a/servers/slapd/attr.c b/servers/slapd/attr.c
index 99cd92b2cad32406e94da37b251056adfd08722f..51752ee9ac4008b37ca6ac5de3907c7e19c94d15 100644
--- a/servers/slapd/attr.c
+++ b/servers/slapd/attr.c
@@ -9,6 +9,7 @@
 #endif
 
 #include <ac/ctype.h>
+#include <ac/errno.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
@@ -21,8 +22,6 @@
 
 #include "slap.h"
 
-extern int	errno;
-
 void
 attr_free( Attribute *a )
 {
diff --git a/servers/slapd/back-ldbm/bind.c b/servers/slapd/back-ldbm/bind.c
index dce574b95e4696c2581a2c5ae96258a8066078c8..591e488c9cc98e27ef309a81dc5268c8dd8b1965 100644
--- a/servers/slapd/back-ldbm/bind.c
+++ b/servers/slapd/back-ldbm/bind.c
@@ -8,7 +8,6 @@
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/unistd.h>
-extern char *crypt ();
 
 #include "slap.h"
 #include "back-ldbm.h"
diff --git a/servers/slapd/lock.c b/servers/slapd/lock.c
index 8d5d7f2ec539e05d6eec9623f9b9b9c68917f278..f634b220aa3026518a5e849b037b0a5c38df7dc8 100644
--- a/servers/slapd/lock.c
+++ b/servers/slapd/lock.c
@@ -8,7 +8,6 @@
 #include <ac/socket.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern int flock ();
 
 #include <sys/file.h>
 #include <sys/param.h>
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 733fb10e7f061a65e6e41f651e7d9ace3cc9ece3..3c6ddb62a3288097d5ca4a5f96ea04af797c340d 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -68,7 +68,6 @@ main( int argc, char **argv )
 	char		*myname;
 	Backend		*be = NULL;
 	FILE		*fp = NULL;
-	extern char	*optarg;
 
 	configfile = SLAPD_DEFAULT_CONFIGFILE;
 	port = LDAP_PORT;
diff --git a/servers/slapd/shell-backends/passwd-shell.c b/servers/slapd/shell-backends/passwd-shell.c
index 1f035ca11eebcb0db4f57789d45b4adefb219998..89d2fd73d66ad91f0a66a8b0fccb8aa209d5371b 100644
--- a/servers/slapd/shell-backends/passwd-shell.c
+++ b/servers/slapd/shell-backends/passwd-shell.c
@@ -17,9 +17,11 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <pwd.h>
 
 #include <ac/string.h>
+#include <ac/unistd.h>
+
+#include <pwd.h>
 
 #include <lber.h>
 #include <ldap.h>
@@ -39,8 +41,6 @@ main( int argc, char **argv )
 {
     int			c, errflg;
     struct ldop		op;
-    extern int		optind;
-    extern char		*optarg;
 
     if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
 	progname = estrdup( argv[ 0 ] );
diff --git a/servers/slapd/shell-backends/shellutil.c b/servers/slapd/shell-backends/shellutil.c
index 68266efd37d446b29877b96316b768be4ec23185..59d43cb7073fe07c7d17ff9a17c421d4bd3780d9 100644
--- a/servers/slapd/shell-backends/shellutil.c
+++ b/servers/slapd/shell-backends/shellutil.c
@@ -18,18 +18,13 @@
 
 #include <stdio.h>
 
-#ifdef STDC_HEADERS
 #include <stdlib.h>
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
+#include <ac/stdarg.h>
 
 #include <pwd.h>
 
-#include <ac/string.h>
 #include <ac/ctype.h>
-extern long int random (void);
+#include <ac/string.h>
 
 #include <lber.h>
 #include <ldap.h>
diff --git a/servers/slapd/tools/centipede.c b/servers/slapd/tools/centipede.c
index 1ce576540518060fca37f12e543ef3b57210440d..8792fb58adf3335c20549b93336329dfae476d11 100644
--- a/servers/slapd/tools/centipede.c
+++ b/servers/slapd/tools/centipede.c
@@ -9,9 +9,6 @@
 #include <ac/string.h>
 #include <ac/time.h>
 #include <ac/unistd.h>		/* get link(), unlink() */
-extern char *strdup (const char *);
-extern char *strtok (char *, const char *);
-extern char *strpbrk (const char *, const char *);
 
 #include <lber.h>
 #include <ldap.h>
@@ -89,8 +86,6 @@ main( int argc, char **argv )
 	char		buf[BUFSIZ];
 	int			i, j, k, count;
 	char		*s;
-	extern int	optind;
-	extern char	*optarg;
 
 	ldapsrcurl = NULL;
 	ldapdesturl = NULL;
diff --git a/servers/slapd/tools/edb2ldif.c b/servers/slapd/tools/edb2ldif.c
index 5be7cba614b3a84a151040af9bde9240eee5a9a5..9aad796369a0c46b5f58f5f5395296c5877a19e1 100644
--- a/servers/slapd/tools/edb2ldif.c
+++ b/servers/slapd/tools/edb2ldif.c
@@ -16,8 +16,10 @@
 #include <stdlib.h>
 
 #include <ac/ctype.h>
+#include <ac/errno.h>
 #include <ac/dirent.h>
 #include <ac/string.h>
+#include <ac/unistd.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -88,8 +90,6 @@ main( int argc, char **argv )
     char	*usage = "usage: %s [-d] [-o] [-r] [-v] [-b basedn] [-a addvalsfile] [-f fileattrdir] [-i ignoreattr...] [edbfile...]\n";
     char	edbfile[ MAXNAMLEN ], *basedn;
     int		c, rc, errflg, ignore_count, recurse;
-    extern int  optind;
-    extern char	*optarg;
     extern char	dsa_mode;
 #ifdef HAVE_FILE_ATTR_DIR
     extern char	*file_attr_directory;
@@ -856,10 +856,6 @@ free_edbmap( struct edbmap *edbmap )
 static void
 print_err( char *msg )
 {
-    extern int	sys_nerr;
-    extern char	*sys_errlist[];
-    extern int	errno;
-
 #ifdef LDAP_DEBUG
     if ( debugflg ) {
 	fprintf( stderr, "print_err( \"%s\" )\n", msg );
diff --git a/servers/slapd/tools/ldbmtest.c b/servers/slapd/tools/ldbmtest.c
index beee4046cff625a9cb6af30eb3d95be1e8cfe1a2..99fdee67c80bce435d58485e4e2cb45631a72d43 100644
--- a/servers/slapd/tools/ldbmtest.c
+++ b/servers/slapd/tools/ldbmtest.c
@@ -14,7 +14,6 @@
 #include <sys/resource.h>
 #include <sys/param.h>
 #include <sys/stat.h>
-extern int mkstemp (char *);
 
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
@@ -66,7 +65,6 @@ main( int argc, char **argv )
 	IDList		*idl;
 	Backend		*tbe;
 	int		i;
-	extern char	*optarg;
 
 #ifdef HAVE_BERKELEY_DB2
 	DBC	*cursorp;
diff --git a/servers/slapd/tools/ldif2id2children.c b/servers/slapd/tools/ldif2id2children.c
index 7af679e06870cce3bbead115d3e62c730cbf5ad3..4c802e1f4412684a2ccb19c323479fecb5103b6e 100644
--- a/servers/slapd/tools/ldif2id2children.c
+++ b/servers/slapd/tools/ldif2id2children.c
@@ -4,6 +4,7 @@
 
 #include <ac/string.h>
 #include <ac/socket.h>
+#include <ac/unistd.h>
 
 #include "ldapconfig.h"
 #include "../slap.h"
@@ -61,7 +62,6 @@ main( int argc, char **argv )
 	struct berval	bv;
 	struct berval	*vals[2];
 	Avlnode		*avltypes = NULL;
-	extern char	*optarg;
 
 	tailorfile = SLAPD_DEFAULT_CONFIGFILE;
 	dbnum = -1;
diff --git a/servers/slapd/tools/ldif2id2entry.c b/servers/slapd/tools/ldif2id2entry.c
index e043e6ce25167c597a64740ff294492067c9cfe8..226d8232db6dc4eb7a3621c4449876fce29a3bc7 100644
--- a/servers/slapd/tools/ldif2id2entry.c
+++ b/servers/slapd/tools/ldif2id2entry.c
@@ -4,7 +4,7 @@
 
 #include <ac/string.h>
 #include <ac/socket.h>
-extern int strcasecmp(const char *, const char *);
+#include <ac/unistd.h>
 
 #include "ldapconfig.h"
 #include "../slap.h"
@@ -59,7 +59,6 @@ main( int argc, char **argv )
 	struct berval	*vals[2];
 	Avlnode		*avltypes = NULL;
 	FILE		*fp;
-	extern char	*optarg;
 
 	tailorfile = SLAPD_DEFAULT_CONFIGFILE;
 	dbnum = -1;
diff --git a/servers/slapd/tools/ldif2index.c b/servers/slapd/tools/ldif2index.c
index 6bcc3c9caa446e0431de4709851f501ed0a29cbe..1287f502d08f40eddce01fb70debe7a453c0a99a 100644
--- a/servers/slapd/tools/ldif2index.c
+++ b/servers/slapd/tools/ldif2index.c
@@ -4,6 +4,7 @@
 
 #include <ac/string.h>
 #include <ac/socket.h>
+#include <ac/unistd.h>
 
 #include "../slap.h"
 #include "../back-ldbm/back-ldbm.h"
@@ -57,7 +58,6 @@ main( int argc, char **argv )
 	Backend		*be = NULL;
 	struct berval	bv;
 	struct berval	*vals[2];
-	extern char	*optarg;
 
 	inputfile = NULL;
 	tailorfile = SLAPD_DEFAULT_CONFIGFILE;
diff --git a/servers/slapd/tools/ldif2ldbm.c b/servers/slapd/tools/ldif2ldbm.c
index 38f0dea765ba0acb4f2ed70fe3d916bb1371f125..53c8fa008c9e14875aab5fcc848f29522648bb07 100644
--- a/servers/slapd/tools/ldif2ldbm.c
+++ b/servers/slapd/tools/ldif2ldbm.c
@@ -73,7 +73,6 @@ main( int argc, char **argv )
 	struct berval	bv;
 	struct berval	*vals[2];
 	Avlnode		*avltypes = NULL;
-	extern char	*optarg;
 
 	sbindir = DEFAULT_SBINDIR;
 	tailorfile = SLAPD_DEFAULT_CONFIGFILE;
diff --git a/servers/slurpd/args.c b/servers/slurpd/args.c
index 3a51d53c95f70155ca345028678c004f544163d7..c3b202065c67ed09a08fb16a134a301ff030665f 100644
--- a/servers/slurpd/args.c
+++ b/servers/slurpd/args.c
@@ -21,6 +21,7 @@
 
 #include <ac/string.h>
 #include <ac/time.h>
+#include <ac/unistd.h>
 
 #include <lber.h>
 #include <ldap.h>
@@ -54,7 +55,6 @@ doargs(
 )
 {
     int		i;
-    extern char	*optarg;
     int		rflag = 0;
 
     if ( (g->myname = strrchr( argv[0], '/' )) == NULL ) {
diff --git a/servers/slurpd/lock.c b/servers/slurpd/lock.c
index 6fbd4bbf9cc22cdbe6d36e9437cfd9927ecfa567..692c9c3f88878dc48a41acc41fcd085bc09fb2a8 100644
--- a/servers/slurpd/lock.c
+++ b/servers/slurpd/lock.c
@@ -22,12 +22,11 @@
 #include <ac/socket.h>
 #include <ac/time.h>
 #include <ac/unistd.h>
-extern int flock ();
 
 #include <sys/file.h>
 #include <sys/param.h>
 
-#include "slurp.h"		/* Was ../slapd/slap.h */
+#include "slurp.h"
 
 
 FILE *