misc.c revision 144295
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 144295 2005-03-29 11:44:17Z tobez $"); 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 471553Srgrimestypedef struct _key { 4899802Salfred const char *name; /* key name */ 491553Srgrimes u_int val; /* value */ 501553Srgrimes 511553Srgrimes#define NEEDVALUE 0x01 521553Srgrimes u_int flags; 531553Srgrimes} KEY; 541553Srgrimes 551553Srgrimes/* NB: the following table must be sorted lexically. */ 561553Srgrimesstatic KEY keylist[] = { 572860Srgrimes {"cksum", F_CKSUM, NEEDVALUE}, 5854375Sjoe {"flags", F_FLAGS, NEEDVALUE}, 592860Srgrimes {"gid", F_GID, NEEDVALUE}, 602860Srgrimes {"gname", F_GNAME, NEEDVALUE}, 612860Srgrimes {"ignore", F_IGN, 0}, 622860Srgrimes {"link", F_SLINK, NEEDVALUE}, 6344303Swollman#ifdef MD5 646286Swollman {"md5digest", F_MD5, NEEDVALUE}, 6544303Swollman#endif 662860Srgrimes {"mode", F_MODE, NEEDVALUE}, 672860Srgrimes {"nlink", F_NLINK, NEEDVALUE}, 6836670Speter {"nochange", F_NOCHANGE, 0}, 6944303Swollman#ifdef RMD160 7044303Swollman {"ripemd160digest", F_RMD160, NEEDVALUE}, 7144303Swollman#endif 7244303Swollman#ifdef SHA1 7344303Swollman {"sha1digest", F_SHA1, NEEDVALUE}, 7444303Swollman#endif 75144295Stobez#ifdef SHA256 76144295Stobez {"sha256digest", F_SHA256, NEEDVALUE}, 77144295Stobez#endif 782860Srgrimes {"size", F_SIZE, NEEDVALUE}, 792860Srgrimes {"time", F_TIME, NEEDVALUE}, 802860Srgrimes {"type", F_TYPE, NEEDVALUE}, 812860Srgrimes {"uid", F_UID, NEEDVALUE}, 822860Srgrimes {"uname", F_UNAME, NEEDVALUE}, 831553Srgrimes}; 841553Srgrimes 8599802Salfredint keycompare(const void *, const void *); 8699802Salfred 871553Srgrimesu_int 88121299Sphkparsekey(char *name, int *needvaluep) 891553Srgrimes{ 901553Srgrimes KEY *k, tmp; 911553Srgrimes 921553Srgrimes tmp.name = name; 931553Srgrimes k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY), 941553Srgrimes sizeof(KEY), keycompare); 951553Srgrimes if (k == NULL) 9630027Scharnier errx(1, "line %d: unknown keyword %s", lineno, name); 971553Srgrimes 981553Srgrimes if (needvaluep) 991553Srgrimes *needvaluep = k->flags & NEEDVALUE ? 1 : 0; 1001553Srgrimes return (k->val); 1011553Srgrimes} 1021553Srgrimes 1031553Srgrimesint 104121299Sphkkeycompare(const void *a, const void *b) 1051553Srgrimes{ 10699802Salfred return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name)); 1071553Srgrimes} 10861749Sjoe 10961749Sjoechar * 110121299Sphkflags_to_string(u_long fflags) 11161749Sjoe{ 11261749Sjoe char *string; 11361749Sjoe 11461749Sjoe string = fflagstostr(fflags); 11561749Sjoe if (string != NULL && *string == '\0') { 11661749Sjoe free(string); 11761749Sjoe string = strdup("none"); 11861749Sjoe } 11961749Sjoe if (string == NULL) 12061749Sjoe err(1, NULL); 12161749Sjoe 12261749Sjoe return string; 12361749Sjoe} 124