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

* src/groups.c: Add brackets and parenthesis.
	* src/groups.c: Avoid implicit conversion of pointers / integers
	to booleans.
	* src/groups.c: Avoid assignments in comparisons.
	* src/groups.c: Ignore the return value of putchar(), printf()
	* src/groups.c: Ignore return value of setlocale(),
	bindtextdomain(), and textdomain().
This commit is contained in:
nekral-guest 2008-06-10 19:29:54 +00:00
parent 462e8a3d90
commit 46ce06791a
2 changed files with 75 additions and 40 deletions

View File

@ -1,3 +1,14 @@
2008-06-10 Nicolas François <nicolas.francois@centraliens.net>
* src/groups.c: Use a bool when possible instead of int integers.
* src/groups.c: Add brackets and parenthesis.
* src/groups.c: Avoid implicit conversion of pointers / integers
to booleans.
* src/groups.c: Avoid assignments in comparisons.
* src/groups.c: Ignore the return value of putchar(), printf()
* src/groups.c: Ignore return value of setlocale(),
bindtextdomain(), and textdomain().
2008-06-10 Nicolas François <nicolas.francois@centraliens.net> 2008-06-10 Nicolas François <nicolas.francois@centraliens.net>
* src/grpconv.c: Use a bool when possible instead of int integers. * src/grpconv.c: Use a bool when possible instead of int integers.

View File

@ -58,34 +58,47 @@ static void print_groups (const char *member)
int groups = 0; int groups = 0;
struct group *grp; struct group *grp;
struct passwd *pwd; struct passwd *pwd;
int flag = 0; bool flag = false;
/* local, no need for xgetpwnam */ pwd = getpwnam (member); /* local, no need for xgetpwnam */
if ((pwd = getpwnam (member)) == 0) { if (NULL == pwd) {
fprintf (stderr, _("%s: unknown user %s\n"), Prog, member); (void) fprintf (stderr, _("%s: unknown user %s\n"),
Prog, member);
exit (1); exit (1);
} }
setgrent ();
while ((grp = getgrent ())) {
if (is_on_list (grp->gr_mem, member)) {
if (groups++)
putchar (' ');
printf ("%s", grp->gr_name); setgrent ();
if (grp->gr_gid == pwd->pw_gid) while ((grp = getgrent ()) != NULL) {
flag = 1; if (is_on_list (grp->gr_mem, member)) {
if (0 != groups) {
(void) putchar (' ');
}
groups++;
(void) printf ("%s", grp->gr_name);
if (grp->gr_gid == pwd->pw_gid) {
flag = true;
}
} }
} }
endgrent (); endgrent ();
/* local, no need for xgetgrgid */
if (!flag && (grp = getgrgid (pwd->pw_gid))) {
if (groups++)
putchar (' ');
printf ("%s", grp->gr_name); /* The user may not be in the list of members of its primary group */
if (!flag) {
grp = getgrgid (pwd->pw_gid); /* local, no need for xgetgrgid */
if (NULL != grp) {
if (0 != groups) {
(void) putchar (' ');
}
groups++;
(void) printf ("%s", grp->gr_name);
}
}
if (0 != groups) {
(void) putchar ('\n');
} }
if (groups)
putchar ('\n');
} }
/* /*
@ -98,7 +111,7 @@ int main (int argc, char **argv)
#ifdef HAVE_GETGROUPS #ifdef HAVE_GETGROUPS
int ngroups; int ngroups;
GETGROUPS_T *groups; GETGROUPS_T *groups;
int pri_grp; int pri_grp; /* TODO: should be GETGROUPS_T */
int i; int i;
#else #else
char *logname; char *logname;
@ -109,9 +122,9 @@ int main (int argc, char **argv)
#ifdef HAVE_GETGROUPS #ifdef HAVE_GETGROUPS
groups = (GETGROUPS_T *) malloc (sys_ngroups * sizeof (GETGROUPS_T)); groups = (GETGROUPS_T *) malloc (sys_ngroups * sizeof (GETGROUPS_T));
#endif #endif
setlocale (LC_ALL, ""); (void) setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR); (void) bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE); (void) textdomain (PACKAGE);
/* /*
* Get the program name so that error messages can use it. * Get the program name so that error messages can use it.
@ -141,48 +154,58 @@ int main (int argc, char **argv)
* The groupset includes the primary group as well. * The groupset includes the primary group as well.
*/ */
pri_grp = getegid (); pri_grp = getegid ();
for (i = 0; i < ngroups; i++) for (i = 0; i < ngroups; i++) {
if (pri_grp == (int) groups[i]) if (pri_grp == (int) groups[i]) {
break; break;
}
}
if (i != ngroups) if (i != ngroups) {
pri_grp = -1; pri_grp = -1;
}
/* /*
* Print out the name of every group in the current group * Print out the name of every group in the current group
* set. Unknown groups are printed as their decimal group ID * set. Unknown groups are printed as their decimal group ID
* values. * values.
*/ */
if (pri_grp != -1) { if (-1 != pri_grp) {
struct group *gr; struct group *gr;
/* local, no need for xgetgrgid */ /* local, no need for xgetgrgid */
if ((gr = getgrgid (pri_grp))) gr = getgrgid (pri_grp);
printf ("%s", gr->gr_name); if (NULL != gr) {
else (void) printf ("%s", gr->gr_name);
printf ("%d", pri_grp); } else {
(void) printf ("%d", pri_grp);
}
} }
for (i = 0; i < ngroups; i++) { for (i = 0; i < ngroups; i++) {
struct group *gr; struct group *gr;
if (i || pri_grp != -1) if ((0 != i) || (-1 != pri_grp)) {
putchar (' '); (void) putchar (' ');
}
/* local, no need for xgetgrgid */ /* local, no need for xgetgrgid */
if ((gr = getgrgid (groups[i]))) gr = getgrgid (groups[i]);
printf ("%s", gr->gr_name); if (NULL != gr) {
else (void) printf ("%s", gr->gr_name);
printf ("%ld", (long) groups[i]); } else {
(void) printf ("%ld", (long) groups[i]);
} }
putchar ('\n'); }
(void) putchar ('\n');
#else #else
/* /*
* This system does not have the getgroups() system call, so * This system does not have the getgroups() system call, so
* I must check the groups file directly. * I must check the groups file directly.
*/ */
if ((logname = getlogin ())) logname = getlogin ();
if (NULL != logname) {
print_groups (logname); print_groups (logname);
else } else {
exit (1); exit (1);
}
#endif #endif
} else { } else {
@ -194,3 +217,4 @@ int main (int argc, char **argv)
} }
exit (0); exit (0);
} }