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