diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c
index 4a2e4ca1f6c1b268d70ce8fb0750f260de536aa0..0bcda8a1ae86524ec2d7f46f47209d4363a4f8ce 100644
--- a/clients/tools/ldapdelete.c
+++ b/clients/tools/ldapdelete.c
@@ -45,6 +45,8 @@ main( int argc, char **argv )
 		authmethod = LDAP_AUTH_KRBV4;
 #else
 		fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+		fprintf( stderr, usage, argv[0] );
+		return( EXIT_FAILURE );
 #endif
 	    break;
 	case 'K':	/* kerberos bind, part one only */
@@ -52,6 +54,8 @@ main( int argc, char **argv )
 		authmethod = LDAP_AUTH_KRBV41;
 #else
 		fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+		fprintf( stderr, usage, argv[0] );
+		return( EXIT_FAILURE );
 #endif
 	    break;
 	case 'c':	/* continuous operation mode */
@@ -88,19 +92,23 @@ main( int argc, char **argv )
 		want_bindpw++;
 		break;
 	case 'P':
-		switch(optarg[0])
+		switch( atoi(optarg) )
 		{
-		case '2':
+		case 2:
 			version = LDAP_VERSION2;
 			break;
-		case '3':
+		case 3:
 			version = LDAP_VERSION3;
 			break;
+		default:
+			fprintf( stderr, "protocol version should be 2 or 3\n" );
+		    fprintf( stderr, usage, argv[0] );
+		    return( EXIT_FAILURE );
 		}
 		break;
 	default:
 	    fprintf( stderr, usage, argv[0] );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
     }
 
@@ -111,8 +119,12 @@ main( int argc, char **argv )
     }
 
 	if ( debug ) {
-		ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
-		ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+		if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+		}
+		if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+		}
 	}
 
 #ifdef SIGPIPE
@@ -121,7 +133,7 @@ main( int argc, char **argv )
 
     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
 	perror( "ldap_init" );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
 	{
@@ -133,13 +145,15 @@ main( int argc, char **argv )
 	if (want_bindpw)
 		passwd = getpass("Enter LDAP Password: ");
 
-	if( version != -1 ) {
-		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+	if (version != -1 &&
+		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
+	{
+		fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
 	}
 
     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
 	ldap_perror( ld, "ldap_bind" );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
     if ( fp == NULL ) {
@@ -158,10 +172,7 @@ main( int argc, char **argv )
 
     ldap_unbind( ld );
 
-    exit( rc );
-
-	/* UNREACHABLE */
-	return(0);
+	return( rc );
 }
 
 
diff --git a/clients/tools/ldapmodify.c b/clients/tools/ldapmodify.c
index 327915f942cc757085f9e0fe0a496d038e226fd0..6c2f2b118c61a1f6fa7f505167dff46fe9b52421 100644
--- a/clients/tools/ldapmodify.c
+++ b/clients/tools/ldapmodify.c
@@ -85,7 +85,7 @@ usage( const char *prog )
 	     "       v    - verbose mode\n"
 	     "       w    - password\n"
 	     , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
-    exit( 1 );
+    exit( EXIT_FAILURE );
 }
 
 
@@ -96,8 +96,7 @@ main( int argc, char **argv )
     FILE		*fp;
 	int		rc, i, use_ldif, authmethod, version, want_bindpw, debug;
 
-    if (( prog = strrchr( argv[ 0 ], '/'  )) == NULL &&
-        ( prog = strrchr( argv[ 0 ], '\\' )) == NULL   ) { /*for Windows/DOS*/
+    if (( prog = strrchr( argv[ 0 ], *DIRSEP )) == NULL ) {
 	prog = argv[ 0 ];
     } else {
 	++prog;
@@ -133,6 +132,8 @@ main( int argc, char **argv )
 		authmethod = LDAP_AUTH_KRBV4;
 #else
 		fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+		usage( argv[0] );
+		return( EXIT_FAILURE );
 #endif
 	    break;
 	case 'K':	/* kerberos bind, part 1 only */
@@ -140,6 +141,8 @@ main( int argc, char **argv )
 		authmethod = LDAP_AUTH_KRBV41;
 #else
 		fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+		usage( argv[0] );
+		return( EXIT_FAILURE );
 #endif
 	    break;
 	case 'F':	/* force all changes records to be used */
@@ -173,14 +176,17 @@ main( int argc, char **argv )
 		want_bindpw++;
 		break;
 	case 'P':
-		switch(optarg[0])
+		switch( atoi(optarg) )
 		{
-		case '2':
+		case 2:
 			version = LDAP_VERSION2;
 			break;
-		case '3':
+		case 3:
 			version = LDAP_VERSION3;
 			break;
+		default:
+			fprintf( stderr, "protocol version should be 2 or 3\n" );
+			usage( argv[0] );
 		}
 		break;
 	default:
@@ -194,15 +200,19 @@ main( int argc, char **argv )
     if ( infile != NULL ) {
 	if (( fp = fopen( infile, "r" )) == NULL ) {
 	    perror( infile );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
     } else {
 	fp = stdin;
     }
 
 	if ( debug ) {
-		ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
-		ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+		if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+		}
+		if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+		}
 		ldif_debug = debug;
 	}
 
@@ -213,7 +223,7 @@ main( int argc, char **argv )
     if ( !not ) {
 	if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
 	    perror( "ldap_init" );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
 
 	/* this seems prudent */
@@ -225,13 +235,15 @@ main( int argc, char **argv )
 	if (want_bindpw)
 		passwd = getpass("Enter LDAP Password: ");
 
-	if( version != -1 ) {
-		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+	if (version != -1 &&
+		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
+	{
+		fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
 	}
 
 	if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
 	    ldap_perror( ld, "ldap_bind" );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
     }
 
@@ -279,10 +291,7 @@ main( int argc, char **argv )
 	ldap_unbind( ld );
     }
 
-    exit( rc );
-
-	/* UNREACHABLE */
-	return(0);
+	return( rc );
 }
 
 
diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c
index d75c37dddf26ea13be811f9664d83788760fd310..98c91f3f5e26c0e821d320d249d70f3979bb6d99 100644
--- a/clients/tools/ldapmodrdn.c
+++ b/clients/tools/ldapmodrdn.c
@@ -63,6 +63,7 @@ main(int argc, char **argv)
 		authmethod = LDAP_AUTH_KRBV4;
 #else
 		fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+		return( EXIT_FAILURE );
 #endif
 	    break;
 	case 'K':	/* kerberos bind, part one only */
@@ -70,6 +71,7 @@ main(int argc, char **argv)
 		authmethod = LDAP_AUTH_KRBV41;
 #else
 		fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+		return( EXIT_FAILURE );
 #endif
 	    break;
 	case 'c':	/* continuous operation mode */
@@ -110,60 +112,72 @@ main(int argc, char **argv)
 		want_bindpw++;
 		break;
 	case 'P':
-		switch(optarg[0])
+		switch( atoi(optarg) )
 		{
-		case '2':
+		case 2:
 			version = LDAP_VERSION2;
 			break;
-		case '3':
+		case 3:
 			version = LDAP_VERSION3;
 			break;
+		default:
+			fprintf( stderr, "protocol version should be 2 or 3\n" );
+		    fprintf( stderr, usage, argv[0] );
+		    return( EXIT_FAILURE );
 		}
 		break;
 	default:
 	    fprintf( stderr, usage, argv[0] );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
     }
 
-    if ((newSuperior != NULL) && (version != LDAP_VERSION3))
-    {
-	fprintf( stderr,
-		 "%s: version conflict!, -s newSuperior requires LDAP v3\n",
-		 myname);
-	fprintf( stderr, usage, argv[0] );
-	exit( 1 );
+    if (newSuperior != NULL) {
+		if (version == LDAP_VERSION2) {
+			fprintf( stderr,
+				"%s: version conflict!, -s newSuperior requires LDAPv3\n",
+				myname);
+			fprintf( stderr, usage, argv[0] );
+			return( EXIT_FAILURE );
+		}
+
+		/* promote to LDAPv3 */
+		version = LDAP_VERSION3;
     }
     
     havedn = 0;
     if (argc - optind == 2) {
 	if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
 	    perror( "strdup" );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
 	    perror( "strdup" );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
         }
 	++havedn;
     } else if ( argc - optind != 0 ) {
 	fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
 	fprintf( stderr, usage, argv[0] );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
     if ( infile != NULL ) {
 	if (( fp = fopen( infile, "r" )) == NULL ) {
 	    perror( infile );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
     } else {
 	fp = stdin;
     }
 
 	if ( debug ) {
-		ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
-		ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+		if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+		}
+		if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+		}
 	}
 
 #ifdef SIGPIPE
@@ -172,7 +186,7 @@ main(int argc, char **argv)
 
     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
 	perror( "ldap_init" );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
 	/* this seems prudent */
@@ -184,13 +198,15 @@ main(int argc, char **argv)
 	if (want_bindpw)
 		passwd = getpass("Enter LDAP Password: ");
 
-	if( version != -1) {
-		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+	if (version != -1 &&
+		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
+	{
+		fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
 	}
 
     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
 	ldap_perror( ld, "ldap_bind" );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
     rc = 0;
@@ -203,14 +219,14 @@ main(int argc, char **argv)
 	    if ( havedn ) {	/* have DN, get RDN */
 		if (( rdn = strdup( buf )) == NULL ) {
                     perror( "strdup" );
-                    exit( 1 );
+                    return( EXIT_FAILURE );
 		}
 		rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
 		havedn = 0;
 	    } else if ( !havedn ) {	/* don't have DN yet */
 	        if (( entrydn = strdup( buf )) == NULL ) {
 		    perror( "strdup" );
-		    exit( 1 );
+		    return( EXIT_FAILURE );
 	        }
 		++havedn;
 	    }
@@ -219,10 +235,8 @@ main(int argc, char **argv)
 
     ldap_unbind( ld );
 
-    exit( rc );
-
 	/* UNREACHABLE */
-	return(0);
+	return( rc );
 }
 
 static int domodrdn(
diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c
index 6bbe6ab5c93b30f1d4aee57905eddc189a25b349..597d5d1ecbaf138c023b15df0f41006bcc4367bc 100644
--- a/clients/tools/ldappasswd.c
+++ b/clients/tools/ldappasswd.c
@@ -35,10 +35,9 @@
 
 /* local macros */
 #define CEILING(x)	((double)(x) > (int)(x) ? (int)(x) + 1 : (int)(x))
-#define STRDUP(x)	((x) ? strcpy(malloc(strlen(x) + 1), x) : NULL)
 
 #define LDAP_PASSWD_ATTRIB "userPassword"
-#define LDAP_PASSWD_CONF   DEFAULT_SYSCONFDIR"/passwd.conf"
+#define LDAP_PASSWD_CONF   DEFAULT_SYSCONFDIR DIRSEP "passwd.conf"
 
 #define HS_NONE  0
 #define HS_PLAIN 1
@@ -159,7 +158,7 @@ gen_pass (unsigned int len)
 char *
 hash_none (const char *pw_in, Salt * salt)
 {
-	return (STRDUP (pw_in));
+	return (strdup (pw_in));
 }
 #endif
 
@@ -390,11 +389,11 @@ main (int argc, char *argv[])
 		switch (i)
 		{
 		case 'a':	/* password attribute */
-			pwattr = STRDUP (optarg);
+			pwattr = strdup (optarg);
 			break;
 
 		case 'b':	/* base search dn */
-			base = STRDUP (optarg);
+			base = strdup (optarg);
 			break;
 
 		case 'C':
@@ -402,7 +401,7 @@ main (int argc, char *argv[])
 			break;
 
 		case 'D':	/* bind distinguished name */
-			binddn = STRDUP (optarg);
+			binddn = strdup (optarg);
 			break;
 
 		case 'd':	/* debugging option */
@@ -414,7 +413,7 @@ main (int argc, char *argv[])
 			break;
 
 		case 'e':	/* new password */
-			newpw = STRDUP (optarg);
+			newpw = strdup (optarg);
 			break;
 
 		case 'g':
@@ -439,7 +438,7 @@ main (int argc, char *argv[])
 			break;
 
 		case 'h':	/* ldap host */
-			ldaphost = STRDUP (optarg);
+			ldaphost = strdup (optarg);
 			break;
 
 		case 'K':	/* use kerberos bind, 1st part only */
@@ -447,6 +446,7 @@ main (int argc, char *argv[])
 			authmethod = LDAP_AUTH_KRBV41;
 #else
 			fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+			usage (argv[0]);
 #endif
 			break;
 
@@ -455,6 +455,7 @@ main (int argc, char *argv[])
 			authmethod = LDAP_AUTH_KRBV4;
 #else
 			fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+			usage (argv[0]);
 #endif
 			break;
 
@@ -467,14 +468,16 @@ main (int argc, char *argv[])
 			break;
 
 		case 'P':
-			switch(optarg[0])
-			{
-			case '2':
+			switch( atoi( optarg ) ) {
+			case 2:
 				version = LDAP_VERSION2;
 				break;
-			case '3':
+			case 3:
 				version = LDAP_VERSION3;
 				break;
+			default:
+				fprintf( stderr, "protocol version should be 2 or 3\n" );
+				usage( argv[0] );
 			}
 			break;
 
@@ -483,11 +486,11 @@ main (int argc, char *argv[])
 			break;
 
 		case 's':	/* scope */
-			if (strncasecmp (optarg, "base", 4) == 0)
+			if (strcasecmp (optarg, "base") == 0)
 				scope = LDAP_SCOPE_BASE;
-			else if (strncasecmp (optarg, "one", 3) == 0)
+			else if (strcasecmp (optarg, "one") == 0)
 				scope = LDAP_SCOPE_ONELEVEL;
-			else if (strncasecmp (optarg, "sub", 3) == 0)
+			else if (strcasecmp (optarg, "sub") == 0)
 				scope = LDAP_SCOPE_SUBTREE;
 			else
 			{
@@ -497,7 +500,7 @@ main (int argc, char *argv[])
 			break;
 
 		case 't':	/* target dn */
-			targetdn = STRDUP (optarg);
+			targetdn = strdup (optarg);
 			break;
 
 		case 'v':	/* verbose */
@@ -509,7 +512,7 @@ main (int argc, char *argv[])
 			break;
 
 		case 'w':	/* bind password */
-			bindpw = STRDUP (optarg);
+			bindpw = strdup (optarg);
 			break;
 
 		case 'Y':	/* salt length */
@@ -518,7 +521,7 @@ main (int argc, char *argv[])
 
 		case 'y':	/* user specified salt */
 			salt.len = strlen (optarg);
-			salt.salt = (unsigned char *)STRDUP (optarg);
+			salt.salt = (unsigned char *)strdup (optarg);
 			break;
 
 		case 'z':	/* time limit */
@@ -532,7 +535,7 @@ main (int argc, char *argv[])
 
 	/* grab filter */
 	if (!(argc - optind < 1))
-		filtpattern = STRDUP (argv[optind]);
+		filtpattern = strdup (argv[optind]);
 
 	/* check for target(s) */
 	if (!filtpattern && !targetdn)
@@ -552,13 +555,17 @@ main (int argc, char *argv[])
 		if (strncmp (newpw, cknewpw, strlen (newpw)))
 		{
 			fprintf (stderr, "passwords do not match\n");
-			exit (1);
+			return ( EXIT_FAILURE );
 		}
 	}
 
 	if ( debug ) {
-		ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
-		ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+		if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+		}
+		if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+		}
 	}
 
 #ifdef SIGPIPE
@@ -584,15 +591,19 @@ main (int argc, char *argv[])
 	if ((ld = ldap_init (ldaphost, ldapport)) == NULL)
 	{
 		perror ("ldap_init");
-		exit (1);
+		return ( EXIT_FAILURE );
 	}
 
 	/* set options */
-	if( timelimit != -1 ) {
-		ldap_set_option (ld, LDAP_OPT_TIMELIMIT, (void *)&timelimit);
+	if (timelimit != -1 &&
+		ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == LDAP_OPT_ERROR )
+	{
+		fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
 	}
-	if( sizelimit != -1 ) {
-		ldap_set_option (ld, LDAP_OPT_SIZELIMIT, (void *)&sizelimit);
+	if (sizelimit != -1 &&
+		ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == LDAP_OPT_ERROR )
+	{
+		fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
 	}
 
 	/* this seems prudent */
@@ -601,15 +612,17 @@ main (int argc, char *argv[])
 		ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
 	}
 
-	if( version != -1 ) {
-		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+	if (version != -1 &&
+		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
+	{
+		fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
 	}
 
 	/* authenticate to server */
 	if (ldap_bind_s (ld, binddn, bindpw, authmethod) != LDAP_SUCCESS)
 	{
 		ldap_perror (ld, "ldap_bind");
-		exit (1);
+		return ( EXIT_FAILURE );
 	}
 
 	if (targetdn)
@@ -639,7 +652,7 @@ main (int argc, char *argv[])
 		    i != LDAP_SIZELIMIT_EXCEEDED)
 		{
 			ldap_perror (ld, "ldap_search");
-			exit (1);
+			return ( EXIT_FAILURE );
 		}
 
 		for (e = ldap_first_entry (ld, result); e; e = ldap_next_entry (ld, e))
@@ -658,8 +671,6 @@ main (int argc, char *argv[])
 
 	/* disconnect from server */
 	ldap_unbind (ld);
-	exit(0);
 
-	/* unreached */
-	return (0);
+	return ( EXIT_SUCCESS );
 }
diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c
index 7a8a84e6a8741ffa1a96d0c693f70be5887bb595..7e97e9822825308f5c362d42fa72c3c503a3e7e3 100644
--- a/clients/tools/ldapsearch.c
+++ b/clients/tools/ldapsearch.c
@@ -51,7 +51,7 @@ usage( char *s )
     fprintf( stderr, "    -h host\tldap server\n" );
     fprintf( stderr, "    -p port\tport on ldap server\n" );
     fprintf( stderr, "    -P version\tprocotol version (2 or 3)\n" );
-    exit( 1 );
+    exit( EXIT_FAILURE );
 }
 
 static void print_entry LDAP_P((
@@ -146,11 +146,11 @@ main( int argc, char **argv )
 	    ++allow_binary;
 	    break;
 	case 's':	/* search scope */
-	    if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
+	    if ( strcasecmp( optarg, "base" ) == 0 ) {
 		scope = LDAP_SCOPE_BASE;
-	    } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
+	    } else if ( strcasecmp( optarg, "one" ) == 0 ) {
 		scope = LDAP_SCOPE_ONELEVEL;
-	    } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
+	    } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
 		scope = LDAP_SCOPE_SUBTREE;
 	    } else {
 		fprintf( stderr, "scope should be base, one, or sub\n" );
@@ -159,13 +159,13 @@ main( int argc, char **argv )
 	    break;
 
 	case 'a':	/* set alias deref option */
-	    if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
+	    if ( strcasecmp( optarg, "never" ) == 0 ) {
 		deref = LDAP_DEREF_NEVER;
-	    } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
+	    } else if ( strcasecmp( optarg, "search" ) == 0 ) {
 		deref = LDAP_DEREF_SEARCHING;
-	    } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
+	    } else if ( strcasecmp( optarg, "find" ) == 0 ) {
 		deref = LDAP_DEREF_FINDING;
-	    } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
+	    } else if ( strcasecmp( optarg, "always" ) == 0 ) {
 		deref = LDAP_DEREF_ALWAYS;
 	    } else {
 		fprintf( stderr, "alias deref should be never, search, find, or always\n" );
@@ -207,14 +207,17 @@ main( int argc, char **argv )
 		want_bindpw++;
 		break;
 	case 'P':
-		switch(optarg[0])
+		switch( atoi( optarg ) )
 		{
-		case '2':
+		case 2:
 			version = LDAP_VERSION2;
 			break;
-		case '3':
+		case 3:
 			version = LDAP_VERSION3;
 			break;
+		default:
+			fprintf( stderr, "protocol version should be 2 or 3\n" );
+			usage( argv[0] );
 		}
 		break;
 	default:
@@ -250,13 +253,17 @@ main( int argc, char **argv )
 	    fp = stdin;
 	} else if (( fp = fopen( infile, "r" )) == NULL ) {
 	    perror( infile );
-	    exit( 1 );
+	    return( EXIT_FAILURE );
 	}
     }
 
 	if ( debug ) {
-		ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
-		ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+		if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+		}
+		if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
+			fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+		}
 		ldif_debug = debug;
 	}
 
@@ -265,42 +272,43 @@ main( int argc, char **argv )
 #endif
 
     if ( verbose ) {
-	printf( "ldap_init( %s, %d )\n",
+	fprintf( stderr, "ldap_init( %s, %d )\n",
 		(ldaphost != NULL) ? ldaphost : "<DEFAULT>",
 		ldapport );
     }
 
     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
 	perror( "ldap_init" );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
 	if (deref != -1 &&
-		ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 )
+		ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == LDAP_OPT_ERROR )
 	{
-		/* set option error */
+		fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
 	}
 	if (timelimit != -1 &&
-		ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 )
+		ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == LDAP_OPT_ERROR )
 	{
-		/* set option error */
+		fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
 	}
 	if (sizelimit != -1 &&
-		ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 )
+		ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == LDAP_OPT_ERROR )
 	{
-		/* set option error */
+		fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
 	}
 	if (referrals != -1 &&
 		ldap_set_option( ld, LDAP_OPT_REFERRALS,
-				 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) == -1 )
+				 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) == LDAP_OPT_ERROR )
 	{
-		/* set option error */
+		fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
+			referrals ? "on" : "off" );
 	}
 
 	if (version != -1 &&
-		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == -1)
+		ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
 	{
-		/* set option error */
+		fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
 	}
 
 	if (want_bindpw)
@@ -308,19 +316,19 @@ main( int argc, char **argv )
 
     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
 	ldap_perror( ld, "ldap_bind" );
-	exit( 1 );
+	return( EXIT_FAILURE );
     }
 
     if ( verbose ) {
-	printf( "filter pattern: %s\nreturning: ", filtpattern );
+	fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
 	if ( attrs == NULL ) {
 	    printf( "ALL" );
 	} else {
 	    for ( i = 0; attrs[ i ] != NULL; ++i ) {
-		printf( "%s ", attrs[ i ] );
+		fprintf( stderr, "%s ", attrs[ i ] );
 	    }
 	}
-	putchar( '\n' );
+	fprintf( stderr, "\n" );
     }
 
     if ( infile == NULL ) {
@@ -344,10 +352,9 @@ main( int argc, char **argv )
     }
 
     ldap_unbind( ld );
-    exit( rc );
 
-	/* UNREACHABLE */
-	return(0);
+
+	return( rc );
 }
 
 
@@ -367,7 +374,7 @@ static int dosearch(
     sprintf( filter, filtpatt, value );
 
     if ( verbose ) {
-	printf( "filter is: (%s)\n", filter );
+	fprintf( stderr, "filter is: (%s)\n", filter );
     }
 
     if ( not ) {