spec.c revision 1.76 1 /* $NetBSD: spec.c,v 1.76 2009/09/19 20:42:06 apb 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. 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
32 /*-
33 * Copyright (c) 2001-2004 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Luke Mewburn of Wasabi Systems.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #if HAVE_NBTOOL_CONFIG_H
62 #include "nbtool_config.h"
63 #endif
64
65 #include <sys/cdefs.h>
66 #if defined(__RCSID) && !defined(lint)
67 #if 0
68 static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
69 #else
70 __RCSID("$NetBSD: spec.c,v 1.76 2009/09/19 20:42:06 apb Exp $");
71 #endif
72 #endif /* not lint */
73
74 #include <sys/param.h>
75 #include <sys/stat.h>
76
77 #include <assert.h>
78 #include <ctype.h>
79 #include <errno.h>
80 #include <grp.h>
81 #include <pwd.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <unistd.h>
86 #include <vis.h>
87 #include <util.h>
88
89 #include "extern.h"
90 #include "pack_dev.h"
91
92 size_t mtree_lineno; /* Current spec line number */
93 int mtree_Mflag; /* Merge duplicate entries */
94 int mtree_Wflag; /* Don't "whack" permissions */
95 int mtree_Sflag; /* Sort entries */
96
97 static dev_t parsedev(char *);
98 static void replacenode(NODE *, NODE *);
99 static void set(char *, NODE *);
100 static void unset(char *, NODE *);
101 static void addchild(NODE *, NODE *);
102 static int nodecmp(const NODE *, const NODE *);
103
104 #define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0)
105
106 NODE *
107 spec(FILE *fp)
108 {
109 NODE *centry, *last, *pathparent, *cur;
110 char *p, *e, *next;
111 NODE ginfo, *root;
112 char *buf, *tname, *ntname;
113 size_t tnamelen, plen;
114
115 root = NULL;
116 centry = last = NULL;
117 tname = NULL;
118 tnamelen = 0;
119 memset(&ginfo, 0, sizeof(ginfo));
120 for (mtree_lineno = 0;
121 (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
122 FPARSELN_UNESCCOMM));
123 free(buf)) {
124 /* Skip leading whitespace. */
125 for (p = buf; *p && isspace((unsigned char)*p); ++p)
126 continue;
127
128 /* If nothing but whitespace, continue. */
129 if (!*p)
130 continue;
131
132 #ifdef DEBUG
133 fprintf(stderr, "line %lu: {%s}\n",
134 (u_long)mtree_lineno, p);
135 #endif
136 /* Grab file name, "$", "set", or "unset". */
137 next = buf;
138 while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
139 continue;
140 if (p == NULL)
141 mtree_err("missing field");
142
143 if (p[0] == '/') {
144 if (strcmp(p + 1, "set") == 0)
145 set(next, &ginfo);
146 else if (strcmp(p + 1, "unset") == 0)
147 unset(next, &ginfo);
148 else
149 mtree_err("invalid specification `%s'", p);
150 continue;
151 }
152
153 if (strcmp(p, "..") == 0) {
154 /* Don't go up, if haven't gone down. */
155 if (root == NULL)
156 goto noparent;
157 if (last->type != F_DIR || last->flags & F_DONE) {
158 if (last == root)
159 goto noparent;
160 last = last->parent;
161 }
162 last->flags |= F_DONE;
163 continue;
164
165 noparent: mtree_err("no parent node");
166 }
167
168 plen = strlen(p) + 1;
169 if (plen > tnamelen) {
170 if ((ntname = realloc(tname, plen)) == NULL)
171 mtree_err("realloc: %s", strerror(errno));
172 tname = ntname;
173 tnamelen = plen;
174 }
175 if (strunvis(tname, p) == -1)
176 mtree_err("strunvis failed on `%s'", p);
177 p = tname;
178
179 pathparent = NULL;
180 if (strchr(p, '/') != NULL) {
181 cur = root;
182 for (; (e = strchr(p, '/')) != NULL; p = e+1) {
183 if (p == e)
184 continue; /* handle // */
185 *e = '\0';
186 if (strcmp(p, ".") != 0) {
187 while (cur &&
188 strcmp(cur->name, p) != 0) {
189 cur = cur->next;
190 }
191 }
192 if (cur == NULL || cur->type != F_DIR) {
193 mtree_err("%s: %s", tname,
194 "missing directory in specification");
195 }
196 *e = '/';
197 pathparent = cur;
198 cur = cur->child;
199 }
200 if (*p == '\0')
201 mtree_err("%s: empty leaf element", tname);
202 }
203
204 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
205 mtree_err("%s", strerror(errno));
206 *centry = ginfo;
207 centry->lineno = mtree_lineno;
208 strcpy(centry->name, p);
209 #define MAGIC "?*["
210 if (strpbrk(p, MAGIC))
211 centry->flags |= F_MAGIC;
212 set(next, centry);
213
214 if (root == NULL) {
215 /*
216 * empty tree
217 */
218 if (strcmp(centry->name, ".") != 0 ||
219 centry->type != F_DIR)
220 mtree_err(
221 "root node must be the directory `.'");
222 last = root = centry;
223 root->parent = root;
224 } else if (pathparent != NULL) {
225 /*
226 * full path entry; add or replace
227 */
228 centry->parent = pathparent;
229 addchild(pathparent, centry);
230 last = centry;
231 } else if (strcmp(centry->name, ".") == 0) {
232 /*
233 * duplicate "." entry; always replace
234 */
235 replacenode(root, centry);
236 } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
237 /*
238 * new relative child in current dir;
239 * add or replace
240 */
241 centry->parent = last;
242 addchild(last, centry);
243 last = centry;
244 } else {
245 /*
246 * new relative child in parent dir
247 * (after encountering ".." entry);
248 * add or replace
249 */
250 centry->parent = last->parent;
251 addchild(last->parent, centry);
252 last = centry;
253 }
254 }
255 return (root);
256 }
257
258 void
259 free_nodes(NODE *root)
260 {
261 NODE *cur, *next;
262
263 if (root == NULL)
264 return;
265
266 next = NULL;
267 for (cur = root; cur != NULL; cur = next) {
268 next = cur->next;
269 free_nodes(cur->child);
270 REPLACEPTR(cur->slink, NULL);
271 REPLACEPTR(cur->md5digest, NULL);
272 REPLACEPTR(cur->rmd160digest, NULL);
273 REPLACEPTR(cur->sha1digest, NULL);
274 REPLACEPTR(cur->sha256digest, NULL);
275 REPLACEPTR(cur->sha384digest, NULL);
276 REPLACEPTR(cur->sha512digest, NULL);
277 REPLACEPTR(cur->tags, NULL);
278 REPLACEPTR(cur, NULL);
279 }
280 }
281
282 /*
283 * dump_nodes --
284 * dump the NODEs from `cur', based in the directory `dir'.
285 * if pathlast is none zero, print the path last, otherwise print
286 * it first.
287 */
288 void
289 dump_nodes(const char *dir, NODE *root, int pathlast)
290 {
291 NODE *cur;
292 char path[MAXPATHLEN];
293 const char *name;
294 char *str;
295 char *p, *q;
296
297 for (cur = root; cur != NULL; cur = cur->next) {
298 if (cur->type != F_DIR && !matchtags(cur))
299 continue;
300
301 if (snprintf(path, sizeof(path), "%s%s%s",
302 dir, *dir ? "/" : "", cur->name)
303 >= (int)sizeof(path))
304 mtree_err("Pathname too long.");
305
306 if (!pathlast)
307 printf("%s ", vispath(path));
308
309 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
310 if (MATCHFLAG(F_TYPE))
311 printf("type=%s ", nodetype(cur->type));
312 if (MATCHFLAG(F_UID | F_UNAME)) {
313 if (keys & F_UNAME &&
314 (name = user_from_uid(cur->st_uid, 1)) != NULL)
315 printf("uname=%s ", name);
316 else
317 printf("uid=%u ", cur->st_uid);
318 }
319 if (MATCHFLAG(F_GID | F_GNAME)) {
320 if (keys & F_GNAME &&
321 (name = group_from_gid(cur->st_gid, 1)) != NULL)
322 printf("gname=%s ", name);
323 else
324 printf("gid=%u ", cur->st_gid);
325 }
326 if (MATCHFLAG(F_MODE))
327 printf("mode=%#o ", cur->st_mode);
328 if (MATCHFLAG(F_DEV) &&
329 (cur->type == F_BLOCK || cur->type == F_CHAR))
330 printf("device=%#llx ", (long long)cur->st_rdev);
331 if (MATCHFLAG(F_NLINK))
332 printf("nlink=%d ", cur->st_nlink);
333 if (MATCHFLAG(F_SLINK))
334 printf("link=%s ", vispath(cur->slink));
335 if (MATCHFLAG(F_SIZE))
336 printf("size=%lld ", (long long)cur->st_size);
337 if (MATCHFLAG(F_TIME))
338 printf("time=%lld.%ld ",
339 (long long)cur->st_mtimespec.tv_sec,
340 cur->st_mtimespec.tv_nsec);
341 if (MATCHFLAG(F_CKSUM))
342 printf("cksum=%lu ", cur->cksum);
343 if (MATCHFLAG(F_MD5))
344 printf("md5=%s ", cur->md5digest);
345 if (MATCHFLAG(F_RMD160))
346 printf("rmd160=%s ", cur->rmd160digest);
347 if (MATCHFLAG(F_SHA1))
348 printf("sha1=%s ", cur->sha1digest);
349 if (MATCHFLAG(F_SHA256))
350 printf("sha256=%s ", cur->sha256digest);
351 if (MATCHFLAG(F_SHA384))
352 printf("sha384=%s ", cur->sha384digest);
353 if (MATCHFLAG(F_SHA512))
354 printf("sha512=%s ", cur->sha512digest);
355 if (MATCHFLAG(F_FLAGS)) {
356 str = flags_to_string(cur->st_flags, "none");
357 printf("flags=%s ", str);
358 free(str);
359 }
360 if (MATCHFLAG(F_IGN))
361 printf("ignore ");
362 if (MATCHFLAG(F_OPT))
363 printf("optional ");
364 if (MATCHFLAG(F_TAGS)) {
365 /* don't output leading or trailing commas */
366 p = cur->tags;
367 while (*p == ',')
368 p++;
369 q = p + strlen(p);
370 while(q > p && q[-1] == ',')
371 q--;
372 printf("tags=%.*s ", (int)(q - p), p);
373 }
374 puts(pathlast ? vispath(path) : "");
375
376 if (cur->child)
377 dump_nodes(path, cur->child, pathlast);
378 }
379 }
380
381 /*
382 * vispath --
383 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN
384 * characters long, and returns a pointer to a static buffer containing
385 * the result.
386 */
387 char *
388 vispath(const char *path)
389 {
390 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
391 static char pathbuf[4*MAXPATHLEN + 1];
392
393 strsvis(pathbuf, path, VIS_CSTYLE, extra);
394 return(pathbuf);
395 }
396
397
398 static dev_t
399 parsedev(char *arg)
400 {
401 #define MAX_PACK_ARGS 3
402 u_long numbers[MAX_PACK_ARGS];
403 char *p, *ep, *dev;
404 int argc;
405 pack_t *pack;
406 dev_t result;
407 const char *error = NULL;
408
409 if ((dev = strchr(arg, ',')) != NULL) {
410 *dev++='\0';
411 if ((pack = pack_find(arg)) == NULL)
412 mtree_err("unknown format `%s'", arg);
413 argc = 0;
414 while ((p = strsep(&dev, ",")) != NULL) {
415 if (*p == '\0')
416 mtree_err("missing number");
417 numbers[argc++] = strtoul(p, &ep, 0);
418 if (*ep != '\0')
419 mtree_err("invalid number `%s'",
420 p);
421 if (argc > MAX_PACK_ARGS)
422 mtree_err("too many arguments");
423 }
424 if (argc < 2)
425 mtree_err("not enough arguments");
426 result = (*pack)(argc, numbers, &error);
427 if (error != NULL)
428 mtree_err("%s", error);
429 } else {
430 result = (dev_t)strtoul(arg, &ep, 0);
431 if (*ep != '\0')
432 mtree_err("invalid device `%s'", arg);
433 }
434 return (result);
435 }
436
437 static void
438 replacenode(NODE *cur, NODE *new)
439 {
440
441 #define REPLACE(x) cur->x = new->x
442 #define REPLACESTR(x) REPLACEPTR(cur->x,new->x)
443
444 if (cur->type != new->type) {
445 if (mtree_Mflag) {
446 /*
447 * merge entries with different types; we
448 * don't want children retained in this case.
449 */
450 REPLACE(type);
451 free_nodes(cur->child);
452 cur->child = NULL;
453 } else {
454 mtree_err(
455 "existing entry for `%s', type `%s'"
456 " does not match type `%s'",
457 cur->name, nodetype(cur->type),
458 nodetype(new->type));
459 }
460 }
461
462 REPLACE(st_size);
463 REPLACE(st_mtimespec);
464 REPLACESTR(slink);
465 if (cur->slink != NULL) {
466 if ((cur->slink = strdup(new->slink)) == NULL)
467 mtree_err("memory allocation error");
468 if (strunvis(cur->slink, new->slink) == -1)
469 mtree_err("strunvis failed on `%s'", new->slink);
470 free(new->slink);
471 }
472 REPLACE(st_uid);
473 REPLACE(st_gid);
474 REPLACE(st_mode);
475 REPLACE(st_rdev);
476 REPLACE(st_flags);
477 REPLACE(st_nlink);
478 REPLACE(cksum);
479 REPLACESTR(md5digest);
480 REPLACESTR(rmd160digest);
481 REPLACESTR(sha1digest);
482 REPLACESTR(sha256digest);
483 REPLACESTR(sha384digest);
484 REPLACESTR(sha512digest);
485 REPLACESTR(tags);
486 REPLACE(lineno);
487 REPLACE(flags);
488 free(new);
489 }
490
491 static void
492 set(char *t, NODE *ip)
493 {
494 int type, value, len;
495 gid_t gid;
496 uid_t uid;
497 char *kw, *val, *md, *ep;
498 void *m;
499
500 while ((kw = strsep(&t, "= \t")) != NULL) {
501 if (*kw == '\0')
502 continue;
503 if (strcmp(kw, "all") == 0)
504 mtree_err("invalid keyword `all'");
505 ip->flags |= type = parsekey(kw, &value);
506 if (!value)
507 /* Just set flag bit (F_IGN and F_OPT) */
508 continue;
509 while ((val = strsep(&t, " \t")) != NULL && *val == '\0')
510 continue;
511 if (val == NULL)
512 mtree_err("missing value");
513 switch (type) {
514 case F_CKSUM:
515 ip->cksum = strtoul(val, &ep, 10);
516 if (*ep)
517 mtree_err("invalid checksum `%s'", val);
518 break;
519 case F_DEV:
520 ip->st_rdev = parsedev(val);
521 break;
522 case F_FLAGS:
523 if (strcmp("none", val) == 0)
524 ip->st_flags = 0;
525 else if (string_to_flags(&val, &ip->st_flags, NULL)
526 != 0)
527 mtree_err("invalid flag `%s'", val);
528 break;
529 case F_GID:
530 ip->st_gid = (gid_t)strtoul(val, &ep, 10);
531 if (*ep)
532 mtree_err("invalid gid `%s'", val);
533 break;
534 case F_GNAME:
535 if (mtree_Wflag) /* don't parse if whacking */
536 break;
537 if (gid_from_group(val, &gid) == -1)
538 mtree_err("unknown group `%s'", val);
539 ip->st_gid = gid;
540 break;
541 case F_MD5:
542 if (val[0]=='0' && val[1]=='x')
543 md=&val[2];
544 else
545 md=val;
546 if ((ip->md5digest = strdup(md)) == NULL)
547 mtree_err("memory allocation error");
548 break;
549 case F_MODE:
550 if ((m = setmode(val)) == NULL)
551 mtree_err("cannot set file mode `%s' (%s)",
552 val, strerror(errno));
553 ip->st_mode = getmode(m, 0);
554 free(m);
555 break;
556 case F_NLINK:
557 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
558 if (*ep)
559 mtree_err("invalid link count `%s'", val);
560 break;
561 case F_RMD160:
562 if (val[0]=='0' && val[1]=='x')
563 md=&val[2];
564 else
565 md=val;
566 if ((ip->rmd160digest = strdup(md)) == NULL)
567 mtree_err("memory allocation error");
568 break;
569 case F_SHA1:
570 if (val[0]=='0' && val[1]=='x')
571 md=&val[2];
572 else
573 md=val;
574 if ((ip->sha1digest = strdup(md)) == NULL)
575 mtree_err("memory allocation error");
576 break;
577 case F_SIZE:
578 ip->st_size = (off_t)strtoll(val, &ep, 10);
579 if (*ep)
580 mtree_err("invalid size `%s'", val);
581 break;
582 case F_SLINK:
583 if ((ip->slink = strdup(val)) == NULL)
584 mtree_err("memory allocation error");
585 if (strunvis(ip->slink, val) == -1)
586 mtree_err("strunvis failed on `%s'", val);
587 break;
588 case F_TAGS:
589 len = strlen(val) + 3; /* "," + str + ",\0" */
590 if ((ip->tags = malloc(len)) == NULL)
591 mtree_err("memory allocation error");
592 snprintf(ip->tags, len, ",%s,", val);
593 break;
594 case F_TIME:
595 ip->st_mtimespec.tv_sec =
596 (time_t)strtoll(val, &ep, 10);
597 if (*ep != '.')
598 mtree_err("invalid time `%s'", val);
599 val = ep + 1;
600 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
601 if (*ep)
602 mtree_err("invalid time `%s'", val);
603 break;
604 case F_TYPE:
605 ip->type = parsetype(val);
606 break;
607 case F_UID:
608 ip->st_uid = (uid_t)strtoul(val, &ep, 10);
609 if (*ep)
610 mtree_err("invalid uid `%s'", val);
611 break;
612 case F_UNAME:
613 if (mtree_Wflag) /* don't parse if whacking */
614 break;
615 if (uid_from_user(val, &uid) == -1)
616 mtree_err("unknown user `%s'", val);
617 ip->st_uid = uid;
618 break;
619 case F_SHA256:
620 if (val[0]=='0' && val[1]=='x')
621 md=&val[2];
622 else
623 md=val;
624 if ((ip->sha256digest = strdup(md)) == NULL)
625 mtree_err("memory allocation error");
626 break;
627 case F_SHA384:
628 if (val[0]=='0' && val[1]=='x')
629 md=&val[2];
630 else
631 md=val;
632 if ((ip->sha384digest = strdup(md)) == NULL)
633 mtree_err("memory allocation error");
634 break;
635 case F_SHA512:
636 if (val[0]=='0' && val[1]=='x')
637 md=&val[2];
638 else
639 md=val;
640 if ((ip->sha512digest = strdup(md)) == NULL)
641 mtree_err("memory allocation error");
642 break;
643 default:
644 mtree_err(
645 "set(): unsupported key type 0x%x (INTERNAL ERROR)",
646 type);
647 /* NOTREACHED */
648 }
649 }
650 }
651
652 static void
653 unset(char *t, NODE *ip)
654 {
655 char *p;
656
657 while ((p = strsep(&t, " \t")) != NULL) {
658 if (*p == '\0')
659 continue;
660 ip->flags &= ~parsekey(p, NULL);
661 }
662 }
663
664 /*
665 * addchild --
666 * Add the centry node as a child of the pathparent node. If
667 * centry is a duplicate, call replacenode(). If centry is not
668 * a duplicate, insert it into the linked list referenced by
669 * pathparent->child. Keep the list sorted if Sflag is set.
670 */
671 static void
672 addchild(NODE *pathparent, NODE *centry)
673 {
674 NODE *samename; /* node with the same name as centry */
675 NODE *replacepos; /* if non-NULL, centry should replace this node */
676 NODE *insertpos; /* if non-NULL, centry should be inserted
677 * after this node */
678 NODE *cur; /* for stepping through the list */
679 NODE *last; /* the last node in the list */
680 int cmp;
681
682 samename = NULL;
683 replacepos = NULL;
684 insertpos = NULL;
685 last = NULL;
686 cur = pathparent->child;
687 if (cur == NULL) {
688 /* centry is pathparent's first and only child node so far */
689 pathparent->child = centry;
690 return;
691 }
692
693 /*
694 * pathparent already has at least one other child, so add the
695 * centry node to the list.
696 *
697 * We first scan through the list looking for an existing node
698 * with the same name (setting samename), and also looking
699 * for the correct position to replace or insert the new node
700 * (setting replacepos and/or insertpos).
701 */
702 for (; cur != NULL; last = cur, cur = cur->next) {
703 if (strcmp(centry->name, cur->name) == 0) {
704 samename = cur;
705 }
706 if (mtree_Sflag) {
707 cmp = nodecmp(centry, cur);
708 if (cmp == 0) {
709 replacepos = cur;
710 } else if (cmp > 0) {
711 insertpos = cur;
712 }
713 }
714 }
715 if (! mtree_Sflag) {
716 if (samename != NULL) {
717 /* replace node with same name */
718 replacepos = samename;
719 } else {
720 /* add new node at end of list */
721 insertpos = last;
722 }
723 }
724
725 if (samename != NULL) {
726 /*
727 * We found a node with the same name above. Call
728 * replacenode(), which will either exit with an error,
729 * or replace the information in the samename node and
730 * free the information in the centry node.
731 */
732 replacenode(samename, centry);
733 if (samename == replacepos) {
734 /* The just-replaced node was in the correct position */
735 return;
736 }
737 if (samename == insertpos || samename->prev == insertpos) {
738 /*
739 * We thought the new node should be just before
740 * or just after the replaced node, but that would
741 * be equivalent to just retaining the replaced node.
742 */
743 return;
744 }
745
746 /*
747 * The just-replaced node is in the wrong position in
748 * the list. This can happen if sort order depends on
749 * criteria other than the node name.
750 *
751 * Make centry point to the just-replaced node. Unlink
752 * the just-replaced node from the list, and allow it to
753 * be insterted in the correct position later.
754 */
755 centry = samename;
756 if (centry->prev)
757 centry->prev->next = centry->next;
758 else {
759 /* centry->next is the new head of the list */
760 pathparent->child = centry->next;
761 assert(centry->next != NULL);
762 }
763 if (centry->next)
764 centry->next->prev = centry->prev;
765 centry->prev = NULL;
766 centry->next = NULL;
767 }
768
769 if (insertpos == NULL) {
770 /* insert centry at the beginning of the list */
771 pathparent->child->prev = centry;
772 centry->next = pathparent->child;
773 centry->prev = NULL;
774 pathparent->child = centry;
775 } else {
776 /* insert centry into the list just after insertpos */
777 centry->next = insertpos->next;
778 insertpos->next = centry;
779 centry->prev = insertpos;
780 if (centry->next)
781 centry->next->prev = centry;
782 }
783 return;
784 }
785
786 /*
787 * nodecmp --
788 * used as a comparison function by addchild() to control the order
789 * in which entries appear within a list of sibling nodes. We make
790 * directories sort after non-directories, but otherwise sort in
791 * strcmp() order.
792 *
793 * Keep this in sync with dcmp() in create.c.
794 */
795 static int
796 nodecmp(const NODE *a, const NODE *b)
797 {
798
799 if ((a->type & F_DIR) != 0) {
800 if ((b->type & F_DIR) == 0)
801 return 1;
802 } else if ((b->type & F_DIR) != 0)
803 return -1;
804 return strcmp(a->name, b->name);
805 }
806