Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.69
      1  1.69  christos /*	$NetBSD: create.c,v 1.69 2013/02/03 19:15:17 christos Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*-
      4   1.8       cgd  * Copyright (c) 1989, 1993
      5   1.8       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.43       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.45       jmc #if HAVE_NBTOOL_CONFIG_H
     33  1.45       jmc #include "nbtool_config.h"
     34  1.45       jmc #endif
     35  1.45       jmc 
     36  1.13     lukem #include <sys/cdefs.h>
     37  1.39        tv #if defined(__RCSID) && !defined(lint)
     38   1.9       cgd #if 0
     39   1.8       cgd static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
     40   1.9       cgd #else
     41  1.69  christos __RCSID("$NetBSD: create.c,v 1.69 2013/02/03 19:15:17 christos Exp $");
     42   1.9       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46   1.1       cgd #include <sys/stat.h>
     47  1.29    simonb 
     48  1.44     lukem #if ! HAVE_NBTOOL_CONFIG_H
     49  1.13     lukem #include <dirent.h>
     50  1.38        tv #endif
     51  1.38        tv 
     52  1.13     lukem #include <errno.h>
     53   1.5       cgd #include <fcntl.h>
     54   1.5       cgd #include <grp.h>
     55   1.5       cgd #include <pwd.h>
     56  1.37     lukem #include <stdio.h>
     57  1.29    simonb #include <stdarg.h>
     58  1.35     lukem #include <stdlib.h>
     59  1.13     lukem #include <string.h>
     60  1.13     lukem #include <time.h>
     61   1.5       cgd #include <unistd.h>
     62  1.29    simonb 
     63  1.37     lukem #ifndef NO_MD5
     64  1.37     lukem #include <md5.h>
     65  1.37     lukem #endif
     66  1.37     lukem #ifndef NO_RMD160
     67  1.50  christos #include <rmd160.h>
     68  1.37     lukem #endif
     69  1.37     lukem #ifndef NO_SHA1
     70  1.37     lukem #include <sha1.h>
     71  1.37     lukem #endif
     72  1.47      elad #ifndef NO_SHA2
     73  1.50  christos #include <sha2.h>
     74  1.47      elad #endif
     75  1.37     lukem 
     76   1.5       cgd #include "extern.h"
     77   1.1       cgd 
     78   1.5       cgd #define	INDENTNAMELEN	15
     79   1.5       cgd #define	MAXLINELEN	80
     80   1.5       cgd 
     81   1.5       cgd static gid_t gid;
     82   1.5       cgd static uid_t uid;
     83   1.5       cgd static mode_t mode;
     84  1.18       mrg static u_long flags;
     85   1.5       cgd 
     86  1.61  christos #ifdef __FreeBSD__
     87  1.61  christos #define	FTS_CONST const
     88  1.61  christos #else
     89  1.61  christos #define	FTS_CONST
     90  1.61  christos #endif
     91  1.61  christos 
     92  1.61  christos static int	dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
     93  1.63  christos static void	output(int, int *, const char *, ...)
     94  1.63  christos 	__attribute__((__format__(__printf__, 3, 4)));
     95  1.29    simonb static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
     96  1.63  christos static void	statf(int, FTSENT *);
     97   1.1       cgd 
     98   1.5       cgd void
     99  1.29    simonb cwalk(void)
    100   1.1       cgd {
    101  1.13     lukem 	FTS *t;
    102  1.13     lukem 	FTSENT *p;
    103  1.33     lukem 	time_t clocktime;
    104  1.34     lukem 	char host[MAXHOSTNAMELEN + 1];
    105  1.55  christos 	const char *user;
    106  1.41     grant 	char *argv[2];
    107  1.41     grant 	char  dot[] = ".";
    108  1.63  christos 	int indent = 0;
    109  1.55  christos 
    110  1.41     grant 	argv[0] = dot;
    111  1.41     grant 	argv[1] = NULL;
    112   1.5       cgd 
    113  1.36     lukem 	time(&clocktime);
    114  1.36     lukem 	gethostname(host, sizeof(host));
    115  1.14       mrg 	host[sizeof(host) - 1] = '\0';
    116  1.55  christos 	if ((user = getlogin()) == NULL) {
    117  1.55  christos 		struct passwd *pw;
    118  1.55  christos 		user = (pw = getpwuid(getuid())) == NULL ? pw->pw_name :
    119  1.55  christos 		    "<unknown>";
    120  1.55  christos 	}
    121  1.55  christos 
    122  1.62  christos 	if (!nflag)
    123  1.62  christos 		printf(
    124  1.62  christos 	    	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n"
    125  1.62  christos 		    "#\t   date: %s",
    126  1.62  christos 		    user, host, fullpath, ctime(&clocktime));
    127   1.1       cgd 
    128  1.54    rillig 	if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
    129  1.17  wsanchez 		mtree_err("fts_open: %s", strerror(errno));
    130  1.36     lukem 	while ((p = fts_read(t)) != NULL) {
    131  1.63  christos 		if (jflag)
    132  1.63  christos 			indent = p->fts_level * 4;
    133  1.36     lukem 		if (check_excludes(p->fts_name, p->fts_path)) {
    134  1.36     lukem 			fts_set(t, p, FTS_SKIP);
    135  1.36     lukem 			continue;
    136  1.36     lukem 		}
    137  1.69  christos 		if (!find_only(p->fts_path)) {
    138  1.69  christos 			fts_set(t, p, FTS_SKIP);
    139  1.69  christos 			continue;
    140  1.69  christos 		}
    141   1.1       cgd 		switch(p->fts_info) {
    142   1.1       cgd 		case FTS_D:
    143  1.68  christos 			if (!bflag)
    144  1.68  christos 				printf("\n");
    145  1.62  christos 			if (!nflag)
    146  1.67  christos 				printf("# %s\n", p->fts_path);
    147  1.18       mrg 			statd(t, p, &uid, &gid, &mode, &flags);
    148  1.63  christos 			statf(indent, p);
    149   1.1       cgd 			break;
    150   1.1       cgd 		case FTS_DP:
    151  1.68  christos 			if (p->fts_level > 0)
    152  1.66  christos 				if (!nflag)
    153  1.66  christos 					printf("%*s# %s\n", indent, "",
    154  1.66  christos 					    p->fts_path);
    155  1.68  christos 			if (p->fts_level > 0 || flavor == F_FREEBSD9) {
    156  1.68  christos 				printf("%*s..\n", indent, "");
    157  1.68  christos 				if (!bflag)
    158  1.68  christos 					printf("\n");
    159  1.66  christos 			}
    160   1.5       cgd 			break;
    161   1.1       cgd 		case FTS_DNR:
    162   1.1       cgd 		case FTS_ERR:
    163   1.1       cgd 		case FTS_NS:
    164  1.36     lukem 			mtree_err("%s: %s",
    165  1.36     lukem 			    p->fts_path, strerror(p->fts_errno));
    166   1.5       cgd 			break;
    167   1.1       cgd 		default:
    168   1.5       cgd 			if (!dflag)
    169  1.63  christos 				statf(indent, p);
    170   1.5       cgd 			break;
    171  1.36     lukem 
    172   1.1       cgd 		}
    173  1.36     lukem 	}
    174  1.36     lukem 	fts_close(t);
    175   1.5       cgd 	if (sflag && keys & F_CKSUM)
    176  1.40     soren 		mtree_err("%s checksum: %u", fullpath, crc_total);
    177   1.5       cgd }
    178   1.1       cgd 
    179   1.5       cgd static void
    180  1.63  christos statf(int indent, FTSENT *p)
    181   1.5       cgd {
    182  1.17  wsanchez 	u_int32_t len, val;
    183  1.63  christos 	int fd, offset;
    184  1.65  christos 	const char *name = NULL;
    185  1.47      elad #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
    186  1.51      elad 	char *digestbuf;
    187  1.37     lukem #endif
    188   1.5       cgd 
    189  1.63  christos 	offset = printf("%*s%s%s", indent, "",
    190  1.46     lukem 	    S_ISDIR(p->fts_statp->st_mode) ? "" : "    ", vispath(p->fts_name));
    191   1.5       cgd 
    192  1.63  christos 	if (offset > (INDENTNAMELEN + indent))
    193  1.63  christos 		offset = MAXLINELEN;
    194   1.5       cgd 	else
    195  1.63  christos 		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
    196   1.5       cgd 
    197  1.68  christos 	if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
    198  1.63  christos 		output(indent, &offset, "type=%s",
    199  1.63  christos 		    inotype(p->fts_statp->st_mode));
    200  1.15      ross 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    201  1.30     lukem 		if (keys & F_UNAME &&
    202  1.30     lukem 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
    203  1.63  christos 			output(indent, &offset, "uname=%s", name);
    204  1.65  christos 		if (keys & F_UID || (keys & F_UNAME && name == NULL))
    205  1.63  christos 			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
    206  1.15      ross 	}
    207  1.15      ross 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    208  1.30     lukem 		if (keys & F_GNAME &&
    209  1.30     lukem 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
    210  1.63  christos 			output(indent, &offset, "gname=%s", name);
    211  1.65  christos 		if (keys & F_GID || (keys & F_GNAME && name == NULL))
    212  1.63  christos 			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
    213  1.15      ross 	}
    214   1.5       cgd 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    215  1.63  christos 		output(indent, &offset, "mode=%#o",
    216  1.63  christos 		    p->fts_statp->st_mode & MBITS);
    217  1.32     lukem 	if (keys & F_DEV &&
    218  1.32     lukem 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
    219  1.63  christos 		output(indent, &offset, "device=%#llx",
    220  1.56  christos 		    (long long)p->fts_statp->st_rdev);
    221   1.5       cgd 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    222  1.63  christos 		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
    223  1.68  christos 	if (keys & F_SIZE &&
    224  1.68  christos 	    (flavor != F_NETBSD6 || S_ISREG(p->fts_statp->st_mode)))
    225  1.63  christos 		output(indent, &offset, "size=%lld",
    226  1.63  christos 		    (long long)p->fts_statp->st_size);
    227  1.53    rillig 	if (keys & F_TIME)
    228  1.45       jmc #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
    229  1.64  christos 		output(indent, &offset, "time=%ld.%09ld",
    230  1.27        is 		    (long)p->fts_statp->st_mtimespec.tv_sec,
    231  1.10       jtc 		    p->fts_statp->st_mtimespec.tv_nsec);
    232  1.28   hubertf #else
    233  1.64  christos 		output(indent, &offset, "time=%ld.%09ld",
    234  1.52  christos 		    (long)p->fts_statp->st_mtime, (long)0);
    235  1.17  wsanchez #endif
    236   1.5       cgd 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
    237   1.5       cgd 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
    238   1.5       cgd 		    crc(fd, &val, &len))
    239  1.17  wsanchez 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
    240  1.36     lukem 		close(fd);
    241  1.63  christos 		output(indent, &offset, "cksum=%lu", (long)val);
    242  1.22   hubertf 	}
    243  1.37     lukem #ifndef NO_MD5
    244  1.22   hubertf 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
    245  1.51      elad 		if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
    246  1.61  christos 			mtree_err("%s: MD5File failed: %s", p->fts_accpath,
    247  1.61  christos 			    strerror(errno));
    248  1.63  christos 		output(indent, &offset, "%s=%s", MD5KEY, digestbuf);
    249  1.51      elad 		free(digestbuf);
    250  1.36     lukem 	}
    251  1.37     lukem #endif	/* ! NO_MD5 */
    252  1.37     lukem #ifndef NO_RMD160
    253  1.36     lukem 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
    254  1.51      elad 		if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
    255  1.61  christos 			mtree_err("%s: RMD160File failed: %s", p->fts_accpath,
    256  1.61  christos 			    strerror(errno));
    257  1.63  christos 		output(indent, &offset, "%s=%s", RMD160KEY, digestbuf);
    258  1.51      elad 		free(digestbuf);
    259  1.36     lukem 	}
    260  1.37     lukem #endif	/* ! NO_RMD160 */
    261  1.37     lukem #ifndef NO_SHA1
    262  1.36     lukem 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
    263  1.51      elad 		if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
    264  1.61  christos 			mtree_err("%s: SHA1File failed: %s", p->fts_accpath,
    265  1.61  christos 			    strerror(errno));
    266  1.63  christos 		output(indent, &offset, "%s=%s", SHA1KEY, digestbuf);
    267  1.51      elad 		free(digestbuf);
    268   1.1       cgd 	}
    269  1.37     lukem #endif	/* ! NO_SHA1 */
    270  1.47      elad #ifndef NO_SHA2
    271  1.47      elad 	if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
    272  1.51      elad 		if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
    273  1.61  christos 			mtree_err("%s: SHA256_File failed: %s", p->fts_accpath,
    274  1.61  christos 			    strerror(errno));
    275  1.63  christos 		output(indent, &offset, "%s=%s", SHA256KEY, digestbuf);
    276  1.51      elad 		free(digestbuf);
    277  1.47      elad 	}
    278  1.60  christos #ifdef SHA384_BLOCK_LENGTH
    279  1.47      elad 	if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
    280  1.51      elad 		if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
    281  1.61  christos 			mtree_err("%s: SHA384_File failed: %s", p->fts_accpath,
    282  1.61  christos 			    strerror(errno));
    283  1.63  christos 		output(indent, &offset, "%s=%s", SHA384KEY, digestbuf);
    284  1.51      elad 		free(digestbuf);
    285  1.47      elad 	}
    286  1.60  christos #endif
    287  1.47      elad 	if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
    288  1.51      elad 		if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
    289  1.61  christos 			mtree_err("%s: SHA512_File failed: %s", p->fts_accpath,
    290  1.61  christos 			    strerror(errno));
    291  1.63  christos 		output(indent, &offset, "%s=%s", SHA512KEY, digestbuf);
    292  1.51      elad 		free(digestbuf);
    293  1.47      elad 	}
    294  1.47      elad #endif	/* ! NO_SHA2 */
    295   1.5       cgd 	if (keys & F_SLINK &&
    296   1.5       cgd 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    297  1.63  christos 		output(indent, &offset, "link=%s",
    298  1.63  christos 		    vispath(rlink(p->fts_accpath)));
    299  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    300  1.59       spz 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
    301  1.59       spz 		char *str = flags_to_string(p->fts_statp->st_flags, "none");
    302  1.63  christos 		output(indent, &offset, "flags=%s", str);
    303  1.59       spz 		free(str);
    304  1.59       spz 	}
    305  1.38        tv #endif
    306  1.36     lukem 	putchar('\n');
    307   1.1       cgd }
    308   1.1       cgd 
    309  1.21       mrg /* XXX
    310  1.21       mrg  * FLAGS2INDEX will fail once the user and system settable bits need more
    311  1.21       mrg  * than one byte, respectively.
    312  1.21       mrg  */
    313  1.21       mrg #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
    314  1.21       mrg 
    315  1.19  christos #define	MTREE_MAXGID	5000
    316  1.19  christos #define	MTREE_MAXUID	5000
    317  1.21       mrg #define	MTREE_MAXMODE	(MBITS + 1)
    318  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    319  1.21       mrg #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
    320  1.38        tv #else
    321  1.38        tv #define MTREE_MAXFLAGS	1
    322  1.38        tv #endif
    323  1.19  christos #define	MTREE_MAXS 16
    324   1.1       cgd 
    325   1.5       cgd static int
    326  1.29    simonb statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
    327  1.29    simonb       u_long *pflags)
    328   1.1       cgd {
    329  1.13     lukem 	FTSENT *p;
    330  1.13     lukem 	gid_t sgid;
    331  1.13     lukem 	uid_t suid;
    332  1.13     lukem 	mode_t smode;
    333  1.38        tv 	u_long sflags = 0;
    334  1.65  christos 	const char *name = NULL;
    335   1.1       cgd 	gid_t savegid;
    336   1.1       cgd 	uid_t saveuid;
    337   1.1       cgd 	mode_t savemode;
    338  1.18       mrg 	u_long saveflags;
    339  1.18       mrg 	u_short maxgid, maxuid, maxmode, maxflags;
    340  1.19  christos 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
    341  1.19  christos 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
    342  1.36     lukem 	static int first = 1;
    343   1.1       cgd 
    344  1.36     lukem 	savegid = *pgid;
    345  1.36     lukem 	saveuid = *puid;
    346  1.36     lukem 	savemode = *pmode;
    347  1.36     lukem 	saveflags = *pflags;
    348   1.5       cgd 	if ((p = fts_children(t, 0)) == NULL) {
    349   1.5       cgd 		if (errno)
    350  1.17  wsanchez 			mtree_err("%s: %s", RP(parent), strerror(errno));
    351   1.5       cgd 		return (1);
    352   1.1       cgd 	}
    353   1.1       cgd 
    354  1.13     lukem 	memset(g, 0, sizeof(g));
    355  1.13     lukem 	memset(u, 0, sizeof(u));
    356  1.13     lukem 	memset(m, 0, sizeof(m));
    357  1.18       mrg 	memset(f, 0, sizeof(f));
    358   1.1       cgd 
    359  1.18       mrg 	maxuid = maxgid = maxmode = maxflags = 0;
    360   1.1       cgd 	for (; p; p = p->fts_link) {
    361  1.68  christos 		if (flavor == F_NETBSD6 || !dflag ||
    362  1.68  christos 		    (dflag && S_ISDIR(p->fts_statp->st_mode))) {
    363  1.68  christos 			smode = p->fts_statp->st_mode & MBITS;
    364  1.68  christos 			if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
    365  1.68  christos 				savemode = smode;
    366  1.68  christos 				maxmode = m[smode];
    367  1.68  christos 			}
    368  1.68  christos 			sgid = p->fts_statp->st_gid;
    369  1.68  christos 			if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
    370  1.68  christos 				savegid = sgid;
    371  1.68  christos 				maxgid = g[sgid];
    372  1.68  christos 			}
    373  1.68  christos 			suid = p->fts_statp->st_uid;
    374  1.68  christos 			if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
    375  1.68  christos 				saveuid = suid;
    376  1.68  christos 				maxuid = u[suid];
    377  1.68  christos 			}
    378  1.18       mrg 
    379  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    380  1.68  christos 			sflags = FLAGS2INDEX(p->fts_statp->st_flags);
    381  1.68  christos 			if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
    382  1.68  christos 				saveflags = p->fts_statp->st_flags;
    383  1.68  christos 				maxflags = f[sflags];
    384  1.68  christos 			}
    385  1.68  christos #endif
    386  1.18       mrg 		}
    387   1.1       cgd 	}
    388  1.36     lukem 	/*
    389  1.36     lukem 	 * If the /set record is the same as the last one we do not need to
    390  1.36     lukem 	 * output a new one.  So first we check to see if anything changed.
    391  1.36     lukem 	 * Note that we always output a /set record for the first directory.
    392  1.36     lukem 	 */
    393  1.36     lukem 	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
    394  1.36     lukem 	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
    395  1.36     lukem 	    ((keys & F_MODE) && (*pmode != savemode)) ||
    396  1.36     lukem 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
    397  1.36     lukem 	    first) {
    398  1.36     lukem 		first = 0;
    399  1.68  christos 		if (flavor != F_NETBSD6 && dflag)
    400  1.68  christos 			printf("/set type=dir");
    401  1.68  christos 		else
    402  1.68  christos 			printf("/set type=file");
    403  1.36     lukem 		if (keys & (F_UID | F_UNAME)) {
    404  1.36     lukem 			if (keys & F_UNAME &&
    405  1.36     lukem 			    (name = user_from_uid(saveuid, 1)) != NULL)
    406  1.36     lukem 				printf(" uname=%s", name);
    407  1.65  christos 			if (keys & F_UID || (keys & F_UNAME && name == NULL))
    408  1.36     lukem 				printf(" uid=%lu", (u_long)saveuid);
    409  1.36     lukem 		}
    410  1.36     lukem 		if (keys & (F_GID | F_GNAME)) {
    411  1.36     lukem 			if (keys & F_GNAME &&
    412  1.36     lukem 			    (name = group_from_gid(savegid, 1)) != NULL)
    413  1.36     lukem 				printf(" gname=%s", name);
    414  1.65  christos 			if (keys & F_GID || (keys & F_GNAME && name == NULL))
    415  1.36     lukem 				printf(" gid=%lu", (u_long)savegid);
    416  1.36     lukem 		}
    417  1.36     lukem 		if (keys & F_MODE)
    418  1.36     lukem 			printf(" mode=%#lo", (u_long)savemode);
    419  1.36     lukem 		if (keys & F_NLINK)
    420  1.36     lukem 			printf(" nlink=1");
    421  1.59       spz 		if (keys & F_FLAGS) {
    422  1.59       spz 			char *str = flags_to_string(saveflags, "none");
    423  1.59       spz 			printf(" flags=%s", str);
    424  1.59       spz 			free(str);
    425  1.59       spz 		}
    426  1.36     lukem 		printf("\n");
    427  1.36     lukem 		*puid = saveuid;
    428  1.36     lukem 		*pgid = savegid;
    429  1.36     lukem 		*pmode = savemode;
    430  1.36     lukem 		*pflags = saveflags;
    431  1.15      ross 	}
    432   1.5       cgd 	return (0);
    433   1.1       cgd }
    434   1.1       cgd 
    435  1.58       apb /*
    436  1.58       apb  * dcmp --
    437  1.58       apb  *	used as a comparison function passed to fts_open() to control
    438  1.58       apb  *	the order in which fts_read() returns results.	We make
    439  1.58       apb  *	directories sort after non-directories, but otherwise sort in
    440  1.58       apb  *	strcmp() order.
    441  1.58       apb  *
    442  1.58       apb  * Keep this in sync with nodecmp() in spec.c.
    443  1.58       apb  */
    444   1.5       cgd static int
    445  1.61  christos dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
    446   1.1       cgd {
    447  1.29    simonb 
    448   1.5       cgd 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    449   1.5       cgd 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    450   1.5       cgd 			return (1);
    451   1.5       cgd 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    452   1.5       cgd 		return (-1);
    453   1.5       cgd 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    454   1.5       cgd }
    455   1.1       cgd 
    456   1.5       cgd void
    457  1.63  christos output(int indent, int *offset, const char *fmt, ...)
    458   1.5       cgd {
    459   1.5       cgd 	va_list ap;
    460   1.5       cgd 	char buf[1024];
    461  1.29    simonb 
    462   1.5       cgd 	va_start(ap, fmt);
    463  1.36     lukem 	vsnprintf(buf, sizeof(buf), fmt, ap);
    464   1.5       cgd 	va_end(ap);
    465   1.5       cgd 
    466   1.8       cgd 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    467  1.63  christos 		printf(" \\\n%*s", INDENTNAMELEN + indent, "");
    468  1.63  christos 		*offset = INDENTNAMELEN + indent;
    469   1.5       cgd 	}
    470   1.5       cgd 	*offset += printf(" %s", buf) + 1;
    471   1.1       cgd }
    472