xref: /original-bsd/lib/libc/compat-43/setregid.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)setregid.c	8.1 (Berkeley) 06/02/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <errno.h>
14 
15 int
16 setregid(rgid, egid)
17 	gid_t rgid, egid;
18 {
19 	static gid_t savedgid = -1;
20 
21 	if (savedgid == -1)
22 		savedgid = getegid();
23 	/*
24 	 * we assume that the intent here is to be able to
25 	 * get back rgid priviledge. So we make sure that
26 	 * we will be able to do so, but do not actually
27 	 * set the rgid.
28 	 */
29 	if (rgid != -1 && rgid != getgid() && rgid != savedgid) {
30 		errno = EPERM;
31 		return (-1);
32 	}
33 	if (egid != -1 && setegid(egid) < 0)
34 		return (-1);
35 	return (0);
36 }
37