spec.c revision 41848
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$Id: spec.c,v 1.7 1997/10/01 06:30:02 charnier Exp $"; 40#endif /* not lint */ 41 42#include <sys/types.h> 43#include <sys/stat.h> 44#include <ctype.h> 45#include <err.h> 46#include <errno.h> 47#include <fts.h> 48#include <grp.h> 49#include <pwd.h> 50#include <stdio.h> 51#include <unistd.h> 52#include "mtree.h" 53#include "extern.h" 54 55int lineno; /* Current spec line number. */ 56 57static void set __P((char *, NODE *)); 58static void unset __P((char *, NODE *)); 59 60NODE * 61spec() 62{ 63 register NODE *centry, *last; 64 register char *p; 65 NODE ginfo, *root; 66 int c_cur, c_next; 67 char buf[2048]; 68 69 centry = last = root = NULL; 70 bzero(&ginfo, sizeof(ginfo)); 71 c_cur = c_next = 0; 72 for (lineno = 1; fgets(buf, sizeof(buf), stdin); 73 ++lineno, c_cur = c_next, c_next = 0) { 74 /* Skip empty lines. */ 75 if (buf[0] == '\n') 76 continue; 77 78 /* Find end of line. */ 79 if ((p = index(buf, '\n')) == NULL) 80 errx(1, "line %d too long", lineno); 81 82 /* See if next line is continuation line. */ 83 if (p[-1] == '\\') { 84 --p; 85 c_next = 1; 86 } 87 88 /* Null-terminate the line. */ 89 *p = '\0'; 90 91 /* Skip leading whitespace. */ 92 for (p = buf; *p && isspace(*p); ++p); 93 94 /* If nothing but whitespace or comment char, continue. */ 95 if (!*p || *p == '#') 96 continue; 97 98#ifdef DEBUG 99 (void)fprintf(stderr, "line %d: {%s}\n", lineno, p); 100#endif 101 if (c_cur) { 102 set(p, centry); 103 continue; 104 } 105 106 /* Grab file name, "$", "set", or "unset". */ 107 if ((p = strtok(p, "\n\t ")) == NULL) 108 errx(1, "line %d: missing field", lineno); 109 110 if (p[0] == '/') 111 switch(p[1]) { 112 case 's': 113 if (strcmp(p + 1, "set")) 114 break; 115 set(NULL, &ginfo); 116 continue; 117 case 'u': 118 if (strcmp(p + 1, "unset")) 119 break; 120 unset(NULL, &ginfo); 121 continue; 122 } 123 124 if (index(p, '/')) 125 errx(1, "line %d: slash character in file name", 126 lineno); 127 128 if (!strcmp(p, "..")) { 129 /* Don't go up, if haven't gone down. */ 130 if (!root) 131 goto noparent; 132 if (last->type != F_DIR || last->flags & F_DONE) { 133 if (last == root) 134 goto noparent; 135 last = last->parent; 136 } 137 last->flags |= F_DONE; 138 continue; 139 140noparent: errx(1, "line %d: no parent node", lineno); 141 } 142 143 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 144 errx(1, "calloc"); 145 *centry = ginfo; 146 (void)strcpy(centry->name, p); 147#define MAGIC "?*[" 148 if (strpbrk(p, MAGIC)) 149 centry->flags |= F_MAGIC; 150 set(NULL, centry); 151 152 if (!root) { 153 last = root = centry; 154 root->parent = root; 155 } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 156 centry->parent = last; 157 last = last->child = centry; 158 } else { 159 centry->parent = last->parent; 160 centry->prev = last; 161 last = last->next = centry; 162 } 163 } 164 return (root); 165} 166 167static void 168set(t, ip) 169 char *t; 170 register NODE *ip; 171{ 172 register int type; 173 register char *kw, *val = NULL; 174 struct group *gr; 175 struct passwd *pw; 176 mode_t *m; 177 int value; 178 char *ep; 179 180 for (; (kw = strtok(t, "= \t\n")); t = NULL) { 181 ip->flags |= type = parsekey(kw, &value); 182 if (value && (val = strtok(NULL, " \t\n")) == NULL) 183 errx(1, "line %d: missing value", lineno); 184 switch(type) { 185 case F_CKSUM: 186 ip->cksum = strtoul(val, &ep, 10); 187 if (*ep) 188 errx(1, "line %d: invalid checksum %s", 189 lineno, val); 190 break; 191 case F_MD5: 192 ip->md5digest = strdup(val); 193 if(!ip->md5digest) { 194 errx(1, "strdup"); 195 } 196 break; 197 case F_GID: 198 ip->st_gid = strtoul(val, &ep, 10); 199 if (*ep) 200 errx(1, "line %d: invalid gid %s", lineno, val); 201 break; 202 case F_GNAME: 203 if ((gr = getgrnam(val)) == NULL) 204 errx(1, "line %d: unknown group %s", lineno, val); 205 ip->st_gid = gr->gr_gid; 206 break; 207 case F_IGN: 208 /* just set flag bit */ 209 break; 210 case F_MODE: 211 if ((m = setmode(val)) == NULL) 212 errx(1, "line %d: invalid file mode %s", 213 lineno, val); 214 ip->st_mode = getmode(m, 0); 215 free(m); 216 break; 217 case F_NLINK: 218 ip->st_nlink = strtoul(val, &ep, 10); 219 if (*ep) 220 errx(1, "line %d: invalid link count %s", 221 lineno, val); 222 break; 223 case F_SIZE: 224 ip->st_size = strtoq(val, &ep, 10); 225 if (*ep) 226 errx(1, "line %d: invalid size %s", 227 lineno, val); 228 break; 229 case F_SLINK: 230 if ((ip->slink = strdup(val)) == NULL) 231 errx(1, "strdup"); 232 break; 233 case F_TIME: 234 ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10); 235 if (*ep != '.') 236 errx(1, "line %d: invalid time %s", 237 lineno, val); 238 val = ep + 1; 239 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 240 if (*ep) 241 errx(1, "line %d: invalid time %s", 242 lineno, val); 243 break; 244 case F_TYPE: 245 switch(*val) { 246 case 'b': 247 if (!strcmp(val, "block")) 248 ip->type = F_BLOCK; 249 break; 250 case 'c': 251 if (!strcmp(val, "char")) 252 ip->type = F_CHAR; 253 break; 254 case 'd': 255 if (!strcmp(val, "dir")) 256 ip->type = F_DIR; 257 break; 258 case 'f': 259 if (!strcmp(val, "file")) 260 ip->type = F_FILE; 261 if (!strcmp(val, "fifo")) 262 ip->type = F_FIFO; 263 break; 264 case 'l': 265 if (!strcmp(val, "link")) 266 ip->type = F_LINK; 267 break; 268 case 's': 269 if (!strcmp(val, "socket")) 270 ip->type = F_SOCK; 271 break; 272 default: 273 errx(1, "line %d: unknown file type %s", 274 lineno, val); 275 } 276 break; 277 case F_UID: 278 ip->st_uid = strtoul(val, &ep, 10); 279 if (*ep) 280 errx(1, "line %d: invalid uid %s", lineno, val); 281 break; 282 case F_UNAME: 283 if ((pw = getpwnam(val)) == NULL) 284 errx(1, "line %d: unknown user %s", lineno, val); 285 ip->st_uid = pw->pw_uid; 286 break; 287 } 288 } 289} 290 291static void 292unset(t, ip) 293 char *t; 294 register NODE *ip; 295{ 296 register char *p; 297 298 while ((p = strtok(t, "\n\t "))) 299 ip->flags &= ~parsekey(p, NULL); 300} 301