Home | History | Annotate | Line # | Download | only in ls
ls.c revision 1.23
      1  1.23   mycroft /*	$NetBSD: ls.c,v 1.23 1998/01/17 12:00:42 mycroft Exp $	*/
      2  1.14       cgd 
      3   1.1       cgd /*
      4  1.12   mycroft  * Copyright (c) 1989, 1993, 1994
      5  1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Michael Fischbein.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39  1.19  christos #include <sys/cdefs.h>
     40   1.1       cgd #ifndef lint
     41  1.19  christos __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
     42  1.19  christos 	The Regents of the University of California.  All rights reserved.\n");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #ifndef lint
     46  1.14       cgd #if 0
     47  1.14       cgd static char sccsid[] = "@(#)ls.c	8.7 (Berkeley) 8/5/94";
     48  1.14       cgd #else
     49  1.23   mycroft __RCSID("$NetBSD: ls.c,v 1.23 1998/01/17 12:00:42 mycroft Exp $");
     50  1.14       cgd #endif
     51   1.1       cgd #endif /* not lint */
     52   1.1       cgd 
     53   1.7   mycroft #include <sys/types.h>
     54   1.1       cgd #include <sys/stat.h>
     55   1.1       cgd #include <sys/ioctl.h>
     56  1.12   mycroft 
     57   1.1       cgd #include <dirent.h>
     58  1.12   mycroft #include <err.h>
     59  1.12   mycroft #include <errno.h>
     60   1.7   mycroft #include <fts.h>
     61  1.12   mycroft #include <stdio.h>
     62   1.7   mycroft #include <stdlib.h>
     63   1.1       cgd #include <string.h>
     64  1.12   mycroft #include <unistd.h>
     65  1.21  christos #include <pwd.h>
     66  1.21  christos #include <grp.h>
     67  1.12   mycroft 
     68   1.1       cgd #include "ls.h"
     69   1.7   mycroft #include "extern.h"
     70   1.1       cgd 
     71  1.19  christos int	main __P((int, char *[]));
     72   1.1       cgd 
     73   1.7   mycroft static void	 display __P((FTSENT *, FTSENT *));
     74   1.7   mycroft static int	 mastercmp __P((const FTSENT **, const FTSENT **));
     75   1.7   mycroft static void	 traverse __P((int, char **, int));
     76   1.7   mycroft 
     77   1.7   mycroft static void (*printfcn) __P((DISPLAY *));
     78   1.7   mycroft static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
     79   1.7   mycroft 
     80  1.12   mycroft #define	BY_NAME 0
     81  1.12   mycroft #define	BY_SIZE 1
     82   1.8   mycroft #define	BY_TIME	2
     83   1.8   mycroft 
     84   1.7   mycroft long blocksize;			/* block size units */
     85   1.1       cgd int termwidth = 80;		/* default terminal width */
     86   1.8   mycroft int sortkey = BY_NAME;
     87   1.1       cgd 
     88   1.1       cgd /* flags */
     89   1.1       cgd int f_accesstime;		/* use time of last access */
     90   1.1       cgd int f_column;			/* columnated format */
     91   1.7   mycroft int f_flags;			/* show flags associated with a file */
     92   1.1       cgd int f_inode;			/* print inode */
     93   1.1       cgd int f_listdir;			/* list actual directory, not contents */
     94   1.1       cgd int f_listdot;			/* list files beginning with . */
     95   1.1       cgd int f_longform;			/* long listing format */
     96   1.1       cgd int f_newline;			/* if precede with newline */
     97   1.1       cgd int f_nonprint;			/* show unprintables as ? */
     98   1.1       cgd int f_nosort;			/* don't sort output */
     99   1.1       cgd int f_recursive;		/* ls subdirectories also */
    100   1.1       cgd int f_reversesort;		/* reverse whatever sort is used */
    101   1.1       cgd int f_sectime;			/* print the real time for all files */
    102   1.1       cgd int f_singlecol;		/* use single column output */
    103   1.1       cgd int f_size;			/* list size in short listing */
    104   1.1       cgd int f_statustime;		/* use time of last mode change */
    105   1.1       cgd int f_dirname;			/* if precede with directory name */
    106   1.1       cgd int f_type;			/* add type character for non-regular files */
    107  1.13   mycroft int f_whiteout;			/* show whiteout entries */
    108   1.1       cgd 
    109   1.7   mycroft int
    110   1.1       cgd main(argc, argv)
    111   1.1       cgd 	int argc;
    112   1.7   mycroft 	char *argv[];
    113   1.1       cgd {
    114   1.7   mycroft 	static char dot[] = ".", *dotav[] = { dot, NULL };
    115   1.1       cgd 	struct winsize win;
    116   1.7   mycroft 	int ch, fts_options, notused;
    117   1.7   mycroft 	int kflag = 0;
    118   1.7   mycroft 	char *p;
    119   1.7   mycroft 
    120   1.7   mycroft 	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
    121   1.7   mycroft 	if (isatty(STDOUT_FILENO)) {
    122  1.16       jtc 		if ((p = getenv("COLUMNS")) != NULL)
    123  1.16       jtc 			termwidth = atoi(p);
    124  1.16       jtc 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
    125  1.16       jtc 		    win.ws_col > 0)
    126   1.1       cgd 			termwidth = win.ws_col;
    127   1.7   mycroft 		f_column = f_nonprint = 1;
    128   1.1       cgd 	} else
    129   1.1       cgd 		f_singlecol = 1;
    130   1.1       cgd 
    131   1.7   mycroft 	/* Root is -A automatically. */
    132   1.1       cgd 	if (!getuid())
    133   1.1       cgd 		f_listdot = 1;
    134   1.1       cgd 
    135   1.7   mycroft 	fts_options = FTS_PHYSICAL;
    136  1.13   mycroft 	while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgikloqrstu")) != -1) {
    137   1.1       cgd 		switch (ch) {
    138   1.1       cgd 		/*
    139   1.7   mycroft 		 * The -1, -C and -l options all override each other so shell
    140   1.7   mycroft 		 * aliasing works right.
    141   1.1       cgd 		 */
    142   1.1       cgd 		case '1':
    143   1.1       cgd 			f_singlecol = 1;
    144   1.1       cgd 			f_column = f_longform = 0;
    145   1.1       cgd 			break;
    146   1.1       cgd 		case 'C':
    147   1.1       cgd 			f_column = 1;
    148   1.1       cgd 			f_longform = f_singlecol = 0;
    149   1.1       cgd 			break;
    150   1.1       cgd 		case 'l':
    151   1.1       cgd 			f_longform = 1;
    152   1.1       cgd 			f_column = f_singlecol = 0;
    153   1.1       cgd 			break;
    154   1.7   mycroft 		/* The -c and -u options override each other. */
    155   1.1       cgd 		case 'c':
    156   1.1       cgd 			f_statustime = 1;
    157   1.1       cgd 			f_accesstime = 0;
    158   1.1       cgd 			break;
    159   1.1       cgd 		case 'u':
    160   1.1       cgd 			f_accesstime = 1;
    161   1.1       cgd 			f_statustime = 0;
    162   1.1       cgd 			break;
    163   1.1       cgd 		case 'F':
    164   1.1       cgd 			f_type = 1;
    165   1.1       cgd 			break;
    166   1.1       cgd 		case 'L':
    167   1.7   mycroft 			fts_options &= ~FTS_PHYSICAL;
    168   1.7   mycroft 			fts_options |= FTS_LOGICAL;
    169   1.1       cgd 			break;
    170   1.1       cgd 		case 'R':
    171   1.1       cgd 			f_recursive = 1;
    172   1.1       cgd 			break;
    173   1.1       cgd 		case 'a':
    174   1.7   mycroft 			fts_options |= FTS_SEEDOT;
    175   1.1       cgd 			/* FALLTHROUGH */
    176   1.1       cgd 		case 'A':
    177   1.1       cgd 			f_listdot = 1;
    178   1.1       cgd 			break;
    179   1.7   mycroft 		/* The -d option turns off the -R option. */
    180   1.1       cgd 		case 'd':
    181   1.1       cgd 			f_listdir = 1;
    182   1.7   mycroft 			f_recursive = 0;
    183   1.1       cgd 			break;
    184   1.1       cgd 		case 'f':
    185   1.1       cgd 			f_nosort = 1;
    186   1.1       cgd 			break;
    187   1.7   mycroft 		case 'g':		/* Compatibility with 4.3BSD. */
    188   1.1       cgd 			break;
    189   1.1       cgd 		case 'i':
    190   1.1       cgd 			f_inode = 1;
    191   1.1       cgd 			break;
    192   1.1       cgd 		case 'k':
    193   1.7   mycroft 			blocksize = 1024;
    194   1.7   mycroft 			kflag = 1;
    195   1.1       cgd 			break;
    196   1.7   mycroft 		case 'o':
    197   1.7   mycroft 			f_flags = 1;
    198   1.7   mycroft 			break;
    199   1.1       cgd 		case 'q':
    200   1.1       cgd 			f_nonprint = 1;
    201   1.1       cgd 			break;
    202   1.1       cgd 		case 'r':
    203   1.1       cgd 			f_reversesort = 1;
    204   1.1       cgd 			break;
    205   1.8   mycroft 		case 'S':
    206   1.8   mycroft 			sortkey = BY_SIZE;
    207   1.8   mycroft 			break;
    208   1.1       cgd 		case 's':
    209   1.1       cgd 			f_size = 1;
    210   1.1       cgd 			break;
    211   1.1       cgd 		case 'T':
    212   1.1       cgd 			f_sectime = 1;
    213   1.1       cgd 			break;
    214   1.1       cgd 		case 't':
    215   1.8   mycroft 			sortkey = BY_TIME;
    216   1.1       cgd 			break;
    217  1.13   mycroft 		case 'W':
    218  1.13   mycroft 			f_whiteout = 1;
    219  1.13   mycroft 			break;
    220   1.1       cgd 		default:
    221   1.1       cgd 		case '?':
    222   1.1       cgd 			usage();
    223   1.1       cgd 		}
    224   1.1       cgd 	}
    225   1.1       cgd 	argc -= optind;
    226   1.1       cgd 	argv += optind;
    227   1.1       cgd 
    228   1.7   mycroft 	/*
    229   1.8   mycroft 	 * If not -F, -i, -l, -S, -s or -t options, don't require stat
    230   1.7   mycroft 	 * information.
    231   1.7   mycroft 	 */
    232   1.8   mycroft 	if (!f_inode && !f_longform && !f_size && !f_type &&
    233   1.8   mycroft 	    sortkey == BY_NAME)
    234   1.7   mycroft 		fts_options |= FTS_NOSTAT;
    235   1.7   mycroft 
    236   1.7   mycroft 	/*
    237   1.7   mycroft 	 * If not -F, -d or -l options, follow any symbolic links listed on
    238   1.7   mycroft 	 * the command line.
    239   1.7   mycroft 	 */
    240   1.7   mycroft 	if (!f_longform && !f_listdir && !f_type)
    241   1.7   mycroft 		fts_options |= FTS_COMFOLLOW;
    242  1.13   mycroft 
    243  1.13   mycroft 	/*
    244  1.15       jtc 	 * If -W, show whiteout entries
    245  1.13   mycroft 	 */
    246  1.13   mycroft #ifdef FTS_WHITEOUT
    247  1.13   mycroft 	if (f_whiteout)
    248  1.13   mycroft 		fts_options |= FTS_WHITEOUT;
    249  1.13   mycroft #endif
    250   1.1       cgd 
    251   1.7   mycroft 	/* If -l or -s, figure out block size. */
    252   1.7   mycroft 	if (f_longform || f_size) {
    253   1.9       cgd 		if (!kflag)
    254   1.9       cgd 			(void)getbsize(&notused, &blocksize);
    255   1.7   mycroft 		blocksize /= 512;
    256   1.7   mycroft 	}
    257   1.1       cgd 
    258   1.7   mycroft 	/* Select a sort function. */
    259   1.1       cgd 	if (f_reversesort) {
    260   1.8   mycroft 		switch (sortkey) {
    261   1.8   mycroft 		case BY_NAME:
    262   1.1       cgd 			sortfcn = revnamecmp;
    263   1.8   mycroft 			break;
    264   1.8   mycroft 		case BY_SIZE:
    265   1.8   mycroft 			sortfcn = revsizecmp;
    266   1.8   mycroft 			break;
    267   1.8   mycroft 		case BY_TIME:
    268   1.8   mycroft 			if (f_accesstime)
    269   1.8   mycroft 				sortfcn = revacccmp;
    270   1.8   mycroft 			else if (f_statustime)
    271   1.8   mycroft 				sortfcn = revstatcmp;
    272  1.12   mycroft 			else /* Use modification time. */
    273   1.8   mycroft 				sortfcn = revmodcmp;
    274   1.8   mycroft 			break;
    275   1.8   mycroft 		}
    276   1.1       cgd 	} else {
    277   1.8   mycroft 		switch (sortkey) {
    278   1.8   mycroft 		case BY_NAME:
    279   1.1       cgd 			sortfcn = namecmp;
    280   1.8   mycroft 			break;
    281   1.8   mycroft 		case BY_SIZE:
    282   1.8   mycroft 			sortfcn = sizecmp;
    283   1.8   mycroft 			break;
    284   1.8   mycroft 		case BY_TIME:
    285   1.8   mycroft 			if (f_accesstime)
    286   1.8   mycroft 				sortfcn = acccmp;
    287   1.8   mycroft 			else if (f_statustime)
    288   1.8   mycroft 				sortfcn = statcmp;
    289  1.12   mycroft 			else /* Use modification time. */
    290   1.8   mycroft 				sortfcn = modcmp;
    291   1.8   mycroft 			break;
    292   1.8   mycroft 		}
    293   1.1       cgd 	}
    294   1.1       cgd 
    295   1.7   mycroft 	/* Select a print function. */
    296   1.1       cgd 	if (f_singlecol)
    297   1.1       cgd 		printfcn = printscol;
    298   1.1       cgd 	else if (f_longform)
    299   1.1       cgd 		printfcn = printlong;
    300   1.1       cgd 	else
    301   1.1       cgd 		printfcn = printcol;
    302   1.1       cgd 
    303   1.7   mycroft 	if (argc)
    304   1.7   mycroft 		traverse(argc, argv, fts_options);
    305   1.7   mycroft 	else
    306   1.7   mycroft 		traverse(1, dotav, fts_options);
    307   1.1       cgd 	exit(0);
    308   1.1       cgd }
    309   1.1       cgd 
    310   1.7   mycroft static int output;			/* If anything output. */
    311   1.1       cgd 
    312   1.7   mycroft /*
    313   1.7   mycroft  * Traverse() walks the logical directory structure specified by the argv list
    314   1.7   mycroft  * in the order specified by the mastercmp() comparison function.  During the
    315   1.7   mycroft  * traversal it passes linked lists of structures to display() which represent
    316   1.7   mycroft  * a superset (may be exact set) of the files to be displayed.
    317   1.7   mycroft  */
    318   1.7   mycroft static void
    319   1.7   mycroft traverse(argc, argv, options)
    320   1.7   mycroft 	int argc, options;
    321   1.7   mycroft 	char *argv[];
    322   1.1       cgd {
    323  1.12   mycroft 	FTS *ftsp;
    324  1.12   mycroft 	FTSENT *p, *chp;
    325   1.7   mycroft 	int ch_options;
    326   1.7   mycroft 
    327   1.7   mycroft 	if ((ftsp =
    328   1.7   mycroft 	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
    329  1.19  christos 		err(1, "%s", "");
    330   1.7   mycroft 
    331   1.7   mycroft 	display(NULL, fts_children(ftsp, 0));
    332   1.7   mycroft 	if (f_listdir)
    333   1.7   mycroft 		return;
    334   1.1       cgd 
    335   1.1       cgd 	/*
    336   1.7   mycroft 	 * If not recursing down this tree and don't need stat info, just get
    337   1.7   mycroft 	 * the names.
    338   1.1       cgd 	 */
    339   1.7   mycroft 	ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
    340   1.1       cgd 
    341  1.12   mycroft 	while ((p = fts_read(ftsp)) != NULL)
    342  1.12   mycroft 		switch (p->fts_info) {
    343   1.7   mycroft 		case FTS_DC:
    344   1.9       cgd 			warnx("%s: directory causes a cycle", p->fts_name);
    345   1.7   mycroft 			break;
    346  1.12   mycroft 		case FTS_DNR:
    347  1.12   mycroft 		case FTS_ERR:
    348  1.12   mycroft 			warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
    349  1.12   mycroft 			break;
    350   1.7   mycroft 		case FTS_D:
    351   1.7   mycroft 			if (p->fts_level != FTS_ROOTLEVEL &&
    352   1.7   mycroft 			    p->fts_name[0] == '.' && !f_listdot)
    353   1.7   mycroft 				break;
    354   1.1       cgd 
    355   1.7   mycroft 			/*
    356   1.7   mycroft 			 * If already output something, put out a newline as
    357   1.7   mycroft 			 * a separator.  If multiple arguments, precede each
    358   1.7   mycroft 			 * directory with its name.
    359   1.7   mycroft 			 */
    360   1.7   mycroft 			if (output)
    361   1.7   mycroft 				(void)printf("\n%s:\n", p->fts_path);
    362   1.7   mycroft 			else if (argc > 1) {
    363   1.7   mycroft 				(void)printf("%s:\n", p->fts_path);
    364   1.7   mycroft 				output = 1;
    365   1.7   mycroft 			}
    366   1.1       cgd 
    367  1.12   mycroft 			chp = fts_children(ftsp, ch_options);
    368  1.12   mycroft 			display(p, chp);
    369   1.1       cgd 
    370  1.12   mycroft 			if (!f_recursive && chp != NULL)
    371   1.7   mycroft 				(void)fts_set(ftsp, p, FTS_SKIP);
    372   1.7   mycroft 			break;
    373   1.1       cgd 		}
    374  1.12   mycroft 	if (errno)
    375  1.12   mycroft 		err(1, "fts_read");
    376   1.1       cgd }
    377   1.1       cgd 
    378   1.7   mycroft /*
    379   1.7   mycroft  * Display() takes a linked list of FTSENT structures and passes the list
    380   1.7   mycroft  * along with any other necessary information to the print function.  P
    381   1.7   mycroft  * points to the parent directory of the display list.
    382   1.7   mycroft  */
    383   1.7   mycroft static void
    384   1.7   mycroft display(p, list)
    385  1.12   mycroft 	FTSENT *p, *list;
    386   1.1       cgd {
    387   1.7   mycroft 	struct stat *sp;
    388   1.7   mycroft 	DISPLAY d;
    389  1.12   mycroft 	FTSENT *cur;
    390   1.7   mycroft 	NAMES *np;
    391  1.12   mycroft 	u_quad_t maxsize;
    392   1.7   mycroft 	u_long btotal, maxblock, maxinode, maxlen, maxnlink;
    393  1.23   mycroft 	u_long maxmajor, maxminor;
    394   1.7   mycroft 	int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
    395   1.7   mycroft 	int entries, needstats;
    396  1.19  christos 	char *user, *group, buf[20];	/* 32 bits == 10 digits */
    397  1.19  christos 	char *flags = NULL;	/* pacify gcc */
    398   1.1       cgd 
    399   1.7   mycroft 	/*
    400   1.7   mycroft 	 * If list is NULL there are two possibilities: that the parent
    401   1.7   mycroft 	 * directory p has no children, or that fts_children() returned an
    402  1.12   mycroft 	 * error.  We ignore the error case since it will be replicated
    403  1.12   mycroft 	 * on the next call to fts_read() on the post-order visit to the
    404  1.12   mycroft 	 * directory p, and will be signalled in traverse().
    405   1.7   mycroft 	 */
    406  1.12   mycroft 	if (list == NULL)
    407   1.7   mycroft 		return;
    408   1.1       cgd 
    409   1.7   mycroft 	needstats = f_inode || f_longform || f_size;
    410   1.7   mycroft 	flen = 0;
    411   1.7   mycroft 	btotal = maxblock = maxinode = maxlen = maxnlink = 0;
    412   1.7   mycroft 	bcfile = 0;
    413   1.7   mycroft 	maxuser = maxgroup = maxflags = 0;
    414   1.7   mycroft 	maxsize = 0;
    415  1.23   mycroft 	maxmajor = maxminor = 0;
    416   1.7   mycroft 	for (cur = list, entries = 0; cur; cur = cur->fts_link) {
    417   1.7   mycroft 		if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
    418  1.12   mycroft 			warnx("%s: %s",
    419  1.12   mycroft 			    cur->fts_name, strerror(cur->fts_errno));
    420   1.7   mycroft 			cur->fts_number = NO_PRINT;
    421   1.7   mycroft 			continue;
    422   1.7   mycroft 		}
    423   1.1       cgd 
    424   1.7   mycroft 		/*
    425   1.7   mycroft 		 * P is NULL if list is the argv list, to which different rules
    426   1.7   mycroft 		 * apply.
    427   1.7   mycroft 		 */
    428   1.7   mycroft 		if (p == NULL) {
    429   1.7   mycroft 			/* Directories will be displayed later. */
    430   1.7   mycroft 			if (cur->fts_info == FTS_D && !f_listdir) {
    431   1.7   mycroft 				cur->fts_number = NO_PRINT;
    432   1.1       cgd 				continue;
    433   1.7   mycroft 			}
    434   1.7   mycroft 		} else {
    435   1.7   mycroft 			/* Only display dot file if -a/-A set. */
    436   1.7   mycroft 			if (cur->fts_name[0] == '.' && !f_listdot) {
    437   1.7   mycroft 				cur->fts_number = NO_PRINT;
    438   1.1       cgd 				continue;
    439   1.7   mycroft 			}
    440   1.7   mycroft 		}
    441   1.7   mycroft 		if (f_nonprint)
    442   1.7   mycroft 			prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
    443   1.7   mycroft 		if (cur->fts_namelen > maxlen)
    444   1.7   mycroft 			maxlen = cur->fts_namelen;
    445   1.7   mycroft 		if (needstats) {
    446   1.7   mycroft 			sp = cur->fts_statp;
    447   1.7   mycroft 			if (sp->st_blocks > maxblock)
    448   1.7   mycroft 				maxblock = sp->st_blocks;
    449   1.7   mycroft 			if (sp->st_ino > maxinode)
    450   1.7   mycroft 				maxinode = sp->st_ino;
    451   1.7   mycroft 			if (sp->st_nlink > maxnlink)
    452   1.7   mycroft 				maxnlink = sp->st_nlink;
    453   1.7   mycroft 			if (sp->st_size > maxsize)
    454   1.7   mycroft 				maxsize = sp->st_size;
    455  1.23   mycroft 			if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
    456  1.23   mycroft 				bcfile = 1;
    457  1.23   mycroft 				if (major(sp->st_rdev) > maxmajor)
    458  1.23   mycroft 					maxmajor = major(sp->st_rdev);
    459  1.23   mycroft 				if (minor(sp->st_rdev) > maxminor)
    460  1.23   mycroft 					maxminor = minor(sp->st_rdev);
    461  1.23   mycroft 			}
    462   1.7   mycroft 
    463   1.7   mycroft 			btotal += sp->st_blocks;
    464   1.7   mycroft 			if (f_longform) {
    465   1.7   mycroft 				user = user_from_uid(sp->st_uid, 0);
    466   1.7   mycroft 				if ((ulen = strlen(user)) > maxuser)
    467   1.7   mycroft 					maxuser = ulen;
    468   1.7   mycroft 				group = group_from_gid(sp->st_gid, 0);
    469   1.7   mycroft 				if ((glen = strlen(group)) > maxgroup)
    470   1.7   mycroft 					maxgroup = glen;
    471   1.7   mycroft 				if (f_flags) {
    472  1.12   mycroft 					flags =
    473  1.12   mycroft 					    flags_to_string(sp->st_flags, "-");
    474   1.7   mycroft 					if ((flen = strlen(flags)) > maxflags)
    475   1.7   mycroft 						maxflags = flen;
    476   1.7   mycroft 				} else
    477   1.7   mycroft 					flen = 0;
    478   1.7   mycroft 
    479   1.7   mycroft 				if ((np = malloc(sizeof(NAMES) +
    480   1.7   mycroft 				    ulen + glen + flen + 3)) == NULL)
    481  1.19  christos 					err(1, "%s", "");
    482   1.7   mycroft 
    483   1.7   mycroft 				np->user = &np->data[0];
    484   1.7   mycroft 				(void)strcpy(np->user, user);
    485   1.7   mycroft 				np->group = &np->data[ulen + 1];
    486   1.7   mycroft 				(void)strcpy(np->group, group);
    487   1.7   mycroft 
    488   1.7   mycroft 				if (f_flags) {
    489   1.7   mycroft 					np->flags = &np->data[ulen + glen + 2];
    490   1.7   mycroft 				  	(void)strcpy(np->flags, flags);
    491   1.7   mycroft 				}
    492   1.7   mycroft 				cur->fts_pointer = np;
    493   1.7   mycroft 			}
    494   1.1       cgd 		}
    495   1.7   mycroft 		++entries;
    496   1.1       cgd 	}
    497   1.1       cgd 
    498   1.7   mycroft 	if (!entries)
    499   1.1       cgd 		return;
    500   1.7   mycroft 
    501   1.7   mycroft 	d.list = list;
    502   1.7   mycroft 	d.entries = entries;
    503   1.7   mycroft 	d.maxlen = maxlen;
    504   1.7   mycroft 	if (needstats) {
    505   1.7   mycroft 		d.btotal = btotal;
    506   1.7   mycroft 		(void)snprintf(buf, sizeof(buf), "%lu", maxblock);
    507   1.7   mycroft 		d.s_block = strlen(buf);
    508   1.7   mycroft 		d.s_flags = maxflags;
    509   1.7   mycroft 		d.s_group = maxgroup;
    510   1.7   mycroft 		(void)snprintf(buf, sizeof(buf), "%lu", maxinode);
    511   1.7   mycroft 		d.s_inode = strlen(buf);
    512   1.7   mycroft 		(void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
    513   1.7   mycroft 		d.s_nlink = strlen(buf);
    514  1.22   thorpej 		(void)snprintf(buf, sizeof(buf), "%qu", (long long)maxsize);
    515   1.7   mycroft 		d.s_size = strlen(buf);
    516   1.7   mycroft 		d.s_user = maxuser;
    517  1.23   mycroft 		if (bcfile) {
    518  1.23   mycroft 			(void)snprintf(buf, sizeof(buf), "%lu", maxmajor);
    519  1.23   mycroft 			d.s_major = strlen(buf);
    520  1.23   mycroft 			(void)snprintf(buf, sizeof(buf), "%lu", maxminor);
    521  1.23   mycroft 			d.s_minor = strlen(buf);
    522  1.23   mycroft 			if (d.s_major + d.s_minor + 2 > d.s_size)
    523  1.23   mycroft 				d.s_size = d.s_major + d.s_minor + 2;
    524  1.23   mycroft 			else if (d.s_size - d.s_minor - 2 > d.s_major)
    525  1.23   mycroft 				d.s_major = d.s_size - d.s_minor - 2;
    526  1.23   mycroft 		} else {
    527  1.23   mycroft 			d.s_major = 0;
    528  1.23   mycroft 			d.s_minor = 0;
    529  1.23   mycroft 		}
    530   1.7   mycroft 	}
    531   1.7   mycroft 
    532   1.7   mycroft 	printfcn(&d);
    533   1.7   mycroft 	output = 1;
    534   1.7   mycroft 
    535   1.7   mycroft 	if (f_longform)
    536   1.7   mycroft 		for (cur = list; cur; cur = cur->fts_link)
    537   1.7   mycroft 			free(cur->fts_pointer);
    538   1.1       cgd }
    539   1.1       cgd 
    540   1.7   mycroft /*
    541   1.7   mycroft  * Ordering for mastercmp:
    542   1.7   mycroft  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
    543   1.7   mycroft  * as larger than directories.  Within either group, use the sort function.
    544   1.7   mycroft  * All other levels use the sort function.  Error entries remain unsorted.
    545   1.7   mycroft  */
    546   1.7   mycroft static int
    547   1.7   mycroft mastercmp(a, b)
    548   1.7   mycroft 	const FTSENT **a, **b;
    549   1.1       cgd {
    550  1.12   mycroft 	int a_info, b_info;
    551   1.1       cgd 
    552   1.7   mycroft 	a_info = (*a)->fts_info;
    553   1.7   mycroft 	if (a_info == FTS_ERR)
    554   1.7   mycroft 		return (0);
    555   1.7   mycroft 	b_info = (*b)->fts_info;
    556   1.7   mycroft 	if (b_info == FTS_ERR)
    557   1.7   mycroft 		return (0);
    558   1.7   mycroft 
    559   1.7   mycroft 	if (a_info == FTS_NS || b_info == FTS_NS)
    560  1.17   mycroft 		if (b_info != FTS_NS)
    561  1.17   mycroft 			return (1);
    562  1.17   mycroft 		else if (a_info != FTS_NS)
    563  1.17   mycroft 			return (-1);
    564  1.17   mycroft 		else
    565  1.18   mycroft 			return (namecmp(*a, *b));
    566   1.7   mycroft 
    567   1.7   mycroft 	if (a_info == b_info)
    568   1.7   mycroft 		return (sortfcn(*a, *b));
    569   1.7   mycroft 
    570   1.7   mycroft 	if ((*a)->fts_level == FTS_ROOTLEVEL)
    571   1.7   mycroft 		if (a_info == FTS_D)
    572   1.7   mycroft 			return (1);
    573   1.7   mycroft 		else if (b_info == FTS_D)
    574   1.7   mycroft 			return (-1);
    575   1.1       cgd 		else
    576   1.7   mycroft 			return (sortfcn(*a, *b));
    577   1.7   mycroft 	else
    578   1.7   mycroft 		return (sortfcn(*a, *b));
    579   1.1       cgd }
    580