bug-coreutils
[Top][All Lists]
Advanced

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

Re: Options --enable{,-no}-install-program


From: Jim Meyering
Subject: Re: Options --enable{,-no}-install-program
Date: Thu, 14 Feb 2008 11:53:06 +0100

"James Youngman" <address@hidden> wrote:
> On Jan 26, 2008 10:55 PM, Jim Meyering <address@hidden> wrote:
>
>> After a partial review and tweaks to make "make distcheck" pass,
>> I have the patch below.  Most of the changes so far:
>>
>> - A few of the files had trailing spaces: removed them
>> - converted all TABs in src/gr*.[ch] to sequences of spaces
>>     and added the Local variables: block at end of each new .c file.
>> - factored out the duplication in the coreutils.texi change
>> - added new names to POTFILES.in
>> - updated AUTHORS
>> - while I know too well how bad stdio is, I'm tempted to remove the
>>     new tests of each printf return value.  I noticed that you check
>>     those while not checking putchar.
>>
>> I'll let this sit for a couple more days...
>
> Does this need more work from me?    If so, what still needs to be improved?

Hi James,

In a way, yes.

Considering that I want to get rid of the uses of getugroups,
I am reluctant to add new uses (see the TODO item re getgrouplist).
But for now there is no gnulib code to avoid the bug that makes
glibc-2.3.2's getgrouplist function segfault.

On the other hand, your patch is a clear improvement, so I'll go ahead and
publish what I have.  For your reference, here are the two change-sets
I've just pushed:

>From 167b8025aca487de001da2448c1aebc2747bc1d3 Mon Sep 17 00:00:00 2001
From: James Youngman <address@hidden>
Date: Fri, 25 Jan 2008 16:05:52 +0000
Subject: [PATCH] Replace groups.sh with groups.c.

* src/groups.c (main): New file, replacing groups.sh.
* src/group-list.c, src/group-list.h: New files, factored out of id.c,
implementing the functionality that "id" and "groups" have in common.
* src/id.c (print_full_info): Avoid a segfault when trying to print
an error message if getgroups fails.
(print_group_list): Move to group-list.c.
(print_group): Likewise.
* man/Makefile.am: When building groups.1, obtain the help text
from src/groups.c, not src/groups.sh.
(noinst_HEADERS): Add group-list.h.
(group): Remove rule.
(dist_man_MANS): Remove groups.1.
* doc/coreutils.texi (groups: Print group names a user is in):
Explain why "groups" and "groups $(id -un)" give different results
in existing login sessions after you change the group database.
(id: Print user identity): Likewise for "id".
* po/POTFILES.in: Add src/group-list.c and src/groups.c.
* NEWS: mention this.
* AUTHORS: Update.

Signed-off-by: Jim Meyering <address@hidden>
---
 AUTHORS            |    2 +-
 NEWS               |    3 +
 doc/coreutils.texi |   14 +++++-
 man/Makefile.am    |    4 +-
 po/POTFILES.in     |    4 +-
 src/Makefile.am    |   22 ++------
 src/group-list.c   |  134 +++++++++++++++++++++++++++++++++++++++++++++
 src/group-list.h   |   19 +++++++
 src/groups.c       |  153 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/groups.sh      |   84 ----------------------------
 src/id.c           |  108 ++++++++++---------------------------
 11 files changed, 361 insertions(+), 186 deletions(-)
 create mode 100644 src/group-list.c
 create mode 100644 src/group-list.h
 create mode 100644 src/groups.c
 delete mode 100755 src/groups.sh

diff --git a/AUTHORS b/AUTHORS
index 200e141..807857f 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -31,7 +31,7 @@ false: Jim Meyering
 fmt: Ross Paterson
 fold: David MacKenzie
 ginstall: David MacKenzie
-groups: David MacKenzie
+groups: David MacKenzie, James Youngman
 head: David MacKenzie, Jim Meyering
 hostid: Jim Meyering
 hostname: Jim Meyering
diff --git a/NEWS b/NEWS
index e05e1c3..af27aab 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-

 ** Bug fixes

+  configure --enable-no-install-program=groups now works.
+
+
   ls no longer segfaults on files in /proc when linked with an older version
   of libselinux.  E.g., ls -l /proc/sys would dereference a NULL pointer.

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index dfab040..23d0ab4 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -12226,6 +12226,15 @@ Print only the user ID.

 @exitstatus

address@hidden primaryAndSupplementaryGroups{cmd,arg}
+Primary and supplementary groups for a process are normally inherited
+from its parent and are usually unchanged since login.  This means
+that if you change the group database after logging in, @command{\cmd\}
+will not reflect your changes within your existing login session.
+Running @command{\cmd\} with a \arg\ causes the user and group
+database to be consulted afresh, and so will give a different result.
address@hidden macro
address@hidden,user argument}

 @node logname invocation
 @section @command{logname}: Print current login name
@@ -12275,7 +12284,8 @@ options}.
 groups for each given @var{username}, or the current process if no names
 are given.  If more than one name is given, the name of each user is
 printed before
-the list of that user's groups.  Synopsis:
+the list of that user's groups and the user name is separated from the
+group list by a colon.  Synopsis:

 @example
 groups address@hidden@dots{}
@@ -12283,6 +12293,8 @@ groups address@hidden@dots{}

 The group lists are equivalent to the output of the command @samp{id -Gn}.

address@hidden,list of users}
+
 The only options are @option{--help} and @option{--version}.  @xref{Common
 options}.

diff --git a/man/Makefile.am b/man/Makefile.am
index a4351d6..9076afc 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -15,7 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.

-dist_man_MANS = groups.1 $(MAN)
+dist_man_MANS = $(MAN)

 man_aux = $(dist_man_MANS:.1=.x)

@@ -59,7 +59,7 @@ factor.1:     $(common_dep)   $(srcdir)/factor.x      
../src/factor.c
 false.1:       $(common_dep)   $(srcdir)/false.x       ../src/false.c
 fmt.1:         $(common_dep)   $(srcdir)/fmt.x         ../src/fmt.c
 fold.1:                $(common_dep)   $(srcdir)/fold.x        ../src/fold.c
-groups.1:      $(common_dep)   $(srcdir)/groups.x      ../src/groups.sh
+groups.1:      $(common_dep)   $(srcdir)/groups.x      ../src/groups.c
 head.1:                $(common_dep)   $(srcdir)/head.x        ../src/head.c
 hostid.1:      $(common_dep)   $(srcdir)/hostid.x      ../src/hostid.c
 hostname.1:    $(common_dep)   $(srcdir)/hostname.x    ../src/hostname.c
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 686332d..e975109 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -1,5 +1,5 @@
 # List of files which contain translatable strings.
-# Copyright (C) 1996-2007 Free Software Foundation, Inc.
+# Copyright (C) 1996-2008 Free Software Foundation, Inc.

 # These are nominally temporary...
 lib/acl.c
@@ -60,6 +60,8 @@ src/factor.c
 src/false.c
 src/fmt.c
 src/fold.c
+src/group-list.c
+src/groups.c
 src/head.c
 src/hostid.c
 src/hostname.c
diff --git a/src/Makefile.am b/src/Makefile.am
index a0b1d0c..155f22b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -33,7 +33,7 @@ EXTRA_PROGRAMS = \
   ginstall link ln dir vdir ls mkdir \
   mkfifo mknod mktemp \
   mv nohup readlink rm rmdir shred stat sync touch unlink \
-  cat cksum comm csplit cut expand fmt fold head join md5sum \
+  cat cksum comm csplit cut expand fmt fold head join groups md5sum \
   nl od paste pr ptx sha1sum sha224sum sha256sum sha384sum sha512sum \
   shuf sort split sum tac tail tr tsort unexpand uniq wc \
   basename date dirname echo env expr factor false \
@@ -42,7 +42,6 @@ EXTRA_PROGRAMS = \
   test true tty whoami yes \
   base64

-bin_SCRIPTS = groups
 bin_PROGRAMS = $(OPTIONAL_BIN_PROGS)

 noinst_PROGRAMS = setuidgid
@@ -53,6 +52,7 @@ noinst_HEADERS = \
   cp-hash.h \
   dircolors.h \
   fs.h \
+  group-list.h \
   ls.h \
   remove.h \
   system.h \
@@ -61,7 +61,7 @@ noinst_HEADERS = \
   uname.h

 EXTRA_DIST = dcgen dircolors.hin tac-pipe.c \
-  groups.sh wheel-gen.pl extract-magic c99-to-c89.diff
+  wheel-gen.pl extract-magic c99-to-c89.diff
 BUILT_SOURCES =
 CLEANFILES = $(SCRIPTS) su

@@ -142,20 +142,6 @@ RELEASE_YEAR = \
   `sed -n '/.*COPYRIGHT_YEAR = \([0-9][0-9][0-9][0-9]\) };/s//\1/p' \
     $(top_srcdir)/lib/version-etc.c`

-# This depends on 'Makefile', so that version changes
-#(reflected in Makefile's VERSION definition)
-# are reflected into groups --version also between releases.
-groups: groups.sh Makefile
-       rm -f $@ address@hidden
-       sed \
-         -e 's!@''bindir''@!$(bindir)!' \
-         -e 's/@''RELEASE_YEAR'@/$(RELEASE_YEAR)/ \
-         -e 's/@''PACKAGE_NAME''@/$(PACKAGE_NAME)/' \
-         -e 's/@''PACKAGE_BUGREPORT''@/$(PACKAGE_BUGREPORT)/' \
-         -e 's/@''VERSION''@/$(VERSION)/' $(srcdir)/groups.sh > address@hidden
-       chmod +x address@hidden
-       mv address@hidden $@
-
 all-local: su$(EXEEXT)

 installed_su = $(DESTDIR)$(bindir)/`echo su|sed '$(transform)'`
@@ -225,6 +211,8 @@ __SOURCES = lbracket.c
 cp_SOURCES = cp.c $(copy_sources)
 dir_SOURCES = ls.c ls-dir.c
 vdir_SOURCES = ls.c ls-vdir.c
+id_SOURCES = id.c group-list.c
+groups_SOURCES = groups.c group-list.c
 ln_SOURCES = ln.c
 ls_SOURCES = ls.c ls-ls.c
 chown_SOURCES = chown.c chown-core.c
diff --git a/src/group-list.c b/src/group-list.c
new file mode 100644
index 0000000..e788f8e
--- /dev/null
+++ b/src/group-list.c
@@ -0,0 +1,134 @@
+/* group-list.c --Print a list of group IDs or names.
+   Copyright (C) 1989-2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Arnold Robbins.
+   Major rewrite by David MacKenzie, address@hidden
+   Extracted from id.c by James Youngman. */
+
+#include <config.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <getopt.h>
+#include <pwd.h>
+#include <grp.h>
+
+#include "system.h"
+#include "error.h"
+#include "mgetgroups.h"
+#include "quote.h"
+#include "group-list.h"
+
+
+/* Print all of the distinct groups the user is in. */
+extern bool
+print_group_list (const char *username,
+                  uid_t ruid, gid_t rgid, gid_t egid,
+                  bool use_names)
+{
+  bool ok = true;
+  struct passwd *pwd;
+
+  pwd = getpwuid (ruid);
+  if (pwd == NULL)
+    ok = false;
+
+  if (!print_group (rgid, use_names))
+    ok = false;
+
+  if (egid != rgid)
+    {
+      putchar (' ');
+      if (!print_group (egid, use_names))
+        ok = false;
+    }
+
+#if HAVE_GETGROUPS
+  {
+    GETGROUPS_T *groups;
+    size_t i;
+
+    int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
+                               &groups);
+    if (n_groups < 0)
+      {
+        if (username)
+          {
+            error (0, errno, _("failed to get groups for user %s"),
+                   quote (username));
+          }
+        else
+          {
+            error (0, errno, _("failed to get groups for the current 
process"));
+          }
+        return false;
+      }
+
+    for (i = 0; i < n_groups; i++)
+      if (groups[i] != rgid && groups[i] != egid)
+        {
+          putchar (' ');
+          if (!print_group (groups[i], use_names))
+            ok = false;
+        }
+    free (groups);
+    return ok;
+  }
+#endif /* HAVE_GETGROUPS */
+}
+
+
+/* Print the name or value of group ID GID. */
+extern bool
+print_group (gid_t gid, bool use_name)
+{
+  struct group *grp = NULL;
+  bool ok = true;
+
+  if (use_name)
+    {
+      grp = getgrgid (gid);
+      if (grp == NULL)
+        {
+          error (0, 0, _("cannot find name for group ID %lu"),
+                 (unsigned long int) gid);
+          ok = false;
+        }
+    }
+
+  if (grp == NULL)
+    {
+      if (printf ("%lu", (unsigned long int) gid) < 0)
+        {
+          error (0, errno, _("write error"));
+          ok = false;
+        }
+    }
+  else
+    {
+      if (printf ("%s", grp->gr_name) < 0)
+        {
+          error (0, errno, _("write error"));
+          ok = false;
+        }
+    }
+  return ok;
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/group-list.h b/src/group-list.h
new file mode 100644
index 0000000..868585e
--- /dev/null
+++ b/src/group-list.h
@@ -0,0 +1,19 @@
+/* group-list.h -- prototypes shared by id and groups.
+
+   Copyright (C) 2008 Free Software Foundation.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+bool print_group (gid_t, bool);
+bool print_group_list (const char *, uid_t, gid_t, gid_t, bool);
diff --git a/src/groups.c b/src/groups.c
new file mode 100644
index 0000000..8a4673c
--- /dev/null
+++ b/src/groups.c
@@ -0,0 +1,153 @@
+/* groups -- print the groups a user is in
+   Copyright (C) 1989-2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by James Youngman based on id.c and groups.sh,
+   which were written by Arnold Robbins and David MacKenzie. */
+
+#include <config.h>
+#include <stdio.h>
+#include <getopt.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+#include <getopt.h>
+
+#include "system.h"
+#include "error.h"
+#include "group-list.h"
+
+/* The name this program was run with. */
+char *program_name;
+
+/* The official name of this program (e.g., no `g' prefix).  */
+#define PROGRAM_NAME "groups"
+
+#define AUTHORS "David MacKenzie", "James Youngman"
+
+
+static struct option const longopts[] =
+{
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
+  {NULL, 0, NULL, 0}
+};
+
+void
+usage (int status)
+{
+  if (status != EXIT_SUCCESS)
+    fprintf (stderr, _("Try `%s --help' for more information.\n"),
+             program_name);
+  else
+    {
+      printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
+      fputs (_("\
+Print information for USERNAME or, if no USERNAME is specified,\n\
+the current process (which is different if the groups database has 
changed).\n"),
+             stdout);
+      fputs (HELP_OPTION_DESCRIPTION, stdout);
+      fputs (VERSION_OPTION_DESCRIPTION, stdout);
+      emit_bug_reporting_address ();
+    }
+  exit (status);
+}
+
+static void
+write_error (void)
+{
+  error (0, errno, _("write error"));
+}
+
+
+int
+main (int argc, char **argv)
+{
+  int optc;
+  bool ok = true;
+  gid_t rgid, egid;
+  uid_t ruid;
+
+  initialize_main (&argc, &argv);
+  program_name = argv[0];
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+
+  atexit (close_stdout);
+
+  /* Processing the arguments this way makes groups.c behave differently to
+   * groups.sh if one of the arguments is "--".
+   */
+  while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1)
+    {
+      switch (optc)
+        {
+        case_GETOPT_HELP_CHAR;
+        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+        default:
+          usage (EXIT_FAILURE);
+        }
+    }
+
+  if (optind == argc)
+    {
+      /* No arguments.  Divulge the details of the current process. */
+      ruid = getuid ();
+      egid = getegid ();
+      rgid = getgid ();
+
+      if (!print_group_list (NULL, ruid, rgid, egid, true))
+        ok = false;
+      if (EOF == putchar ('\n'))
+        {
+          write_error ();
+          ok = false;
+        }
+    }
+  else
+    {
+      /* At least one argument.  Divulge the details of the specified users. */
+      while (optind < argc)
+        {
+          struct passwd *pwd = getpwnam (argv[optind]);
+          if (pwd == NULL)
+            error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]);
+          ruid = pwd->pw_uid;
+          rgid = egid = pwd->pw_gid;
+
+          if (printf ("%s : ", argv[optind]) < 0)
+            {
+              write_error ();
+              ok = false;
+            }
+          if (!print_group_list (argv[optind++], ruid, rgid, egid, true))
+            ok = false;
+          if (EOF == putchar ('\n'))
+            {
+              write_error ();
+              ok = false;
+            }
+        }
+    }
+
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/groups.sh b/src/groups.sh
deleted file mode 100755
index 7afe52e..0000000
--- a/src/groups.sh
+++ /dev/null
@@ -1,84 +0,0 @@
-#!/bin/sh
-# groups -- print the groups a user is in
-# Copyright (C) 1991, 1997, 2000, 2002, 2004-2007 Free Software Foundation, 
Inc.
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-# Written by David MacKenzie <address@hidden>.
-
-# Make sure we get GNU id, if possible; also allow
-# it to be somewhere else in PATH if not installed yet.
address@hidden@:$PATH
-
-usage="Usage: $0 [OPTION]... [USERNAME]...
-
-  --help      display this help and exit
-  --version   output version information and exit
-
-Same as id -Gn.  If no USERNAME, use current process.
-
-Report bugs to <@PACKAGE_BUGREPORT@>."
-
-version='groups (@PACKAGE_NAME@) @VERSION@
-Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
-License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
-This is free software: you are free to change and redistribute it.
-There is NO WARRANTY, to the extent permitted by law.
-
-Written by David MacKenzie.'
-
-
-for arg
-do
-  case $arg in
-    --help | --hel | --he | --h)
-      exec echo "$usage" ;;
-    --version | --versio | --versi | --vers | --ver | --ve | --v)
-      exec echo "$version" ;;
-    --)
-      shift
-      break ;;
-    -*)
-      echo "$0: invalid option: $arg" >&2
-      exit 1 ;;
-    *)
-      break ;;
-  esac
-done
-
-# With fewer than two arguments, simply exec "id".
-case $# in
-  0|1) exec id -Gn -- "$@" ;;
-esac
-
-# With more, we need a loop, and be sure to exit nonzero upon failure.
-status=0
-write_error=0
-
-for name
-do
-  if groups=`id -Gn -- "$name"`; then
-    echo "$name : $groups" || {
-      status=$?
-      if test $write_error = 0; then
-       echo "$0: write error" >&2
-       write_error=1
-      fi
-    }
-  else
-    status=$?
-  fi
-done
-
-exit $status
diff --git a/src/id.c b/src/id.c
index caf71e8..e4eda40 100644
--- a/src/id.c
+++ b/src/id.c
@@ -1,5 +1,5 @@
 /* id -- print real and effective UIDs and GIDs
-   Copyright (C) 1989-2007 Free Software Foundation, Inc.
+   Copyright (C) 1989-2008 Free Software Foundation, Inc.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -19,7 +19,6 @@

 #include <config.h>
 #include <stdio.h>
-#include <getopt.h>
 #include <sys/types.h>
 #include <pwd.h>
 #include <grp.h>
@@ -28,9 +27,9 @@

 #include "system.h"
 #include "error.h"
-#include "getugroups.h"
 #include "mgetgroups.h"
 #include "quote.h"
+#include "group-list.h"

 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "id"
@@ -41,8 +40,6 @@
 static int just_context = 0;

 static void print_user (uid_t uid);
-static void print_group (gid_t gid);
-static void print_group_list (const char *username);
 static void print_full_info (const char *username);

 /* The name this program was run with. */
@@ -216,15 +213,27 @@ of a different user"));
     }

   if (just_user)
-    print_user (use_real ? ruid : euid);
+    {
+      print_user (use_real ? ruid : euid);
+    }
   else if (just_group)
-    print_group (use_real ? rgid : egid);
+    {
+      if (!print_group (use_real ? rgid : egid, use_name))
+       ok = false;
+    }
   else if (just_group_list)
-    print_group_list (argv[optind]);
+    {
+      if (!print_group_list (argv[optind], ruid, rgid, egid, use_name))
+       ok = false;
+    }
   else if (just_context)
-    fputs (context, stdout);
+    {
+      fputs (context, stdout);
+    }
   else
-    print_full_info (argv[optind]);
+    {
+      print_full_info (argv[optind]);
+    }
   putchar ('\n');

   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
@@ -254,74 +263,6 @@ print_user (uid_t uid)
     printf ("%s", pwd->pw_name);
 }

-/* Print the name or value of group ID GID. */
-
-static void
-print_group (gid_t gid)
-{
-  struct group *grp = NULL;
-
-  if (use_name)
-    {
-      grp = getgrgid (gid);
-      if (grp == NULL)
-       {
-         error (0, 0, _("cannot find name for group ID %lu"),
-                (unsigned long int) gid);
-         ok = false;
-       }
-    }
-
-  if (grp == NULL)
-    printf ("%lu", (unsigned long int) gid);
-  else
-    printf ("%s", grp->gr_name);
-}
-
-/* Print all of the distinct groups the user is in. */
-
-static void
-print_group_list (const char *username)
-{
-  struct passwd *pwd;
-
-  pwd = getpwuid (ruid);
-  if (pwd == NULL)
-    ok = false;
-
-  print_group (rgid);
-  if (egid != rgid)
-    {
-      putchar (' ');
-      print_group (egid);
-    }
-
-#if HAVE_GETGROUPS
-  {
-    GETGROUPS_T *groups;
-    size_t i;
-
-    int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
-                              &groups);
-    if (n_groups < 0)
-      {
-       error (0, errno, _("failed to get groups for user %s"),
-              quote (username));
-       ok = false;
-       return;
-      }
-
-    for (i = 0; i < n_groups; i++)
-      if (groups[i] != rgid && groups[i] != egid)
-       {
-         putchar (' ');
-         print_group (groups[i]);
-       }
-    free (groups);
-  }
-#endif /* HAVE_GETGROUPS */
-}
-
 /* Print all of the info about the user's user and group IDs. */

 static void
@@ -365,8 +306,15 @@ print_full_info (const char *username)
                               &groups);
     if (n_groups < 0)
       {
-       error (0, errno, _("failed to get groups for user %s"),
-              quote (username));
+       if (username)
+         {
+           error (0, errno, _("failed to get groups for user %s"),
+                  quote (username));
+         }
+       else
+         {
+           error (0, errno, _("failed to get groups for the current process"));
+         }
        ok = false;
        return;
       }
--
1.5.4.1.98.gf3293


>From cc8b73b1d9cb63c89a76db8ece8a699fb98fff1a Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 28 Jan 2008 00:08:32 +0100
Subject: [PATCH] groups: don't test printf and putchar calls for failure

They are unnecessary, since we use gnulib's closeout module.
* src/group-list.c (print_group): Remove explicit error tests.
* src/groups.c (main): Likewise.
(write_error): Remove function.

Signed-off-by: Jim Meyering <address@hidden>
---
 src/group-list.c |   16 ++--------------
 src/groups.c     |   25 +++----------------------
 2 files changed, 5 insertions(+), 36 deletions(-)

diff --git a/src/group-list.c b/src/group-list.c
index e788f8e..ace4908 100644
--- a/src/group-list.c
+++ b/src/group-list.c
@@ -109,21 +109,9 @@ print_group (gid_t gid, bool use_name)
     }

   if (grp == NULL)
-    {
-      if (printf ("%lu", (unsigned long int) gid) < 0)
-        {
-          error (0, errno, _("write error"));
-          ok = false;
-        }
-    }
+    printf ("%lu", (unsigned long int) gid);
   else
-    {
-      if (printf ("%s", grp->gr_name) < 0)
-        {
-          error (0, errno, _("write error"));
-          ok = false;
-        }
-    }
+    printf ("%s", grp->gr_name);
   return ok;
 }

diff --git a/src/groups.c b/src/groups.c
index 8a4673c..baac7b9 100644
--- a/src/groups.c
+++ b/src/groups.c
@@ -65,13 +65,6 @@ the current process (which is different if the groups 
database has changed).\n")
   exit (status);
 }

-static void
-write_error (void)
-{
-  error (0, errno, _("write error"));
-}
-
-
 int
 main (int argc, char **argv)
 {
@@ -111,11 +104,7 @@ main (int argc, char **argv)

       if (!print_group_list (NULL, ruid, rgid, egid, true))
         ok = false;
-      if (EOF == putchar ('\n'))
-        {
-          write_error ();
-          ok = false;
-        }
+      putchar ('\n');
     }
   else
     {
@@ -128,18 +117,10 @@ main (int argc, char **argv)
           ruid = pwd->pw_uid;
           rgid = egid = pwd->pw_gid;

-          if (printf ("%s : ", argv[optind]) < 0)
-            {
-              write_error ();
-              ok = false;
-            }
+          printf ("%s : ", argv[optind]);
           if (!print_group_list (argv[optind++], ruid, rgid, egid, true))
             ok = false;
-          if (EOF == putchar ('\n'))
-            {
-              write_error ();
-              ok = false;
-            }
+          putchar ('\n');
         }
     }

--
1.5.4.1.98.gf3293




reply via email to

[Prev in Thread] Current Thread [Next in Thread]