misc.c revision 121299
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
34114601Sobrien#if 0
3530027Scharnier#ifndef lint
3630027Scharnierstatic char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
37114601Sobrien#endif /*not lint */
3830027Scharnier#endif
39114601Sobrien#include <sys/cdefs.h>
40114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/mtree/misc.c 121299 2003-10-21 07:58:52Z phk $");
4130027Scharnier
421553Srgrimes#include <sys/types.h>
431553Srgrimes#include <sys/stat.h>
4430027Scharnier#include <err.h>
451553Srgrimes#include <fts.h>
461553Srgrimes#include <stdio.h>
4761749Sjoe#include <unistd.h>
481553Srgrimes#include "mtree.h"
491553Srgrimes#include "extern.h"
501553Srgrimes
511553Srgrimesextern int lineno;
521553Srgrimes
531553Srgrimestypedef struct _key {
5499802Salfred	const char *name;			/* key name */
551553Srgrimes	u_int val;			/* value */
561553Srgrimes
571553Srgrimes#define	NEEDVALUE	0x01
581553Srgrimes	u_int flags;
591553Srgrimes} KEY;
601553Srgrimes
611553Srgrimes/* NB: the following table must be sorted lexically. */
621553Srgrimesstatic KEY keylist[] = {
632860Srgrimes	{"cksum",	F_CKSUM,	NEEDVALUE},
6454375Sjoe	{"flags",	F_FLAGS,	NEEDVALUE},
652860Srgrimes	{"gid",		F_GID,		NEEDVALUE},
662860Srgrimes	{"gname",	F_GNAME,	NEEDVALUE},
672860Srgrimes	{"ignore",	F_IGN,		0},
682860Srgrimes	{"link",	F_SLINK,	NEEDVALUE},
6944303Swollman#ifdef MD5
706286Swollman	{"md5digest",	F_MD5,		NEEDVALUE},
7144303Swollman#endif
722860Srgrimes	{"mode",	F_MODE,		NEEDVALUE},
732860Srgrimes	{"nlink",	F_NLINK,	NEEDVALUE},
7436670Speter	{"nochange",	F_NOCHANGE,	0},
7544303Swollman#ifdef RMD160
7644303Swollman	{"ripemd160digest", F_RMD160,	NEEDVALUE},
7744303Swollman#endif
7844303Swollman#ifdef SHA1
7944303Swollman	{"sha1digest",	F_SHA1,		NEEDVALUE},
8044303Swollman#endif
812860Srgrimes	{"size",	F_SIZE,		NEEDVALUE},
822860Srgrimes	{"time",	F_TIME,		NEEDVALUE},
832860Srgrimes	{"type",	F_TYPE,		NEEDVALUE},
842860Srgrimes	{"uid",		F_UID,		NEEDVALUE},
852860Srgrimes	{"uname",	F_UNAME,	NEEDVALUE},
861553Srgrimes};
871553Srgrimes
8899802Salfredint keycompare(const void *, const void *);
8999802Salfred
901553Srgrimesu_int
91121299Sphkparsekey(char *name, int *needvaluep)
921553Srgrimes{
931553Srgrimes	KEY *k, tmp;
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
107121299Sphkkeycompare(const void *a, const void *b)
1081553Srgrimes{
10999802Salfred	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
1101553Srgrimes}
11161749Sjoe
11261749Sjoechar *
113121299Sphkflags_to_string(u_long fflags)
11461749Sjoe{
11561749Sjoe	char *string;
11661749Sjoe
11761749Sjoe	string = fflagstostr(fflags);
11861749Sjoe	if (string != NULL && *string == '\0') {
11961749Sjoe		free(string);
12061749Sjoe		string = strdup("none");
12161749Sjoe	}
12261749Sjoe	if (string == NULL)
12361749Sjoe		err(1, NULL);
12461749Sjoe
12561749Sjoe	return string;
12661749Sjoe}
127