2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
11 * Copyright (c) 1980 Regents of the University of California.
12 * All rights reserved. The Berkeley software License Agreement
13 * specifies the terms and conditions for redistribution.
20 #include <sys/types.h>
21 #include <sys/param.h>
30 static void showgroups(char *user
);
33 main(int argc
, char *argv
[])
39 int maxgrp
= sysconf(_SC_NGROUPS_MAX
);
41 groups
= alloca(maxgrp
* sizeof (gid_t
));
44 for (i
= 1; i
< argc
; i
++)
49 ngroups
= getgroups(maxgrp
, groups
);
50 if (getpwuid(getuid()) == NULL
) {
51 (void) fprintf(stderr
, "groups: could not find passwd entry\n");
55 for (i
= 0; i
< ngroups
; i
++) {
56 gr
= getgrgid(groups
[i
]);
58 (void) printf("%s%u", sep
, groups
[i
]);
62 (void) printf("%s%s", sep
, gr
->gr_name
);
71 showgroups(char *user
)
77 int pwgid_printed
= 0;
79 if ((pw
= getpwnam(user
)) == NULL
) {
80 (void) fprintf(stderr
, "groups: %s : No such user\n", user
);
84 (void) printf("%s : ", user
);
85 while (gr
= getgrent()) {
86 if (pw
->pw_gid
== gr
->gr_gid
) {
88 * To avoid duplicate group entries
90 if (pwgid_printed
== 0) {
91 (void) printf("%s%s", sep
, gr
->gr_name
);
97 for (cp
= gr
->gr_mem
; cp
&& *cp
; cp
++)
98 if (strcmp(*cp
, user
) == 0) {
99 (void) printf("%s%s", sep
, gr
->gr_name
);