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