1 /* groups -- print the groups a user is in
2 Copyright (C) 1989-2016 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. */
22 #include <sys/types.h>
29 #include "group-list.h"
32 /* The official name of this program (e.g., no 'g' prefix). */
33 #define PROGRAM_NAME "groups"
36 proper_name ("David MacKenzie"), \
37 proper_name ("James Youngman")
40 static struct option
const longopts
[] =
42 {GETOPT_HELP_OPTION_DECL
},
43 {GETOPT_VERSION_OPTION_DECL
},
50 if (status
!= EXIT_SUCCESS
)
54 printf (_("Usage: %s [OPTION]... [USERNAME]...\n"), program_name
);
56 Print group memberships for each USERNAME or, if no USERNAME is specified, for\
58 the current process (which may differ if the groups database has changed).\n"),
60 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
61 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
62 emit_ancillary_info (PROGRAM_NAME
);
68 main (int argc
, char **argv
)
75 initialize_main (&argc
, &argv
);
76 set_program_name (argv
[0]);
77 setlocale (LC_ALL
, "");
78 bindtextdomain (PACKAGE
, LOCALEDIR
);
81 atexit (close_stdout
);
83 /* Processing the arguments this way makes groups.c behave differently to
84 * groups.sh if one of the arguments is "--".
86 while ((optc
= getopt_long (argc
, argv
, "", longopts
, NULL
)) != -1)
90 case_GETOPT_HELP_CHAR
;
91 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
99 /* No arguments. Divulge the details of the current process. */
105 if (ruid
== NO_UID
&& errno
)
106 error (EXIT_FAILURE
, errno
, _("cannot get real UID"));
110 if (egid
== NO_GID
&& errno
)
111 error (EXIT_FAILURE
, errno
, _("cannot get effective GID"));
115 if (rgid
== NO_GID
&& errno
)
116 error (EXIT_FAILURE
, errno
, _("cannot get real GID"));
118 if (!print_group_list (NULL
, ruid
, rgid
, egid
, true, ' '))
124 /* At least one argument. Divulge the details of the specified users. */
125 while (optind
< argc
)
127 struct passwd
*pwd
= getpwnam (argv
[optind
]);
129 error (EXIT_FAILURE
, 0, _("%s: no such user"),
130 quote (argv
[optind
]));
132 rgid
= egid
= pwd
->pw_gid
;
134 printf ("%s : ", argv
[optind
]);
135 if (!print_group_list (argv
[optind
++], ruid
, rgid
, egid
, true, ' '))
141 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;