]> err.no Git - util-linux/commitdiff
newgrp: Use err() and EXIT_*
authorMarek Polacek <mmpolacek@gmail.com>
Tue, 30 Nov 2010 21:53:31 +0000 (22:53 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 2 Dec 2010 16:15:51 +0000 (17:15 +0100)
Use err() or errx() where appropriate.  Also use EXIT_* macros instead
of 0/1.  All error messages shall start with lowercase.

Signed-off-by: Marek Polacek <mmpolacek@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/newgrp.c

index 976a0493956cdc018b46f781fd6e20451051fc3e..6323af19838d3b1081de39491e654ba109083244 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <err.h>
 
 #include "c.h"
 #include "pathnames.h"
@@ -54,7 +55,7 @@ get_gshadow_pwd(char *groupname)
 }
 
 static int
-allow_setgid(struct passwd *pe, struct group *ge) 
+allow_setgid(struct passwd *pe, struct group *ge)
 {
     char **look;
     int notfound = 1;
@@ -84,7 +85,7 @@ allow_setgid(struct passwd *pe, struct group *ge)
     return FALSE;                      /* default to denial */
 }
 
-int 
+int
 main(int argc, char *argv[])
 {
     struct passwd *pw_entry;
@@ -95,47 +96,37 @@ main(int argc, char *argv[])
     bindtextdomain(PACKAGE, LOCALEDIR);
     textdomain(PACKAGE);
 
-    if (!(pw_entry = getpwuid(getuid()))) {
-       perror(_("newgrp: Who are you?"));
-       exit(1);
-    }
+    if (!(pw_entry = getpwuid(getuid())))
+       err(EXIT_FAILURE, _("who are you?"));
 
     shell = (pw_entry->pw_shell[0] ? pw_entry->pw_shell : _PATH_BSHELL);
 
     if (argc < 2) {
-       if(setgid(pw_entry->pw_gid) < 0) {
-           perror(_("newgrp: setgid"));
-           exit(1);
-       }
+       if(setgid(pw_entry->pw_gid) < 0)
+           err(EXIT_FAILURE, _("setgid failed"));
     } else {
        errno = 0;
        if (!(gr_entry = getgrnam(argv[1]))) {
            if (errno)
-                   perror(_("newgrp: No such group."));                        /* error */
+                   err(EXIT_FAILURE, _("no such group"));
            else
-                   fprintf(stderr, "%s\n", _("newgrp: No such group."));       /* no group */
-           exit(1);
+                   errx(EXIT_FAILURE, _("no such group"));     /* No group */
        } else {
-           if(allow_setgid(pw_entry, gr_entry)) {
-               if(setgid(gr_entry->gr_gid) < 0) {
-                   perror(_("newgrp: setgid"));
-                   exit(1);
-               }
-           } else {
-               puts(_("newgrp: Permission denied"));
-               exit(1);
-           }
+           if (allow_setgid(pw_entry, gr_entry)) {
+               if (setgid(gr_entry->gr_gid) < 0)
+                   err(EXIT_FAILURE, _("setgid failed"));
+           } else
+               errx(EXIT_FAILURE, _("permission denied"));
        }
     }
 
-    if(setuid(getuid()) < 0) {
-       perror(_("newgrp: setuid"));
-       exit(1);
-    }
+    if (setuid(getuid()) < 0)
+       err(EXIT_FAILURE, _("setuid failed"));
 
     fflush(stdout); fflush(stderr);
     execl(shell,shell,(char*)0);
-    perror(_("No shell"));
+    warn(_("exec %s failed"), shell);
     fflush(stderr);
-    exit(1);
+
+    return EXIT_FAILURE;
 }