* src/gpasswd.c: Use a bool when possible instead of int integers.

* src/gpasswd.c: Avoid implicit conversion of pointers / integers
	/ chars to booleans.
	* src/gpasswd.c: Ignore the return value of putchar() and fflush()
	before exiting.
	* src/gpasswd.c: check_list() renamed is_valid_user_list(), and
	return a bool.
	* src/gpasswd.c: Ignore return value of setlocale(),
	bindtextdomain(), and textdomain().
This commit is contained in:
nekral-guest 2008-06-09 19:26:19 +00:00
parent 5e2b49dad4
commit e41460cae5
2 changed files with 70 additions and 38 deletions

View File

@ -1,3 +1,15 @@
2008-06-09 Nicolas François <nicolas.francois@centraliens.net>
* src/gpasswd.c: Use a bool when possible instead of int integers.
* src/gpasswd.c: Avoid implicit conversion of pointers / integers
/ chars to booleans.
* src/gpasswd.c: Ignore the return value of putchar() and fflush()
before exiting.
* src/gpasswd.c: check_list() renamed is_valid_user_list(), and
return a bool.
* src/gpasswd.c: Ignore return value of setlocale(),
bindtextdomain(), and textdomain().
2008-06-09 Nicolas François <nicolas.francois@centraliens.net> 2008-06-09 Nicolas François <nicolas.francois@centraliens.net>
* src/grpck.c: Use a bool when possible instead of int integers. * src/grpck.c: Use a bool when possible instead of int integers.

View File

@ -58,14 +58,18 @@ static char *Prog;
#ifdef SHADOWGRP #ifdef SHADOWGRP
/* Indicate if shadow groups are enabled on the system /* Indicate if shadow groups are enabled on the system
* (/etc/gshadow present) */ * (/etc/gshadow present) */
static int is_shadowgrp; static bool is_shadowgrp;
static int gshadow_locked = 0; static bool gshadow_locked = false;
#endif #endif
static int group_locked = 0; static bool group_locked = false;
/* Flags set by options */ /* Flags set by options */
static int static bool aflg = false;
aflg = 0, Aflg = 0, dflg = 0, Mflg = 0, rflg = 0, Rflg = 0; static bool Aflg = false;
static bool dflg = false;
static bool Mflg = false;
static bool rflg = false;
static bool Rflg = false;
/* The name of the group that is being affected */ /* The name of the group that is being affected */
static char *group = NULL; static char *group = NULL;
/* The name of the user being added (-a) or removed (-d) from group */ /* The name of the user being added (-a) or removed (-d) from group */
@ -79,7 +83,7 @@ static char *admins = NULL;
/* The name of the caller */ /* The name of the caller */
static char *myname = NULL; static char *myname = NULL;
/* The UID of the caller */ /* The UID of the caller */
static unsigned long bywho = -1; static uid_t bywho;
/* Indicate if gpasswd was called by root */ /* Indicate if gpasswd was called by root */
#define amroot (0 == bywho) #define amroot (0 == bywho)
@ -92,7 +96,7 @@ static unsigned long bywho = -1;
static void usage (void); static void usage (void);
static RETSIGTYPE catch_signals (int killed); static RETSIGTYPE catch_signals (int killed);
static void fail_exit (int status); static void fail_exit (int status);
static int check_list (const char *users); static bool is_valid_user_list (const char *users);
static void process_flags (int argc, char **argv); static void process_flags (int argc, char **argv);
static void check_flags (int argc, int opt_index); static void check_flags (int argc, int opt_index);
static void open_files (void); static void open_files (void);
@ -138,15 +142,15 @@ static RETSIGTYPE catch_signals (int killed)
{ {
static TERMIO sgtty; static TERMIO sgtty;
if (killed) { if (0 != killed) {
STTY (0, &sgtty); STTY (0, &sgtty);
} else { } else {
GTTY (0, &sgtty); GTTY (0, &sgtty);
} }
if (killed) { if (0 != killed) {
putchar ('\n'); (void) putchar ('\n');
fflush (stdout); (void) fflush (stdout);
fail_exit (killed); fail_exit (killed);
} }
} }
@ -169,18 +173,18 @@ static void fail_exit (int status)
} }
/* /*
* check_list - check a comma-separated list of user names for validity * is_valid_user_list - check a comma-separated list of user names for validity
* *
* check_list scans a comma-separated list of user names and checks * is_valid_user_list scans a comma-separated list of user names and
* that each listed name exists. * checks that each listed name exists is the user database.
* *
* It returns 0 on success. * It returns true if the list of users is valid.
*/ */
static int check_list (const char *users) static bool is_valid_user_list (const char *users)
{ {
const char *start, *end; const char *start, *end;
char username[32]; char username[32];
int errors = 0; bool valid = true;
size_t len; size_t len;
for (start = users; (NULL != start) && ('\0' != *start); start = end) { for (start = users; (NULL != start) && ('\0' != *start); start = end) {
@ -206,10 +210,10 @@ static int check_list (const char *users)
if (getpwnam (username) == NULL) { if (getpwnam (username) == NULL) {
fprintf (stderr, _("%s: unknown user %s\n"), fprintf (stderr, _("%s: unknown user %s\n"),
Prog, username); Prog, username);
errors++; valid = false;
} }
} }
return errors; return valid;
} }
static void failure (void) static void failure (void)
@ -240,7 +244,7 @@ static void process_flags (int argc, char **argv)
#endif #endif
fail_exit (1); fail_exit (1);
} }
aflg++; aflg = true;
break; break;
#ifdef SHADOWGRP #ifdef SHADOWGRP
case 'A': case 'A':
@ -260,14 +264,14 @@ static void process_flags (int argc, char **argv)
fail_exit (2); fail_exit (2);
} }
admins = optarg; admins = optarg;
if (check_list (admins) != 0) { if (!is_valid_user_list (admins)) {
fail_exit (1); fail_exit (1);
} }
Aflg++; Aflg = true;
break; break;
#endif #endif
case 'd': /* delete a user */ case 'd': /* delete a user */
dflg++; dflg = true;
user = optarg; user = optarg;
break; break;
case 'g': /* no-op from normal password */ case 'g': /* no-op from normal password */
@ -282,16 +286,16 @@ static void process_flags (int argc, char **argv)
failure (); failure ();
} }
members = optarg; members = optarg;
if (check_list (members) != 0) { if (!is_valid_user_list (members)) {
fail_exit (1); fail_exit (1);
} }
Mflg++; Mflg = true;
break; break;
case 'r': /* remove group password */ case 'r': /* remove group password */
rflg++; rflg = true;
break; break;
case 'R': /* restrict group password */ case 'R': /* restrict group password */
Rflg++; Rflg = true;
break; break;
default: default:
usage (); usage ();
@ -309,10 +313,26 @@ static void process_flags (int argc, char **argv)
*/ */
static void check_flags (int argc, int opt_index) static void check_flags (int argc, int opt_index)
{ {
int exclusive = 0;
/* /*
* Make sure exclusive flags are exclusive * Make sure exclusive flags are exclusive
*/ */
if (aflg + dflg + rflg + Rflg + (Aflg || Mflg) > 1) { if (aflg) {
exclusive++;
}
if (dflg) {
exclusive++;
}
if (rflg) {
exclusive++;
}
if (Rflg) {
exclusive++;
}
if (Aflg || Mflg) {
exclusive++;
}
if (exclusive > 1) {
usage (); usage ();
} }
@ -340,7 +360,7 @@ static void open_files (void)
#endif #endif
fail_exit (1); fail_exit (1);
} }
group_locked++; group_locked = true;
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadowgrp) { if (is_shadowgrp) {
if (sgr_lock () == 0) { if (sgr_lock () == 0) {
@ -353,7 +373,7 @@ static void open_files (void)
#endif #endif
fail_exit (1); fail_exit (1);
} }
gshadow_locked++; gshadow_locked = true;
} }
#endif #endif
if (gr_open (O_RDWR) == 0) { if (gr_open (O_RDWR) == 0) {
@ -409,7 +429,7 @@ static void close_files (void)
if (is_shadowgrp) { if (is_shadowgrp) {
/* TODO: same logging as in open_files & for /etc/group */ /* TODO: same logging as in open_files & for /etc/group */
sgr_unlock (); sgr_unlock ();
gshadow_locked--; gshadow_locked = false;
} }
#endif #endif
if (gr_unlock () == 0) { if (gr_unlock () == 0) {
@ -420,7 +440,7 @@ static void close_files (void)
#endif #endif
exit (1); exit (1);
} }
group_locked--; group_locked = false;
} }
/* /*
@ -736,9 +756,9 @@ int main (int argc, char **argv)
#endif #endif
sanitize_env (); sanitize_env ();
setlocale (LC_ALL, ""); (void) setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR); (void) bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE); (void) textdomain (PACKAGE);
/* /*
* Make a note of whether or not this command was invoked by root. * Make a note of whether or not this command was invoked by root.
@ -857,18 +877,18 @@ int main (int argc, char **argv)
* one, except the routine is different. * one, except the routine is different.
*/ */
if (dflg) { if (dflg) {
int removed = 0; bool removed = false;
printf (_("Removing user %s from group %s\n"), user, group); printf (_("Removing user %s from group %s\n"), user, group);
if (is_on_list (grent.gr_mem, user)) { if (is_on_list (grent.gr_mem, user)) {
removed = 1; removed = true;
grent.gr_mem = del_list (grent.gr_mem, user); grent.gr_mem = del_list (grent.gr_mem, user);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadowgrp) { if (is_shadowgrp) {
if (is_on_list (sgent.sg_mem, user)) { if (is_on_list (sgent.sg_mem, user)) {
removed = 1; removed = true;
sgent.sg_mem = del_list (sgent.sg_mem, user); sgent.sg_mem = del_list (sgent.sg_mem, user);
} }
} }