1 /* groups -- print the groups a user is in
2 Copyright (C) 1989-2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by James Youngman based on id.c and groups.sh,
18 which were written by Arnold Robbins and David MacKenzie. */
23 #include <sys/types.h>
30 #include "group-list.h"
32 /* The name this program was run with. */
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "groups"
38 #define AUTHORS "David MacKenzie", "James Youngman"
41 static struct option const longopts[] =
43 {GETOPT_HELP_OPTION_DECL},
44 {GETOPT_VERSION_OPTION_DECL},
51 if (status != EXIT_SUCCESS)
52 fprintf (stderr, _("Try `%s --help' for more information.\n"),
56 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
58 Print information for USERNAME or, if no USERNAME is specified,\n\
59 the current process (which is different if the groups database has changed).\n"),
61 fputs (HELP_OPTION_DESCRIPTION, stdout);
62 fputs (VERSION_OPTION_DESCRIPTION, stdout);
63 emit_bug_reporting_address ();
71 error (0, errno, _("write error"));
76 main (int argc, char **argv)
83 initialize_main (&argc, &argv);
84 program_name = argv[0];
85 setlocale (LC_ALL, "");
86 bindtextdomain (PACKAGE, LOCALEDIR);
89 atexit (close_stdout);
91 /* Processing the arguments this way makes groups.c behave differently to
92 * groups.sh if one of the arguments is "--".
94 while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1)
98 case_GETOPT_HELP_CHAR;
99 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
101 usage (EXIT_FAILURE);
107 /* No arguments. Divulge the details of the current process. */
112 if (!print_group_list (NULL, ruid, rgid, egid, true))
114 if (EOF == putchar ('\n'))
122 /* At least one argument. Divulge the details of the specified users. */
123 while (optind < argc)
125 struct passwd *pwd = getpwnam (argv[optind]);
127 error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]);
129 rgid = egid = pwd->pw_gid;
131 if (printf ("%s : ", argv[optind]) < 0)
136 if (!print_group_list (argv[optind++], ruid, rgid, egid, true))
138 if (EOF == putchar ('\n'))
146 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
151 * indent-tabs-mode: nil