#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
void
setprotoent_r(int f, struct protoent_data *pd)
{
if (pd->fp == NULL) {
int fd = __pledge_open(_PATH_PROTOCOLS, O_RDONLY|O_CLOEXEC);
if (fd != -1)
pd->fp = fdopen(fd, "r" );
if (pd->fp == NULL)
close(fd);
} else
rewind(pd->fp);
pd->stayopen |= f;
}
DEF_WEAK(setprotoent_r);
void
endprotoent_r(struct protoent_data *pd)
{
if (pd->fp) {
fclose(pd->fp);
pd->fp = NULL;
}
free(pd->aliases);
pd->aliases = NULL;
pd->maxaliases = 0;
free(pd->line);
pd->line = NULL;
pd->stayopen = 0;
}
DEF_WEAK(endprotoent_r);
int
getprotoent_r(struct protoent *pe, struct protoent_data *pd)
{
char *p, *cp, **q, *endp;
size_t len;
long l;
int serrno;
if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "re" )) == NULL)
return (-1);
again:
if ((p = fgetln(pd->fp, &len)) == NULL)
return (-1);
if (len == 0 || *p == '#' || *p == '\n')
goto again;
if (p[len-1] == '\n')
len--;
if ((cp = memchr(p, '#', len)) != NULL)
len = cp - p;
cp = realloc(pd->line, len + 1);
if (cp == NULL)
return (-1);
pd->line = pe->p_name = memcpy(cp, p, len);
cp[len] = '\0';
cp = strpbrk(cp, " \t");
if (cp == NULL)
goto again;
*cp++ = '\0';
while (*cp == ' ' || *cp == '\t')
cp++;
p = strpbrk(cp, " \t");
if (p != NULL)
*p++ = '\0';
l = strtol(cp, &endp, 10);
if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX)
goto again;
pe->p_proto = l;
if (pd->aliases == NULL) {
pd->maxaliases = 5;
pd->aliases = calloc(pd->maxaliases, sizeof(char *));
if (pd->aliases == NULL) {
serrno = errno;
endprotoent_r(pd);
errno = serrno;
return (-1);
}
}
q = pe->p_aliases = pd->aliases;
if (p != NULL) {
cp = p;
while (cp && *cp) {
if (*cp == ' ' || *cp == '\t') {
cp++;
continue;
}
if (q == &pe->p_aliases[pd->maxaliases - 1]) {
p = reallocarray(pe->p_aliases,
pd->maxaliases, 2 * sizeof(char *));
if (p == NULL) {
serrno = errno;
endprotoent_r(pd);
errno = serrno;
return (-1);
}
pd->maxaliases *= 2;
q = (char **)p + (q - pe->p_aliases);
pe->p_aliases = pd->aliases = (char **)p;
}
*q++ = cp;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
}
}
*q = NULL;
return (0);
}
DEF_WEAK(getprotoent_r);
struct protoent_data _protoent_data;
void
setprotoent(int f)
{
setprotoent_r(f, &_protoent_data);
}
void
endprotoent(void)
{
endprotoent_r(&_protoent_data);
}
struct protoent *
getprotoent(void)
{
static struct protoent proto;
if (getprotoent_r(&proto, &_protoent_data) != 0)
return (NULL);
return (&proto);
}