srchcfile.c revision 62224350e5355e6834f7deb9d8a7d062a50cb7c2
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER START
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The contents of this file are subject to the terms of the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Common Development and Distribution License (the "License").
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You may not use this file except in compliance with the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * or http://www.opensolaris.org/os/licensing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See the License for the specific language governing permissions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and limitations under the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER END
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use is subject to license terms.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* All Rights Reserved */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <stdio.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <limits.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <stdlib.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <string.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <strings.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <ctype.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <sys/types.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <libintl.h>
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include "pkglib.h"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include "pkgstrct.h"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include "pkglocale.h"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include "pkglibmsgs.h"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Forward declarations
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int getend(char **cp);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int getstr(char **cp, int n, char *str, int separator[]);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* from gpkgmap.c */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandint getnumvfp(char **cp, int base, long *d, long bad);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandint getlnumvfp(char **cp, int base, fsblkcnt_t *d, long bad);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Module globals
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic char lpath[PATH_MAX]; /* for ept->path */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic char mylocal[PATH_MAX]; /* for ept->ainfo.local */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int decisionTableInit = 0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * These arrays must be indexable by an unsigned char.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int ISWORDSEP[UCHAR_MAX+1];
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int ISPKGNAMESEP[UCHAR_MAX+1];
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Name: COPYPATH
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Description: copy path limiting size to destination capacity
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Arguments: DEST - (char []) - [RW]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * SRC - (char *) - [RO, *RO]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Pointer to first byte of path to copy
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * LEN - (int) - [RO]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Number of bytes to copy
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define COPYPATH(DEST, SRC, LEN) \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland { \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* assure return path does not overflow */ \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((LEN) > sizeof ((DEST))) { \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (LEN) = sizeof ((DEST))-1; \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* copy return path to local storage */ \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) memcpy((DEST), (SRC), (LEN)); \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (DEST)[(LEN)] = '\0'; \
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Name: srchcfile
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Description: search contents file looking for closest match to entry,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * creating a new contents file if output contents file specified
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Arguments: ept - (struct cfent *) - [RO, *RW]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - contents file entry, describing last item found
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * path - (char *) - [RO, *RO]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - path to search for in contents file
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - If path is "*", then the next entry is returned;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the next entry always matches this path
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * PKGserver
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * - our door to the database server.
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik *
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Returns: int
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * < 0 - error occurred
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - Use getErrstr to retrieve character-string describing
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the reason for failure
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == 0 - no match found
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - specified path not in the contents file
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == 1 - exact match found
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - specified path found in contents file
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - this value is always returned if path is "*" and the
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * next entry is returned - 0 is returned when no more
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * entries are left to process
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Side Effects:
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - The ept structure supplied is filled in with a description of
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the item that caused the search to terminate, except in the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * case of '0' in which case the contents of 'ept' is undefined.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - NOTE: the ept->path item points to a path that is statically
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * allocated and will be overwritten on the next call.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - NOTE: the ept->ainfo.local item points to a path that is
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * statically allocated and will be overwritten on the next call.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandint
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Diksrchcfile(struct cfent *ept, char *path, PKGserver server)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland{
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik char *cpath_start = NULL;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char classname[CLSSIZ+1];
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char pkgname[PKGSIZ+1];
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland int anypath = 0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland int c;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik int cpath_len = 0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland struct pinfo *lastpinfo;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland struct pinfo *pinfo;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik char *p;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik char *curbuf;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik int linelen; /* includes NUL */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * this code does not use nested subroutines because execution time
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of this routine is especially critical to installation and upgrade
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* initialize local variables */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(NULL); /* no error message currently cached */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland lpath[0] = '\0';
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland lpath[sizeof (lpath)-1] = '\0';
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* initialize ept structure values */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(ept->ainfo.group, BADGROUP, sizeof (ept->ainfo.group));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(ept->ainfo.owner, BADOWNER, sizeof (ept->ainfo.owner));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(ept->pkg_class, BADCLASS, sizeof (ept->pkg_class));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->ainfo.local = (char *)NULL;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->ainfo.mode = BADMODE;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->cinfo.cksum = BADCONT;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->cinfo.modtime = BADCONT;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->cinfo.size = (fsblkcnt_t)BADCONT;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->ftype = BADFTYPE;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->npkgs = 0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->path = (char *)NULL;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->pinfo = (struct pinfo *)NULL;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->pkg_class_idx = -1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->volno = 0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * populate decision tables that implement fast character checking;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * this is much faster than the equivalent strpbrk() call or a
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * while() loop checking for the characters. It is only faster if
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * there are at least 3 characters to scan for - when checking for
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * one or two characters (such as '\n' or '\0') its faster to do
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * a simple while() loop.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (decisionTableInit == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * any chars listed stop scan;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * scan stops on first byte found that is set to '1' below
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Separators for normal words
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland bzero(ISWORDSEP, sizeof (ISWORDSEP));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISWORDSEP[' '] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISWORDSEP['\t'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISWORDSEP['\n'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISWORDSEP['\0'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Separators for list of packages, includes \\ for
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * alternate ftype and : for classname
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland bzero(ISPKGNAMESEP, sizeof (ISPKGNAMESEP));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISPKGNAMESEP[' '] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISPKGNAMESEP['\t'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISPKGNAMESEP['\n'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISPKGNAMESEP[':'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISPKGNAMESEP['\\'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ISPKGNAMESEP['\0'] = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland decisionTableInit = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if the path to scan for is empty, act like no path was specified */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if ((path != NULL) && (*path == '\0')) {
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik path = NULL;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if path to search for is "*", then we will return the first path
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * we encounter as a match, otherwise we return an error
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if ((path != NULL) && (path[0] != '/')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (strcmp(path, "*") != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(pkg_gt(ERR_ILLEGAL_SEARCH_PATH));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland anypath = 1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* attempt to narrow down the search for the specified path */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (anypath == 0 && path == NULL)
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* determine first character of the next entry */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (anypath == 0)
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik curbuf = pkggetentry_named(server, path, &linelen, &cpath_len);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik else
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik curbuf = pkggetentry(server, &linelen, &cpath_len);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (curbuf == NULL)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * current entry DOES start with absolute path
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * set ept->path to point to lpath
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * set cpath_start/cpath_len to point to the file name
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* copy first token into path element of passed structure */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik cpath_start = curbuf;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik p = cpath_start + cpath_len;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ept->path = lpath;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* copy path found to 'lpath' */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik COPYPATH(lpath, cpath_start, cpath_len);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* get first character following the end of the path */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik c = *p++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /*
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * we want to return information about this path in
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * the structure provided, so parse any local path
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * and jump to code which parses rest of the input line
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (c == '=') {
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* parse local path specification */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (getstr(&p, PATH_MAX, mylocal, ISWORDSEP)) {
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik setErrstr(ERR_CANNOT_READ_LL_PATH);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ept->ainfo.local = mylocal;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /*
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * if an exact match and processing a new style entry, read the
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik * remaining information from the new style entry.
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik while (isspace((c = *p++)))
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik switch (c) {
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case '?': case 'f': case 'v': case 'e': case 'l':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case 's': case 'p': case 'c': case 'b': case 'd':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case 'x':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* save ftype */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ept->ftype = (char)c;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* save class */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (getstr(&p, CLSSIZ, ept->pkg_class, ISWORDSEP)) {
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik setErrstr(ERR_CANNOT_READ_CLASS_TOKEN);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik break; /* we already read the pathname */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case '\0':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* end of line before new-line seen */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik setErrstr(ERR_INCOMPLETE_ENTRY);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case '0': case '1': case '2': case '3': case '4':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case '5': case '6': case '7': case '8': case '9':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik setErrstr(ERR_VOLUMENO_UNEXPECTED);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik case 'i':
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik setErrstr(ERR_FTYPE_I_UNEXPECTED);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik default:
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik /* unknown ftype */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik setErrstr(ERR_UNKNOWN_FTYPE);
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* link/symbolic link must have link destination */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (((ept->ftype == 's') || (ept->ftype == 'l')) &&
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (ept->ainfo.local == NULL)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_NO_LINK_SOURCE_SPECIFIED);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* character/block devices have major/minor device numbers */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (((ept->ftype == 'c') || (ept->ftype == 'b'))) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->ainfo.major = BADMAJOR;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->ainfo.minor = BADMINOR;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (getnumvfp(&p, 10, (long *)&ept->ainfo.major, BADMAJOR) ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik getnumvfp(&p, 10, (long *)&ept->ainfo.minor, BADMINOR)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(pkg_gt(ERR_CANNOT_READ_MM_NUMS));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* most types have mode, owner, group identification components */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((ept->ftype == 'd') || (ept->ftype == 'x') || (ept->ftype == 'c') ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (ept->ftype == 'b') || (ept->ftype == 'p') ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (ept->ftype == 'f') || (ept->ftype == 'v') ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (ept->ftype == 'e')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* mode, owner, group should be here */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (getnumvfp(&p, 8, (long *)&ept->ainfo.mode, BADMODE) ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik getstr(&p, sizeof (ept->ainfo.owner), ept->ainfo.owner,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ISWORDSEP) ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik getstr(&p, sizeof (ept->ainfo.group), ept->ainfo.group,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ISWORDSEP)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_CANNOT_READ_MOG);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* i/f/v/e have size, checksum, modification time components */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((ept->ftype == 'i') || (ept->ftype == 'f') ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (ept->ftype == 'v') || (ept->ftype == 'e')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* look for content description */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (getlnumvfp(&p, 10, (fsblkcnt_t *)&ept->cinfo.size,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik BADCONT) ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik getnumvfp(&p, 10, (long *)&ept->cinfo.cksum, BADCONT) ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik getnumvfp(&p, 10, (long *)&ept->cinfo.modtime, BADCONT)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_CANNOT_READ_CONTENT_INFO);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* i files processing is completed - return 'exact match found' */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (ept->ftype == 'i') {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * determine list of packages which reference this entry
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland lastpinfo = (struct pinfo *)NULL;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik while ((c = getstr(&p, sizeof (pkgname), pkgname, ISPKGNAMESEP)) <= 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if c < 0 the string was too long to fix in the buffer */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (c < 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_PACKAGE_NAME_TOO_LONG);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* a package is present - create and populate pinfo structure */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland pinfo = (struct pinfo *)calloc(1, sizeof (struct pinfo));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (!pinfo) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_NO_MEMORY);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (!lastpinfo) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->pinfo = pinfo; /* first one */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland lastpinfo->next = pinfo; /* link list */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland lastpinfo = pinfo;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((pkgname[0] == '-') || (pkgname[0] == '+') ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (pkgname[0] == '*') || (pkgname[0] == '~') ||
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (pkgname[0] == '!') || (pkgname[0] == '%')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland pinfo->status = pkgname[0];
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(pinfo->pkg, pkgname+1,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik sizeof (pinfo->pkg));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(pinfo->pkg, pkgname,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik sizeof (pinfo->pkg));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* pkg/[:[ftype][:class] */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik c = *p++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (c == '\\') {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* get alternate ftype */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland pinfo->editflag++;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik c = *p++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (c == ':') {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* get special classname */
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik (void) getstr(&p, sizeof (classname), classname,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik ISWORDSEP);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(pinfo->aclass, classname,
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik sizeof (pinfo->aclass));
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik c = *p++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ept->npkgs++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* break out of while if at end of entry */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((c == '\n') || (c == '\0')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland break;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if package not separated by a space return an error */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (!isspace(c)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_BAD_ENTRY_END);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * parsing of the entry is complete
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if not at the end of the entry, make it so */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((c != '\n') && (c != '\0')) {
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik if (getend(&p) && ept->pinfo) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland setErrstr(ERR_EXTRA_TOKENS);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland}
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandgetstr(char **cp, int n, char *str, int separator[])
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland{
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland int c;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *p = *cp;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *p1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland size_t len;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (*p == '\0') {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* leading white space ignored */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while (((c = *p) != '\0') && (isspace(*p++)))
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((c == '\0') || (c == '\n')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p--;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *cp = p;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (1); /* nothing there */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p--;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* compute length based on delimiter found or not */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p1 = p;
62224350e5355e6834f7deb9d8a7d062a50cb7c2Casper H.S. Dik while (separator[(int)(*(unsigned char *)p1)] == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p1++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland len = (ptrdiff_t)p1 - (ptrdiff_t)p;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if string will fit in result buffer copy string and return success */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (len < n) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) memcpy(str, p, len);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland str[len] = '\0';
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p += len;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *cp = p;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* result buffer too small; copy partial string, return error */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) memcpy(str, p, n-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland str[n-1] = '\0';
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p += n;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *cp = p;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (-1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland}
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandgetend(char **cp)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland{
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland int n;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *p = *cp;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland n = 0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if at end of buffer return no more characters left */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (*p == '\0') {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while ((*p != '\0') && (*p != '\n')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (n == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (!isspace(*p)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland n++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland p++;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland }
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *cp = ++p;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (n);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland}