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