misc.c revision 121300
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.
13121300Sphk * 3. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
30114601Sobrien#if 0
3130027Scharnier#ifndef lint
3230027Scharnierstatic char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
33114601Sobrien#endif /*not lint */
3430027Scharnier#endif
35114601Sobrien#include <sys/cdefs.h>
36114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/mtree/misc.c 121300 2003-10-21 08:27:05Z phk $");
3730027Scharnier
381553Srgrimes#include <sys/types.h>
391553Srgrimes#include <sys/stat.h>
4030027Scharnier#include <err.h>
411553Srgrimes#include <fts.h>
421553Srgrimes#include <stdio.h>
4361749Sjoe#include <unistd.h>
441553Srgrimes#include "mtree.h"
451553Srgrimes#include "extern.h"
461553Srgrimes
471553Srgrimesextern int lineno;
481553Srgrimes
491553Srgrimestypedef struct _key {
5099802Salfred	const char *name;			/* key name */
511553Srgrimes	u_int val;			/* value */
521553Srgrimes
531553Srgrimes#define	NEEDVALUE	0x01
541553Srgrimes	u_int flags;
551553Srgrimes} KEY;
561553Srgrimes
571553Srgrimes/* NB: the following table must be sorted lexically. */
581553Srgrimesstatic KEY keylist[] = {
592860Srgrimes	{"cksum",	F_CKSUM,	NEEDVALUE},
6054375Sjoe	{"flags",	F_FLAGS,	NEEDVALUE},
612860Srgrimes	{"gid",		F_GID,		NEEDVALUE},
622860Srgrimes	{"gname",	F_GNAME,	NEEDVALUE},
632860Srgrimes	{"ignore",	F_IGN,		0},
642860Srgrimes	{"link",	F_SLINK,	NEEDVALUE},
6544303Swollman#ifdef MD5
666286Swollman	{"md5digest",	F_MD5,		NEEDVALUE},
6744303Swollman#endif
682860Srgrimes	{"mode",	F_MODE,		NEEDVALUE},
692860Srgrimes	{"nlink",	F_NLINK,	NEEDVALUE},
7036670Speter	{"nochange",	F_NOCHANGE,	0},
7144303Swollman#ifdef RMD160
7244303Swollman	{"ripemd160digest", F_RMD160,	NEEDVALUE},
7344303Swollman#endif
7444303Swollman#ifdef SHA1
7544303Swollman	{"sha1digest",	F_SHA1,		NEEDVALUE},
7644303Swollman#endif
772860Srgrimes	{"size",	F_SIZE,		NEEDVALUE},
782860Srgrimes	{"time",	F_TIME,		NEEDVALUE},
792860Srgrimes	{"type",	F_TYPE,		NEEDVALUE},
802860Srgrimes	{"uid",		F_UID,		NEEDVALUE},
812860Srgrimes	{"uname",	F_UNAME,	NEEDVALUE},
821553Srgrimes};
831553Srgrimes
8499802Salfredint keycompare(const void *, const void *);
8599802Salfred
861553Srgrimesu_int
87121299Sphkparsekey(char *name, int *needvaluep)
881553Srgrimes{
891553Srgrimes	KEY *k, tmp;
901553Srgrimes
911553Srgrimes	tmp.name = name;
921553Srgrimes	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
931553Srgrimes	    sizeof(KEY), keycompare);
941553Srgrimes	if (k == NULL)
9530027Scharnier		errx(1, "line %d: unknown keyword %s", lineno, name);
961553Srgrimes
971553Srgrimes	if (needvaluep)
981553Srgrimes		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
991553Srgrimes	return (k->val);
1001553Srgrimes}
1011553Srgrimes
1021553Srgrimesint
103121299Sphkkeycompare(const void *a, const void *b)
1041553Srgrimes{
10599802Salfred	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
1061553Srgrimes}
10761749Sjoe
10861749Sjoechar *
109121299Sphkflags_to_string(u_long fflags)
11061749Sjoe{
11161749Sjoe	char *string;
11261749Sjoe
11361749Sjoe	string = fflagstostr(fflags);
11461749Sjoe	if (string != NULL && *string == '\0') {
11561749Sjoe		free(string);
11661749Sjoe		string = strdup("none");
11761749Sjoe	}
11861749Sjoe	if (string == NULL)
11961749Sjoe		err(1, NULL);
12061749Sjoe
12161749Sjoe	return string;
12261749Sjoe}
123