Module Name:    src
Committed By:   christos
Date:           Wed Sep  8 13:58:47 UTC 2010

Modified Files:
        src/usr.bin/passwd: yp_passwd.c

Log Message:
KNF, make error messages consistent.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/passwd/yp_passwd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/passwd/yp_passwd.c
diff -u src/usr.bin/passwd/yp_passwd.c:1.34 src/usr.bin/passwd/yp_passwd.c:1.35
--- src/usr.bin/passwd/yp_passwd.c:1.34	Wed Sep  8 09:44:44 2010
+++ src/usr.bin/passwd/yp_passwd.c	Wed Sep  8 09:58:46 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: yp_passwd.c,v 1.34 2010/09/08 13:44:44 christos Exp $	*/
+/*	$NetBSD: yp_passwd.c,v 1.35 2010/09/08 13:58:46 christos Exp $	*/
 
 /*
  * Copyright (c) 1988, 1990, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "from:  @(#)local_passwd.c    8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: yp_passwd.c,v 1.34 2010/09/08 13:44:44 christos Exp $");
+__RCSID("$NetBSD: yp_passwd.c,v 1.35 2010/09/08 13:58:46 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -85,8 +85,8 @@
 	int tries;
 	const char *p, *t;
 	char *result;
-	static char buf[_PASSWORD_LEN+1];
-	char salt[_PASSWORD_LEN+1];
+	static char buf[_PASSWORD_LEN + 1];
+	char salt[_PASSWORD_LEN + 1];
 	char option[LINE_MAX], *key, *opt;
 
 	(void)printf("Changing NIS password for %s.\n", pw->pw_name);
@@ -96,7 +96,7 @@
 	
 		if (pw->pw_passwd[0]) {
 			if (strcmp(crypt(p = getpass("Old password:"),
-					 pw->pw_passwd),  pw->pw_passwd)) {
+			    pw->pw_passwd),  pw->pw_passwd)) {
 				(void)printf("Sorry.\n");
 				pwerror(NULL, 0, 1);
 			}
@@ -123,9 +123,9 @@
 		for (t = p; *t && islower((unsigned char)*t); ++t);
 		if (!*t && ++tries < 2) {
 			(void)printf("Please don't use an all-lower case "
-				     "password.\nUnusual capitalization, "
-				     "control characters or digits are "
-				     "suggested.\n");
+			    "password.\nUnusual capitalization, "
+			    "control characters or digits are "
+			    "suggested.\n");
 			continue;
 		}
 		(void)strlcpy(buf, p, sizeof(buf));
@@ -146,7 +146,7 @@
 		(void)printf("not enough core.\n");
 		pwerror(NULL, 0, 0);
 	}
-	return (result);
+	return result;
 }
 
 static void
@@ -169,15 +169,16 @@
 {
 	char *val;
 	int reason, vallen, namlen = (int)strlen(nam);
-	int flags = 0;
-	int ok = 0;
+	int flags;
+	int ok;
 	
+	flags = ok = 0;
 	val = NULL;
 	reason = yp_match(domain, "master.passwd.byname", nam, namlen,
-                          &val, &vallen);
+	    &val, &vallen);
 	if (reason == YPERR_MAP) {
 		reason = yp_match(domain, "passwd.byname", nam, namlen,
-				  &val, &vallen);
+		    &val, &vallen);
 		flags = _PASSWORD_OLDFMT;
 	}
 	if (reason != 0)
@@ -200,7 +201,7 @@
 pwyp_usage(const char *prefix)
 {
 
-	(void) fprintf(stderr, "%s %s [-d nis | -y] [user]\n",
+	(void)fprintf(stderr, "%s %s [-d nis | -y] [user]\n",
 	    prefix, getprogname());
 }
 
@@ -208,7 +209,7 @@
 pwyp_argv0_usage(const char *prefix)
 {
 
-	(void) fprintf(stderr, "%s %s [user]\n",
+	(void)fprintf(stderr, "%s %s [user]\n",
 	    prefix, getprogname());
 }
 
@@ -217,6 +218,7 @@
 {
 	char *master;
 	int ch, r, rpcport, status;
+	enum clnt_stat yr;
 	struct yppasswd ypp;
 	struct passwd pwb, pwb2, *pw;
 	char pwbuf[1024];
@@ -249,12 +251,12 @@
 		break;
 	default:
 		usage();
-		/* NOTREACHED */
+		/*NOTREACHED*/
 	}
 
 	if (_yp_check(NULL) == 0) {
 		/* can't use YP. */
-		errx(1, "NIS not in use.");
+		errx(EXIT_FAILURE, "NIS not in use.");
 	}
 
 	uid = getuid();
@@ -263,7 +265,7 @@
 	 * Get local domain
 	 */
 	if ((r = yp_get_default_domain(&domain)) != 0)
-		errx(1, "can't get local NIS domain.  Reason: %s",
+		errx(EXIT_FAILURE, "Can't get local NIS domain (%s)",
 		    yperr_string(r));
 
 	/*
@@ -271,7 +273,7 @@
 	 * the daemon.
 	 */
 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0)
-		errx(1, "can't find the master NIS server. Reason: %s",
+		errx(EXIT_FAILURE, "Can't find the master NIS server (%s)",
 		    yperr_string(r));
 
 	/*
@@ -279,40 +281,42 @@
 	 */
 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0)
-		errx(1, "master NIS server not running yppasswd daemon.\n\t%s\n",
-		    "Can't change NIS password.");
+		errx(EXIT_FAILURE, "Master NIS server not running yppasswd "
+		    "daemon");
 
 	/*
 	 * Be sure the port is privileged
 	 */
 	if (rpcport >= IPPORT_RESERVED)
-		errx(1, "yppasswd daemon is on an invalid port.");
+		errx(EXIT_FAILURE, "Yppasswd daemon is on an invalid port");
 
 	/* Bail out if this is a local (non-yp) user, */
 	/* then get user's login identity */
 	if (!ypgetpwnam(username, &pwb) ||
 	    getpwnam_r(username, &pwb2, pwbuf, sizeof(pwbuf), &pw) ||
 	    pw == NULL)
-		errx(1, "NIS unknown user %s", username);
+		errx(EXIT_FAILURE, "NIS unknown user %s", username);
 
-	if (uid && uid != pwb.pw_uid)
-		errx(1, "you may only change your own password: %s",
-		    strerror(EACCES));
+	if (uid && uid != pwb.pw_uid) {
+		errno = EACCES;
+		err(EXIT_FAILURE, "You may only change your own password");
+	}
 
 	makeypp(&ypp, &pwb);
 
 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
 	if (client == NULL)
-		errx(1, "cannot contact yppasswdd on %s:  Reason: %s",
+		errx(EXIT_FAILURE, "Cannot contact yppasswdd on %s (%s)",
 		    master, yperr_string(YPERR_YPBIND));
 
 	client->cl_auth = authunix_create_default();
 	tv.tv_sec = 2;
 	tv.tv_usec = 0;
-	r = clnt_call(client, YPPASSWDPROC_UPDATE,
+	yr = clnt_call(client, YPPASSWDPROC_UPDATE,
 	    xdr_yppasswd, &ypp, xdr_int, &status, tv);
-	if (r)
-		errx(1, "rpc to yppasswdd failed.");
+	if (yr != RPC_SUCCESS)
+		errx(EXIT_FAILURE, "RPC to yppasswdd failed (%s)",
+		    clnt_sperrno(yr));
 	else if (status)
 		printf("Couldn't change NIS password.\n");
 	else
@@ -338,47 +342,45 @@
 	if (_yp_check(NULL) == 0) {
 		/* can't use YP. */
 		if (yppwd)
-			errx(1, "NIS not in use.");
-		return(-1);
+			errx(EXIT_FAILURE, "NIS not in use");
+		return -1;
 	}
-	return (0);
+	return 0;
 }
 
 int
-yp_arg(ch, arg)
-	char ch;
-	const char *arg;
+yp_arg(char ch, const char *arg)
 {
 	switch (ch) {
 	case 'y':
 		yflag = 1;
 		break;
 	default:
-		return(0);
+		return 0;
 	}
-	return(1);
+	return 1;
 }
 
 int
-yp_arg_end()
+yp_arg_end(void)
 {
 	if (yflag)
-		return (PW_USE_FORCE);
-	return (PW_USE);
+		return PW_USE_FORCE;
+	return PW_USE;
 }
 
 void
-yp_end()
+yp_end(void)
 {
 	/* NOOP */
 }
 
 int
-yp_chpw(username)
-	const char *username;
+yp_chpw(const char *username)
 {
 	char *master;
 	int r, rpcport, status;
+	enum clnt_stat yr;
 	struct yppasswd ypp;
 	struct passwd *pw, pwb;
 	char pwbuf[1024];
@@ -391,7 +393,7 @@
 	 * Get local domain
 	 */
 	if ((r = yp_get_default_domain(&domain)) != 0)
-		errx(1, "can't get local NIS domain.  Reason: %s",
+		errx(EXIT_FAILURE, "can't get local NIS domain.  Reason: %s",
 		    yperr_string(r));
 
 	/*
@@ -402,7 +404,7 @@
 		warnx("can't find the master NIS server.  Reason: %s",
 		    yperr_string(r));
 		/* continuation */
-		return(-1);
+		return -1;
 	}
 
 	/*
@@ -410,17 +412,16 @@
 	 */
 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
-		warnx("master NIS server not running yppasswd daemon.\n\t%s\n",
-		    "Can't change NIS password.");
+		warnx("Master NIS server not running yppasswd daemon");
 		/* continuation */
-		return(-1);
+		return -1;
 	}
 
 	/*
 	 * Be sure the port is privileged
 	 */
 	if (rpcport >= IPPORT_RESERVED)
-		errx(1, "yppasswd daemon is on an invalid port.");
+		errx(EXIT_FAILURE, "Yppasswd daemon is on an invalid port");
 
 	/* Bail out if this is a local (non-yp) user, */
 	/* then get user's login identity */
@@ -429,35 +430,37 @@
 	    pw == NULL) {
 		warnx("NIS unknown user %s", username);
 		/* continuation */
-		return(-1);
+		return -1;
 	}
 
-	if (uid && uid != pw->pw_uid)
-		errx(1, "you may only change your own password: %s",
-		    strerror(EACCES));
+	if (uid && uid != pw->pw_uid) {
+		errno = EACCES;
+		err(EXIT_FAILURE, "You may only change your own password");
+	}
 
 	makeypp(&ypp, pw);
 
 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
 	if (client == NULL) {
-		warnx("cannot contact yppasswdd on %s:  Reason: %s",
+		warnx("Cannot contact yppasswdd on %s (%s)",
 		    master, yperr_string(YPERR_YPBIND));
-		return (YPERR_YPBIND);
+		return YPERR_YPBIND;
 	}
 
 	client->cl_auth = authunix_create_default();
 	tv.tv_sec = 2;
 	tv.tv_usec = 0;
-	r = clnt_call(client, YPPASSWDPROC_UPDATE,
+	yr = clnt_call(client, YPPASSWDPROC_UPDATE,
 	    xdr_yppasswd, &ypp, xdr_int, &status, tv);
-	if (r)
-		errx(1, "rpc to yppasswdd failed.");
+	if (yr != RPC_SUCCESS)
+		errx(EXIT_FAILURE, "RPC to yppasswdd failed (%s)",
+		    clnt_sperrno(yr));
 	else if (status)
 		printf("Couldn't change NIS password.\n");
 	else
 		printf("The NIS password has been changed on %s, %s\n",
 		    master, "the master NIS passwd server.");
-	return(0);
+	return 0;
 }
 
 #endif /* USE_PAM */

Reply via email to