spec.c revision 1.69
1/* $NetBSD: spec.c,v 1.69 2009/04/03 21:18:59 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 68static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; 69#else 70__RCSID("$NetBSD: spec.c,v 1.69 2009/04/03 21:18:59 apb Exp $"); 71#endif 72#endif /* not lint */ 73 74#include <sys/param.h> 75#include <sys/stat.h> 76 77#include <ctype.h> 78#include <errno.h> 79#include <grp.h> 80#include <pwd.h> 81#include <stdio.h> 82#include <stdlib.h> 83#include <string.h> 84#include <unistd.h> 85#include <vis.h> 86#include <util.h> 87 88#include "extern.h" 89#include "pack_dev.h" 90 91size_t mtree_lineno; /* Current spec line number */ 92int mtree_Mflag; /* Merge duplicate entries */ 93int mtree_Wflag; /* Don't "whack" permissions */ 94 95static dev_t parsedev(char *); 96static void replacenode(NODE *, NODE *); 97static void set(char *, NODE *); 98static void unset(char *, NODE *); 99static void addchild(NODE *, NODE *); 100static int nodecmp(const NODE *, const NODE *); 101 102#define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0) 103 104NODE * 105spec(FILE *fp) 106{ 107 NODE *centry, *last, *pathparent, *cur; 108 char *p, *e, *next; 109 NODE ginfo, *root; 110 char *buf, *tname, *ntname; 111 size_t tnamelen, plen; 112 113 root = NULL; 114 centry = last = NULL; 115 tname = NULL; 116 tnamelen = 0; 117 memset(&ginfo, 0, sizeof(ginfo)); 118 for (mtree_lineno = 0; 119 (buf = fparseln(fp, NULL, &mtree_lineno, NULL, 120 FPARSELN_UNESCCOMM)); 121 free(buf)) { 122 /* Skip leading whitespace. */ 123 for (p = buf; *p && isspace((unsigned char)*p); ++p) 124 continue; 125 126 /* If nothing but whitespace, continue. */ 127 if (!*p) 128 continue; 129 130#ifdef DEBUG 131 fprintf(stderr, "line %lu: {%s}\n", 132 (u_long)mtree_lineno, p); 133#endif 134 /* Grab file name, "$", "set", or "unset". */ 135 next = buf; 136 while ((p = strsep(&next, " \t")) != NULL && *p == '\0') 137 continue; 138 if (p == NULL) 139 mtree_err("missing field"); 140 141 if (p[0] == '/') { 142 if (strcmp(p + 1, "set") == 0) 143 set(next, &ginfo); 144 else if (strcmp(p + 1, "unset") == 0) 145 unset(next, &ginfo); 146 else 147 mtree_err("invalid specification `%s'", p); 148 continue; 149 } 150 151 if (strcmp(p, "..") == 0) { 152 /* Don't go up, if haven't gone down. */ 153 if (root == NULL) 154 goto noparent; 155 if (last->type != F_DIR || last->flags & F_DONE) { 156 if (last == root) 157 goto noparent; 158 last = last->parent; 159 } 160 last->flags |= F_DONE; 161 continue; 162 163noparent: mtree_err("no parent node"); 164 } 165 166 plen = strlen(p) + 1; 167 if (plen > tnamelen) { 168 if ((ntname = realloc(tname, plen)) == NULL) 169 mtree_err("realloc: %s", strerror(errno)); 170 tname = ntname; 171 tnamelen = plen; 172 } 173 if (strunvis(tname, p) == -1) 174 mtree_err("strunvis failed on `%s'", p); 175 p = tname; 176 177 pathparent = NULL; 178 if (strchr(p, '/') != NULL) { 179 cur = root; 180 for (; (e = strchr(p, '/')) != NULL; p = e+1) { 181 if (p == e) 182 continue; /* handle // */ 183 *e = '\0'; 184 if (strcmp(p, ".") != 0) { 185 while (cur && 186 strcmp(cur->name, p) != 0) { 187 cur = cur->next; 188 } 189 } 190 if (cur == NULL || cur->type != F_DIR) { 191 mtree_err("%s: %s", tname, 192 strerror(ENOENT)); 193 } 194 *e = '/'; 195 pathparent = cur; 196 cur = cur->child; 197 } 198 if (*p == '\0') 199 mtree_err("%s: empty leaf element", tname); 200 } 201 202 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 203 mtree_err("%s", strerror(errno)); 204 *centry = ginfo; 205 centry->lineno = mtree_lineno; 206 strcpy(centry->name, p); 207#define MAGIC "?*[" 208 if (strpbrk(p, MAGIC)) 209 centry->flags |= F_MAGIC; 210 set(next, centry); 211 212 if (root == NULL) { 213 /* 214 * empty tree 215 */ 216 if (strcmp(centry->name, ".") != 0 || 217 centry->type != F_DIR) 218 mtree_err( 219 "root node must be the directory `.'"); 220 last = root = centry; 221 root->parent = root; 222 } else if (pathparent != NULL) { 223 /* 224 * full path entry; add or replace 225 */ 226 centry->parent = pathparent; 227 addchild(pathparent, centry); 228 last = centry; 229 } else if (strcmp(centry->name, ".") == 0) { 230 /* 231 * duplicate "." entry; always replace 232 */ 233 replacenode(root, centry); 234 } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 235 /* 236 * new relative child in current dir; 237 * add or replace 238 */ 239 centry->parent = last; 240 addchild(last, centry); 241 last = centry; 242 } else { 243 /* 244 * new relative child in parent dir 245 * (after encountering ".." entry); 246 * add or replace 247 */ 248 centry->parent = last->parent; 249 addchild(last->parent, centry); 250 last = centry; 251 } 252 } 253 return (root); 254} 255 256void 257free_nodes(NODE *root) 258{ 259 NODE *cur, *next; 260 261 if (root == NULL) 262 return; 263 264 next = NULL; 265 for (cur = root; cur != NULL; cur = next) { 266 next = cur->next; 267 free_nodes(cur->child); 268 REPLACEPTR(cur->slink, NULL); 269 REPLACEPTR(cur->md5digest, NULL); 270 REPLACEPTR(cur->rmd160digest, NULL); 271 REPLACEPTR(cur->sha1digest, NULL); 272 REPLACEPTR(cur->sha256digest, NULL); 273 REPLACEPTR(cur->sha384digest, NULL); 274 REPLACEPTR(cur->sha512digest, NULL); 275 REPLACEPTR(cur->tags, NULL); 276 REPLACEPTR(cur, NULL); 277 } 278} 279 280/* 281 * dump_nodes -- 282 * dump the NODEs from `cur', based in the directory `dir'. 283 * if pathlast is none zero, print the path last, otherwise print 284 * it first. 285 */ 286void 287dump_nodes(const char *dir, NODE *root, int pathlast) 288{ 289 NODE *cur; 290 char path[MAXPATHLEN]; 291 const char *name; 292 char *str; 293 294 for (cur = root; cur != NULL; cur = cur->next) { 295 if (cur->type != F_DIR && !matchtags(cur)) 296 continue; 297 298 if (snprintf(path, sizeof(path), "%s%s%s", 299 dir, *dir ? "/" : "", cur->name) 300 >= (int)sizeof(path)) 301 mtree_err("Pathname too long."); 302 303 if (!pathlast) 304 printf("%s ", vispath(path)); 305 306#define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 307 if (MATCHFLAG(F_TYPE)) 308 printf("type=%s ", nodetype(cur->type)); 309 if (MATCHFLAG(F_UID | F_UNAME)) { 310 if (keys & F_UNAME && 311 (name = user_from_uid(cur->st_uid, 1)) != NULL) 312 printf("uname=%s ", name); 313 else 314 printf("uid=%u ", cur->st_uid); 315 } 316 if (MATCHFLAG(F_GID | F_GNAME)) { 317 if (keys & F_GNAME && 318 (name = group_from_gid(cur->st_gid, 1)) != NULL) 319 printf("gname=%s ", name); 320 else 321 printf("gid=%u ", cur->st_gid); 322 } 323 if (MATCHFLAG(F_MODE)) 324 printf("mode=%#o ", cur->st_mode); 325 if (MATCHFLAG(F_DEV) && 326 (cur->type == F_BLOCK || cur->type == F_CHAR)) 327 printf("device=%#llx ", (long long)cur->st_rdev); 328 if (MATCHFLAG(F_NLINK)) 329 printf("nlink=%d ", cur->st_nlink); 330 if (MATCHFLAG(F_SLINK)) 331 printf("link=%s ", vispath(cur->slink)); 332 if (MATCHFLAG(F_SIZE)) 333 printf("size=%lld ", (long long)cur->st_size); 334 if (MATCHFLAG(F_TIME)) 335 printf("time=%lld.%ld ", 336 (long long)cur->st_mtimespec.tv_sec, 337 cur->st_mtimespec.tv_nsec); 338 if (MATCHFLAG(F_CKSUM)) 339 printf("cksum=%lu ", cur->cksum); 340 if (MATCHFLAG(F_MD5)) 341 printf("md5=%s ", cur->md5digest); 342 if (MATCHFLAG(F_RMD160)) 343 printf("rmd160=%s ", cur->rmd160digest); 344 if (MATCHFLAG(F_SHA1)) 345 printf("sha1=%s ", cur->sha1digest); 346 if (MATCHFLAG(F_SHA256)) 347 printf("sha256=%s ", cur->sha256digest); 348 if (MATCHFLAG(F_SHA384)) 349 printf("sha384=%s ", cur->sha384digest); 350 if (MATCHFLAG(F_SHA512)) 351 printf("sha512=%s ", cur->sha512digest); 352 if (MATCHFLAG(F_FLAGS)) { 353 str = flags_to_string(cur->st_flags, "none"); 354 printf("flags=%s ", str); 355 free(str); 356 } 357 if (MATCHFLAG(F_IGN)) 358 printf("ignore "); 359 if (MATCHFLAG(F_OPT)) 360 printf("optional "); 361 if (MATCHFLAG(F_TAGS)) 362 printf("tags=%s ", cur->tags); 363 puts(pathlast ? vispath(path) : ""); 364 365 if (cur->child) 366 dump_nodes(path, cur->child, pathlast); 367 } 368} 369 370/* 371 * vispath -- 372 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN 373 * characters long, and returns a pointer to a static buffer containing 374 * the result. 375 */ 376char * 377vispath(const char *path) 378{ 379 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' }; 380 static char pathbuf[4*MAXPATHLEN + 1]; 381 382 strsvis(pathbuf, path, VIS_CSTYLE, extra); 383 return(pathbuf); 384} 385 386 387static dev_t 388parsedev(char *arg) 389{ 390#define MAX_PACK_ARGS 3 391 u_long numbers[MAX_PACK_ARGS]; 392 char *p, *ep, *dev; 393 int argc; 394 pack_t *pack; 395 dev_t result; 396 const char *error = NULL; 397 398 if ((dev = strchr(arg, ',')) != NULL) { 399 *dev++='\0'; 400 if ((pack = pack_find(arg)) == NULL) 401 mtree_err("unknown format `%s'", arg); 402 argc = 0; 403 while ((p = strsep(&dev, ",")) != NULL) { 404 if (*p == '\0') 405 mtree_err("missing number"); 406 numbers[argc++] = strtoul(p, &ep, 0); 407 if (*ep != '\0') 408 mtree_err("invalid number `%s'", 409 p); 410 if (argc > MAX_PACK_ARGS) 411 mtree_err("too many arguments"); 412 } 413 if (argc < 2) 414 mtree_err("not enough arguments"); 415 result = (*pack)(argc, numbers, &error); 416 if (error != NULL) 417 mtree_err("%s", error); 418 } else { 419 result = (dev_t)strtoul(arg, &ep, 0); 420 if (*ep != '\0') 421 mtree_err("invalid device `%s'", arg); 422 } 423 return (result); 424} 425 426static void 427replacenode(NODE *cur, NODE *new) 428{ 429 430#define REPLACE(x) cur->x = new->x 431#define REPLACESTR(x) REPLACEPTR(cur->x,new->x) 432 433 if (cur->type != new->type) { 434 if (mtree_Mflag) { 435 /* 436 * merge entries with different types; we 437 * don't want children retained in this case. 438 */ 439 REPLACE(type); 440 free_nodes(cur->child); 441 cur->child = NULL; 442 } else { 443 mtree_err( 444 "existing entry for `%s', type `%s'" 445 " does not match type `%s'", 446 cur->name, nodetype(cur->type), 447 nodetype(new->type)); 448 } 449 } 450 451 REPLACE(st_size); 452 REPLACE(st_mtimespec); 453 REPLACESTR(slink); 454 if (cur->slink != NULL) { 455 if ((cur->slink = strdup(new->slink)) == NULL) 456 mtree_err("memory allocation error"); 457 if (strunvis(cur->slink, new->slink) == -1) 458 mtree_err("strunvis failed on `%s'", new->slink); 459 free(new->slink); 460 } 461 REPLACE(st_uid); 462 REPLACE(st_gid); 463 REPLACE(st_mode); 464 REPLACE(st_rdev); 465 REPLACE(st_flags); 466 REPLACE(st_nlink); 467 REPLACE(cksum); 468 REPLACESTR(md5digest); 469 REPLACESTR(rmd160digest); 470 REPLACESTR(sha1digest); 471 REPLACESTR(sha256digest); 472 REPLACESTR(sha384digest); 473 REPLACESTR(sha512digest); 474 REPLACESTR(tags); 475 REPLACE(lineno); 476 REPLACE(flags); 477 free(new); 478} 479 480static void 481set(char *t, NODE *ip) 482{ 483 int type, value, len; 484 gid_t gid; 485 uid_t uid; 486 char *kw, *val, *md, *ep; 487 void *m; 488 489 while ((kw = strsep(&t, "= \t")) != NULL) { 490 if (*kw == '\0') 491 continue; 492 if (strcmp(kw, "all") == 0) 493 mtree_err("invalid keyword `all'"); 494 ip->flags |= type = parsekey(kw, &value); 495 if (!value) 496 /* Just set flag bit (F_IGN and F_OPT) */ 497 continue; 498 while ((val = strsep(&t, " \t")) != NULL && *val == '\0') 499 continue; 500 if (val == NULL) 501 mtree_err("missing value"); 502 switch (type) { 503 case F_CKSUM: 504 ip->cksum = strtoul(val, &ep, 10); 505 if (*ep) 506 mtree_err("invalid checksum `%s'", val); 507 break; 508 case F_DEV: 509 ip->st_rdev = parsedev(val); 510 break; 511 case F_FLAGS: 512 if (strcmp("none", val) == 0) 513 ip->st_flags = 0; 514 else if (string_to_flags(&val, &ip->st_flags, NULL) 515 != 0) 516 mtree_err("invalid flag `%s'", val); 517 break; 518 case F_GID: 519 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 520 if (*ep) 521 mtree_err("invalid gid `%s'", val); 522 break; 523 case F_GNAME: 524 if (mtree_Wflag) /* don't parse if whacking */ 525 break; 526 if (gid_from_group(val, &gid) == -1) 527 mtree_err("unknown group `%s'", val); 528 ip->st_gid = gid; 529 break; 530 case F_MD5: 531 if (val[0]=='0' && val[1]=='x') 532 md=&val[2]; 533 else 534 md=val; 535 if ((ip->md5digest = strdup(md)) == NULL) 536 mtree_err("memory allocation error"); 537 break; 538 case F_MODE: 539 if ((m = setmode(val)) == NULL) 540 mtree_err("cannot set file mode `%s' (%s)", 541 val, strerror(errno)); 542 ip->st_mode = getmode(m, 0); 543 free(m); 544 break; 545 case F_NLINK: 546 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 547 if (*ep) 548 mtree_err("invalid link count `%s'", val); 549 break; 550 case F_RMD160: 551 if (val[0]=='0' && val[1]=='x') 552 md=&val[2]; 553 else 554 md=val; 555 if ((ip->rmd160digest = strdup(md)) == NULL) 556 mtree_err("memory allocation error"); 557 break; 558 case F_SHA1: 559 if (val[0]=='0' && val[1]=='x') 560 md=&val[2]; 561 else 562 md=val; 563 if ((ip->sha1digest = strdup(md)) == NULL) 564 mtree_err("memory allocation error"); 565 break; 566 case F_SIZE: 567 ip->st_size = (off_t)strtoll(val, &ep, 10); 568 if (*ep) 569 mtree_err("invalid size `%s'", val); 570 break; 571 case F_SLINK: 572 if ((ip->slink = strdup(val)) == NULL) 573 mtree_err("memory allocation error"); 574 if (strunvis(ip->slink, val) == -1) 575 mtree_err("strunvis failed on `%s'", val); 576 break; 577 case F_TAGS: 578 len = strlen(val) + 3; /* "," + str + ",\0" */ 579 if ((ip->tags = malloc(len)) == NULL) 580 mtree_err("memory allocation error"); 581 snprintf(ip->tags, len, ",%s,", val); 582 break; 583 case F_TIME: 584 ip->st_mtimespec.tv_sec = 585 (time_t)strtoll(val, &ep, 10); 586 if (*ep != '.') 587 mtree_err("invalid time `%s'", val); 588 val = ep + 1; 589 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10); 590 if (*ep) 591 mtree_err("invalid time `%s'", val); 592 break; 593 case F_TYPE: 594 ip->type = parsetype(val); 595 break; 596 case F_UID: 597 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 598 if (*ep) 599 mtree_err("invalid uid `%s'", val); 600 break; 601 case F_UNAME: 602 if (mtree_Wflag) /* don't parse if whacking */ 603 break; 604 if (uid_from_user(val, &uid) == -1) 605 mtree_err("unknown user `%s'", val); 606 ip->st_uid = uid; 607 break; 608 case F_SHA256: 609 if (val[0]=='0' && val[1]=='x') 610 md=&val[2]; 611 else 612 md=val; 613 if ((ip->sha256digest = strdup(md)) == NULL) 614 mtree_err("memory allocation error"); 615 break; 616 case F_SHA384: 617 if (val[0]=='0' && val[1]=='x') 618 md=&val[2]; 619 else 620 md=val; 621 if ((ip->sha384digest = strdup(md)) == NULL) 622 mtree_err("memory allocation error"); 623 break; 624 case F_SHA512: 625 if (val[0]=='0' && val[1]=='x') 626 md=&val[2]; 627 else 628 md=val; 629 if ((ip->sha512digest = strdup(md)) == NULL) 630 mtree_err("memory allocation error"); 631 break; 632 default: 633 mtree_err( 634 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 635 type); 636 /* NOTREACHED */ 637 } 638 } 639} 640 641static void 642unset(char *t, NODE *ip) 643{ 644 char *p; 645 646 while ((p = strsep(&t, " \t")) != NULL) { 647 if (*p == '\0') 648 continue; 649 ip->flags &= ~parsekey(p, NULL); 650 } 651} 652 653/* 654 * addchild -- 655 * Add the centry node as a child of the pathparent node. If 656 * centry is a duplicate, call replacenode(). If centry is not 657 * a duplicate, insert it into the linked list referenced by 658 * pathparent->child. Keep the list sorted. 659 */ 660static void 661addchild(NODE *pathparent, NODE *centry) 662{ 663 NODE *cur, *insertpos; 664 int cmp; 665 666 cur = pathparent->child; 667 if (cur == NULL) { 668 /* centry is pathparent's first and only child node so far */ 669 pathparent->child = centry; 670 return; 671 } 672 673 /* 674 * pathparent already has at least one other child, so add the 675 * centry node to the list. 676 * 677 * To keep the list sorted, the new centry node will be 678 * inserted just after the existing insertpos node, if any; 679 * otherwise it will be inserted at the start of the list. 680 */ 681 insertpos = NULL; 682 for (; cur != NULL; cur = cur->next) { 683 cmp = nodecmp(centry, cur); 684 if (cmp == 0) { 685 /* existing entry; replace */ 686 replacenode(cur, centry); 687 break; 688 } else if (cmp > 0) { 689 /* centry appears after cur in sort order */ 690 insertpos = cur; 691 } 692 if (cmp < 0 || cur->next == NULL) { 693 /* 694 * centry appears before cur in sort order, 695 * or we reached the end of the list; insert 696 * centry either just after insertpos, or at the 697 * beginning of the list. 698 */ 699 if (insertpos) { 700 centry->next = insertpos->next; 701 insertpos->next = centry; 702 if (centry->next) 703 centry->next->prev = centry; 704 } else { 705 centry->next = pathparent->child; 706 pathparent->child = centry; 707 } 708 break; 709 } 710 } 711 return; 712} 713 714/* 715 * nodecmp -- 716 * used as a comparison function by addchild() to control the order 717 * in which entries appear within a list of sibling nodes. We make 718 * directories sort after non-directories, but otherwise sort in 719 * strcmp() order. 720 * 721 * Keep this in sync with dcmp() in create.c. 722 */ 723static int 724nodecmp(const NODE *a, const NODE *b) 725{ 726 727 if ((a->type & F_DIR) != 0) { 728 if ((b->type & F_DIR) == 0) 729 return 1; 730 } else if ((b->type & F_DIR) != 0) 731 return -1; 732 return strcmp(a->name, b->name); 733} 734