#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94";
#else
__RCSID("$NetBSD: getcap.c,v 1.58 2023/09/21 13:46:12 shm Exp $");
#endif
#endif
#ifndef LIBHACK
#include "namespace.h"
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <assert.h>
#include <stddef.h>
#include <ctype.h>
#ifndef SMALL
#include <db.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(__weak_alias) && !defined(LIBHACK)
__weak_alias(cgetcap,_cgetcap)
__weak_alias(cgetclose,_cgetclose)
__weak_alias(cgetent,_cgetent)
__weak_alias(cgetfirst,_cgetfirst)
__weak_alias(cgetmatch,_cgetmatch)
__weak_alias(cgetnext,_cgetnext)
__weak_alias(cgetnum,_cgetnum)
__weak_alias(cgetset,_cgetset)
__weak_alias(cgetstr,_cgetstr)
__weak_alias(cgetustr,_cgetustr)
__weak_alias(csetexpandtc,_csetexpandtc)
#endif
#define BFRAG 1024
#define BSIZE 1024
#define ESC ('[' & 037)
#define MAX_RECURSION 32
#define SFRAG 100
#define RECOK (char)0
#define TCERR (char)1
#define SHADOW (char)2
static size_t topreclen;
static char *toprec;
static int gottoprec;
static int expandtc = 1;
#ifndef SMALL
static int cdbget(DB *, char **, const char *);
#endif
static int getent(char **, size_t *, const char * const *, int,
const char *, int, char *);
static int nfcmp(char *, char *);
int
cgetset(const char *ent)
{
const char *source, *check;
char *dest;
if (ent == NULL) {
if (toprec != NULL)
free(toprec);
toprec = NULL;
topreclen = 0;
return 0;
}
topreclen = strlen(ent);
if ((toprec = malloc(topreclen + 1)) == NULL) {
errno = ENOMEM;
return -1;
}
gottoprec = 0;
source = ent;
dest = toprec;
while (*source != '\0') {
*dest++ = *source++;
while (*source == ':') {
check = source + 1;
while (*check && (isspace((unsigned char)*check) ||
(*check=='\\' && isspace((unsigned char)check[1]))))
++check;
if (*check == ':')
source = check;
else
break;
}
}
*dest = 0;
return 0;
}
char *
cgetcap(char *buf, const char *cap, int type)
{
char *bp;
const char *cp;
_DIAGASSERT(buf != NULL);
_DIAGASSERT(cap != NULL);
bp = buf;
for (;;) {
for (;;)
if (*bp == '\0')
return NULL;
else if (*bp++ == ':')
break;
for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
continue;
if (*cp != '\0')
continue;
if (*bp == '@')
return NULL;
if (type == ':') {
if (*bp != '\0' && *bp != ':')
continue;
return bp;
}
if (*bp != type)
continue;
bp++;
return *bp == '@' ? NULL : bp;
}
}
int
cgetent(char **buf, const char * const *db_array, const char *name)
{
size_t dummy;
_DIAGASSERT(buf != NULL);
_DIAGASSERT(db_array != NULL);
_DIAGASSERT(name != NULL);
return getent(buf, &dummy, db_array, -1, name, 0, NULL);
}
void
csetexpandtc(int etc)
{
expandtc = etc;
}
static int
getent(char **cap, size_t *len, const char * const *db_array, int fd,
const char *name, int depth, char *nfield)
{
char *record, *newrecord;
char *r_end, *rp;
const char * const *db_p;
int myfd, eof, foundit;
int tc_not_resolved;
_DIAGASSERT(cap != NULL);
_DIAGASSERT(len != NULL);
_DIAGASSERT(db_array != NULL);
_DIAGASSERT(name != NULL);
myfd = 0;
rp = NULL;
if (depth > MAX_RECURSION)
return -3;
if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) {
if ((record = malloc(topreclen + BFRAG)) == NULL) {
errno = ENOMEM;
return -2;
}
(void)strcpy(record, toprec);
db_p = db_array;
rp = record + topreclen + 1;
r_end = rp + BFRAG;
goto tc_exp;
}
if ((record = malloc(BFRAG)) == NULL) {
errno = ENOMEM;
return -2;
}
r_end = record + BFRAG;
foundit = 0;
for (db_p = db_array; *db_p != NULL; db_p++) {
eof = 0;
if (fd >= 0) {
(void)lseek(fd, (off_t)0, SEEK_SET);
} else {
#ifndef SMALL
DB *capdbp;
char pbuf[MAXPATHLEN];
char *cbuf;
int retval;
size_t clen;
(void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p);
if ((capdbp = dbopen(pbuf, O_RDONLY | O_CLOEXEC, 0,
DB_HASH, 0)) != NULL) {
free(record);
retval = cdbget(capdbp, &record, name);
if (retval < 0) {
(void)capdbp->close(capdbp);
return retval;
}
clen = strlen(record);
if ((cbuf = malloc(clen + 1)) == NULL) {
(void)capdbp->close(capdbp);
errno = ENOMEM;
return -2;
}
memmove(cbuf, record, clen + 1);
if (capdbp->close(capdbp) < 0) {
int serrno = errno;
free(cbuf);
errno = serrno;
return -2;
}
*len = clen;
*cap = cbuf;
return retval;
} else
#endif
{
fd = open(*db_p, O_RDONLY | O_CLOEXEC, 0);
if (fd < 0) {
continue;
}
myfd = 1;
}
}
{
char buf[BUFSIZ];
char *b_end, *bp, *cp;
int c, slash;
b_end = buf;
bp = buf;
cp = NULL;
slash = 0;
for (;;) {
rp = record;
for (;;) {
if (bp >= b_end) {
ssize_t n;
n = read(fd, buf, sizeof(buf));
if (n <= 0) {
if (myfd)
(void)close(fd);
if (n < 0) {
int serrno = errno;
free(record);
errno = serrno;
return -2;
} else {
fd = -1;
eof = 1;
break;
}
}
b_end = buf+n;
bp = buf;
}
c = *bp++;
if (c == '\n') {
if (slash) {
slash = 0;
rp--;
continue;
} else
break;
}
if (slash) {
slash = 0;
cp = 0;
}
if (c == ':') {
if (cp != NULL)
rp = cp;
else
cp = rp;
} else if (c == '\\') {
slash = 1;
} else if (c != ' ' && c != '\t') {
cp = 0;
}
*rp++ = c;
if (rp >= r_end) {
ptrdiff_t pos;
size_t newsize;
pos = rp - record;
newsize = r_end - record + BFRAG;
newrecord = realloc(record, newsize);
if (newrecord == NULL) {
free(record);
if (myfd)
(void)close(fd);
errno = ENOMEM;
return -2;
}
record = newrecord;
r_end = record + newsize;
rp = record + pos;
}
}
if (cp)
rp = cp + 1;
*rp++ = '\0';
if (eof)
break;
if (*record == '\0' || *record == '#')
continue;
if (cgetmatch(record, name) == 0)
if (nfield == NULL || !nfcmp(nfield, record)) {
foundit = 1;
break;
}
}
}
if (foundit)
break;
}
if (!foundit) {
free(record);
return -1;
}
tc_exp:
tc_not_resolved = 0;
if (expandtc) {
char *newicap, *s;
size_t ilen, newilen;
int iret;
ptrdiff_t diff, tclen;
char *icap, *scan, *tc, *tcstart, *tcend;
scan = record;
for (;;) {
if ((tc = cgetcap(scan, "tc", '=')) == NULL)
break;
s = tc;
for (;;)
if (*s == '\0')
break;
else
if (*s++ == ':') {
*(s - 1) = '\0';
break;
}
tcstart = tc - 3;
tclen = s - tcstart;
tcend = s;
iret = getent(&icap, &ilen, db_p, fd, tc, depth+1,
NULL);
newicap = icap;
newilen = ilen;
if (iret != 0) {
if (iret < -1) {
if (myfd)
(void)close(fd);
free(record);
return iret;
}
if (iret == 1)
tc_not_resolved = 1;
if (iret == -1) {
*(s - 1) = ':';
scan = s - 1;
tc_not_resolved = 1;
continue;
}
}
s = newicap;
for (;;)
if (*s == '\0')
break;
else if (*s++ == ':')
break;
newilen -= s - newicap;
newicap = s;
s += newilen;
if (*(s - 1) != ':') {
*s = ':';
newilen++;
}
diff = newilen - tclen;
if (diff >= r_end - rp) {
ptrdiff_t pos, tcpos, tcposend;
size_t newsize;
pos = rp - record;
newsize = r_end - record + diff + BFRAG;
tcpos = tcstart - record;
tcposend = tcend - record;
newrecord = realloc(record, newsize);
if (newrecord == NULL) {
free(record);
if (myfd)
(void)close(fd);
free(icap);
errno = ENOMEM;
return -2;
}
record = newrecord;
r_end = record + newsize;
rp = record + pos;
tcstart = record + tcpos;
tcend = record + tcposend;
}
s = tcstart + newilen;
memmove(s, tcend, (size_t)(rp - tcend));
memmove(tcstart, newicap, newilen);
rp += diff;
free(icap);
scan = s - 1;
}
}
if (myfd)
(void)close(fd);
*len = rp - record - 1;
if (r_end > rp) {
if ((newrecord =
realloc(record, (size_t)(rp - record))) == NULL) {
free(record);
errno = ENOMEM;
return -2;
}
record = newrecord;
}
*cap = record;
if (tc_not_resolved)
return 1;
return 0;
}
#ifndef SMALL
static int
cdbget(DB *capdbp, char **bp, const char *name)
{
DBT key;
DBT data;
_DIAGASSERT(capdbp != NULL);
_DIAGASSERT(bp != NULL);
_DIAGASSERT(name != NULL);
key.data = __UNCONST(name);
key.size = strlen(name);
for (;;) {
switch(capdbp->get(capdbp, &key, &data, 0)) {
case -1:
return -2;
case 1:
return -1;
}
if (((char *)data.data)[0] != SHADOW)
break;
key.data = (char *)data.data + 1;
key.size = data.size - 1;
}
*bp = (char *)data.data + 1;
return ((char *)(data.data))[0] == TCERR ? 1 : 0;
}
#endif
int
cgetmatch(const char *buf, const char *name)
{
const char *np, *bp;
_DIAGASSERT(buf != NULL);
_DIAGASSERT(name != NULL);
bp = buf;
for (;;) {
np = name;
for (;;)
if (*np == '\0') {
if (*bp == '|' || *bp == ':' || *bp == '\0')
return 0;
else
break;
} else if (*bp++ != *np++)
break;
if (bp > buf)
bp--;
else
return -1;
for (;;)
if (*bp == '\0' || *bp == ':')
return -1;
else if (*bp++ == '|')
break;
}
}
int
cgetfirst(char **buf, const char * const *db_array)
{
_DIAGASSERT(buf != NULL);
_DIAGASSERT(db_array != NULL);
(void)cgetclose();
return cgetnext(buf, db_array);
}
static FILE *pfp;
static int slash;
static const char * const *dbp;
int
cgetclose(void)
{
if (pfp != NULL) {
(void)fclose(pfp);
pfp = NULL;
}
dbp = NULL;
gottoprec = 0;
slash = 0;
return 0;
}
int
cgetnext(char **bp, const char * const *db_array)
{
size_t len = 0;
int status, done;
char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
size_t dummy;
_DIAGASSERT(bp != NULL);
_DIAGASSERT(db_array != NULL);
if (dbp == NULL)
dbp = db_array;
if (pfp == NULL && (pfp = fopen(*dbp, "re")) == NULL) {
(void)cgetclose();
return -1;
}
for (;;) {
if (toprec != NULL && !gottoprec) {
gottoprec = 1;
line = toprec;
} else {
line = fgetln(pfp, &len);
if (line == NULL) {
if (pfp == NULL)
return -1;
if (ferror(pfp)) {
(void)cgetclose();
return -1;
} else {
(void)fclose(pfp);
pfp = NULL;
if (*++dbp == NULL) {
(void)cgetclose();
return 0;
} else if ((pfp =
fopen(*dbp, "re")) == NULL) {
(void)cgetclose();
return -1;
} else
continue;
}
} else
line[len - 1] = '\0';
if (len == 1) {
slash = 0;
continue;
}
if (isspace((unsigned char)*line) ||
*line == ':' || *line == '#' || slash) {
if (line[len - 2] == '\\')
slash = 1;
else
slash = 0;
continue;
}
if (line[len - 2] == '\\')
slash = 1;
else
slash = 0;
}
if (len > sizeof(nbuf))
return -1;
done = 0;
np = nbuf;
for (;;) {
for (cp = line; *cp != '\0'; cp++) {
if (*cp == ':') {
*np++ = ':';
done = 1;
break;
}
if (*cp == '\\')
break;
*np++ = *cp;
}
if (done) {
*np = '\0';
break;
} else {
line = fgetln(pfp, &len);
if (line == NULL && pfp) {
if (ferror(pfp)) {
(void)cgetclose();
return -1;
}
(void)fclose(pfp);
pfp = NULL;
*np = '\0';
break;
} else
line[len - 1] = '\0';
}
}
if (len > sizeof(buf))
return -1;
rp = buf;
for (cp = nbuf; *cp != '\0'; cp++)
if (*cp == '|' || *cp == ':')
break;
else
*rp++ = *cp;
*rp = '\0';
status = getent(bp, &dummy, db_array, -1, buf, 0, NULL);
if (status == -2 || status == -3)
(void)cgetclose();
return status + 1;
}
}
int
cgetstr(char *buf, const char *cap, char **str)
{
u_int m_room;
const char *bp;
char *mp;
ptrdiff_t len;
char *mem, *newmem;
_DIAGASSERT(buf != NULL);
_DIAGASSERT(cap != NULL);
_DIAGASSERT(str != NULL);
bp = cgetcap(buf, cap, '=');
if (bp == NULL)
return -1;
if ((mem = malloc(SFRAG)) == NULL) {
errno = ENOMEM;
return -2;
}
m_room = SFRAG;
mp = mem;
while (*bp != ':' && *bp != '\0') {
if (*bp == '^') {
bp++;
if (*bp == ':' || *bp == '\0')
break;
*mp++ = *bp++ & 037;
} else if (*bp == '\\') {
bp++;
if (*bp == ':' || *bp == '\0')
break;
if ('0' <= *bp && *bp <= '7') {
int n, i;
n = 0;
i = 3;
do {
n = n * 8 + (*bp++ - '0');
} while (--i && '0' <= *bp && *bp <= '7');
*mp++ = n;
}
else switch (*bp++) {
case 'b': case 'B':
*mp++ = '\b';
break;
case 't': case 'T':
*mp++ = '\t';
break;
case 'n': case 'N':
*mp++ = '\n';
break;
case 'f': case 'F':
*mp++ = '\f';
break;
case 'r': case 'R':
*mp++ = '\r';
break;
case 'e': case 'E':
*mp++ = ESC;
break;
case 'c': case 'C':
*mp++ = ':';
break;
default:
*mp++ = *(bp-1);
break;
}
} else
*mp++ = *bp++;
m_room--;
if (m_room == 0) {
size_t size = mp - mem;
if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
free(mem);
return -2;
}
mem = newmem;
m_room = SFRAG;
mp = mem + size;
}
}
*mp++ = '\0';
m_room--;
len = mp - mem - 1;
if (m_room != 0) {
if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
free(mem);
return -2;
}
mem = newmem;
}
*str = mem;
_DIAGASSERT(__type_fit(int, len));
return (int)len;
}
int
cgetustr(char *buf, const char *cap, char **str)
{
u_int m_room;
const char *bp;
char *mp;
size_t len;
char *mem, *newmem;
_DIAGASSERT(buf != NULL);
_DIAGASSERT(cap != NULL);
_DIAGASSERT(str != NULL);
if ((bp = cgetcap(buf, cap, '=')) == NULL)
return -1;
if ((mem = malloc(SFRAG)) == NULL) {
errno = ENOMEM;
return -2;
}
m_room = SFRAG;
mp = mem;
while (*bp != ':' && *bp != '\0') {
*mp++ = *bp++;
m_room--;
if (m_room == 0) {
size_t size = mp - mem;
if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
free(mem);
return -2;
}
mem = newmem;
m_room = SFRAG;
mp = mem + size;
}
}
*mp++ = '\0';
m_room--;
len = mp - mem - 1;
if (m_room != 0) {
if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
free(mem);
return -2;
}
mem = newmem;
}
*str = mem;
_DIAGASSERT(__type_fit(int, len));
return (int)len;
}
int
cgetnum(char *buf, const char *cap, long *num)
{
long n;
int base, digit;
const char *bp;
_DIAGASSERT(buf != NULL);
_DIAGASSERT(cap != NULL);
_DIAGASSERT(num != NULL);
bp = cgetcap(buf, cap, '#');
if (bp == NULL)
return -1;
if (*bp == '0') {
bp++;
if (*bp == 'x' || *bp == 'X') {
bp++;
base = 16;
} else
base = 8;
} else
base = 10;
n = 0;
for (;;) {
if ('0' <= *bp && *bp <= '9')
digit = *bp - '0';
else if ('a' <= *bp && *bp <= 'f')
digit = 10 + *bp - 'a';
else if ('A' <= *bp && *bp <= 'F')
digit = 10 + *bp - 'A';
else
break;
if (digit >= base)
break;
n = n * base + digit;
bp++;
}
*num = n;
return 0;
}
static int
nfcmp(char *nf, char *rec)
{
char *cp, tmp;
int ret;
_DIAGASSERT(nf != NULL);
_DIAGASSERT(rec != NULL);
for (cp = rec; *cp != ':'; cp++)
continue;
tmp = *(cp + 1);
*(cp + 1) = '\0';
ret = strcmp(nf, rec);
*(cp + 1) = tmp;
return ret;
}