/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1998-1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
static int
copy_group(struct group *, struct group *, char *buf, int buflen);
/* POSIX 1003.1c */
#ifdef POSIX_GETGRNAM_R
int
__posix_getgrnam_r(const char *name, struct group *gptr,
char *buf, int buflen, struct group **result) {
#else
int
getgrnam_r(const char *name, struct group *gptr,
char *buf, size_t buflen, struct group **result) {
#endif
struct group *ge = getgrnam(name);
int res;
if (ge == NULL) {
*result = NULL;
return (0);
}
res = copy_group(ge, gptr, buf, buflen);
*result = res ? NULL : gptr;
return (res);
}
#ifdef POSIX_GETGRNAM_R
struct group *
getgrnam_r(const char *name, struct group *gptr,
char *buf, int buflen) {
struct group *ge = getgrnam(name);
int res;
/*%
* These assume a single context is in operation per thread.
* If this is not the case we will need to call irs directly
* rather than through the base functions.
*/
GROUP_R_RETURN
getgrent_r(struct group *gptr, GROUP_R_ARGS) {
struct group *ge = getgrent();
int res;
#if 0
/* XXX irs does not have a fgetgrent() */
GROUP_R_RETURN
fgetgrent_r(FILE *f, struct group *gptr, GROUP_R_ARGS) {
struct group *ge = fgetgrent(f);
int res;
static int
copy_group(struct group *ge, struct group *gptr, char *buf, int buflen) {
char *cp;
int i, n;
int numptr, len;
/* Find out the amount of space required to store the answer. */
numptr = 1; /*%< NULL ptr */
len = (char *)ALIGN(buf) - buf;
for (i = 0; ge->gr_mem[i]; i++, numptr++) {
len += strlen(ge->gr_mem[i]) + 1;
}
len += strlen(ge->gr_name) + 1;
len += strlen(ge->gr_passwd) + 1;
len += numptr * sizeof(char*);