2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.sbin/mtree/spec.c,v 1.13.2.1 2000/06/28 02:33:17 joe Exp $";
42 #include <sys/types.h>
56 int lineno; /* Current spec line number. */
58 static void set __P((char *, NODE *));
59 static void unset __P((char *, NODE *));
64 register NODE *centry, *last;
70 centry = last = root = NULL;
71 bzero(&ginfo, sizeof(ginfo));
73 for (lineno = 1; fgets(buf, sizeof(buf), stdin);
74 ++lineno, c_cur = c_next, c_next = 0) {
75 /* Skip empty lines. */
79 /* Find end of line. */
80 if ((p = index(buf, '\n')) == NULL)
81 errx(1, "line %d too long", lineno);
83 /* See if next line is continuation line. */
89 /* Null-terminate the line. */
92 /* Skip leading whitespace. */
93 for (p = buf; *p && isspace(*p); ++p);
95 /* If nothing but whitespace or comment char, continue. */
100 (void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
107 /* Grab file name, "$", "set", or "unset". */
108 if ((p = strtok(p, "\n\t ")) == NULL)
109 errx(1, "line %d: missing field", lineno);
114 if (strcmp(p + 1, "set"))
119 if (strcmp(p + 1, "unset"))
126 errx(1, "line %d: slash character in file name",
129 if (!strcmp(p, "..")) {
130 /* Don't go up, if haven't gone down. */
133 if (last->type != F_DIR || last->flags & F_DONE) {
138 last->flags |= F_DONE;
141 noparent: errx(1, "line %d: no parent node", lineno);
144 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
148 if (strpbrk(p, MAGIC))
149 centry->flags |= F_MAGIC;
150 if (strunvis(centry->name, p) == -1) {
151 warnx("filename %s is ill-encoded and literally used",
153 strcpy(centry->name, p);
158 last = root = centry;
160 } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
161 centry->parent = last;
162 last = last->child = centry;
164 centry->parent = last->parent;
166 last = last->next = centry;
178 char *kw, *val = NULL;
185 for (; (kw = strtok(t, "= \t\n")); t = NULL) {
186 ip->flags |= type = parsekey(kw, &value);
187 if (value && (val = strtok(NULL, " \t\n")) == NULL)
188 errx(1, "line %d: missing value", lineno);
191 ip->cksum = strtoul(val, &ep, 10);
193 errx(1, "line %d: invalid checksum %s",
197 ip->md5digest = strdup(val);
203 ip->sha1digest = strdup(val);
204 if(!ip->sha1digest) {
209 ip->rmd160digest = strdup(val);
210 if(!ip->rmd160digest) {
215 if (strcmp("none", val) == 0)
217 else if (strtofflags(&val, &ip->st_flags, NULL) != 0)
218 errx(1, "line %d: invalid flag %s",lineno, val);
221 ip->st_gid = strtoul(val, &ep, 10);
223 errx(1, "line %d: invalid gid %s", lineno, val);
226 if ((gr = getgrnam(val)) == NULL)
227 errx(1, "line %d: unknown group %s", lineno, val);
228 ip->st_gid = gr->gr_gid;
231 /* just set flag bit */
234 if ((m = setmode(val)) == NULL)
235 errx(1, "line %d: invalid file mode %s",
237 ip->st_mode = getmode(m, 0);
241 ip->st_nlink = strtoul(val, &ep, 10);
243 errx(1, "line %d: invalid link count %s",
247 ip->st_size = strtoq(val, &ep, 10);
249 errx(1, "line %d: invalid size %s",
253 if ((ip->slink = strdup(val)) == NULL)
257 ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10);
259 errx(1, "line %d: invalid time %s",
262 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
264 errx(1, "line %d: invalid time %s",
270 if (!strcmp(val, "block"))
274 if (!strcmp(val, "char"))
278 if (!strcmp(val, "dir"))
282 if (!strcmp(val, "file"))
284 if (!strcmp(val, "fifo"))
288 if (!strcmp(val, "link"))
292 if (!strcmp(val, "socket"))
296 errx(1, "line %d: unknown file type %s",
301 ip->st_uid = strtoul(val, &ep, 10);
303 errx(1, "line %d: invalid uid %s", lineno, val);
306 if ((pw = getpwnam(val)) == NULL)
307 errx(1, "line %d: unknown user %s", lineno, val);
308 ip->st_uid = pw->pw_uid;
321 while ((p = strtok(t, "\n\t ")))
322 ip->flags &= ~parsekey(p, NULL);