file_cmds-45.tar.gz
[apple/file_cmds.git] / mtree / spec.c
1 /* $NetBSD: spec.c,v 1.12 1998/09/23 19:46:00 itohy 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: spec.c,v 1.12 1998/09/23 19:46:00 itohy Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "mtree.h"
57 #include "extern.h"
58
59 int lineno; /* Current spec line number. */
60
61 static void set __P((char *, NODE *));
62 static void unset __P((char *, NODE *));
63
64 NODE *
65 spec()
66 {
67 NODE *centry, *last;
68 char *p;
69 NODE ginfo, *root;
70 int c_cur, c_next;
71 char buf[2048];
72
73 root = NULL;
74 centry = last = NULL;
75 memset(&ginfo, 0, sizeof(ginfo));
76 c_cur = c_next = 0;
77 for (lineno = 1; fgets(buf, sizeof(buf), stdin);
78 ++lineno, c_cur = c_next, c_next = 0) {
79 /* Skip empty lines. */
80 if (buf[0] == '\n')
81 continue;
82
83 /* Find end of line. */
84 if ((p = strchr(buf, '\n')) == NULL)
85 mtree_err("line %d too long", lineno);
86
87 /* See if next line is continuation line. */
88 if (p[-1] == '\\') {
89 --p;
90 c_next = 1;
91 }
92
93 /* Null-terminate the line. */
94 *p = '\0';
95
96 /* Skip leading whitespace. */
97 for (p = buf; *p && isspace(*p); ++p);
98
99 /* If nothing but whitespace or comment char, continue. */
100 if (!*p || *p == '#')
101 continue;
102
103 #ifdef DEBUG
104 (void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
105 #endif
106 if (c_cur) {
107 set(p, centry);
108 continue;
109 }
110
111 /* Grab file name, "$", "set", or "unset". */
112 if ((p = strtok(p, "\n\t ")) == NULL)
113 mtree_err("missing field");
114
115 if (p[0] == '/')
116 switch(p[1]) {
117 case 's':
118 if (strcmp(p + 1, "set"))
119 break;
120 set(NULL, &ginfo);
121 continue;
122 case 'u':
123 if (strcmp(p + 1, "unset"))
124 break;
125 unset(NULL, &ginfo);
126 continue;
127 }
128
129 if (strchr(p, '/'))
130 mtree_err("slash character in file name");
131
132 if (!strcmp(p, "..")) {
133 /* Don't go up, if haven't gone down. */
134 if (!root)
135 goto noparent;
136 if (last->type != F_DIR || last->flags & F_DONE) {
137 if (last == root)
138 goto noparent;
139 last = last->parent;
140 }
141 last->flags |= F_DONE;
142 continue;
143
144 noparent: mtree_err("no parent node");
145 }
146
147 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
148 mtree_err("%s", strerror(errno));
149 *centry = ginfo;
150 (void)strcpy(centry->name, p);
151 #define MAGIC "?*["
152 if (strpbrk(p, MAGIC))
153 centry->flags |= F_MAGIC;
154 set(NULL, centry);
155
156 if (!root) {
157 last = root = centry;
158 root->parent = root;
159 } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
160 centry->parent = last;
161 last = last->child = centry;
162 } else {
163 centry->parent = last->parent;
164 centry->prev = last;
165 last = last->next = centry;
166 }
167 }
168 return (root);
169 }
170
171 static void
172 set(t, ip)
173 char *t;
174 NODE *ip;
175 {
176 int type;
177 char *kw, *val;
178 struct group *gr;
179 struct passwd *pw;
180 mode_t *m;
181 int value;
182 char *ep;
183
184 val = NULL;
185 for (; (kw = strtok(t, "= \t\n")) != NULL; t = NULL) {
186 ip->flags |= type = parsekey(kw, &value);
187 if (value && (val = strtok(NULL, " \t\n")) == NULL)
188 mtree_err("missing value");
189 switch(type) {
190 case F_CKSUM:
191 ip->cksum = strtoul(val, &ep, 10);
192 if (*ep)
193 mtree_err("invalid checksum %s", val);
194 break;
195 case F_GID:
196 ip->st_gid = (gid_t)strtoul(val, &ep, 10);
197 if (*ep)
198 mtree_err("invalid gid %s", val);
199 break;
200 case F_GNAME:
201 if ((gr = getgrnam(val)) == NULL)
202 mtree_err("unknown group %s", val);
203 ip->st_gid = gr->gr_gid;
204 break;
205 case F_IGN:
206 /* just set flag bit */
207 break;
208 case F_MODE:
209 if ((m = setmode(val)) == NULL)
210 mtree_err("invalid file mode %s", val);
211 ip->st_mode = getmode(m, 0);
212 free(m);
213 break;
214 case F_NLINK:
215 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
216 if (*ep)
217 mtree_err("invalid link count %s", val);
218 break;
219 case F_SIZE:
220 ip->st_size = (off_t)strtoq(val, &ep, 10);
221 if (*ep)
222 mtree_err("invalid size %s", val);
223 break;
224 case F_SLINK:
225 if ((ip->slink = strdup(val)) == NULL)
226 mtree_err("%s", strerror(errno));
227 break;
228 case F_TIME:
229 ip->st_mtimespec.tv_sec =
230 (time_t)strtoul(val, &ep, 10);
231 if (*ep != '.')
232 mtree_err("invalid time %s", val);
233 val = ep + 1;
234 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
235 if (*ep)
236 mtree_err("invalid time %s", val);
237 break;
238 case F_TYPE:
239 switch(*val) {
240 case 'b':
241 if (!strcmp(val, "block"))
242 ip->type = F_BLOCK;
243 break;
244 case 'c':
245 if (!strcmp(val, "char"))
246 ip->type = F_CHAR;
247 break;
248 case 'd':
249 if (!strcmp(val, "dir"))
250 ip->type = F_DIR;
251 break;
252 case 'f':
253 if (!strcmp(val, "file"))
254 ip->type = F_FILE;
255 if (!strcmp(val, "fifo"))
256 ip->type = F_FIFO;
257 break;
258 case 'l':
259 if (!strcmp(val, "link"))
260 ip->type = F_LINK;
261 break;
262 case 's':
263 if (!strcmp(val, "socket"))
264 ip->type = F_SOCK;
265 break;
266 default:
267 mtree_err("unknown file type %s", val);
268 }
269 break;
270 case F_UID:
271 ip->st_uid = (uid_t)strtoul(val, &ep, 10);
272 if (*ep)
273 mtree_err("invalid uid %s", val);
274 break;
275 case F_UNAME:
276 if ((pw = getpwnam(val)) == NULL)
277 mtree_err("unknown user %s", val);
278 ip->st_uid = pw->pw_uid;
279 break;
280 }
281 }
282 }
283
284 static void
285 unset(t, ip)
286 char *t;
287 NODE *ip;
288 {
289 char *p;
290
291 while ((p = strtok(t, "\n\t ")) != NULL)
292 ip->flags &= ~parsekey(p, NULL);
293 }