From 1fa30199c69ac25bbfb72df661ee17a13c757539 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 30 Nov 2010 22:53:31 +0100 Subject: [PATCH] newgrp: Use err() and EXIT_* 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 Signed-off-by: Karel Zak --- login-utils/newgrp.c | 47 ++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c index 976a0493..6323af19 100644 --- a/login-utils/newgrp.c +++ b/login-utils/newgrp.c @@ -14,6 +14,7 @@ #include #include #include +#include #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; } -- 2.39.5