Home | History | Annotate | Line # | Download | only in ls
ls.c revision 1.24
      1  1.24     lukem /*	$NetBSD: ls.c,v 1.24 1998/01/18 13:30:07 lukem 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.24     lukem __RCSID("$NetBSD: ls.c,v 1.24 1998/01/18 13:30:07 lukem 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.24     lukem int f_columnacross;		/* columnated format, sorted across */
     92   1.7   mycroft int f_flags;			/* show flags associated with a file */
     93   1.1       cgd int f_inode;			/* print inode */
     94   1.1       cgd int f_listdir;			/* list actual directory, not contents */
     95   1.1       cgd int f_listdot;			/* list files beginning with . */
     96   1.1       cgd int f_longform;			/* long listing format */
     97   1.1       cgd int f_newline;			/* if precede with newline */
     98   1.1       cgd int f_nonprint;			/* show unprintables as ? */
     99   1.1       cgd int f_nosort;			/* don't sort output */
    100  1.24     lukem int f_numericonly;		/* don't convert uid/gid to name */
    101   1.1       cgd int f_recursive;		/* ls subdirectories also */
    102   1.1       cgd int f_reversesort;		/* reverse whatever sort is used */
    103   1.1       cgd int f_sectime;			/* print the real time for all files */
    104   1.1       cgd int f_singlecol;		/* use single column output */
    105   1.1       cgd int f_size;			/* list size in short listing */
    106   1.1       cgd int f_statustime;		/* use time of last mode change */
    107   1.1       cgd int f_dirname;			/* if precede with directory name */
    108   1.1       cgd int f_type;			/* add type character for non-regular files */
    109  1.13   mycroft int f_whiteout;			/* show whiteout entries */
    110   1.1       cgd 
    111   1.7   mycroft int
    112   1.1       cgd main(argc, argv)
    113   1.1       cgd 	int argc;
    114   1.7   mycroft 	char *argv[];
    115   1.1       cgd {
    116   1.7   mycroft 	static char dot[] = ".", *dotav[] = { dot, NULL };
    117   1.1       cgd 	struct winsize win;
    118   1.7   mycroft 	int ch, fts_options, notused;
    119   1.7   mycroft 	int kflag = 0;
    120   1.7   mycroft 	char *p;
    121   1.7   mycroft 
    122   1.7   mycroft 	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
    123   1.7   mycroft 	if (isatty(STDOUT_FILENO)) {
    124  1.16       jtc 		if ((p = getenv("COLUMNS")) != NULL)
    125  1.16       jtc 			termwidth = atoi(p);
    126  1.16       jtc 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
    127  1.16       jtc 		    win.ws_col > 0)
    128   1.1       cgd 			termwidth = win.ws_col;
    129   1.7   mycroft 		f_column = f_nonprint = 1;
    130   1.1       cgd 	} else
    131   1.1       cgd 		f_singlecol = 1;
    132   1.1       cgd 
    133   1.7   mycroft 	/* Root is -A automatically. */
    134   1.1       cgd 	if (!getuid())
    135   1.1       cgd 		f_listdot = 1;
    136   1.1       cgd 
    137   1.7   mycroft 	fts_options = FTS_PHYSICAL;
    138  1.24     lukem 	while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklnoqrstux")) != -1) {
    139   1.1       cgd 		switch (ch) {
    140   1.1       cgd 		/*
    141  1.24     lukem 		 * The -1, -C, -l and -x options all override each other so
    142  1.24     lukem 		 * shell aliasing works correctly.
    143   1.1       cgd 		 */
    144   1.1       cgd 		case '1':
    145   1.1       cgd 			f_singlecol = 1;
    146  1.24     lukem 			f_column = f_columnacross = f_longform = 0;
    147   1.1       cgd 			break;
    148   1.1       cgd 		case 'C':
    149   1.1       cgd 			f_column = 1;
    150  1.24     lukem 			f_columnacross = f_longform = f_singlecol = 0;
    151   1.1       cgd 			break;
    152   1.1       cgd 		case 'l':
    153   1.1       cgd 			f_longform = 1;
    154  1.24     lukem 			f_column = f_columnacross = f_singlecol = 0;
    155  1.24     lukem 			break;
    156  1.24     lukem 		case 'x':
    157  1.24     lukem 			f_columnacross = 1;
    158  1.24     lukem 			f_column = f_longform = f_singlecol = 0;
    159   1.1       cgd 			break;
    160   1.7   mycroft 		/* The -c and -u options override each other. */
    161   1.1       cgd 		case 'c':
    162   1.1       cgd 			f_statustime = 1;
    163   1.1       cgd 			f_accesstime = 0;
    164   1.1       cgd 			break;
    165   1.1       cgd 		case 'u':
    166   1.1       cgd 			f_accesstime = 1;
    167   1.1       cgd 			f_statustime = 0;
    168   1.1       cgd 			break;
    169   1.1       cgd 		case 'F':
    170   1.1       cgd 			f_type = 1;
    171   1.1       cgd 			break;
    172   1.1       cgd 		case 'L':
    173   1.7   mycroft 			fts_options &= ~FTS_PHYSICAL;
    174   1.7   mycroft 			fts_options |= FTS_LOGICAL;
    175   1.1       cgd 			break;
    176   1.1       cgd 		case 'R':
    177   1.1       cgd 			f_recursive = 1;
    178   1.1       cgd 			break;
    179   1.1       cgd 		case 'a':
    180   1.7   mycroft 			fts_options |= FTS_SEEDOT;
    181   1.1       cgd 			/* FALLTHROUGH */
    182   1.1       cgd 		case 'A':
    183   1.1       cgd 			f_listdot = 1;
    184   1.1       cgd 			break;
    185   1.7   mycroft 		/* The -d option turns off the -R option. */
    186   1.1       cgd 		case 'd':
    187   1.1       cgd 			f_listdir = 1;
    188   1.7   mycroft 			f_recursive = 0;
    189   1.1       cgd 			break;
    190   1.1       cgd 		case 'f':
    191   1.1       cgd 			f_nosort = 1;
    192   1.1       cgd 			break;
    193   1.7   mycroft 		case 'g':		/* Compatibility with 4.3BSD. */
    194   1.1       cgd 			break;
    195   1.1       cgd 		case 'i':
    196   1.1       cgd 			f_inode = 1;
    197   1.1       cgd 			break;
    198   1.1       cgd 		case 'k':
    199   1.7   mycroft 			blocksize = 1024;
    200   1.7   mycroft 			kflag = 1;
    201   1.1       cgd 			break;
    202  1.24     lukem 		case 'n':
    203  1.24     lukem 			f_numericonly = 1;
    204  1.24     lukem 			break;
    205   1.7   mycroft 		case 'o':
    206   1.7   mycroft 			f_flags = 1;
    207   1.7   mycroft 			break;
    208   1.1       cgd 		case 'q':
    209   1.1       cgd 			f_nonprint = 1;
    210   1.1       cgd 			break;
    211   1.1       cgd 		case 'r':
    212   1.1       cgd 			f_reversesort = 1;
    213   1.1       cgd 			break;
    214   1.8   mycroft 		case 'S':
    215   1.8   mycroft 			sortkey = BY_SIZE;
    216   1.8   mycroft 			break;
    217   1.1       cgd 		case 's':
    218   1.1       cgd 			f_size = 1;
    219   1.1       cgd 			break;
    220   1.1       cgd 		case 'T':
    221   1.1       cgd 			f_sectime = 1;
    222   1.1       cgd 			break;
    223   1.1       cgd 		case 't':
    224   1.8   mycroft 			sortkey = BY_TIME;
    225   1.1       cgd 			break;
    226  1.13   mycroft 		case 'W':
    227  1.13   mycroft 			f_whiteout = 1;
    228  1.13   mycroft 			break;
    229   1.1       cgd 		default:
    230   1.1       cgd 		case '?':
    231   1.1       cgd 			usage();
    232   1.1       cgd 		}
    233   1.1       cgd 	}
    234   1.1       cgd 	argc -= optind;
    235   1.1       cgd 	argv += optind;
    236   1.1       cgd 
    237   1.7   mycroft 	/*
    238   1.8   mycroft 	 * If not -F, -i, -l, -S, -s or -t options, don't require stat
    239   1.7   mycroft 	 * information.
    240   1.7   mycroft 	 */
    241   1.8   mycroft 	if (!f_inode && !f_longform && !f_size && !f_type &&
    242   1.8   mycroft 	    sortkey == BY_NAME)
    243   1.7   mycroft 		fts_options |= FTS_NOSTAT;
    244   1.7   mycroft 
    245   1.7   mycroft 	/*
    246   1.7   mycroft 	 * If not -F, -d or -l options, follow any symbolic links listed on
    247   1.7   mycroft 	 * the command line.
    248   1.7   mycroft 	 */
    249   1.7   mycroft 	if (!f_longform && !f_listdir && !f_type)
    250   1.7   mycroft 		fts_options |= FTS_COMFOLLOW;
    251  1.13   mycroft 
    252  1.13   mycroft 	/*
    253  1.15       jtc 	 * If -W, show whiteout entries
    254  1.13   mycroft 	 */
    255  1.13   mycroft #ifdef FTS_WHITEOUT
    256  1.13   mycroft 	if (f_whiteout)
    257  1.13   mycroft 		fts_options |= FTS_WHITEOUT;
    258  1.13   mycroft #endif
    259   1.1       cgd 
    260   1.7   mycroft 	/* If -l or -s, figure out block size. */
    261   1.7   mycroft 	if (f_longform || f_size) {
    262   1.9       cgd 		if (!kflag)
    263   1.9       cgd 			(void)getbsize(&notused, &blocksize);
    264   1.7   mycroft 		blocksize /= 512;
    265   1.7   mycroft 	}
    266   1.1       cgd 
    267   1.7   mycroft 	/* Select a sort function. */
    268   1.1       cgd 	if (f_reversesort) {
    269   1.8   mycroft 		switch (sortkey) {
    270   1.8   mycroft 		case BY_NAME:
    271   1.1       cgd 			sortfcn = revnamecmp;
    272   1.8   mycroft 			break;
    273   1.8   mycroft 		case BY_SIZE:
    274   1.8   mycroft 			sortfcn = revsizecmp;
    275   1.8   mycroft 			break;
    276   1.8   mycroft 		case BY_TIME:
    277   1.8   mycroft 			if (f_accesstime)
    278   1.8   mycroft 				sortfcn = revacccmp;
    279   1.8   mycroft 			else if (f_statustime)
    280   1.8   mycroft 				sortfcn = revstatcmp;
    281  1.12   mycroft 			else /* Use modification time. */
    282   1.8   mycroft 				sortfcn = revmodcmp;
    283   1.8   mycroft 			break;
    284   1.8   mycroft 		}
    285   1.1       cgd 	} else {
    286   1.8   mycroft 		switch (sortkey) {
    287   1.8   mycroft 		case BY_NAME:
    288   1.1       cgd 			sortfcn = namecmp;
    289   1.8   mycroft 			break;
    290   1.8   mycroft 		case BY_SIZE:
    291   1.8   mycroft 			sortfcn = sizecmp;
    292   1.8   mycroft 			break;
    293   1.8   mycroft 		case BY_TIME:
    294   1.8   mycroft 			if (f_accesstime)
    295   1.8   mycroft 				sortfcn = acccmp;
    296   1.8   mycroft 			else if (f_statustime)
    297   1.8   mycroft 				sortfcn = statcmp;
    298  1.12   mycroft 			else /* Use modification time. */
    299   1.8   mycroft 				sortfcn = modcmp;
    300   1.8   mycroft 			break;
    301   1.8   mycroft 		}
    302   1.1       cgd 	}
    303   1.1       cgd 
    304   1.7   mycroft 	/* Select a print function. */
    305   1.1       cgd 	if (f_singlecol)
    306   1.1       cgd 		printfcn = printscol;
    307  1.24     lukem 	else if (f_columnacross)
    308  1.24     lukem 		printfcn = printacol;
    309   1.1       cgd 	else if (f_longform)
    310   1.1       cgd 		printfcn = printlong;
    311   1.1       cgd 	else
    312   1.1       cgd 		printfcn = printcol;
    313   1.1       cgd 
    314   1.7   mycroft 	if (argc)
    315   1.7   mycroft 		traverse(argc, argv, fts_options);
    316   1.7   mycroft 	else
    317   1.7   mycroft 		traverse(1, dotav, fts_options);
    318   1.1       cgd 	exit(0);
    319   1.1       cgd }
    320   1.1       cgd 
    321   1.7   mycroft static int output;			/* If anything output. */
    322   1.1       cgd 
    323   1.7   mycroft /*
    324   1.7   mycroft  * Traverse() walks the logical directory structure specified by the argv list
    325   1.7   mycroft  * in the order specified by the mastercmp() comparison function.  During the
    326   1.7   mycroft  * traversal it passes linked lists of structures to display() which represent
    327   1.7   mycroft  * a superset (may be exact set) of the files to be displayed.
    328   1.7   mycroft  */
    329   1.7   mycroft static void
    330   1.7   mycroft traverse(argc, argv, options)
    331   1.7   mycroft 	int argc, options;
    332   1.7   mycroft 	char *argv[];
    333   1.1       cgd {
    334  1.12   mycroft 	FTS *ftsp;
    335  1.12   mycroft 	FTSENT *p, *chp;
    336   1.7   mycroft 	int ch_options;
    337   1.7   mycroft 
    338   1.7   mycroft 	if ((ftsp =
    339   1.7   mycroft 	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
    340  1.19  christos 		err(1, "%s", "");
    341   1.7   mycroft 
    342   1.7   mycroft 	display(NULL, fts_children(ftsp, 0));
    343   1.7   mycroft 	if (f_listdir)
    344   1.7   mycroft 		return;
    345   1.1       cgd 
    346   1.1       cgd 	/*
    347   1.7   mycroft 	 * If not recursing down this tree and don't need stat info, just get
    348   1.7   mycroft 	 * the names.
    349   1.1       cgd 	 */
    350   1.7   mycroft 	ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
    351   1.1       cgd 
    352  1.12   mycroft 	while ((p = fts_read(ftsp)) != NULL)
    353  1.12   mycroft 		switch (p->fts_info) {
    354   1.7   mycroft 		case FTS_DC:
    355   1.9       cgd 			warnx("%s: directory causes a cycle", p->fts_name);
    356   1.7   mycroft 			break;
    357  1.12   mycroft 		case FTS_DNR:
    358  1.12   mycroft 		case FTS_ERR:
    359  1.12   mycroft 			warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
    360  1.12   mycroft 			break;
    361   1.7   mycroft 		case FTS_D:
    362   1.7   mycroft 			if (p->fts_level != FTS_ROOTLEVEL &&
    363   1.7   mycroft 			    p->fts_name[0] == '.' && !f_listdot)
    364   1.7   mycroft 				break;
    365   1.1       cgd 
    366   1.7   mycroft 			/*
    367   1.7   mycroft 			 * If already output something, put out a newline as
    368   1.7   mycroft 			 * a separator.  If multiple arguments, precede each
    369   1.7   mycroft 			 * directory with its name.
    370   1.7   mycroft 			 */
    371   1.7   mycroft 			if (output)
    372   1.7   mycroft 				(void)printf("\n%s:\n", p->fts_path);
    373   1.7   mycroft 			else if (argc > 1) {
    374   1.7   mycroft 				(void)printf("%s:\n", p->fts_path);
    375   1.7   mycroft 				output = 1;
    376   1.7   mycroft 			}
    377   1.1       cgd 
    378  1.12   mycroft 			chp = fts_children(ftsp, ch_options);
    379  1.12   mycroft 			display(p, chp);
    380   1.1       cgd 
    381  1.12   mycroft 			if (!f_recursive && chp != NULL)
    382   1.7   mycroft 				(void)fts_set(ftsp, p, FTS_SKIP);
    383   1.7   mycroft 			break;
    384   1.1       cgd 		}
    385  1.12   mycroft 	if (errno)
    386  1.12   mycroft 		err(1, "fts_read");
    387   1.1       cgd }
    388   1.1       cgd 
    389   1.7   mycroft /*
    390   1.7   mycroft  * Display() takes a linked list of FTSENT structures and passes the list
    391   1.7   mycroft  * along with any other necessary information to the print function.  P
    392   1.7   mycroft  * points to the parent directory of the display list.
    393   1.7   mycroft  */
    394   1.7   mycroft static void
    395   1.7   mycroft display(p, list)
    396  1.12   mycroft 	FTSENT *p, *list;
    397   1.1       cgd {
    398   1.7   mycroft 	struct stat *sp;
    399   1.7   mycroft 	DISPLAY d;
    400  1.12   mycroft 	FTSENT *cur;
    401   1.7   mycroft 	NAMES *np;
    402  1.12   mycroft 	u_quad_t maxsize;
    403   1.7   mycroft 	u_long btotal, maxblock, maxinode, maxlen, maxnlink;
    404  1.23   mycroft 	u_long maxmajor, maxminor;
    405   1.7   mycroft 	int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
    406   1.7   mycroft 	int entries, needstats;
    407  1.19  christos 	char *user, *group, buf[20];	/* 32 bits == 10 digits */
    408  1.24     lukem 	char nuser[12], ngroup[12];
    409  1.19  christos 	char *flags = NULL;	/* pacify gcc */
    410   1.1       cgd 
    411   1.7   mycroft 	/*
    412   1.7   mycroft 	 * If list is NULL there are two possibilities: that the parent
    413   1.7   mycroft 	 * directory p has no children, or that fts_children() returned an
    414  1.12   mycroft 	 * error.  We ignore the error case since it will be replicated
    415  1.12   mycroft 	 * on the next call to fts_read() on the post-order visit to the
    416  1.12   mycroft 	 * directory p, and will be signalled in traverse().
    417   1.7   mycroft 	 */
    418  1.12   mycroft 	if (list == NULL)
    419   1.7   mycroft 		return;
    420   1.1       cgd 
    421   1.7   mycroft 	needstats = f_inode || f_longform || f_size;
    422   1.7   mycroft 	flen = 0;
    423   1.7   mycroft 	btotal = maxblock = maxinode = maxlen = maxnlink = 0;
    424   1.7   mycroft 	bcfile = 0;
    425   1.7   mycroft 	maxuser = maxgroup = maxflags = 0;
    426   1.7   mycroft 	maxsize = 0;
    427  1.23   mycroft 	maxmajor = maxminor = 0;
    428   1.7   mycroft 	for (cur = list, entries = 0; cur; cur = cur->fts_link) {
    429   1.7   mycroft 		if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
    430  1.12   mycroft 			warnx("%s: %s",
    431  1.12   mycroft 			    cur->fts_name, strerror(cur->fts_errno));
    432   1.7   mycroft 			cur->fts_number = NO_PRINT;
    433   1.7   mycroft 			continue;
    434   1.7   mycroft 		}
    435   1.1       cgd 
    436   1.7   mycroft 		/*
    437   1.7   mycroft 		 * P is NULL if list is the argv list, to which different rules
    438   1.7   mycroft 		 * apply.
    439   1.7   mycroft 		 */
    440   1.7   mycroft 		if (p == NULL) {
    441   1.7   mycroft 			/* Directories will be displayed later. */
    442   1.7   mycroft 			if (cur->fts_info == FTS_D && !f_listdir) {
    443   1.7   mycroft 				cur->fts_number = NO_PRINT;
    444   1.1       cgd 				continue;
    445   1.7   mycroft 			}
    446   1.7   mycroft 		} else {
    447   1.7   mycroft 			/* Only display dot file if -a/-A set. */
    448   1.7   mycroft 			if (cur->fts_name[0] == '.' && !f_listdot) {
    449   1.7   mycroft 				cur->fts_number = NO_PRINT;
    450   1.1       cgd 				continue;
    451   1.7   mycroft 			}
    452   1.7   mycroft 		}
    453   1.7   mycroft 		if (f_nonprint)
    454   1.7   mycroft 			prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
    455   1.7   mycroft 		if (cur->fts_namelen > maxlen)
    456   1.7   mycroft 			maxlen = cur->fts_namelen;
    457   1.7   mycroft 		if (needstats) {
    458   1.7   mycroft 			sp = cur->fts_statp;
    459   1.7   mycroft 			if (sp->st_blocks > maxblock)
    460   1.7   mycroft 				maxblock = sp->st_blocks;
    461   1.7   mycroft 			if (sp->st_ino > maxinode)
    462   1.7   mycroft 				maxinode = sp->st_ino;
    463   1.7   mycroft 			if (sp->st_nlink > maxnlink)
    464   1.7   mycroft 				maxnlink = sp->st_nlink;
    465   1.7   mycroft 			if (sp->st_size > maxsize)
    466   1.7   mycroft 				maxsize = sp->st_size;
    467  1.23   mycroft 			if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
    468  1.23   mycroft 				bcfile = 1;
    469  1.23   mycroft 				if (major(sp->st_rdev) > maxmajor)
    470  1.23   mycroft 					maxmajor = major(sp->st_rdev);
    471  1.23   mycroft 				if (minor(sp->st_rdev) > maxminor)
    472  1.23   mycroft 					maxminor = minor(sp->st_rdev);
    473  1.23   mycroft 			}
    474   1.7   mycroft 
    475   1.7   mycroft 			btotal += sp->st_blocks;
    476   1.7   mycroft 			if (f_longform) {
    477  1.24     lukem 				if (f_numericonly) {
    478  1.24     lukem 					snprintf(nuser, 12, "%u", sp->st_uid);
    479  1.24     lukem 					snprintf(ngroup, 12, "%u", sp->st_gid);
    480  1.24     lukem 					user = nuser;
    481  1.24     lukem 					group = ngroup;
    482  1.24     lukem 				} else {
    483  1.24     lukem 					user = user_from_uid(sp->st_uid, 0);
    484  1.24     lukem 					group = group_from_gid(sp->st_gid, 0);
    485  1.24     lukem 				}
    486   1.7   mycroft 				if ((ulen = strlen(user)) > maxuser)
    487   1.7   mycroft 					maxuser = ulen;
    488   1.7   mycroft 				if ((glen = strlen(group)) > maxgroup)
    489   1.7   mycroft 					maxgroup = glen;
    490   1.7   mycroft 				if (f_flags) {
    491  1.12   mycroft 					flags =
    492  1.12   mycroft 					    flags_to_string(sp->st_flags, "-");
    493   1.7   mycroft 					if ((flen = strlen(flags)) > maxflags)
    494   1.7   mycroft 						maxflags = flen;
    495   1.7   mycroft 				} else
    496   1.7   mycroft 					flen = 0;
    497   1.7   mycroft 
    498   1.7   mycroft 				if ((np = malloc(sizeof(NAMES) +
    499   1.7   mycroft 				    ulen + glen + flen + 3)) == NULL)
    500  1.19  christos 					err(1, "%s", "");
    501   1.7   mycroft 
    502   1.7   mycroft 				np->user = &np->data[0];
    503   1.7   mycroft 				(void)strcpy(np->user, user);
    504   1.7   mycroft 				np->group = &np->data[ulen + 1];
    505   1.7   mycroft 				(void)strcpy(np->group, group);
    506   1.7   mycroft 
    507   1.7   mycroft 				if (f_flags) {
    508   1.7   mycroft 					np->flags = &np->data[ulen + glen + 2];
    509   1.7   mycroft 				  	(void)strcpy(np->flags, flags);
    510   1.7   mycroft 				}
    511   1.7   mycroft 				cur->fts_pointer = np;
    512   1.7   mycroft 			}
    513   1.1       cgd 		}
    514   1.7   mycroft 		++entries;
    515   1.1       cgd 	}
    516   1.1       cgd 
    517   1.7   mycroft 	if (!entries)
    518   1.1       cgd 		return;
    519   1.7   mycroft 
    520   1.7   mycroft 	d.list = list;
    521   1.7   mycroft 	d.entries = entries;
    522   1.7   mycroft 	d.maxlen = maxlen;
    523   1.7   mycroft 	if (needstats) {
    524   1.7   mycroft 		d.btotal = btotal;
    525   1.7   mycroft 		(void)snprintf(buf, sizeof(buf), "%lu", maxblock);
    526   1.7   mycroft 		d.s_block = strlen(buf);
    527   1.7   mycroft 		d.s_flags = maxflags;
    528   1.7   mycroft 		d.s_group = maxgroup;
    529   1.7   mycroft 		(void)snprintf(buf, sizeof(buf), "%lu", maxinode);
    530   1.7   mycroft 		d.s_inode = strlen(buf);
    531   1.7   mycroft 		(void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
    532   1.7   mycroft 		d.s_nlink = strlen(buf);
    533  1.22   thorpej 		(void)snprintf(buf, sizeof(buf), "%qu", (long long)maxsize);
    534   1.7   mycroft 		d.s_size = strlen(buf);
    535   1.7   mycroft 		d.s_user = maxuser;
    536  1.23   mycroft 		if (bcfile) {
    537  1.23   mycroft 			(void)snprintf(buf, sizeof(buf), "%lu", maxmajor);
    538  1.23   mycroft 			d.s_major = strlen(buf);
    539  1.23   mycroft 			(void)snprintf(buf, sizeof(buf), "%lu", maxminor);
    540  1.23   mycroft 			d.s_minor = strlen(buf);
    541  1.23   mycroft 			if (d.s_major + d.s_minor + 2 > d.s_size)
    542  1.23   mycroft 				d.s_size = d.s_major + d.s_minor + 2;
    543  1.23   mycroft 			else if (d.s_size - d.s_minor - 2 > d.s_major)
    544  1.23   mycroft 				d.s_major = d.s_size - d.s_minor - 2;
    545  1.23   mycroft 		} else {
    546  1.23   mycroft 			d.s_major = 0;
    547  1.23   mycroft 			d.s_minor = 0;
    548  1.23   mycroft 		}
    549   1.7   mycroft 	}
    550   1.7   mycroft 
    551   1.7   mycroft 	printfcn(&d);
    552   1.7   mycroft 	output = 1;
    553   1.7   mycroft 
    554   1.7   mycroft 	if (f_longform)
    555   1.7   mycroft 		for (cur = list; cur; cur = cur->fts_link)
    556   1.7   mycroft 			free(cur->fts_pointer);
    557   1.1       cgd }
    558   1.1       cgd 
    559   1.7   mycroft /*
    560   1.7   mycroft  * Ordering for mastercmp:
    561   1.7   mycroft  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
    562   1.7   mycroft  * as larger than directories.  Within either group, use the sort function.
    563   1.7   mycroft  * All other levels use the sort function.  Error entries remain unsorted.
    564   1.7   mycroft  */
    565   1.7   mycroft static int
    566   1.7   mycroft mastercmp(a, b)
    567   1.7   mycroft 	const FTSENT **a, **b;
    568   1.1       cgd {
    569  1.12   mycroft 	int a_info, b_info;
    570   1.1       cgd 
    571   1.7   mycroft 	a_info = (*a)->fts_info;
    572   1.7   mycroft 	if (a_info == FTS_ERR)
    573   1.7   mycroft 		return (0);
    574   1.7   mycroft 	b_info = (*b)->fts_info;
    575   1.7   mycroft 	if (b_info == FTS_ERR)
    576   1.7   mycroft 		return (0);
    577   1.7   mycroft 
    578   1.7   mycroft 	if (a_info == FTS_NS || b_info == FTS_NS)
    579  1.17   mycroft 		if (b_info != FTS_NS)
    580  1.17   mycroft 			return (1);
    581  1.17   mycroft 		else if (a_info != FTS_NS)
    582  1.17   mycroft 			return (-1);
    583  1.17   mycroft 		else
    584  1.18   mycroft 			return (namecmp(*a, *b));
    585   1.7   mycroft 
    586  1.24     lukem 	if (a_info != b_info && !f_listdir &&
    587  1.24     lukem 	    (*a)->fts_level == FTS_ROOTLEVEL) {
    588   1.7   mycroft 		if (a_info == FTS_D)
    589   1.7   mycroft 			return (1);
    590   1.7   mycroft 		else if (b_info == FTS_D)
    591   1.7   mycroft 			return (-1);
    592  1.24     lukem 	}
    593  1.24     lukem 	return (sortfcn(*a, *b));
    594   1.1       cgd }
    595