Home | History | Annotate | Line # | Download | only in ls
ls.c revision 1.70
      1  1.70       abs /*	$NetBSD: ls.c,v 1.70 2012/11/20 12:37:29 abs 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.50       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.64     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
     38  1.64     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42  1.14       cgd #if 0
     43  1.14       cgd static char sccsid[] = "@(#)ls.c	8.7 (Berkeley) 8/5/94";
     44  1.14       cgd #else
     45  1.70       abs __RCSID("$NetBSD: ls.c,v 1.70 2012/11/20 12:37:29 abs Exp $");
     46  1.14       cgd #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49  1.67     rmind #include <sys/param.h>
     50   1.7   mycroft #include <sys/types.h>
     51   1.1       cgd #include <sys/stat.h>
     52   1.1       cgd #include <sys/ioctl.h>
     53  1.12   mycroft 
     54   1.1       cgd #include <dirent.h>
     55  1.12   mycroft #include <err.h>
     56  1.12   mycroft #include <errno.h>
     57   1.7   mycroft #include <fts.h>
     58  1.39      tron #include <locale.h>
     59  1.12   mycroft #include <stdio.h>
     60   1.7   mycroft #include <stdlib.h>
     61   1.1       cgd #include <string.h>
     62  1.12   mycroft #include <unistd.h>
     63  1.33  christos #include <termios.h>
     64  1.21  christos #include <pwd.h>
     65  1.21  christos #include <grp.h>
     66  1.63        he #include <util.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.43     lukem static void	 display(FTSENT *, FTSENT *);
     72  1.43     lukem static int	 mastercmp(const FTSENT **, const FTSENT **);
     73  1.43     lukem static void	 traverse(int, char **, int);
     74   1.7   mycroft 
     75  1.43     lukem static void (*printfcn)(DISPLAY *);
     76  1.43     lukem static int (*sortfcn)(const FTSENT *, const FTSENT *);
     77   1.7   mycroft 
     78  1.12   mycroft #define	BY_NAME 0
     79  1.12   mycroft #define	BY_SIZE 1
     80   1.8   mycroft #define	BY_TIME	2
     81   1.8   mycroft 
     82   1.7   mycroft long blocksize;			/* block size units */
     83   1.1       cgd int termwidth = 80;		/* default terminal width */
     84   1.8   mycroft int sortkey = BY_NAME;
     85  1.37    simonb int rval = EXIT_SUCCESS;	/* exit value - set if error encountered */
     86   1.1       cgd 
     87   1.1       cgd /* flags */
     88   1.1       cgd int f_accesstime;		/* use time of last access */
     89   1.1       cgd int f_column;			/* columnated format */
     90  1.24     lukem int f_columnacross;		/* columnated format, sorted across */
     91   1.7   mycroft int f_flags;			/* show flags associated with a file */
     92  1.46     grant int f_grouponly;		/* long listing without owner */
     93  1.53     grant int f_humanize;			/* humanize the size field */
     94  1.68       erh int f_commas;           /* separate size field with comma */
     95   1.1       cgd int f_inode;			/* print inode */
     96   1.1       cgd int f_listdir;			/* list actual directory, not contents */
     97   1.1       cgd int f_listdot;			/* list files beginning with . */
     98   1.1       cgd int f_longform;			/* long listing format */
     99   1.1       cgd int f_nonprint;			/* show unprintables as ? */
    100   1.1       cgd int f_nosort;			/* don't sort output */
    101  1.24     lukem int f_numericonly;		/* don't convert uid/gid to name */
    102  1.52  jschauma int f_octal;			/* print octal escapes for nongraphic characters */
    103  1.52  jschauma int f_octal_escape;		/* like f_octal but use C escapes if possible */
    104   1.1       cgd int f_recursive;		/* ls subdirectories also */
    105   1.1       cgd int f_reversesort;		/* reverse whatever sort is used */
    106   1.1       cgd int f_sectime;			/* print the real time for all files */
    107   1.1       cgd int f_singlecol;		/* use single column output */
    108   1.1       cgd int f_size;			/* list size in short listing */
    109   1.1       cgd int f_statustime;		/* use time of last mode change */
    110  1.35    kleink int f_stream;			/* stream format */
    111   1.1       cgd int f_type;			/* add type character for non-regular files */
    112  1.36    kleink int f_typedir;			/* add type character for directories */
    113  1.13   mycroft int f_whiteout;			/* show whiteout entries */
    114   1.1       cgd 
    115  1.69     joerg __dead static void
    116  1.69     joerg usage(void)
    117  1.69     joerg {
    118  1.69     joerg 
    119  1.69     joerg 	(void)fprintf(stderr,
    120  1.70       abs 	    "usage: %s [-1AaBbCcdFfghikLlMmnopqRrSsTtuWwx] [file ...]\n",
    121  1.69     joerg 	    getprogname());
    122  1.69     joerg 	exit(EXIT_FAILURE);
    123  1.69     joerg 	/* NOTREACHED */
    124  1.69     joerg }
    125  1.69     joerg 
    126   1.7   mycroft int
    127  1.43     lukem ls_main(int argc, char *argv[])
    128   1.1       cgd {
    129   1.7   mycroft 	static char dot[] = ".", *dotav[] = { dot, NULL };
    130   1.1       cgd 	struct winsize win;
    131  1.49    simonb 	int ch, fts_options;
    132   1.7   mycroft 	int kflag = 0;
    133  1.27   mycroft 	const char *p;
    134  1.39      tron 
    135  1.56      hira 	setprogname(argv[0]);
    136  1.62  christos 	(void)setlocale(LC_ALL, "");
    137   1.7   mycroft 
    138   1.7   mycroft 	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
    139   1.7   mycroft 	if (isatty(STDOUT_FILENO)) {
    140  1.57  jschauma 		if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
    141  1.16       jtc 		    win.ws_col > 0)
    142   1.1       cgd 			termwidth = win.ws_col;
    143   1.7   mycroft 		f_column = f_nonprint = 1;
    144   1.1       cgd 	} else
    145   1.1       cgd 		f_singlecol = 1;
    146   1.1       cgd 
    147   1.7   mycroft 	/* Root is -A automatically. */
    148   1.1       cgd 	if (!getuid())
    149   1.1       cgd 		f_listdot = 1;
    150   1.1       cgd 
    151   1.7   mycroft 	fts_options = FTS_PHYSICAL;
    152  1.68       erh 	while ((ch = getopt(argc, argv, "1ABCFLMRSTWabcdfghiklmnopqrstuwx")) != -1) {
    153   1.1       cgd 		switch (ch) {
    154   1.1       cgd 		/*
    155  1.35    kleink 		 * The -1, -C, -l, -m and -x options all override each other so
    156  1.24     lukem 		 * shell aliasing works correctly.
    157   1.1       cgd 		 */
    158   1.1       cgd 		case '1':
    159   1.1       cgd 			f_singlecol = 1;
    160  1.35    kleink 			f_column = f_columnacross = f_longform = f_stream = 0;
    161   1.1       cgd 			break;
    162   1.1       cgd 		case 'C':
    163   1.1       cgd 			f_column = 1;
    164  1.35    kleink 			f_columnacross = f_longform = f_singlecol = f_stream =
    165  1.35    kleink 			    0;
    166   1.1       cgd 			break;
    167  1.46     grant 		case 'g':
    168  1.48    kleink 			if (f_grouponly != -1)
    169  1.48    kleink 				f_grouponly = 1;
    170  1.48    kleink 			f_longform = 1;
    171  1.48    kleink 			f_column = f_columnacross = f_singlecol = f_stream = 0;
    172  1.48    kleink 			break;
    173   1.1       cgd 		case 'l':
    174   1.1       cgd 			f_longform = 1;
    175  1.35    kleink 			f_column = f_columnacross = f_singlecol = f_stream = 0;
    176  1.48    kleink 			/* Never let -g take precedence over -l. */
    177  1.48    kleink 			f_grouponly = -1;
    178  1.35    kleink 			break;
    179  1.35    kleink 		case 'm':
    180  1.35    kleink 			f_stream = 1;
    181  1.35    kleink 			f_column = f_columnacross = f_longform = f_singlecol =
    182  1.35    kleink 			    0;
    183  1.24     lukem 			break;
    184  1.24     lukem 		case 'x':
    185  1.24     lukem 			f_columnacross = 1;
    186  1.35    kleink 			f_column = f_longform = f_singlecol = f_stream = 0;
    187   1.1       cgd 			break;
    188   1.7   mycroft 		/* The -c and -u options override each other. */
    189   1.1       cgd 		case 'c':
    190   1.1       cgd 			f_statustime = 1;
    191   1.1       cgd 			f_accesstime = 0;
    192   1.1       cgd 			break;
    193   1.1       cgd 		case 'u':
    194   1.1       cgd 			f_accesstime = 1;
    195   1.1       cgd 			f_statustime = 0;
    196   1.1       cgd 			break;
    197   1.1       cgd 		case 'F':
    198   1.1       cgd 			f_type = 1;
    199   1.1       cgd 			break;
    200   1.1       cgd 		case 'L':
    201   1.7   mycroft 			fts_options &= ~FTS_PHYSICAL;
    202   1.7   mycroft 			fts_options |= FTS_LOGICAL;
    203   1.1       cgd 			break;
    204   1.1       cgd 		case 'R':
    205   1.1       cgd 			f_recursive = 1;
    206   1.1       cgd 			break;
    207   1.1       cgd 		case 'a':
    208   1.7   mycroft 			fts_options |= FTS_SEEDOT;
    209   1.1       cgd 			/* FALLTHROUGH */
    210   1.1       cgd 		case 'A':
    211   1.1       cgd 			f_listdot = 1;
    212   1.1       cgd 			break;
    213  1.58  jschauma 		/* The -B option turns off the -b, -q and -w options. */
    214  1.52  jschauma 		case 'B':
    215  1.52  jschauma 			f_nonprint = 0;
    216  1.52  jschauma 			f_octal = 1;
    217  1.52  jschauma 			f_octal_escape = 0;
    218  1.52  jschauma 			break;
    219  1.58  jschauma 		/* The -b option turns off the -B, -q and -w options. */
    220  1.51  jschauma 		case 'b':
    221  1.51  jschauma 			f_nonprint = 0;
    222  1.52  jschauma 			f_octal = 0;
    223  1.52  jschauma 			f_octal_escape = 1;
    224  1.51  jschauma 			break;
    225   1.7   mycroft 		/* The -d option turns off the -R option. */
    226   1.1       cgd 		case 'd':
    227   1.1       cgd 			f_listdir = 1;
    228   1.7   mycroft 			f_recursive = 0;
    229   1.1       cgd 			break;
    230   1.1       cgd 		case 'f':
    231   1.1       cgd 			f_nosort = 1;
    232   1.1       cgd 			break;
    233   1.1       cgd 		case 'i':
    234   1.1       cgd 			f_inode = 1;
    235   1.1       cgd 			break;
    236   1.1       cgd 		case 'k':
    237   1.7   mycroft 			blocksize = 1024;
    238   1.7   mycroft 			kflag = 1;
    239  1.60  christos 			f_humanize = 0;
    240   1.1       cgd 			break;
    241  1.58  jschauma 		/* The -h option forces all sizes to be measured in bytes. */
    242  1.53     grant 		case 'h':
    243  1.53     grant 			f_humanize = 1;
    244  1.60  christos 			kflag = 0;
    245  1.68       erh 			f_commas = 0;
    246  1.68       erh 			break;
    247  1.68       erh 		case 'M':
    248  1.68       erh 			f_humanize = 0;
    249  1.68       erh 			f_commas = 1;
    250  1.53     grant 			break;
    251  1.24     lukem 		case 'n':
    252  1.24     lukem 			f_numericonly = 1;
    253  1.65     lukem 			f_longform = 1;
    254  1.65     lukem 			f_column = f_columnacross = f_singlecol = f_stream = 0;
    255  1.24     lukem 			break;
    256   1.7   mycroft 		case 'o':
    257   1.7   mycroft 			f_flags = 1;
    258   1.7   mycroft 			break;
    259  1.36    kleink 		case 'p':
    260  1.36    kleink 			f_typedir = 1;
    261  1.36    kleink 			break;
    262  1.58  jschauma 		/* The -q option turns off the -B, -b and -w options. */
    263   1.1       cgd 		case 'q':
    264   1.1       cgd 			f_nonprint = 1;
    265  1.52  jschauma 			f_octal = 0;
    266  1.52  jschauma 			f_octal_escape = 0;
    267   1.1       cgd 			break;
    268   1.1       cgd 		case 'r':
    269   1.1       cgd 			f_reversesort = 1;
    270   1.1       cgd 			break;
    271   1.8   mycroft 		case 'S':
    272   1.8   mycroft 			sortkey = BY_SIZE;
    273   1.8   mycroft 			break;
    274   1.1       cgd 		case 's':
    275   1.1       cgd 			f_size = 1;
    276   1.1       cgd 			break;
    277   1.1       cgd 		case 'T':
    278   1.1       cgd 			f_sectime = 1;
    279   1.1       cgd 			break;
    280   1.1       cgd 		case 't':
    281   1.8   mycroft 			sortkey = BY_TIME;
    282   1.1       cgd 			break;
    283  1.13   mycroft 		case 'W':
    284  1.13   mycroft 			f_whiteout = 1;
    285  1.52  jschauma 			break;
    286  1.58  jschauma 		/* The -w option turns off the -B, -b and -q options. */
    287  1.52  jschauma 		case 'w':
    288  1.52  jschauma 			f_nonprint = 0;
    289  1.52  jschauma 			f_octal = 0;
    290  1.52  jschauma 			f_octal_escape = 0;
    291  1.13   mycroft 			break;
    292   1.1       cgd 		default:
    293   1.1       cgd 		case '?':
    294   1.1       cgd 			usage();
    295   1.1       cgd 		}
    296   1.1       cgd 	}
    297   1.1       cgd 	argc -= optind;
    298   1.1       cgd 	argv += optind;
    299  1.48    kleink 
    300  1.57  jschauma 	if (f_column || f_columnacross || f_stream) {
    301  1.57  jschauma 		if ((p = getenv("COLUMNS")) != NULL)
    302  1.57  jschauma 			termwidth = atoi(p);
    303  1.57  jschauma 	}
    304  1.57  jschauma 
    305  1.48    kleink 	/*
    306  1.48    kleink 	 * If both -g and -l options, let -l take precedence.
    307  1.48    kleink 	 */
    308  1.48    kleink 	if (f_grouponly == -1)
    309  1.48    kleink 		f_grouponly = 0;
    310   1.1       cgd 
    311   1.7   mycroft 	/*
    312  1.36    kleink 	 * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
    313   1.7   mycroft 	 * information.
    314   1.7   mycroft 	 */
    315  1.36    kleink 	if (!f_inode && !f_longform && !f_size && !f_type && !f_typedir &&
    316   1.8   mycroft 	    sortkey == BY_NAME)
    317   1.7   mycroft 		fts_options |= FTS_NOSTAT;
    318   1.7   mycroft 
    319   1.7   mycroft 	/*
    320   1.7   mycroft 	 * If not -F, -d or -l options, follow any symbolic links listed on
    321   1.7   mycroft 	 * the command line.
    322   1.7   mycroft 	 */
    323   1.7   mycroft 	if (!f_longform && !f_listdir && !f_type)
    324   1.7   mycroft 		fts_options |= FTS_COMFOLLOW;
    325  1.13   mycroft 
    326  1.13   mycroft 	/*
    327  1.15       jtc 	 * If -W, show whiteout entries
    328  1.13   mycroft 	 */
    329  1.13   mycroft #ifdef FTS_WHITEOUT
    330  1.13   mycroft 	if (f_whiteout)
    331  1.13   mycroft 		fts_options |= FTS_WHITEOUT;
    332  1.13   mycroft #endif
    333   1.1       cgd 
    334  1.70       abs 	/* If -i, -l, or -s, figure out block size. */
    335  1.45    simonb 	if (f_inode || f_longform || f_size) {
    336   1.9       cgd 		if (!kflag)
    337  1.49    simonb 			(void)getbsize(NULL, &blocksize);
    338   1.7   mycroft 		blocksize /= 512;
    339   1.7   mycroft 	}
    340   1.1       cgd 
    341   1.7   mycroft 	/* Select a sort function. */
    342   1.1       cgd 	if (f_reversesort) {
    343   1.8   mycroft 		switch (sortkey) {
    344   1.8   mycroft 		case BY_NAME:
    345   1.1       cgd 			sortfcn = revnamecmp;
    346   1.8   mycroft 			break;
    347   1.8   mycroft 		case BY_SIZE:
    348   1.8   mycroft 			sortfcn = revsizecmp;
    349   1.8   mycroft 			break;
    350   1.8   mycroft 		case BY_TIME:
    351   1.8   mycroft 			if (f_accesstime)
    352   1.8   mycroft 				sortfcn = revacccmp;
    353   1.8   mycroft 			else if (f_statustime)
    354   1.8   mycroft 				sortfcn = revstatcmp;
    355  1.12   mycroft 			else /* Use modification time. */
    356   1.8   mycroft 				sortfcn = revmodcmp;
    357   1.8   mycroft 			break;
    358   1.8   mycroft 		}
    359   1.1       cgd 	} else {
    360   1.8   mycroft 		switch (sortkey) {
    361   1.8   mycroft 		case BY_NAME:
    362   1.1       cgd 			sortfcn = namecmp;
    363   1.8   mycroft 			break;
    364   1.8   mycroft 		case BY_SIZE:
    365   1.8   mycroft 			sortfcn = sizecmp;
    366   1.8   mycroft 			break;
    367   1.8   mycroft 		case BY_TIME:
    368   1.8   mycroft 			if (f_accesstime)
    369   1.8   mycroft 				sortfcn = acccmp;
    370   1.8   mycroft 			else if (f_statustime)
    371   1.8   mycroft 				sortfcn = statcmp;
    372  1.12   mycroft 			else /* Use modification time. */
    373   1.8   mycroft 				sortfcn = modcmp;
    374   1.8   mycroft 			break;
    375   1.8   mycroft 		}
    376   1.1       cgd 	}
    377   1.1       cgd 
    378   1.7   mycroft 	/* Select a print function. */
    379   1.1       cgd 	if (f_singlecol)
    380   1.1       cgd 		printfcn = printscol;
    381  1.24     lukem 	else if (f_columnacross)
    382  1.24     lukem 		printfcn = printacol;
    383   1.1       cgd 	else if (f_longform)
    384   1.1       cgd 		printfcn = printlong;
    385  1.35    kleink 	else if (f_stream)
    386  1.35    kleink 		printfcn = printstream;
    387   1.1       cgd 	else
    388   1.1       cgd 		printfcn = printcol;
    389   1.1       cgd 
    390   1.7   mycroft 	if (argc)
    391   1.7   mycroft 		traverse(argc, argv, fts_options);
    392   1.7   mycroft 	else
    393   1.7   mycroft 		traverse(1, dotav, fts_options);
    394  1.62  christos 	return rval;
    395  1.30   mycroft 	/* NOTREACHED */
    396   1.1       cgd }
    397   1.1       cgd 
    398   1.7   mycroft static int output;			/* If anything output. */
    399   1.1       cgd 
    400   1.7   mycroft /*
    401   1.7   mycroft  * Traverse() walks the logical directory structure specified by the argv list
    402   1.7   mycroft  * in the order specified by the mastercmp() comparison function.  During the
    403   1.7   mycroft  * traversal it passes linked lists of structures to display() which represent
    404   1.7   mycroft  * a superset (may be exact set) of the files to be displayed.
    405   1.7   mycroft  */
    406   1.7   mycroft static void
    407  1.43     lukem traverse(int argc, char *argv[], int options)
    408   1.1       cgd {
    409  1.12   mycroft 	FTS *ftsp;
    410  1.12   mycroft 	FTSENT *p, *chp;
    411  1.59  christos 	int ch_options, error;
    412   1.7   mycroft 
    413   1.7   mycroft 	if ((ftsp =
    414   1.7   mycroft 	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
    415  1.40  drochner 		err(EXIT_FAILURE, NULL);
    416   1.7   mycroft 
    417   1.7   mycroft 	display(NULL, fts_children(ftsp, 0));
    418  1.59  christos 	if (f_listdir) {
    419  1.62  christos 		(void)fts_close(ftsp);
    420   1.7   mycroft 		return;
    421  1.59  christos 	}
    422   1.1       cgd 
    423   1.1       cgd 	/*
    424   1.7   mycroft 	 * If not recursing down this tree and don't need stat info, just get
    425   1.7   mycroft 	 * the names.
    426   1.1       cgd 	 */
    427   1.7   mycroft 	ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
    428   1.1       cgd 
    429  1.12   mycroft 	while ((p = fts_read(ftsp)) != NULL)
    430  1.12   mycroft 		switch (p->fts_info) {
    431   1.7   mycroft 		case FTS_DC:
    432   1.9       cgd 			warnx("%s: directory causes a cycle", p->fts_name);
    433   1.7   mycroft 			break;
    434  1.12   mycroft 		case FTS_DNR:
    435  1.12   mycroft 		case FTS_ERR:
    436  1.12   mycroft 			warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
    437  1.37    simonb 			rval = EXIT_FAILURE;
    438  1.12   mycroft 			break;
    439   1.7   mycroft 		case FTS_D:
    440   1.7   mycroft 			if (p->fts_level != FTS_ROOTLEVEL &&
    441   1.7   mycroft 			    p->fts_name[0] == '.' && !f_listdot)
    442   1.7   mycroft 				break;
    443   1.1       cgd 
    444   1.7   mycroft 			/*
    445   1.7   mycroft 			 * If already output something, put out a newline as
    446   1.7   mycroft 			 * a separator.  If multiple arguments, precede each
    447   1.7   mycroft 			 * directory with its name.
    448   1.7   mycroft 			 */
    449   1.7   mycroft 			if (output)
    450   1.7   mycroft 				(void)printf("\n%s:\n", p->fts_path);
    451   1.7   mycroft 			else if (argc > 1) {
    452   1.7   mycroft 				(void)printf("%s:\n", p->fts_path);
    453   1.7   mycroft 				output = 1;
    454   1.7   mycroft 			}
    455   1.1       cgd 
    456  1.12   mycroft 			chp = fts_children(ftsp, ch_options);
    457  1.12   mycroft 			display(p, chp);
    458   1.1       cgd 
    459  1.12   mycroft 			if (!f_recursive && chp != NULL)
    460   1.7   mycroft 				(void)fts_set(ftsp, p, FTS_SKIP);
    461   1.7   mycroft 			break;
    462   1.1       cgd 		}
    463  1.59  christos 	error = errno;
    464  1.62  christos 	(void)fts_close(ftsp);
    465  1.59  christos 	errno = error;
    466  1.12   mycroft 	if (errno)
    467  1.35    kleink 		err(EXIT_FAILURE, "fts_read");
    468   1.1       cgd }
    469   1.1       cgd 
    470   1.7   mycroft /*
    471   1.7   mycroft  * Display() takes a linked list of FTSENT structures and passes the list
    472   1.7   mycroft  * along with any other necessary information to the print function.  P
    473   1.7   mycroft  * points to the parent directory of the display list.
    474   1.7   mycroft  */
    475   1.7   mycroft static void
    476  1.43     lukem display(FTSENT *p, FTSENT *list)
    477   1.1       cgd {
    478   1.7   mycroft 	struct stat *sp;
    479   1.7   mycroft 	DISPLAY d;
    480  1.12   mycroft 	FTSENT *cur;
    481   1.7   mycroft 	NAMES *np;
    482  1.66     lukem 	u_int64_t btotal, stotal;
    483  1.66     lukem 	off_t maxsize;
    484  1.66     lukem 	blkcnt_t maxblock;
    485  1.62  christos 	ino_t maxinode;
    486  1.66     lukem 	int maxmajor, maxminor;
    487  1.66     lukem 	uint32_t maxnlink;
    488  1.66     lukem 	int bcfile, entries, flen, glen, ulen, maxflags, maxgroup;
    489  1.66     lukem 	unsigned int maxlen;
    490  1.26     lukem 	int maxuser, needstats;
    491  1.27   mycroft 	const char *user, *group;
    492  1.41     enami 	char buf[21];		/* 64 bits == 20 digits, +1 for NUL */
    493  1.24     lukem 	char nuser[12], ngroup[12];
    494  1.26     lukem 	char *flags = NULL;
    495  1.25   mycroft 
    496   1.7   mycroft 	/*
    497   1.7   mycroft 	 * If list is NULL there are two possibilities: that the parent
    498   1.7   mycroft 	 * directory p has no children, or that fts_children() returned an
    499  1.12   mycroft 	 * error.  We ignore the error case since it will be replicated
    500  1.12   mycroft 	 * on the next call to fts_read() on the post-order visit to the
    501  1.12   mycroft 	 * directory p, and will be signalled in traverse().
    502   1.7   mycroft 	 */
    503  1.12   mycroft 	if (list == NULL)
    504   1.7   mycroft 		return;
    505   1.1       cgd 
    506   1.7   mycroft 	needstats = f_inode || f_longform || f_size;
    507   1.7   mycroft 	flen = 0;
    508  1.26     lukem 	maxinode = maxnlink = 0;
    509   1.7   mycroft 	bcfile = 0;
    510  1.26     lukem 	maxuser = maxgroup = maxflags = maxlen = 0;
    511  1.53     grant 	btotal = stotal = maxblock = maxsize = 0;
    512  1.23   mycroft 	maxmajor = maxminor = 0;
    513   1.7   mycroft 	for (cur = list, entries = 0; cur; cur = cur->fts_link) {
    514   1.7   mycroft 		if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
    515  1.12   mycroft 			warnx("%s: %s",
    516  1.12   mycroft 			    cur->fts_name, strerror(cur->fts_errno));
    517   1.7   mycroft 			cur->fts_number = NO_PRINT;
    518  1.37    simonb 			rval = EXIT_FAILURE;
    519   1.7   mycroft 			continue;
    520   1.7   mycroft 		}
    521   1.1       cgd 
    522   1.7   mycroft 		/*
    523   1.7   mycroft 		 * P is NULL if list is the argv list, to which different rules
    524   1.7   mycroft 		 * apply.
    525   1.7   mycroft 		 */
    526   1.7   mycroft 		if (p == NULL) {
    527   1.7   mycroft 			/* Directories will be displayed later. */
    528   1.7   mycroft 			if (cur->fts_info == FTS_D && !f_listdir) {
    529   1.7   mycroft 				cur->fts_number = NO_PRINT;
    530   1.1       cgd 				continue;
    531   1.7   mycroft 			}
    532   1.7   mycroft 		} else {
    533   1.7   mycroft 			/* Only display dot file if -a/-A set. */
    534   1.7   mycroft 			if (cur->fts_name[0] == '.' && !f_listdot) {
    535   1.7   mycroft 				cur->fts_number = NO_PRINT;
    536   1.1       cgd 				continue;
    537   1.7   mycroft 			}
    538   1.7   mycroft 		}
    539   1.7   mycroft 		if (cur->fts_namelen > maxlen)
    540   1.7   mycroft 			maxlen = cur->fts_namelen;
    541   1.7   mycroft 		if (needstats) {
    542   1.7   mycroft 			sp = cur->fts_statp;
    543   1.7   mycroft 			if (sp->st_blocks > maxblock)
    544   1.7   mycroft 				maxblock = sp->st_blocks;
    545   1.7   mycroft 			if (sp->st_ino > maxinode)
    546   1.7   mycroft 				maxinode = sp->st_ino;
    547   1.7   mycroft 			if (sp->st_nlink > maxnlink)
    548   1.7   mycroft 				maxnlink = sp->st_nlink;
    549   1.7   mycroft 			if (sp->st_size > maxsize)
    550   1.7   mycroft 				maxsize = sp->st_size;
    551  1.23   mycroft 			if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
    552  1.23   mycroft 				bcfile = 1;
    553  1.23   mycroft 				if (major(sp->st_rdev) > maxmajor)
    554  1.23   mycroft 					maxmajor = major(sp->st_rdev);
    555  1.23   mycroft 				if (minor(sp->st_rdev) > maxminor)
    556  1.23   mycroft 					maxminor = minor(sp->st_rdev);
    557  1.23   mycroft 			}
    558   1.7   mycroft 
    559   1.7   mycroft 			btotal += sp->st_blocks;
    560  1.53     grant 			stotal += sp->st_size;
    561   1.7   mycroft 			if (f_longform) {
    562  1.41     enami 				if (f_numericonly ||
    563  1.41     enami 				    (user = user_from_uid(sp->st_uid, 0)) ==
    564  1.41     enami 				    NULL) {
    565  1.29   mycroft 					(void)snprintf(nuser, sizeof(nuser),
    566  1.29   mycroft 					    "%u", sp->st_uid);
    567  1.41     enami 					user = nuser;
    568  1.41     enami 				}
    569  1.41     enami 				if (f_numericonly ||
    570  1.41     enami 				    (group = group_from_gid(sp->st_gid, 0)) ==
    571  1.41     enami 				    NULL) {
    572  1.29   mycroft 					(void)snprintf(ngroup, sizeof(ngroup),
    573  1.29   mycroft 					    "%u", sp->st_gid);
    574  1.24     lukem 					group = ngroup;
    575  1.24     lukem 				}
    576   1.7   mycroft 				if ((ulen = strlen(user)) > maxuser)
    577   1.7   mycroft 					maxuser = ulen;
    578   1.7   mycroft 				if ((glen = strlen(group)) > maxgroup)
    579   1.7   mycroft 					maxgroup = glen;
    580   1.7   mycroft 				if (f_flags) {
    581  1.12   mycroft 					flags =
    582  1.62  christos 					    flags_to_string((u_long)sp->st_flags, "-");
    583   1.7   mycroft 					if ((flen = strlen(flags)) > maxflags)
    584   1.7   mycroft 						maxflags = flen;
    585   1.7   mycroft 				} else
    586   1.7   mycroft 					flen = 0;
    587   1.7   mycroft 
    588   1.7   mycroft 				if ((np = malloc(sizeof(NAMES) +
    589  1.61      elad 				    ulen + glen + flen + 2)) == NULL)
    590  1.40  drochner 					err(EXIT_FAILURE, NULL);
    591   1.7   mycroft 
    592   1.7   mycroft 				np->user = &np->data[0];
    593   1.7   mycroft 				(void)strcpy(np->user, user);
    594   1.7   mycroft 				np->group = &np->data[ulen + 1];
    595   1.7   mycroft 				(void)strcpy(np->group, group);
    596   1.7   mycroft 
    597   1.7   mycroft 				if (f_flags) {
    598   1.7   mycroft 					np->flags = &np->data[ulen + glen + 2];
    599   1.7   mycroft 				  	(void)strcpy(np->flags, flags);
    600  1.63        he 					free(flags);
    601   1.7   mycroft 				}
    602   1.7   mycroft 				cur->fts_pointer = np;
    603   1.7   mycroft 			}
    604   1.1       cgd 		}
    605   1.7   mycroft 		++entries;
    606   1.1       cgd 	}
    607   1.1       cgd 
    608   1.7   mycroft 	if (!entries)
    609   1.1       cgd 		return;
    610   1.7   mycroft 
    611   1.7   mycroft 	d.list = list;
    612   1.7   mycroft 	d.entries = entries;
    613   1.7   mycroft 	d.maxlen = maxlen;
    614   1.7   mycroft 	if (needstats) {
    615   1.7   mycroft 		d.btotal = btotal;
    616  1.53     grant 		d.stotal = stotal;
    617  1.55    simonb 		if (f_humanize) {
    618  1.55    simonb 			d.s_block = 4; /* min buf length for humanize_number */
    619  1.55    simonb 		} else {
    620  1.53     grant 			(void)snprintf(buf, sizeof(buf), "%llu",
    621  1.53     grant 			    (long long)howmany(maxblock, blocksize));
    622  1.53     grant 			d.s_block = strlen(buf);
    623  1.68       erh 			if (f_commas) /* allow for commas before every third digit */
    624  1.68       erh 				d.s_block += (d.s_block - 1) / 3;
    625  1.53     grant 		}
    626   1.7   mycroft 		d.s_flags = maxflags;
    627   1.7   mycroft 		d.s_group = maxgroup;
    628  1.62  christos 		(void)snprintf(buf, sizeof(buf), "%llu",
    629  1.62  christos 		    (unsigned long long)maxinode);
    630   1.7   mycroft 		d.s_inode = strlen(buf);
    631  1.26     lukem 		(void)snprintf(buf, sizeof(buf), "%u", maxnlink);
    632   1.7   mycroft 		d.s_nlink = strlen(buf);
    633  1.53     grant 		if (f_humanize) {
    634  1.54    simonb 			d.s_size = 4; /* min buf length for humanize_number */
    635  1.53     grant 		} else {
    636  1.54    simonb 			(void)snprintf(buf, sizeof(buf), "%llu",
    637  1.54    simonb 			    (long long)maxsize);
    638  1.53     grant 			d.s_size = strlen(buf);
    639  1.68       erh 			if (f_commas) /* allow for commas before every third digit */
    640  1.68       erh 				d.s_size += (d.s_size - 1) / 3;
    641  1.53     grant 		}
    642   1.7   mycroft 		d.s_user = maxuser;
    643  1.23   mycroft 		if (bcfile) {
    644  1.26     lukem 			(void)snprintf(buf, sizeof(buf), "%u", maxmajor);
    645  1.23   mycroft 			d.s_major = strlen(buf);
    646  1.26     lukem 			(void)snprintf(buf, sizeof(buf), "%u", maxminor);
    647  1.23   mycroft 			d.s_minor = strlen(buf);
    648  1.23   mycroft 			if (d.s_major + d.s_minor + 2 > d.s_size)
    649  1.23   mycroft 				d.s_size = d.s_major + d.s_minor + 2;
    650  1.23   mycroft 			else if (d.s_size - d.s_minor - 2 > d.s_major)
    651  1.23   mycroft 				d.s_major = d.s_size - d.s_minor - 2;
    652  1.23   mycroft 		} else {
    653  1.23   mycroft 			d.s_major = 0;
    654  1.23   mycroft 			d.s_minor = 0;
    655  1.23   mycroft 		}
    656   1.7   mycroft 	}
    657   1.7   mycroft 
    658   1.7   mycroft 	printfcn(&d);
    659   1.7   mycroft 	output = 1;
    660   1.7   mycroft 
    661   1.7   mycroft 	if (f_longform)
    662   1.7   mycroft 		for (cur = list; cur; cur = cur->fts_link)
    663   1.7   mycroft 			free(cur->fts_pointer);
    664   1.1       cgd }
    665   1.1       cgd 
    666   1.7   mycroft /*
    667   1.7   mycroft  * Ordering for mastercmp:
    668   1.7   mycroft  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
    669   1.7   mycroft  * as larger than directories.  Within either group, use the sort function.
    670   1.7   mycroft  * All other levels use the sort function.  Error entries remain unsorted.
    671   1.7   mycroft  */
    672   1.7   mycroft static int
    673  1.43     lukem mastercmp(const FTSENT **a, const FTSENT **b)
    674   1.1       cgd {
    675  1.12   mycroft 	int a_info, b_info;
    676   1.1       cgd 
    677   1.7   mycroft 	a_info = (*a)->fts_info;
    678   1.7   mycroft 	if (a_info == FTS_ERR)
    679   1.7   mycroft 		return (0);
    680   1.7   mycroft 	b_info = (*b)->fts_info;
    681   1.7   mycroft 	if (b_info == FTS_ERR)
    682   1.7   mycroft 		return (0);
    683   1.7   mycroft 
    684  1.31   thorpej 	if (a_info == FTS_NS || b_info == FTS_NS) {
    685  1.17   mycroft 		if (b_info != FTS_NS)
    686  1.17   mycroft 			return (1);
    687  1.17   mycroft 		else if (a_info != FTS_NS)
    688  1.17   mycroft 			return (-1);
    689  1.17   mycroft 		else
    690  1.18   mycroft 			return (namecmp(*a, *b));
    691  1.31   thorpej 	}
    692   1.7   mycroft 
    693  1.24     lukem 	if (a_info != b_info && !f_listdir &&
    694  1.24     lukem 	    (*a)->fts_level == FTS_ROOTLEVEL) {
    695   1.7   mycroft 		if (a_info == FTS_D)
    696   1.7   mycroft 			return (1);
    697   1.7   mycroft 		else if (b_info == FTS_D)
    698   1.7   mycroft 			return (-1);
    699  1.24     lukem 	}
    700  1.24     lukem 	return (sortfcn(*a, *b));
    701   1.1       cgd }
    702