misc.c revision 54375
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1991, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
3430027Scharnier#ifndef lint
3530027Scharnier#if 0
3630027Scharnierstatic char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
3730027Scharnier#endif
3830027Scharnierstatic const char rcsid[] =
3950479Speter  "$FreeBSD: head/usr.sbin/mtree/misc.c 54375 1999-12-09 20:38:36Z joe $";
4030027Scharnier#endif /*not lint */
4130027Scharnier
421553Srgrimes#include <sys/types.h>
431553Srgrimes#include <sys/stat.h>
4430027Scharnier#include <err.h>
451553Srgrimes#include <fts.h>
461553Srgrimes#include <stdio.h>
471553Srgrimes#include "mtree.h"
481553Srgrimes#include "extern.h"
491553Srgrimes
501553Srgrimesextern int lineno;
511553Srgrimes
521553Srgrimestypedef struct _key {
531553Srgrimes	char *name;			/* key name */
541553Srgrimes	u_int val;			/* value */
551553Srgrimes
561553Srgrimes#define	NEEDVALUE	0x01
571553Srgrimes	u_int flags;
581553Srgrimes} KEY;
591553Srgrimes
601553Srgrimes/* NB: the following table must be sorted lexically. */
611553Srgrimesstatic KEY keylist[] = {
622860Srgrimes	{"cksum",	F_CKSUM,	NEEDVALUE},
6354375Sjoe	{"flags",	F_FLAGS,	NEEDVALUE},
642860Srgrimes	{"gid",		F_GID,		NEEDVALUE},
652860Srgrimes	{"gname",	F_GNAME,	NEEDVALUE},
662860Srgrimes	{"ignore",	F_IGN,		0},
672860Srgrimes	{"link",	F_SLINK,	NEEDVALUE},
6844303Swollman#ifdef MD5
696286Swollman	{"md5digest",	F_MD5,		NEEDVALUE},
7044303Swollman#endif
712860Srgrimes	{"mode",	F_MODE,		NEEDVALUE},
722860Srgrimes	{"nlink",	F_NLINK,	NEEDVALUE},
7336670Speter	{"nochange",	F_NOCHANGE,	0},
7444303Swollman#ifdef RMD160
7544303Swollman	{"ripemd160digest", F_RMD160,	NEEDVALUE},
7644303Swollman#endif
7744303Swollman#ifdef SHA1
7844303Swollman	{"sha1digest",	F_SHA1,		NEEDVALUE},
7944303Swollman#endif
802860Srgrimes	{"size",	F_SIZE,		NEEDVALUE},
812860Srgrimes	{"time",	F_TIME,		NEEDVALUE},
822860Srgrimes	{"type",	F_TYPE,		NEEDVALUE},
832860Srgrimes	{"uid",		F_UID,		NEEDVALUE},
842860Srgrimes	{"uname",	F_UNAME,	NEEDVALUE},
851553Srgrimes};
861553Srgrimes
871553Srgrimesu_int
881553Srgrimesparsekey(name, needvaluep)
891553Srgrimes	char *name;
901553Srgrimes	int *needvaluep;
911553Srgrimes{
921553Srgrimes	KEY *k, tmp;
931553Srgrimes	int keycompare __P((const void *, const void *));
941553Srgrimes
951553Srgrimes	tmp.name = name;
961553Srgrimes	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
971553Srgrimes	    sizeof(KEY), keycompare);
981553Srgrimes	if (k == NULL)
9930027Scharnier		errx(1, "line %d: unknown keyword %s", lineno, name);
1001553Srgrimes
1011553Srgrimes	if (needvaluep)
1021553Srgrimes		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
1031553Srgrimes	return (k->val);
1041553Srgrimes}
1051553Srgrimes
1061553Srgrimesint
1071553Srgrimeskeycompare(a, b)
1081553Srgrimes	const void *a, *b;
1091553Srgrimes{
1101553Srgrimes	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
1111553Srgrimes}
112