Commit | Line | Data |
---|---|---|
bb0cfa24 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
2ce81398 | 7 | #if defined(LIBC_SCCS) && !defined(lint) |
b3b3b313 | 8 | static char sccsid[] = "@(#)initgroups.c 5.3 (Berkeley) %G%"; |
2ce81398 | 9 | #endif LIBC_SCCS and not lint |
a4f42d0e SL |
10 | |
11 | /* | |
12 | * initgroups | |
13 | */ | |
14 | #include <stdio.h> | |
15 | #include <sys/param.h> | |
16 | #include <grp.h> | |
17 | ||
18 | struct group *getgrent(); | |
19 | ||
20 | initgroups(uname, agroup) | |
21 | char *uname; | |
22 | int agroup; | |
23 | { | |
24 | int groups[NGROUPS], ngroups = 0; | |
25 | register struct group *grp; | |
26 | register int i; | |
27 | ||
28 | if (agroup >= 0) | |
29 | groups[ngroups++] = agroup; | |
30 | setgrent(); | |
0fb3f429 SL |
31 | while (grp = getgrent()) { |
32 | if (grp->gr_gid == agroup) | |
33 | continue; | |
a4f42d0e SL |
34 | for (i = 0; grp->gr_mem[i]; i++) |
35 | if (!strcmp(grp->gr_mem[i], uname)) { | |
a4f42d0e | 36 | if (ngroups == NGROUPS) { |
0fb3f429 | 37 | fprintf(stderr, "initgroups: %s is in too many groups\n", uname); |
a4f42d0e SL |
38 | goto toomany; |
39 | } | |
7479b5d0 | 40 | groups[ngroups++] = grp->gr_gid; |
a4f42d0e | 41 | } |
0fb3f429 | 42 | } |
a4f42d0e | 43 | toomany: |
4ce247a6 | 44 | endgrent(); |
a4f42d0e | 45 | if (setgroups(ngroups, groups) < 0) { |
0fb3f429 | 46 | perror("setgroups"); |
b3b3b313 | 47 | return (-1); |
a4f42d0e SL |
48 | } |
49 | return (0); | |
50 | } |