spec.c revision 1.8 1 /* $NetBSD: spec.c,v 1.8 1997/04/25 05:45:24 mikel Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$NetBSD: spec.c,v 1.8 1997/04/25 05:45:24 mikel Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fts.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <ctype.h>
53 #include "mtree.h"
54 #include "extern.h"
55
56 int lineno; /* Current spec line number. */
57
58 static void set __P((char *, NODE *));
59 static void unset __P((char *, NODE *));
60
61 NODE *
62 spec()
63 {
64 register NODE *centry, *last;
65 register char *p;
66 NODE ginfo, *root;
67 int c_cur, c_next;
68 char buf[2048];
69
70 root = NULL;
71 bzero(&ginfo, sizeof(ginfo));
72 c_cur = c_next = 0;
73 for (lineno = 1; fgets(buf, sizeof(buf), stdin);
74 ++lineno, c_cur = c_next, c_next = 0) {
75 /* Skip empty lines. */
76 if (buf[0] == '\n')
77 continue;
78
79 /* Find end of line. */
80 if ((p = index(buf, '\n')) == NULL)
81 err("line %d too long", lineno);
82
83 /* See if next line is continuation line. */
84 if (p[-1] == '\\') {
85 --p;
86 c_next = 1;
87 }
88
89 /* Null-terminate the line. */
90 *p = '\0';
91
92 /* Skip leading whitespace. */
93 for (p = buf; *p && isspace(*p); ++p);
94
95 /* If nothing but whitespace or comment char, continue. */
96 if (!*p || *p == '#')
97 continue;
98
99 #ifdef DEBUG
100 (void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
101 #endif
102 if (c_cur) {
103 set(p, centry);
104 continue;
105 }
106
107 /* Grab file name, "$", "set", or "unset". */
108 if ((p = strtok(p, "\n\t ")) == NULL)
109 err("missing field");
110
111 if (p[0] == '/')
112 switch(p[1]) {
113 case 's':
114 if (strcmp(p + 1, "set"))
115 break;
116 set(NULL, &ginfo);
117 continue;
118 case 'u':
119 if (strcmp(p + 1, "unset"))
120 break;
121 unset(NULL, &ginfo);
122 continue;
123 }
124
125 if (index(p, '/'))
126 err("slash character in file name");
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
140 noparent: err("no parent node");
141 }
142
143 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
144 err("%s", strerror(errno));
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
167 static void
168 set(t, ip)
169 char *t;
170 register NODE *ip;
171 {
172 register int type;
173 register char *kw, *val;
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 err("missing value");
184 switch(type) {
185 case F_CKSUM:
186 ip->cksum = strtoul(val, &ep, 10);
187 if (*ep)
188 err("invalid checksum %s", val);
189 break;
190 case F_GID:
191 ip->st_gid = (gid_t)strtoul(val, &ep, 10);
192 if (*ep)
193 err("invalid gid %s", val);
194 break;
195 case F_GNAME:
196 if ((gr = getgrnam(val)) == NULL)
197 err("unknown group %s", val);
198 ip->st_gid = gr->gr_gid;
199 break;
200 case F_IGN:
201 /* just set flag bit */
202 break;
203 case F_MODE:
204 if ((m = setmode(val)) == NULL)
205 err("invalid file mode %s", val);
206 ip->st_mode = getmode(m, 0);
207 break;
208 case F_NLINK:
209 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
210 if (*ep)
211 err("invalid link count %s", val);
212 break;
213 case F_SIZE:
214 ip->st_size = (off_t)strtoq(val, &ep, 10);
215 if (*ep)
216 err("invalid size %s", val);
217 break;
218 case F_SLINK:
219 if ((ip->slink = strdup(val)) == NULL)
220 err("%s", strerror(errno));
221 break;
222 case F_TIME:
223 ip->st_mtimespec.tv_sec =
224 (time_t)strtoul(val, &ep, 10);
225 if (*ep != '.')
226 err("invalid time %s", val);
227 val = ep + 1;
228 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
229 if (*ep)
230 err("invalid time %s", val);
231 break;
232 case F_TYPE:
233 switch(*val) {
234 case 'b':
235 if (!strcmp(val, "block"))
236 ip->type = F_BLOCK;
237 break;
238 case 'c':
239 if (!strcmp(val, "char"))
240 ip->type = F_CHAR;
241 break;
242 case 'd':
243 if (!strcmp(val, "dir"))
244 ip->type = F_DIR;
245 break;
246 case 'f':
247 if (!strcmp(val, "file"))
248 ip->type = F_FILE;
249 if (!strcmp(val, "fifo"))
250 ip->type = F_FIFO;
251 break;
252 case 'l':
253 if (!strcmp(val, "link"))
254 ip->type = F_LINK;
255 break;
256 case 's':
257 if (!strcmp(val, "socket"))
258 ip->type = F_SOCK;
259 break;
260 default:
261 err("unknown file type %s", val);
262 }
263 break;
264 case F_UID:
265 ip->st_uid = (uid_t)strtoul(val, &ep, 10);
266 if (*ep)
267 err("invalid uid %s", val);
268 break;
269 case F_UNAME:
270 if ((pw = getpwnam(val)) == NULL)
271 err("unknown user %s", val);
272 ip->st_uid = pw->pw_uid;
273 break;
274 }
275 }
276 }
277
278 static void
279 unset(t, ip)
280 char *t;
281 register NODE *ip;
282 {
283 register char *p;
284
285 while (p = strtok(t, "\n\t "))
286 ip->flags &= ~parsekey(p, NULL);
287 }
288