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