1 /* $NetBSD: misc.c,v 1.24 2003/08/07 11:25:35 agc Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)misc.c 8.1 (Berkeley) 6/6/93 32 */ 33 34 #include <sys/cdefs.h> 35 #if defined(__RCSID) && !defined(lint) 36 __RCSID("$NetBSD: misc.c,v 1.24 2003/08/07 11:25:35 agc Exp $"); 37 #endif /* not lint */ 38 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 42 #include <stdarg.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "extern.h" 48 49 typedef struct _key { 50 const char *name; /* key name */ 51 u_int val; /* value */ 52 53 #define NEEDVALUE 0x01 54 u_int flags; 55 } KEY; 56 57 /* NB: the following tables must be sorted lexically. */ 58 static KEY keylist[] = { 59 {"cksum", F_CKSUM, NEEDVALUE}, 60 {"device", F_DEV, NEEDVALUE}, 61 {"flags", F_FLAGS, NEEDVALUE}, 62 {"gid", F_GID, NEEDVALUE}, 63 {"gname", F_GNAME, NEEDVALUE}, 64 {"ignore", F_IGN, 0}, 65 {"link", F_SLINK, NEEDVALUE}, 66 {"md5", F_MD5, NEEDVALUE}, 67 {"md5digest", F_MD5, NEEDVALUE}, 68 {"mode", F_MODE, NEEDVALUE}, 69 {"nlink", F_NLINK, NEEDVALUE}, 70 {"optional", F_OPT, 0}, 71 {"rmd160", F_RMD160, NEEDVALUE}, 72 {"rmd160digest",F_RMD160, NEEDVALUE}, 73 {"sha1", F_SHA1, NEEDVALUE}, 74 {"sha1digest", F_SHA1, NEEDVALUE}, 75 {"size", F_SIZE, NEEDVALUE}, 76 {"tags", F_TAGS, NEEDVALUE}, 77 {"time", F_TIME, NEEDVALUE}, 78 {"type", F_TYPE, NEEDVALUE}, 79 {"uid", F_UID, NEEDVALUE}, 80 {"uname", F_UNAME, NEEDVALUE} 81 }; 82 83 static KEY typelist[] = { 84 {"block", F_BLOCK, }, 85 {"char", F_CHAR, }, 86 {"dir", F_DIR, }, 87 {"fifo", F_FIFO, }, 88 {"file", F_FILE, }, 89 {"link", F_LINK, }, 90 {"socket", F_SOCK, }, 91 }; 92 93 slist_t excludetags, includetags; 94 int keys = KEYDEFAULT; 95 96 97 int keycompare(const void *, const void *); 98 99 u_int 100 parsekey(const char *name, int *needvaluep) 101 { 102 static int allbits; 103 KEY *k, tmp; 104 105 if (allbits == 0) { 106 int i; 107 108 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++) 109 allbits |= keylist[i].val; 110 } 111 tmp.name = name; 112 if (strcmp(name, "all") == 0) 113 return (allbits); 114 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY), 115 sizeof(KEY), keycompare); 116 if (k == NULL) 117 mtree_err("unknown keyword `%s'", name); 118 119 if (needvaluep) 120 *needvaluep = k->flags & NEEDVALUE ? 1 : 0; 121 122 return (k->val); 123 } 124 125 u_int 126 parsetype(const char *name) 127 { 128 KEY *k, tmp; 129 130 tmp.name = name; 131 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY), 132 sizeof(KEY), keycompare); 133 if (k == NULL) 134 mtree_err("unknown file type `%s'", name); 135 136 return (k->val); 137 } 138 139 int 140 keycompare(const void *a, const void *b) 141 { 142 143 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name)); 144 } 145 146 void 147 mtree_err(const char *fmt, ...) 148 { 149 va_list ap; 150 151 va_start(ap, fmt); 152 vwarnx(fmt, ap); 153 va_end(ap); 154 if (mtree_lineno) 155 warnx("failed at line %lu of the specification", 156 (u_long) mtree_lineno); 157 exit(1); 158 /* NOTREACHED */ 159 } 160 161 void 162 addtag(slist_t *list, char *elem) 163 { 164 165 #define TAG_CHUNK 20 166 167 if ((list->count % TAG_CHUNK) == 0) { 168 char **new; 169 170 new = (char **)realloc(list->list, (list->count + TAG_CHUNK) 171 * sizeof(char *)); 172 if (new == NULL) 173 mtree_err("memory allocation error"); 174 list->list = new; 175 } 176 list->list[list->count] = elem; 177 list->count++; 178 } 179 180 void 181 parsetags(slist_t *list, char *args) 182 { 183 char *p, *e; 184 int len; 185 186 if (args == NULL) { 187 addtag(list, NULL); 188 return; 189 } 190 while ((p = strsep(&args, ",")) != NULL) { 191 if (*p == '\0') 192 continue; 193 len = strlen(p) + 3; /* "," + p + ",\0" */ 194 if ((e = malloc(len)) == NULL) 195 mtree_err("memory allocation error"); 196 snprintf(e, len, ",%s,", p); 197 addtag(list, e); 198 } 199 } 200 201 /* 202 * matchtags 203 * returns 0 if there's a match from the exclude list in the node's tags, 204 * or there's an include list and no match. 205 * return 1 otherwise. 206 */ 207 int 208 matchtags(NODE *node) 209 { 210 int i; 211 212 if (node->tags) { 213 for (i = 0; i < excludetags.count; i++) 214 if (strstr(node->tags, excludetags.list[i])) 215 break; 216 if (i < excludetags.count) 217 return (0); 218 219 for (i = 0; i < includetags.count; i++) 220 if (strstr(node->tags, includetags.list[i])) 221 break; 222 if (i > 0 && i == includetags.count) 223 return (0); 224 } else if (includetags.count > 0) { 225 return (0); 226 } 227 return (1); 228 } 229 230 u_int 231 nodetoino(u_int type) 232 { 233 234 switch (type) { 235 case F_BLOCK: 236 return S_IFBLK; 237 case F_CHAR: 238 return S_IFCHR; 239 case F_DIR: 240 return S_IFDIR; 241 case F_FIFO: 242 return S_IFIFO; 243 case F_FILE: 244 return S_IFREG; 245 case F_LINK: 246 return S_IFLNK; 247 case F_SOCK: 248 return S_IFSOCK; 249 default: 250 printf("unknown type %d", type); 251 abort(); 252 } 253 /* NOTREACHED */ 254 } 255 256 const char * 257 nodetype(u_int type) 258 { 259 260 return (inotype(nodetoino(type))); 261 } 262 263 264 const char * 265 inotype(u_int type) 266 { 267 268 switch (type & S_IFMT) { 269 case S_IFBLK: 270 return ("block"); 271 case S_IFCHR: 272 return ("char"); 273 case S_IFDIR: 274 return ("dir"); 275 case S_IFIFO: 276 return ("fifo"); 277 case S_IFREG: 278 return ("file"); 279 case S_IFLNK: 280 return ("link"); 281 case S_IFSOCK: 282 return ("socket"); 283 default: 284 return ("unknown"); 285 } 286 /* NOTREACHED */ 287 } 288