[PATCH 3/11] chsh: Use EXIT_*

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Marek Polacek <mmpolacek@xxxxxxxxx>
---
 login-utils/chsh.c |   73
+++++++++++++++++++++++++++------------------------
 1 files changed, 39 insertions(+), 34 deletions(-)

diff --git a/login-utils/chsh.c b/login-utils/chsh.c
index a893ef9..984cf35 100644
--- a/login-utils/chsh.c
+++ b/login-utils/chsh.c
@@ -27,6 +27,7 @@
 #define _POSIX_SOURCE 1
 #endif

+#include <err.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
@@ -52,7 +53,7 @@
 	if ((_rc) != PAM_SUCCESS) { \
 	    fprintf(stderr, "\n%s\n", pam_strerror((_ph), (_rc))); \
 	    pam_end((_ph), (_rc)); \
-	    exit(1); \
+	    exit(EXIT_FAILURE); \
 	} \
     } while(0)

@@ -117,19 +118,21 @@ main (int argc, char *argv[]) {
 	pw = getpwuid (uid);
 	if (! pw) {
 	    fprintf (stderr, _("%s: you (user %d) don't exist.\n"), whoami, uid);
-	    return (-1); }
+	    return EXIT_FAILURE;
+	}
     }
     else {
 	pw = getpwnam (info.username);
 	if (! pw) {
 	    cp = info.username;
 	    fprintf (stderr, _("%s: user \"%s\" does not exist.\n"), whoami, cp);
-	    return (-1); }
+	    return EXIT_FAILURE;
+	}
     }

     if (!(is_local(pw->pw_name))) {
        fprintf (stderr, _("%s: can only change local entries.\n"), whoami);
-       exit(1);
+       return EXIT_FAILURE;
     }

 #ifdef HAVE_LIBSELINUX
@@ -142,13 +145,13 @@ main (int argc, char *argv[]) {
 	  fprintf(stderr, _("%s: %s is not authorized to change the shell of
%s\n"),
 		  whoami, user_context, pw->pw_name);
 	  freecon(user_context);
-	  exit(1);
+	  return EXIT_FAILURE;
 	}
       }
       if (setupDefaultContext("/etc/passwd") != 0) {
 	fprintf(stderr,_("%s: Can't set default context for /etc/passwd"),
 		whoami);
-	exit(1);
+	return EXIT_FAILURE;
       }
     }
 #endif
@@ -162,13 +165,13 @@ main (int argc, char *argv[]) {
 	errno = EACCES;
 	fprintf(stderr,_("%s: Running UID doesn't match UID of user we're "
 			 "altering, shell change denied\n"), whoami);
-	return (-1);
+	return EXIT_FAILURE;
     }
     if (uid != 0 && !get_shell_list(oldshell)) {
 	errno = EACCES;
 	fprintf(stderr,_("%s: Your shell is not in /etc/shells, shell change"
 		" denied\n"),whoami);
-	return (-1);
+	return EXIT_FAILURE;
     }

     shell = info.shell;
@@ -186,7 +189,7 @@ main (int argc, char *argv[]) {
 	if(retcode != PAM_SUCCESS) {
 	    fprintf(stderr, _("%s: PAM failure, aborting: %s\n"),
 		    whoami, pam_strerror(pamh, retcode));
-	    exit(1);
+	    return EXIT_FAILURE;
 	}

 	retcode = pam_authenticate(pamh, 0);
@@ -211,7 +214,7 @@ main (int argc, char *argv[]) {
 	if(strncmp(pw->pw_passwd,
 		   crypt(pwdstr, pw->pw_passwd), 13)) {
 	    puts(_("Incorrect password."));
-	    exit(1);
+	    return EXIT_FAILURE;
 	}
     }
 #endif /* HAVE_SECURITY_PAM_MISC_H */
@@ -219,23 +222,25 @@ main (int argc, char *argv[]) {

     if (! shell) {
 	shell = prompt (_("New shell"), oldshell);
-	if (! shell) return 0;
+	if (! shell)
+	    return EXIT_SUCCESS;
     }

-    if (check_shell (shell) < 0) return (-1);
+    if (check_shell (shell) < 0)
+        return EXIT_FAILURE;

     if (! strcmp (pw->pw_shell, shell)) {
 	printf (_("Shell not changed.\n"));
-	return 0;
+	return EXIT_SUCCESS;
     }
     pw->pw_shell = shell;
     if (setpwnam (pw) < 0) {
 	perror ("setpwnam");
 	printf( _("Shell *NOT* changed.  Try again later.\n") );
-	return (-1);
+	return EXIT_FAILURE;
     }
     printf (_("Shell changed.\n"));
-    return 0;
+    return EXIT_SUCCESS;
 }

 /*
@@ -263,30 +268,30 @@ parse_argv (int argc, char *argv[], struct sinfo
*pinfo) {
 	    break;
 	case 'v':
 	    printf ("%s\n", PACKAGE_STRING);
-	    exit (0);
+	    exit (EXIT_SUCCESS);
 	case 'u':
 	    usage (stdout);
-	    exit (0);
+	    exit (EXIT_SUCCESS);
 	case 'l':
 	    get_shell_list (NULL);
-	    exit (0);
+	    exit (EXIT_SUCCESS);
 	case 's':
 	    if (! optarg) {
 		usage (stderr);
-		exit (-1);
+		exit (EXIT_FAILURE);
 	    }
 	    pinfo->shell = optarg;
 	    break;
 	default:
 	    usage (stderr);
-	    exit (-1);
+	    exit (EXIT_FAILURE);
 	}
     }
     /* done parsing arguments.	check for a username. */
     if (optind < argc) {
 	if (optind + 1 < argc) {
 	    usage (stderr);
-	    exit (-1);
+	    exit (EXIT_FAILURE);
 	}
 	pinfo->username = argv[optind];
     }
@@ -318,7 +323,7 @@ prompt (char *question, char *def_val) {
     *buf = 0;
     if (fgets (buf, sizeof (buf), stdin) == NULL) {
 	printf (_("\nAborted.\n"));
-	exit (-1);
+	exit (EXIT_FAILURE);
     }
     /* remove the newline at the end of buf. */
     ans = buf;
@@ -342,30 +347,30 @@ check_shell (char *shell) {
     int i, c;

     if (!shell)
-	return (-1);
+	return -1;

     if (*shell != '/') {
 	printf (_("%s: shell must be a full path name.\n"), whoami);
-	return (-1);
+	return -1;
     }
     if (access (shell, F_OK) < 0) {
 	printf (_("%s: \"%s\" does not exist.\n"), whoami, shell);
-	return (-1);
+	return -1;
     }
     if (access (shell, X_OK) < 0) {
 	printf (_("%s: \"%s\" is not executable.\n"), whoami, shell);
-	return (-1);
+	return -1;
     }
     /* keep /etc/passwd clean. */
     for (i = 0; i < strlen (shell); i++) {
 	c = shell[i];
 	if (c == ',' || c == ':' || c == '=' || c == '"' || c == '\n') {
 	    printf (_("%s: '%c' is not allowed.\n"), whoami, c);
-	    return (-1);
+	    return -1;
 	}
 	if (iscntrl (c)) {
 	    printf (_("%s: Control characters are not allowed.\n"), whoami);
-	    return (-1);
+	    return -1;
 	}
     }
 #ifdef ONLY_LISTED_SHELLS
@@ -376,7 +381,7 @@ check_shell (char *shell) {
 	  printf (_("%s: \"%s\" is not listed in /etc/shells.\n"),
 		  whoami, shell);
 	  printf( _("%s: Use -l option to see list.\n"), whoami );
-	  exit(1);
+	  exit(EXIT_FAILURE);
        }
     }
 #else
@@ -385,7 +390,7 @@ check_shell (char *shell) {
        printf( _("Use %s -l to see list.\n"), whoami );
     }
 #endif
-    return 0;
+    return EXIT_SUCCESS;
 }

 /*
@@ -428,15 +433,15 @@ get_shell_list (char *shell_name) {

 /*
  *  xmalloc () -- malloc that never fails.
+ *  TODO: Use xmalloc.h instead
  */
 static void *
 xmalloc (int bytes) {
     void *vp;

     vp = malloc (bytes);
-    if (! vp && bytes > 0) {
-	perror (_("malloc failed"));
-	exit (-1);
-    }
+    if (! vp && bytes > 0)
+	err(EXIT_FAILURE, _("malloc failed"));
+
     return vp;
 }
-- 
1.7.3.2


--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux