119844Sdist /*
266637Spendry * Copyright (c) 1989, 1993, 1994
361044Smckusick * The Regents of the University of California. All rights reserved.
438315Sbostic *
538315Sbostic * This code is derived from software contributed to Berkeley by
638315Sbostic * Michael Fischbein.
738315Sbostic *
840862Sbostic * %sccs.include.redist.c%
919844Sdist */
1038315Sbostic
1138315Sbostic #ifndef lint
1256274Selan static char copyright[] =
1366637Spendry "@(#) Copyright (c) 1989, 1993, 1994\n\
1461044Smckusick The Regents of the University of California. All rights reserved.\n";
1538315Sbostic #endif /* not lint */
1638315Sbostic
1738315Sbostic #ifndef lint
18*67594Smckusick static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 08/05/94";
1938315Sbostic #endif /* not lint */
2038315Sbostic
2152927Selan #include <sys/types.h>
2238313Sbostic #include <sys/stat.h>
2338323Sbostic #include <sys/ioctl.h>
2459500Sbostic
2552767Sbostic #include <dirent.h>
2659500Sbostic #include <err.h>
2759500Sbostic #include <errno.h>
2852260Selan #include <fts.h>
2959500Sbostic #include <stdio.h>
3051430Sbostic #include <stdlib.h>
3142014Sbostic #include <string.h>
3259500Sbostic #include <unistd.h>
3359500Sbostic
3438316Sbostic #include "ls.h"
3551430Sbostic #include "extern.h"
366034Swnj
3752927Selan static void display __P((FTSENT *, FTSENT *));
3852927Selan static int mastercmp __P((const FTSENT **, const FTSENT **));
3952927Selan static void traverse __P((int, char **, int));
4052927Selan
4152927Selan static void (*printfcn) __P((DISPLAY *));
4252260Selan static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
4352260Selan
4452927Selan long blocksize; /* block size units */
4538325Sbostic int termwidth = 80; /* default terminal width */
4638325Sbostic
4738315Sbostic /* flags */
4838315Sbostic int f_accesstime; /* use time of last access */
4938332Sbostic int f_column; /* columnated format */
5051901Smckusick int f_flags; /* show flags associated with a file */
5138315Sbostic int f_inode; /* print inode */
5238318Sbostic int f_listdir; /* list actual directory, not contents */
5338318Sbostic int f_listdot; /* list files beginning with . */
5438315Sbostic int f_longform; /* long listing format */
5538343Sbostic int f_newline; /* if precede with newline */
5638315Sbostic int f_nonprint; /* show unprintables as ? */
5738913Sbostic int f_nosort; /* don't sort output */
5838318Sbostic int f_recursive; /* ls subdirectories also */
5938315Sbostic int f_reversesort; /* reverse whatever sort is used */
6044940Sbostic int f_sectime; /* print the real time for all files */
6138318Sbostic int f_singlecol; /* use single column output */
6238315Sbostic int f_size; /* list size in short listing */
6338318Sbostic int f_statustime; /* use time of last mode change */
6438343Sbostic int f_dirname; /* if precede with directory name */
6538315Sbostic int f_timesort; /* sort by time vice name */
6638318Sbostic int f_type; /* add type character for non-regular files */
6767575Spendry int f_whiteout; /* show whiteout entries */
6838315Sbostic
6952289Sbostic int
main(argc,argv)7038313Sbostic main(argc, argv)
7138315Sbostic int argc;
7252260Selan char *argv[];
736034Swnj {
7452767Sbostic static char dot[] = ".", *dotav[] = { dot, NULL };
7538323Sbostic struct winsize win;
7652778Sbostic int ch, fts_options, notused;
7751430Sbostic char *p;
786034Swnj
7952260Selan /* Terminal defaults to -Cq, non-terminal defaults to -1. */
8052767Sbostic if (isatty(STDOUT_FILENO)) {
8152767Sbostic if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
8252767Sbostic !win.ws_col) {
8366559Spendry if ((p = getenv("COLUMNS")) != NULL)
8438325Sbostic termwidth = atoi(p);
8538325Sbostic }
8638323Sbostic else
8738323Sbostic termwidth = win.ws_col;
8852767Sbostic f_column = f_nonprint = 1;
8938315Sbostic } else
9038315Sbostic f_singlecol = 1;
9138313Sbostic
9252260Selan /* Root is -A automatically. */
9338315Sbostic if (!getuid())
9438316Sbostic f_listdot = 1;
9538315Sbostic
9652289Sbostic fts_options = FTS_PHYSICAL;
9767575Spendry while ((ch = getopt(argc, argv, "1ACFLRTWacdfgiloqrstu")) != EOF) {
9838315Sbostic switch (ch) {
9938318Sbostic /*
10052260Selan * The -1, -C and -l options all override each other so shell
10152260Selan * aliasing works right.
10238318Sbostic */
10338315Sbostic case '1':
10438315Sbostic f_singlecol = 1;
10538332Sbostic f_column = f_longform = 0;
10638315Sbostic break;
10738315Sbostic case 'C':
10838332Sbostic f_column = 1;
10938318Sbostic f_longform = f_singlecol = 0;
11038318Sbostic break;
11138318Sbostic case 'l':
11238318Sbostic f_longform = 1;
11338332Sbostic f_column = f_singlecol = 0;
11438315Sbostic break;
11552767Sbostic /* The -c and -u options override each other. */
11638318Sbostic case 'c':
11738318Sbostic f_statustime = 1;
11838318Sbostic f_accesstime = 0;
11938318Sbostic break;
12038318Sbostic case 'u':
12138318Sbostic f_accesstime = 1;
12238318Sbostic f_statustime = 0;
12338318Sbostic break;
12438315Sbostic case 'F':
12538315Sbostic f_type = 1;
12638315Sbostic break;
12738315Sbostic case 'L':
12852289Sbostic fts_options &= ~FTS_PHYSICAL;
12952289Sbostic fts_options |= FTS_LOGICAL;
13038315Sbostic break;
13138315Sbostic case 'R':
13238315Sbostic f_recursive = 1;
13338315Sbostic break;
13438313Sbostic case 'a':
13552260Selan fts_options |= FTS_SEEDOT;
13638315Sbostic /* FALLTHROUGH */
13731742Sbostic case 'A':
13838316Sbostic f_listdot = 1;
13938313Sbostic break;
14052767Sbostic /* The -d option turns off the -R option. */
1416034Swnj case 'd':
14238315Sbostic f_listdir = 1;
14352767Sbostic f_recursive = 0;
14438313Sbostic break;
14538913Sbostic case 'f':
14638913Sbostic f_nosort = 1;
14738913Sbostic break;
14852927Selan case 'g': /* Compatibility with 4.3BSD. */
14938313Sbostic break;
15031742Sbostic case 'i':
15138315Sbostic f_inode = 1;
15238313Sbostic break;
15351901Smckusick case 'o':
15451901Smckusick f_flags = 1;
15551901Smckusick break;
15631742Sbostic case 'q':
15738315Sbostic f_nonprint = 1;
15838313Sbostic break;
1596034Swnj case 'r':
16038315Sbostic f_reversesort = 1;
16138313Sbostic break;
16231742Sbostic case 's':
16338315Sbostic f_size = 1;
16438313Sbostic break;
16544940Sbostic case 'T':
16644940Sbostic f_sectime = 1;
16744940Sbostic break;
1686034Swnj case 't':
16938315Sbostic f_timesort = 1;
17038313Sbostic break;
17167575Spendry case 'W':
17267575Spendry f_whiteout = 1;
17367575Spendry break;
17438313Sbostic default:
17531742Sbostic case '?':
17638315Sbostic usage();
1776046Sroot }
17838315Sbostic }
17938316Sbostic argc -= optind;
18038316Sbostic argv += optind;
18138315Sbostic
18252677Sbostic /*
18352677Sbostic * If not -F, -i, -l, -s or -t options, don't require stat
18452661Selan * information.
18552661Selan */
18652677Sbostic if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
18752260Selan fts_options |= FTS_NOSTAT;
18838316Sbostic
18952346Sbostic /*
19052346Sbostic * If not -F, -d or -l options, follow any symbolic links listed on
19152346Sbostic * the command line.
19252346Sbostic */
19352346Sbostic if (!f_longform && !f_listdir && !f_type)
19452346Sbostic fts_options |= FTS_COMFOLLOW;
19552346Sbostic
19667575Spendry /*
19767575Spendry * If -W, show whiteout entries
19867575Spendry */
199*67594Smckusick #ifdef FTS_WHITEOUT
20067575Spendry if (f_whiteout)
20167575Spendry fts_options |= FTS_WHITEOUT;
202*67594Smckusick #endif
20367575Spendry
20452778Sbostic /* If -l or -s, figure out block size. */
20552778Sbostic if (f_longform || f_size) {
20659467Sbostic (void)getbsize(¬used, &blocksize);
20752778Sbostic blocksize /= 512;
20852778Sbostic }
20952778Sbostic
21052260Selan /* Select a sort function. */
21138315Sbostic if (f_reversesort) {
21238315Sbostic if (!f_timesort)
21338315Sbostic sortfcn = revnamecmp;
21438315Sbostic else if (f_accesstime)
21538315Sbostic sortfcn = revacccmp;
21638315Sbostic else if (f_statustime)
21738315Sbostic sortfcn = revstatcmp;
21852260Selan else /* Use modification time. */
21938318Sbostic sortfcn = revmodcmp;
22038313Sbostic } else {
22138315Sbostic if (!f_timesort)
22238315Sbostic sortfcn = namecmp;
22338315Sbostic else if (f_accesstime)
22438315Sbostic sortfcn = acccmp;
22538315Sbostic else if (f_statustime)
22638315Sbostic sortfcn = statcmp;
22752260Selan else /* Use modification time. */
22838318Sbostic sortfcn = modcmp;
22938315Sbostic }
2306034Swnj
23152260Selan /* Select a print function. */
23238323Sbostic if (f_singlecol)
23338323Sbostic printfcn = printscol;
23438323Sbostic else if (f_longform)
23538323Sbostic printfcn = printlong;
23638647Sbostic else
23738323Sbostic printfcn = printcol;
23838647Sbostic
23951588Sbostic if (argc)
24052260Selan traverse(argc, argv, fts_options);
24152767Sbostic else
24252260Selan traverse(1, dotav, fts_options);
24338315Sbostic exit(0);
2446034Swnj }
2456034Swnj
24652767Sbostic static int output; /* If anything output. */
24752767Sbostic
24852260Selan /*
24952260Selan * Traverse() walks the logical directory structure specified by the argv list
25052260Selan * in the order specified by the mastercmp() comparison function. During the
25152260Selan * traversal it passes linked lists of structures to display() which represent
25252260Selan * a superset (may be exact set) of the files to be displayed.
25352260Selan */
25452927Selan static void
traverse(argc,argv,options)25552260Selan traverse(argc, argv, options)
25652260Selan int argc, options;
25752260Selan char *argv[];
25852260Selan {
25966559Spendry FTS *ftsp;
26066559Spendry FTSENT *p, *chp;
26152767Sbostic int ch_options;
26252927Selan
26352260Selan if ((ftsp =
26452260Selan fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
26559500Sbostic err(1, NULL);
26652346Sbostic
26752767Sbostic display(NULL, fts_children(ftsp, 0));
26852260Selan if (f_listdir)
26952260Selan return;
27052767Sbostic
27152789Sbostic /*
27252789Sbostic * If not recursing down this tree and don't need stat info, just get
27352789Sbostic * the names.
27452789Sbostic */
27552767Sbostic ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
27652789Sbostic
27766559Spendry while ((p = fts_read(ftsp)) != NULL)
27859748Sbostic switch (p->fts_info) {
27952260Selan case FTS_DC:
28059500Sbostic warnx("%s: directory causes a cycle", p->fts_name);
28152260Selan break;
28252260Selan case FTS_DNR:
28352260Selan case FTS_ERR:
28466603Sbostic warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
28552260Selan break;
28652260Selan case FTS_D:
28752289Sbostic if (p->fts_level != FTS_ROOTLEVEL &&
28852289Sbostic p->fts_name[0] == '.' && !f_listdot)
28952289Sbostic break;
29052767Sbostic
29152767Sbostic /*
29252767Sbostic * If already output something, put out a newline as
29352767Sbostic * a separator. If multiple arguments, precede each
29452767Sbostic * directory with its name.
29552767Sbostic */
29652767Sbostic if (output)
29752767Sbostic (void)printf("\n%s:\n", p->fts_path);
29852767Sbostic else if (argc > 1) {
29952767Sbostic (void)printf("%s:\n", p->fts_path);
30052767Sbostic output = 1;
30152767Sbostic }
30252767Sbostic
30357570Selan chp = fts_children(ftsp, ch_options);
30457570Selan display(p, chp);
30552767Sbostic
30657570Selan if (!f_recursive && chp != NULL)
30752289Sbostic (void)fts_set(ftsp, p, FTS_SKIP);
30852260Selan break;
30952260Selan }
31066604Sbostic if (errno)
31166604Sbostic err(1, "fts_read");
31252260Selan }
31352260Selan
31452260Selan /*
31554542Sbostic * Display() takes a linked list of FTSENT structures and passes the list
31654542Sbostic * along with any other necessary information to the print function. P
31754542Sbostic * points to the parent directory of the display list.
31852260Selan */
31952927Selan static void
display(p,list)32052767Sbostic display(p, list)
32166559Spendry FTSENT *p, *list;
3226034Swnj {
32352927Selan struct stat *sp;
32452927Selan DISPLAY d;
32566559Spendry FTSENT *cur;
32652927Selan NAMES *np;
32766559Spendry u_quad_t maxsize;
32853808Sbostic u_long btotal, maxblock, maxinode, maxlen, maxnlink;
32954542Sbostic int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser;
33052927Selan int entries, needstats;
33152927Selan char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */
33252927Selan
33352260Selan /*
33452260Selan * If list is NULL there are two possibilities: that the parent
33552260Selan * directory p has no children, or that fts_children() returned an
33652346Sbostic * error. We ignore the error case since it will be replicated
33752260Selan * on the next call to fts_read() on the post-order visit to the
33852260Selan * directory p, and will be signalled in traverse().
33952260Selan */
34052767Sbostic if (list == NULL)
34152260Selan return;
3426034Swnj
34352927Selan needstats = f_inode || f_longform || f_size;
34452927Selan flen = 0;
34553808Sbostic btotal = maxblock = maxinode = maxlen = maxnlink = 0;
34654542Sbostic bcfile = 0;
34752927Selan maxuser = maxgroup = maxflags = 0;
34853808Sbostic maxsize = 0;
34952767Sbostic for (cur = list, entries = 0; cur; cur = cur->fts_link) {
35052767Sbostic if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
35166603Sbostic warnx("%s: %s",
35266603Sbostic cur->fts_name, strerror(cur->fts_errno));
35352767Sbostic cur->fts_number = NO_PRINT;
35452767Sbostic continue;
35552767Sbostic }
35652767Sbostic
35752767Sbostic /*
35852927Selan * P is NULL if list is the argv list, to which different rules
35952927Selan * apply.
36052767Sbostic */
36152767Sbostic if (p == NULL) {
36252767Sbostic /* Directories will be displayed later. */
36352260Selan if (cur->fts_info == FTS_D && !f_listdir) {
36452260Selan cur->fts_number = NO_PRINT;
36552260Selan continue;
36638332Sbostic }
36752767Sbostic } else {
36852767Sbostic /* Only display dot file if -a/-A set. */
36952260Selan if (cur->fts_name[0] == '.' && !f_listdot) {
37052260Selan cur->fts_number = NO_PRINT;
37138316Sbostic continue;
37252260Selan }
37338316Sbostic }
37452767Sbostic if (f_nonprint)
37552767Sbostic prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
37652927Selan if (cur->fts_namelen > maxlen)
37752767Sbostic maxlen = cur->fts_namelen;
37852927Selan if (needstats) {
37952927Selan sp = cur->fts_statp;
38052927Selan if (sp->st_blocks > maxblock)
38152927Selan maxblock = sp->st_blocks;
38252927Selan if (sp->st_ino > maxinode)
38352927Selan maxinode = sp->st_ino;
38452927Selan if (sp->st_nlink > maxnlink)
38552927Selan maxnlink = sp->st_nlink;
38652927Selan if (sp->st_size > maxsize)
38752927Selan maxsize = sp->st_size;
38852927Selan
38952927Selan btotal += sp->st_blocks;
39052927Selan if (f_longform) {
39152927Selan user = user_from_uid(sp->st_uid, 0);
39252927Selan if ((ulen = strlen(user)) > maxuser)
39352927Selan maxuser = ulen;
39452927Selan group = group_from_gid(sp->st_gid, 0);
39552927Selan if ((glen = strlen(group)) > maxgroup)
39652927Selan maxgroup = glen;
39752927Selan if (f_flags) {
39858434Sbostic flags =
39958434Sbostic flags_to_string(sp->st_flags, "-");
40052927Selan if ((flen = strlen(flags)) > maxflags)
40152927Selan maxflags = flen;
40252927Selan } else
40352927Selan flen = 0;
40452927Selan
40552927Selan if ((np = malloc(sizeof(NAMES) +
40652927Selan ulen + glen + flen + 3)) == NULL)
40759500Sbostic err(1, NULL);
40852927Selan
40952927Selan np->user = &np->data[0];
41052927Selan (void)strcpy(np->user, user);
41152927Selan np->group = &np->data[ulen + 1];
41252927Selan (void)strcpy(np->group, group);
41352927Selan
41454542Sbostic if (S_ISCHR(sp->st_mode) ||
41554542Sbostic S_ISBLK(sp->st_mode))
41654542Sbostic bcfile = 1;
41754542Sbostic
41852927Selan if (f_flags) {
41952927Selan np->flags = &np->data[ulen + glen + 2];
42052927Selan (void)strcpy(np->flags, flags);
42152927Selan }
42252927Selan cur->fts_pointer = np;
42352927Selan }
42452927Selan }
42552767Sbostic ++entries;
42638317Sbostic }
4276034Swnj
42852927Selan if (!entries)
42952927Selan return;
43052927Selan
43152927Selan d.list = list;
43252927Selan d.entries = entries;
43352927Selan d.maxlen = maxlen;
43452927Selan if (needstats) {
43554542Sbostic d.bcfile = bcfile;
43652927Selan d.btotal = btotal;
43756589Sbostic (void)snprintf(buf, sizeof(buf), "%lu", maxblock);
43856589Sbostic d.s_block = strlen(buf);
43952927Selan d.s_flags = maxflags;
44052927Selan d.s_group = maxgroup;
44156589Sbostic (void)snprintf(buf, sizeof(buf), "%lu", maxinode);
44256589Sbostic d.s_inode = strlen(buf);
44356589Sbostic (void)snprintf(buf, sizeof(buf), "%lu", maxnlink);
44456589Sbostic d.s_nlink = strlen(buf);
44556589Sbostic (void)snprintf(buf, sizeof(buf), "%qu", maxsize);
44656589Sbostic d.s_size = strlen(buf);
44752927Selan d.s_user = maxuser;
44852767Sbostic }
44952927Selan
45052927Selan printfcn(&d);
45152927Selan output = 1;
45252927Selan
45352927Selan if (f_longform)
45452927Selan for (cur = list; cur; cur = cur->fts_link)
45552927Selan free(cur->fts_pointer);
45638315Sbostic }
45738315Sbostic
45852260Selan /*
45952260Selan * Ordering for mastercmp:
46052260Selan * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
46152260Selan * as larger than directories. Within either group, use the sort function.
46252260Selan * All other levels use the sort function. Error entries remain unsorted.
46352260Selan */
46452927Selan static int
mastercmp(a,b)46552260Selan mastercmp(a, b)
46652260Selan const FTSENT **a, **b;
46738313Sbostic {
46866559Spendry int a_info, b_info;
4696034Swnj
47052260Selan a_info = (*a)->fts_info;
47152260Selan if (a_info == FTS_ERR)
47251430Sbostic return (0);
47352260Selan b_info = (*b)->fts_info;
47452260Selan if (b_info == FTS_ERR)
47552260Selan return (0);
47652289Sbostic
47752289Sbostic if (a_info == FTS_NS || b_info == FTS_NS)
47852260Selan return (namecmp(*a, *b));
47938324Sbostic
48052927Selan if (a_info == b_info)
48152260Selan return (sortfcn(*a, *b));
48252260Selan
48352260Selan if ((*a)->fts_level == FTS_ROOTLEVEL)
48452927Selan if (a_info == FTS_D)
48552260Selan return (1);
48652260Selan else if (b_info == FTS_D)
48752260Selan return (-1);
48838324Sbostic else
48952260Selan return (sortfcn(*a, *b));
49052260Selan else
49152260Selan return (sortfcn(*a, *b));
4926034Swnj }
493