Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/getcap.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libc/gen/getcap.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.19 retrieving revision 1.31 diff -u -p -r1.19 -r1.31 --- src/lib/libc/gen/getcap.c 1998/03/18 20:29:27 1.19 +++ src/lib/libc/gen/getcap.c 1999/09/20 04:38:59 1.31 @@ -1,4 +1,4 @@ -/* $NetBSD: getcap.c,v 1.19 1998/03/18 20:29:27 tv Exp $ */ +/* $NetBSD: getcap.c,v 1.31 1999/09/20 04:38:59 lukem Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -41,12 +41,14 @@ #if 0 static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; #else -__RCSID("$NetBSD: getcap.c,v 1.19 1998/03/18 20:29:27 tv Exp $"); +__RCSID("$NetBSD: getcap.c,v 1.31 1999/09/20 04:38:59 lukem Exp $"); #endif #endif /* LIBC_SCCS and not lint */ #include "namespace.h" #include + +#include #include #include #include @@ -84,8 +86,8 @@ static size_t topreclen; /* toprec leng static char *toprec; /* Additional record specified by cgetset() */ static int gottoprec; /* Flag indicating retrieval of toprecord */ -static int cdbget __P((DB *, char **, char *)); -static int getent __P((char **, size_t *, char **, int, char *, int, char *)); +static int cdbget __P((DB *, char **, const char *)); +static int getent __P((char **, size_t *, char **, int, const char *, int, char *)); static int nfcmp __P((char *, char *)); /* @@ -95,8 +97,11 @@ static int nfcmp __P((char *, char *)); */ int cgetset(ent) - char *ent; + const char *ent; { + const char *source, *check; + char *dest; + if (ent == NULL) { if (toprec) free(toprec); @@ -110,7 +115,25 @@ cgetset(ent) return (-1); } gottoprec = 0; - (void)strcpy(toprec, ent); /* XXX: strcpy is safe */ + + source=ent; + dest=toprec; + while (*source) { /* Strip whitespace */ + *dest++ = *source++; /* Do not check first field */ + 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); } @@ -128,10 +151,15 @@ cgetset(ent) */ char * cgetcap(buf, cap, type) - char *buf, *cap; + char *buf; + const char *cap; int type; { - char *bp, *cp; + char *bp; + const char *cp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); bp = buf; for (;;) { @@ -180,10 +208,15 @@ cgetcap(buf, cap, type) */ int cgetent(buf, db_array, name) - char **buf, **db_array, *name; + char **buf, **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)); } @@ -207,7 +240,8 @@ cgetent(buf, db_array, name) */ static int getent(cap, len, db_array, fd, name, depth, nfield) - char **cap, **db_array, *name, *nfield; + char **cap, **db_array, *nfield; + const char *name; size_t *len; int fd, depth; { @@ -219,6 +253,13 @@ getent(cap, len, db_array, fd, name, dep int tc_not_resolved; char pbuf[_POSIX_PATH_MAX]; + _DIAGASSERT(cap != NULL); + _DIAGASSERT(len != NULL); + _DIAGASSERT(db_array != NULL); + /* fd may be -1 */ + _DIAGASSERT(name != NULL); + /* nfield may be NULL */ + /* * Return with ``loop detected'' error if we've recursed more than * MAX_RECURSION times. @@ -276,9 +317,12 @@ getent(cap, len, db_array, fd, name, dep /* save the data; close frees it */ clen = strlen(record); cbuf = malloc(clen + 1); - memcpy(cbuf, record, clen + 1); + memmove(cbuf, record, clen + 1); if (capdbp->close(capdbp) < 0) { + int serrno = errno; + free(cbuf); + errno = serrno; return (-2); } *len = clen; @@ -298,8 +342,8 @@ getent(cap, len, db_array, fd, name, dep */ { char buf[BUFSIZ]; - char *b_end, *bp; - int c; + char *b_end, *bp, *cp; + int c, slash; /* * Loop invariants: @@ -308,9 +352,12 @@ getent(cap, len, db_array, fd, name, dep * Rp always points just past last character in record. * B_end always points just past last character in buf. * Bp always points at next character in buf. + * Cp remembers where the last colon was. */ b_end = buf; bp = buf; + cp = 0; + slash = 0; for (;;) { /* @@ -327,7 +374,10 @@ getent(cap, len, db_array, fd, name, dep if (myfd) (void)close(fd); if (n < 0) { + int serrno = errno; + free(record); + errno = serrno; return (-2); } else { fd = -1; @@ -341,12 +391,37 @@ getent(cap, len, db_array, fd, name, dep c = *bp++; if (c == '\n') { - if (rp > record && *(rp-1) == '\\') { + if (slash) { + slash = 0; rp--; continue; } else break; } + if (slash) { + slash = 0; + cp = 0; + } + if (c == ':') { + /* + * If the field was `empty' (i.e. + * contained only white space), back up + * to the colon (eliminating the + * field). + */ + if (cp) + rp = cp; + else + cp = rp; + } else if (c == '\\') { + slash = 1; + } else if (c != ' ' && c != '\t') { + /* + * Forget where the colon was, as this + * is not an empty field. + */ + cp = 0; + } *rp++ = c; /* @@ -362,16 +437,19 @@ getent(cap, len, db_array, fd, name, dep newsize = r_end - record + BFRAG; record = realloc(record, newsize); if (record == NULL) { - errno = ENOMEM; if (myfd) (void)close(fd); + errno = ENOMEM; return (-2); } r_end = record + newsize; rp = record + pos; } } - /* loop invariant let's us do this */ + /* Eliminate any white space after the last colon. */ + if (cp) + rp = cp + 1; + /* Loop invariant lets us do this. */ *rp++ = '\0'; /* @@ -501,10 +579,10 @@ tc_exp: { tcposend = tcend - record; record = realloc(record, newsize); if (record == NULL) { - errno = ENOMEM; if (myfd) (void)close(fd); free(icap); + errno = ENOMEM; return (-2); } r_end = record + newsize; @@ -517,8 +595,8 @@ tc_exp: { * Insert tc'ed record into our record. */ s = tcstart + newilen; - bcopy(tcend, s, (size_t)(rp - tcend)); - bcopy(newicap, tcstart, newilen); + memmove(s, tcend, (size_t)(rp - tcend)); + memmove(tcstart, newicap, newilen); rp += diff; free(icap); @@ -553,11 +631,18 @@ tc_exp: { static int cdbget(capdbp, bp, name) DB *capdbp; - char **bp, *name; + char **bp; + const char *name; { - DBT key, data; + DBT key; + DBT data; - key.data = name; + _DIAGASSERT(capdbp != NULL); + _DIAGASSERT(bp != NULL); + _DIAGASSERT(name != NULL); + + /* LINTED key is not modified */ + key.data = (char *)name; key.size = strlen(name); for (;;) { @@ -587,9 +672,12 @@ cdbget(capdbp, bp, name) */ int cgetmatch(buf, name) - char *buf, *name; + const char *buf, *name; { - char *np, *bp; + const char *np, *bp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(name != NULL); /* * Start search at beginning of record. @@ -601,12 +689,12 @@ cgetmatch(buf, name) */ np = name; for (;;) - if (*np == '\0') + if (*np == '\0') { if (*bp == '|' || *bp == ':' || *bp == '\0') return (0); else break; - else + } else if (*bp++ != *np++) break; @@ -627,6 +715,10 @@ int cgetfirst(buf, db_array) char **buf, **db_array; { + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + (void)cgetclose(); return (cgetnext(buf, db_array)); } @@ -663,6 +755,9 @@ cgetnext(bp, db_array) 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; @@ -699,7 +794,7 @@ cgetnext(bp, db_array) slash = 0; continue; } - if (isspace(*line) || + if (isspace((unsigned char)*line) || *line == ':' || *line == '#' || slash) { if (line[len - 2] == '\\') slash = 1; @@ -784,14 +879,20 @@ cgetnext(bp, db_array) */ int cgetstr(buf, cap, str) - char *buf, *cap; + char *buf; + const char *cap; char **str; { u_int m_room; - char *bp, *mp; + const char *bp; + char *mp; int len; char *mem; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); + /* * Find string capability cap */ @@ -909,13 +1010,20 @@ cgetstr(buf, cap, str) */ int cgetustr(buf, cap, str) - char *buf, *cap, **str; + char *buf; + const char *cap; + char **str; { u_int m_room; - char *bp, *mp; + const char *bp; + char *mp; int len; char *mem; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); + /* * Find string capability cap */ @@ -978,12 +1086,17 @@ cgetustr(buf, cap, str) */ int cgetnum(buf, cap, num) - char *buf, *cap; + char *buf; + const char *cap; long *num; { long n; int base, digit; - char *bp; + const char *bp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(num != NULL); /* * Find numeric capability cap @@ -1046,7 +1159,10 @@ nfcmp(nf, rec) { char *cp, tmp; int ret; - + + _DIAGASSERT(nf != NULL); + _DIAGASSERT(rec != NULL); + for (cp = rec; *cp != ':'; cp++) ;