- if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) {
- if ((record = DOALLOC(topreclen + BFRAG)) == 0) {
- errno = ENOMEM;
- return (TC_SYS_ERR);
- }
- (void)strcpy(record, toprec);
- rp = record + topreclen + 1;
- r_end = rp + BFRAG;
- current = in_array;
- } else {
- int foundit;
-
- /*
- * Allocate first chunk of memory.
- */
- if ((record = DOALLOC(BFRAG)) == 0) {
- errno = ENOMEM;
- return (TC_SYS_ERR);
- }
- rp = r_end = record + BFRAG;
- foundit = FALSE;
-
- /*
- * Loop through database array until finding the record.
- */
- for (current = in_array; db_array[current] != 0; current++) {
- int eof = FALSE;
-
- /*
- * Open database if not already open.
- */
- if (fd >= 0) {
- (void)lseek(fd, (off_t)0, SEEK_SET);
- } else if ((_nc_access(db_array[current], R_OK) < 0)
- || (fd = open(db_array[current], O_RDONLY, 0)) < 0) {
- /* No error on unfound file. */
- if (errno == ENOENT)
- continue;
- free(record);
- return (TC_SYS_ERR);
- } else {
- myfd = TRUE;
- }
- lineno = 0;
-
- /*
- * Find the requested capability record ...
- */
- {
- char buf[2048];
- register char *b_end = buf;
- register char *bp = buf;
- register int c;
-
- /*
- * Loop invariants:
- * There is always room for one more character in record.
- * R_end always points just past end of record.
- * 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.
- */
-
- for (;;) {
- int first = lineno + 1;
-
- /*
- * Read in a line implementing (\, newline)
- * line continuation.
- */
- rp = record;
- for (;;) {
- if (bp >= b_end) {
- int n;
-
- n = read(fd, buf, sizeof(buf));
- if (n <= 0) {
- if (myfd)
- (void)close(fd);
- if (n < 0) {
- free(record);
- return (TC_SYS_ERR);
- }
- fd = -1;
- eof = TRUE;
- break;
- }
- b_end = buf+n;
- bp = buf;
- }
-
- c = *bp++;
- if (c == '\n') {
- lineno++;
- if (rp == record || *(rp-1) != '\\')
- break;
- }
- *rp++ = c;
-
- /*
- * Enforce loop invariant: if no room
- * left in record buffer, try to get
- * some more.
- */
- if (rp >= r_end) {
- unsigned int pos;
- size_t newsize;
-
- pos = rp - record;
- newsize = r_end - record + BFRAG;
- record = DOALLOC(newsize);
- if (record == 0) {
- if (myfd)
- (void)close(fd);
- errno = ENOMEM;
- return (TC_SYS_ERR);
- }
- r_end = record + newsize;
- rp = record + pos;
- }
- }
- /* loop invariant lets us do this */
- *rp++ = '\0';
-
- /*
- * If encountered eof check next file.
- */
- if (eof)
- break;
-
- /*
- * Toss blank lines and comments.
- */
- if (*record == '\0' || *record == '#')
- continue;
-
- /*
- * See if this is the record we want ...
- */
- if (_nc_cgetmatch(record, name) == 0
- && (nfield == 0
- || !_nc_nfcmp(nfield, record))) {
- foundit = TRUE;
- *beginning = first;
- break; /* found it! */
- }
- }
- }
- if (foundit)
- break;
- }
-
- if (!foundit)
- return (TC_NOT_FOUND);