#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: getprotoent_r.c,v 1.3.206.2 2006/08/01 01:19:28 marka Exp $";
#endif
#include <port_before.h>
#if !defined(_REENTRANT) || !defined(DO_PTHREADS)
static int getprotoent_r_not_required = 0;
#else
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <port_after.h>
#ifdef PROTO_R_RETURN
static PROTO_R_RETURN
copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS);
PROTO_R_RETURN
getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) {
struct protoent *pe = getprotobyname(name);
#ifdef PROTO_R_SETANSWER
int n = 0;
if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
*answerp = NULL;
else
*answerp = pptr;
return (n);
#else
if (pe == NULL)
return (PROTO_R_BAD);
return (copy_protoent(pe, pptr, PROTO_R_COPY));
#endif
}
PROTO_R_RETURN
getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) {
struct protoent *pe = getprotobynumber(proto);
#ifdef PROTO_R_SETANSWER
int n = 0;
if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
*answerp = NULL;
else
*answerp = pptr;
return (n);
#else
if (pe == NULL)
return (PROTO_R_BAD);
return (copy_protoent(pe, pptr, PROTO_R_COPY));
#endif
}
PROTO_R_RETURN
getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) {
struct protoent *pe = getprotoent();
#ifdef PROTO_R_SETANSWER
int n = 0;
if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
*answerp = NULL;
else
*answerp = pptr;
return (n);
#else
if (pe == NULL)
return (PROTO_R_BAD);
return (copy_protoent(pe, pptr, PROTO_R_COPY));
#endif
}
PROTO_R_SET_RETURN
#ifdef PROTO_R_ENT_ARGS
setprotoent_r(int stay_open, PROTO_R_ENT_ARGS)
#else
setprotoent_r(int stay_open)
#endif
{
#ifdef PROTO_R_ENT_UNUSED
PROTO_R_ENT_UNUSED;
#endif
setprotoent(stay_open);
#ifdef PROTO_R_SET_RESULT
return (PROTO_R_SET_RESULT);
#endif
}
PROTO_R_END_RETURN
#ifdef PROTO_R_ENT_ARGS
endprotoent_r(PROTO_R_ENT_ARGS)
#else
endprotoent_r()
#endif
{
#ifdef PROTO_R_ENT_UNUSED
PROTO_R_ENT_UNUSED;
#endif
endprotoent();
PROTO_R_END_RESULT(PROTO_R_OK);
}
#ifndef PROTOENT_DATA
static PROTO_R_RETURN
copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
char *cp;
int i, n;
int numptr, len;
numptr = 1;
len = (char *)ALIGN(buf) - buf;
for (i = 0; pe->p_aliases[i]; i++, numptr++) {
len += strlen(pe->p_aliases[i]) + 1;
}
len += strlen(pe->p_name) + 1;
len += numptr * sizeof(char*);
if (len > (int)buflen) {
errno = ERANGE;
return (PROTO_R_BAD);
}
pptr->p_proto = pe->p_proto;
cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
n = strlen(pe->p_name) + 1;
strcpy(cp, pe->p_name);
pptr->p_name = cp;
cp += n;
pptr->p_aliases = (char **)ALIGN(buf);
for (i = 0 ; pe->p_aliases[i]; i++) {
n = strlen(pe->p_aliases[i]) + 1;
strcpy(cp, pe->p_aliases[i]);
pptr->p_aliases[i] = cp;
cp += n;
}
pptr->p_aliases[i] = NULL;
return (PROTO_R_OK);
}
#else
static int
copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
char *cp, *eob;
int i, n;
pptr->p_proto = pe->p_proto;
cp = pdptr->line;
eob = pdptr->line + sizeof(pdptr->line);
if ((n = strlen(pe->p_name) + 1) < (eob - cp)) {
strcpy(cp, pe->p_name);
pptr->p_name = cp;
cp += n;
} else {
return (-1);
}
i = 0;
pptr->p_aliases = pdptr->proto_aliases;
while (pe->p_aliases[i] && i < (_MAXALIASES-1)) {
if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) {
strcpy(cp, pe->p_aliases[i]);
pptr->p_aliases[i] = cp;
cp += n;
} else {
break;
}
i++;
}
pptr->p_aliases[i] = NULL;
return (PROTO_R_OK);
}
#endif
#else
static int getprotoent_r_unknown_system = 0;
#endif
#endif