X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/329806759e413928388e604843baff01e139859b..d662a9c36c88e3aca3cebd9ede5fc7af570de16f:/usr/src/bin/ls/ls.c diff --git a/usr/src/bin/ls/ls.c b/usr/src/bin/ls/ls.c index cf2b8205aa..9628f7cf25 100644 --- a/usr/src/bin/ls/ls.c +++ b/usr/src/bin/ls/ls.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 1989 The Regents of the University of California. - * All rights reserved. + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Michael Fischbein. @@ -9,47 +9,49 @@ */ #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1989 The Regents of the University of California.\n\ - All rights reserved.\n"; +static char copyright[] = +"@(#) Copyright (c) 1989, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)ls.c 5.52 (Berkeley) %G%"; +static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) %G%"; #endif /* not lint */ -#include +#include #include #include + #include -#include -#include -#include +#include #include +#include #include +#include +#include +#include + #include "ls.h" #include "extern.h" -static void displaydir __P((LS *, int)); -static void doargs __P((int, char **)); -static void subdir __P((LS *)); -static int tabdir __P((LS *, LS **, char **)); +static void display __P((FTSENT *, FTSENT *)); +static int mastercmp __P((const FTSENT **, const FTSENT **)); +static void traverse __P((int, char **, int)); + +static void (*printfcn) __P((DISPLAY *)); +static int (*sortfcn) __P((const FTSENT *, const FTSENT *)); +long blocksize; /* block size units */ int termwidth = 80; /* default terminal width */ /* flags */ int f_accesstime; /* use time of last access */ int f_column; /* columnated format */ -int f_group; /* show group ownership of a file */ int f_flags; /* show flags associated with a file */ -int f_ignorelink; /* indirect through symbolic link operands */ int f_inode; /* print inode */ -int f_kblocks; /* print size in kilobytes */ -int f_listalldot; /* list . and .. as well */ int f_listdir; /* list actual directory, not contents */ int f_listdot; /* list files beginning with . */ int f_longform; /* long listing format */ -int f_needstat; /* if need to stat files */ int f_newline; /* if precede with newline */ int f_nonprint; /* show unprintables as ? */ int f_nosort; /* don't sort output */ @@ -61,42 +63,41 @@ int f_size; /* list size in short listing */ int f_statustime; /* use time of last mode change */ int f_dirname; /* if precede with directory name */ int f_timesort; /* sort by time vice name */ -int f_total; /* if precede with "total" line */ int f_type; /* add type character for non-regular files */ -void (*printfcn)(); -int (*statfcn)(), (*sortfcn)(); - +int main(argc, argv) int argc; - char **argv; + char *argv[]; { + static char dot[] = ".", *dotav[] = { dot, NULL }; struct winsize win; - int ch; + int ch, fts_options, notused; char *p; - /* terminal defaults to -Cq, non-terminal defaults to -1 */ - if (isatty(1)) { - f_nonprint = 1; - if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) { - if (p = getenv("COLUMNS")) + /* Terminal defaults to -Cq, non-terminal defaults to -1. */ + if (isatty(STDOUT_FILENO)) { + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 || + !win.ws_col) { + if ((p = getenv("COLUMNS")) != NULL) termwidth = atoi(p); } else termwidth = win.ws_col; - f_column = 1; + f_column = f_nonprint = 1; } else f_singlecol = 1; - /* root is -A automatically */ + /* Root is -A automatically. */ if (!getuid()) f_listdot = 1; - while ((ch = getopt(argc, argv, "1ACFLRTacdfgikloqrstu")) != EOF) { + fts_options = FTS_PHYSICAL; + while ((ch = getopt(argc, argv, "1ACFLRTacdfgiloqrstu")) != EOF) { switch (ch) { /* - * -1, -C and -l all override each other - * so shell aliasing works right + * The -1, -C and -l options all override each other so shell + * aliasing works right. */ case '1': f_singlecol = 1; @@ -110,7 +111,7 @@ main(argc, argv) f_longform = 1; f_column = f_singlecol = 0; break; - /* -c and -u override each other */ + /* The -c and -u options override each other. */ case 'c': f_statustime = 1; f_accesstime = 0; @@ -123,34 +124,33 @@ main(argc, argv) f_type = 1; break; case 'L': - f_ignorelink = 1; + fts_options &= ~FTS_PHYSICAL; + fts_options |= FTS_LOGICAL; break; case 'R': f_recursive = 1; break; case 'a': - f_listalldot = 1; + fts_options |= FTS_SEEDOT; /* FALLTHROUGH */ case 'A': f_listdot = 1; break; + /* The -d option turns off the -R option. */ case 'S': Sflg++; /* fall into... */ case 'd': f_listdir = 1; + f_recursive = 0; break; case 'f': f_nosort = 1; break; - case 'g': - f_group = 1; + case 'g': /* Compatibility with 4.3BSD. */ break; case 'i': f_inode = 1; break; - case 'k': - f_kblocks = 1; - break; case 'o': f_flags = 1; break; @@ -177,15 +177,27 @@ main(argc, argv) argc -= optind; argv += optind; - /* -d turns off -R */ - if (f_listdir) - f_recursive = 0; + /* + * If not -F, -i, -l, -s or -t options, don't require stat + * information. + */ + if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type) + fts_options |= FTS_NOSTAT; + + /* + * If not -F, -d or -l options, follow any symbolic links listed on + * the command line. + */ + if (!f_longform && !f_listdir && !f_type) + fts_options |= FTS_COMFOLLOW; - /* if need to stat files */ - f_needstat = f_longform || f_recursive || f_timesort || - f_size || f_type; + /* If -l or -s, figure out block size. */ + if (f_longform || f_size) { + (void)getbsize(¬used, &blocksize); + blocksize /= 512; + } - /* select a sort function */ + /* Select a sort function. */ if (f_reversesort) { if (!f_timesort) sortfcn = revnamecmp; @@ -193,7 +205,7 @@ main(argc, argv) sortfcn = revacccmp; else if (f_statustime) sortfcn = revstatcmp; - else /* use modification time */ + else /* Use modification time. */ sortfcn = revmodcmp; } else { if (!f_timesort) @@ -202,11 +214,11 @@ main(argc, argv) sortfcn = acccmp; else if (f_statustime) sortfcn = statcmp; - else /* use modification time */ + else /* Use modification time. */ sortfcn = modcmp; } - /* select a print function */ + /* Select a print function. */ if (f_singlecol) printfcn = printscol; else if (f_longform) @@ -214,259 +226,257 @@ main(argc, argv) else printfcn = printcol; - /* if -l, -d or -F, and not ignoring the link, use lstat() */ - statfcn = - (f_longform || f_listdir || f_type) && !f_ignorelink ? lstat : stat; - if (argc) - doargs(argc, argv); - else { - static char dot[] = ".", *dotav[] = { dot, NULL }; - doargs(1, dotav); - } + traverse(argc, argv, fts_options); + else + traverse(1, dotav, fts_options); exit(0); } -static char path[MAXPATHLEN + 1]; -static char *endofpath = path; +static int output; /* If anything output. */ +/* + * Traverse() walks the logical directory structure specified by the argv list + * in the order specified by the mastercmp() comparison function. During the + * traversal it passes linked lists of structures to display() which represent + * a superset (may be exact set) of the files to be displayed. + */ static void -doargs(argc, argv) - int argc; - char **argv; +traverse(argc, argv, options) + int argc, options; + char *argv[]; { - register LS *dstatp, *rstatp; - register int cnt, dircnt, maxlen, regcnt; - LS *dstats, *rstats; - struct stat sb; - char top[MAXPATHLEN + 1]; - u_long blocks; + FTS *ftsp; + FTSENT *p, *chp; + int ch_options; + + if ((ftsp = + fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) + err(1, NULL); + + display(NULL, fts_children(ftsp, 0)); + if (f_listdir) + return; /* - * walk through the operands, building separate arrays of LS - * structures for directory and non-directory files. + * If not recursing down this tree and don't need stat info, just get + * the names. */ - dstats = rstats = NULL; - for (dircnt = regcnt = 0; *argv; ++argv) { - if (statfcn(*argv, &sb) && - (statfcn == lstat || lstat(*argv, &sb))) { - if (errno == ENOENT) { - warn("%s: %s", *argv, strerror(errno)); - continue; - } - err("%s: %s", *argv, strerror(errno)); - } - if (S_ISDIR(sb.st_mode) && !f_listdir) { - if (!dstats) - dstatp = dstats = - emalloc((u_int)argc * (sizeof(LS))); - dstatp->name = *argv; - dstatp->lstat = sb; - ++dstatp; - ++dircnt; - } - else { - if (!rstats) { - rstatp = rstats = - emalloc((u_int)argc * (sizeof(LS))); - blocks = 0; - maxlen = -1; - } - rstatp->name = *argv; - rstatp->lstat = sb; + ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; - /* save name length for -C format */ - rstatp->len = strlen(*argv); - - if (f_nonprint) - prcopy(*argv, *argv, rstatp->len); + while ((p = fts_read(ftsp)) != NULL) + switch (p->fts_info) { + case FTS_DC: + warnx("%s: directory causes a cycle", p->fts_name); + break; + case FTS_DNR: + case FTS_ERR: + warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); + break; + case FTS_D: + if (p->fts_level != FTS_ROOTLEVEL && + p->fts_name[0] == '.' && !f_listdot) + break; - /* calculate number of blocks if -l/-s formats */ - if (f_longform || f_size) - blocks += sb.st_blocks; + /* + * If already output something, put out a newline as + * a separator. If multiple arguments, precede each + * directory with its name. + */ + if (output) + (void)printf("\n%s:\n", p->fts_path); + else if (argc > 1) { + (void)printf("%s:\n", p->fts_path); + output = 1; + } - /* save max length if -C format */ - if (f_column && maxlen < rstatp->len) - maxlen = rstatp->len; + chp = fts_children(ftsp, ch_options); + display(p, chp); - ++rstatp; - ++regcnt; - } - } - /* display regular files */ - if (regcnt) { - rstats[0].lstat.st_btotal = blocks; - rstats[0].lstat.st_maxlen = maxlen; - displaydir(rstats, regcnt); - f_newline = f_dirname = 1; - } - /* display directories */ - if (dircnt) { - register char *p; - - f_total = 1; - if (dircnt > 1) { - (void)getwd(top); - qsort((char *)dstats, dircnt, sizeof(LS), sortfcn); - f_dirname = 1; - } - for (cnt = 0; cnt < dircnt; ++dstats) { - for (endofpath = path, p = dstats->name; - *endofpath = *p++; ++endofpath); - subdir(dstats); - f_newline = 1; - if (++cnt < dircnt && chdir(top)) - err("%s: %s", top, strerror(errno)); + if (!f_recursive && chp != NULL) + (void)fts_set(ftsp, p, FTS_SKIP); + break; } - } + if (errno) + err(1, "fts_read"); } +/* + * Display() takes a linked list of FTSENT structures and passes the list + * along with any other necessary information to the print function. P + * points to the parent directory of the display list. + */ static void -displaydir(stats, num) - LS *stats; - register int num; +display(p, list) + FTSENT *p, *list; { - register char *p, *savedpath; - LS *lp; + struct stat *sp; + DISPLAY d; + FTSENT *cur; + NAMES *np; + u_quad_t maxsize; + u_long btotal, maxblock, maxinode, maxlen, maxnlink; + int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser; + int entries, needstats; + char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */ - if (num > 1 && !f_nosort) { - u_long save1, save2; - - save1 = stats[0].lstat.st_btotal; - save2 = stats[0].lstat.st_maxlen; - qsort((char *)stats, num, sizeof(LS), sortfcn); - stats[0].lstat.st_btotal = save1; - stats[0].lstat.st_maxlen = save2; - } + /* + * If list is NULL there are two possibilities: that the parent + * directory p has no children, or that fts_children() returned an + * error. We ignore the error case since it will be replicated + * on the next call to fts_read() on the post-order visit to the + * directory p, and will be signalled in traverse(). + */ + if (list == NULL) + return; - printfcn(stats, num); + needstats = f_inode || f_longform || f_size; + flen = 0; + btotal = maxblock = maxinode = maxlen = maxnlink = 0; + bcfile = 0; + maxuser = maxgroup = maxflags = 0; + maxsize = 0; + for (cur = list, entries = 0; cur; cur = cur->fts_link) { + if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { + warnx("%s: %s", + cur->fts_name, strerror(cur->fts_errno)); + cur->fts_number = NO_PRINT; + continue; + } - if (f_recursive) { - savedpath = endofpath; - for (lp = stats; num--; ++lp) { - if (!S_ISDIR(lp->lstat.st_mode)) + /* + * P is NULL if list is the argv list, to which different rules + * apply. + */ + if (p == NULL) { + /* Directories will be displayed later. */ + if (cur->fts_info == FTS_D && !f_listdir) { + cur->fts_number = NO_PRINT; continue; - p = lp->name; - if (p[0] == '.' && (!p[1] || p[1] == '.' && !p[2])) + } + } else { + /* Only display dot file if -a/-A set. */ + if (cur->fts_name[0] == '.' && !f_listdot) { + cur->fts_number = NO_PRINT; continue; - if (endofpath != path && endofpath[-1] != '/') - *endofpath++ = '/'; - for (; *endofpath = *p++; ++endofpath); - f_newline = f_dirname = f_total = 1; - subdir(lp); - *(endofpath = savedpath) = '\0'; + } } + if (f_nonprint) + prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen); + if (cur->fts_namelen > maxlen) + maxlen = cur->fts_namelen; + if (needstats) { + sp = cur->fts_statp; + if (sp->st_blocks > maxblock) + maxblock = sp->st_blocks; + if (sp->st_ino > maxinode) + maxinode = sp->st_ino; + if (sp->st_nlink > maxnlink) + maxnlink = sp->st_nlink; + if (sp->st_size > maxsize) + maxsize = sp->st_size; + + btotal += sp->st_blocks; + if (f_longform) { + user = user_from_uid(sp->st_uid, 0); + if ((ulen = strlen(user)) > maxuser) + maxuser = ulen; + group = group_from_gid(sp->st_gid, 0); + if ((glen = strlen(group)) > maxgroup) + maxgroup = glen; + if (f_flags) { + flags = + flags_to_string(sp->st_flags, "-"); + if ((flen = strlen(flags)) > maxflags) + maxflags = flen; + } else + flen = 0; + + if ((np = malloc(sizeof(NAMES) + + ulen + glen + flen + 3)) == NULL) + err(1, NULL); + + np->user = &np->data[0]; + (void)strcpy(np->user, user); + np->group = &np->data[ulen + 1]; + (void)strcpy(np->group, group); + + if (S_ISCHR(sp->st_mode) || + S_ISBLK(sp->st_mode)) + bcfile = 1; + + if (f_flags) { + np->flags = &np->data[ulen + glen + 2]; + (void)strcpy(np->flags, flags); + } + cur->fts_pointer = np; + } + } + ++entries; } -} - -static void -subdir(lp) - LS *lp; -{ - LS *stats; - int num; - char *names; - if (f_newline) - (void)putchar('\n'); - if (f_dirname) - (void)printf("%s:\n", path); - - if (chdir(lp->name)) { - warn("%s: %s", lp->name, strerror(errno)); + if (!entries) return; + + d.list = list; + d.entries = entries; + d.maxlen = maxlen; + if (needstats) { + d.bcfile = bcfile; + d.btotal = btotal; + (void)snprintf(buf, sizeof(buf), "%lu", maxblock); + d.s_block = strlen(buf); + d.s_flags = maxflags; + d.s_group = maxgroup; + (void)snprintf(buf, sizeof(buf), "%lu", maxinode); + d.s_inode = strlen(buf); + (void)snprintf(buf, sizeof(buf), "%lu", maxnlink); + d.s_nlink = strlen(buf); + (void)snprintf(buf, sizeof(buf), "%qu", maxsize); + d.s_size = strlen(buf); + d.s_user = maxuser; } - if (num = tabdir(lp, &stats, &names)) { - displaydir(stats, num); - free(stats); - free(names); - } - if (chdir("..")) - err("..: %s", strerror(errno)); + + printfcn(&d); + output = 1; + + if (f_longform) + for (cur = list; cur; cur = cur->fts_link) + free(cur->fts_pointer); } +/* + * Ordering for mastercmp: + * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories + * as larger than directories. Within either group, use the sort function. + * All other levels use the sort function. Error entries remain unsorted. + */ static int -tabdir(lp, s_stats, s_names) - LS *lp, **s_stats; - char **s_names; +mastercmp(a, b) + const FTSENT **a, **b; { - register DIR *dirp; - register int cnt, maxentry, maxlen; - register char *p, *names; - struct dirent *dp; - LS *stats; - u_long blocks; - - if (!(dirp = opendir("."))) { - warn("%s: %s", lp->name, strerror(errno)); - return (0); - } - blocks = maxentry = maxlen = 0; - stats = NULL; - for (cnt = 0; dp = readdir(dirp);) { - /* this does -A and -a */ - p = dp->d_name; - if (p[0] == '.') { - if (!f_listdot) - continue; - if (!f_listalldot && (!p[1] || p[1] == '.' && !p[2])) - continue; - } - if (cnt == maxentry) { - if (!maxentry) - *s_names = names = - emalloc((u_int)lp->lstat.st_size); -#define DEFNUM 256 - maxentry += DEFNUM; - if (!(*s_stats = stats = realloc((char *)stats, - (u_int)maxentry * sizeof(LS)))) - err("%s", strerror(errno)); - } - if (f_needstat && statfcn(dp->d_name, &stats[cnt].lstat) && - statfcn == stat && lstat(dp->d_name, &stats[cnt].lstat)) { - /* - * don't exit -- this could be an NFS mount that has - * gone away. Flush stdout so the messages line up. - */ - (void)fflush(stdout); - warn("%s: %s", dp->d_name, strerror(errno)); - continue; - } - stats[cnt].name = names; + int a_info, b_info; - if (f_nonprint) - prcopy(dp->d_name, names, (int)dp->d_namlen); - else - bcopy(dp->d_name, names, (int)dp->d_namlen); - names += dp->d_namlen; - *names++ = '\0'; - - /* - * get the inode from the directory, so the -f flag - * works right. - */ - stats[cnt].lstat.st_ino = dp->d_ino; + a_info = (*a)->fts_info; + if (a_info == FTS_ERR) + return (0); + b_info = (*b)->fts_info; + if (b_info == FTS_ERR) + return (0); - /* save name length for -C format */ - stats[cnt].len = dp->d_namlen; + if (a_info == FTS_NS || b_info == FTS_NS) + return (namecmp(*a, *b)); - /* calculate number of blocks if -l/-s formats */ - if (f_longform || f_size) - blocks += stats[cnt].lstat.st_blocks; + if (a_info == b_info) + return (sortfcn(*a, *b)); - /* save max length if -C format */ - if (f_column && maxlen < (int)dp->d_namlen) - maxlen = dp->d_namlen; - ++cnt; - } - (void)closedir(dirp); - - if (cnt) { - stats[0].lstat.st_btotal = blocks; - stats[0].lstat.st_maxlen = maxlen; - } else if (stats) { - free(stats); - free(names); - } - return (cnt); + if ((*a)->fts_level == FTS_ROOTLEVEL) + if (a_info == FTS_D) + return (1); + else if (b_info == FTS_D) + return (-1); + else + return (sortfcn(*a, *b)); + else + return (sortfcn(*a, *b)); }