get redistribution notice from /usr/share/misc
[unix-history] / usr / src / bin / ls / ls.c
index 2516f1a..94c3447 100644 (file)
@@ -5,17 +5,7 @@
  * This code is derived from software contributed to Berkeley by
  * Michael Fischbein.
  *
  * This code is derived from software contributed to Berkeley by
  * Michael Fischbein.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+%sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -25,96 +15,119 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ls.c       5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)ls.c       5.37 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <sys/time.h>
+#include <sys/ioctl.h>
 #include <dirent.h>
 #include <dirent.h>
-#include <grp.h>
-#include <pwd.h>
 #include <strings.h>
 #include <strings.h>
-#include <tzfile.h>
 #include <errno.h>
 #include <stdio.h>
 #include <errno.h>
 #include <stdio.h>
-#include <ctype.h>
+#include "ls.h"
 
 
-typedef struct _lsstruct {
-       struct stat lstat;
-       char *name;             /* pointer to filename */
-} LS;
+int (*sortfcn)(), (*printfcn)();
+int lstat(), strlen();
+char *emalloc();
 
 
-int    qflg, Aflg, Cflg, Fflg, Lflg, Rflg, Sflg;
+int termwidth = 80;            /* default terminal width */
 
 /* flags */
 
 /* flags */
-int f_listall;                 /* list .xxx files? */
-int f_listalways;              /* list . and .. */
-int f_modtime;                 /* use time of last change for time */
 int f_accesstime;              /* use time of last access */
 int f_accesstime;              /* use time of last access */
-int f_statustime;              /* use time of last mode change */
-int f_singlecol;               /* use single column output */
-int f_listdir;                 /* list actual directory, not contents */
-int f_specialdir;              /* force params to be directories */
-int f_type;                    /* add /, * @ = to mark non-regular files */
+int f_column;                  /* columnated format */
 int f_group;                   /* show group ownership of a file */
 int f_group;                   /* show group ownership of a file */
+int f_ignorelink;              /* indirect through symbolic link operands */
 int f_inode;                   /* print inode */
 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_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_nonprint;                        /* show unprintables as ? */
-int f_reversesort;             /* reverse whatever sort is used */
+int f_nosort;                  /* don't sort output */
 int f_recursive;               /* ls subdirectories also */
 int f_recursive;               /* ls subdirectories also */
+int f_reversesort;             /* reverse whatever sort is used */
+int f_singlecol;               /* use single column output */
 int f_size;                    /* list size in short listing */
 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_timesort;                        /* sort by time vice name */
-int f_number;                  /* list UID and GID by number */
-int f_firsttime = 1;           /* to control recursion */
+int f_total;                   /* if precede with "total" line */
+int f_type;                    /* add type character for non-regular files */
 
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern int optind, stat();
 
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern int optind, stat();
+       struct winsize win;
        int ch;
        int ch;
-       int namecmp(), revnamecmp(), acccmp(), revacccmp();
-       int modcmp(), revmodcmp(), statcmp(), revstatcmp();
+       char *p, *getenv();
+       int acccmp(), bcopy(), modcmp(), namecmp(), prcopy(), printcol();
+       int printlong(), printscol(), revacccmp(), revmodcmp(), revnamecmp();
+       int revstatcmp(), statcmp();
 
 
-        /* set up defaults for terminal/nonterminal stdout */
+       /* terminal defaults to -Cq, non-terminal defaults to -1 */
        if (isatty(1)) {
                f_nonprint = 1;
        if (isatty(1)) {
                f_nonprint = 1;
-               lengthfcn = prablelen;
+               if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
+                       if (p = getenv("COLUMNS"))
+                               termwidth = atoi(p);
+               }
+               else
+                       termwidth = win.ws_col;
+               f_column = 1;
        } else
                f_singlecol = 1;
 
        } else
                f_singlecol = 1;
 
-       /* root sees all files automatically */
+       /* root is -A automatically */
        if (!getuid())
        if (!getuid())
-               f_listall = 1;
+               f_listdot = 1;
 
 
-       while ((ch = getopt(argc, argv, "1ACFLRacdfgilqrstu")) != EOF) {
+       while ((ch = getopt(argc, argv, "1ACFLRacdfgiklqrstu")) != EOF) {
                switch (ch) {
                switch (ch) {
+               /*
+                * -1, -C and -l all override each other
+                * so shell aliasing works right
+                */
                case '1':
                        f_singlecol = 1;
                case '1':
                        f_singlecol = 1;
+                       f_column = f_longform = 0;
                        break;
                case 'C':
                        break;
                case 'C':
-                       f_singlecol = 0;
+                       f_column = 1;
+                       f_longform = f_singlecol = 0;
+                       break;
+               case 'l':
+                       f_longform = 1;
+                       f_column = f_singlecol = 0;
+                       break;
+               /* -c and -u override each other */
+               case 'c':
+                       f_statustime = 1;
+                       f_accesstime = 0;
+                       break;
+               case 'u':
+                       f_accesstime = 1;
+                       f_statustime = 0;
                        break;
                case 'F':
                        f_type = 1;
                        break;
                case 'L':
                        break;
                case 'F':
                        f_type = 1;
                        break;
                case 'L':
-                       statfcn = stat;
+                       f_ignorelink = 1;
                        break;
                case 'R':
                        f_recursive = 1;
                        break;
                case 'R':
                        f_recursive = 1;
-                       statfcn = lstat;
                        break;
                case 'a':
                        break;
                case 'a':
-                       f_listalways = 1;
+                       f_listalldot = 1;
                        /* FALLTHROUGH */
                case 'A':
                        /* FALLTHROUGH */
                case 'A':
-                       f_listall = 1;
-                       break;
-               case 'c':
-                       f_statustime = 1;
-                       f_modtime = f_accesstime = 0;
+                       f_listdot = 1;
                        break;
                case 'S':
                        Sflg++; /* fall into... */
                        break;
                case 'S':
                        Sflg++; /* fall into... */
@@ -122,7 +135,7 @@ main(argc, argv)
                        f_listdir = 1;
                        break;
                case 'f':
                        f_listdir = 1;
                        break;
                case 'f':
-                       f_specialdir = 1;
+                       f_nosort = 1;
                        break;
                case 'g':
                        f_group = 1;
                        break;
                case 'g':
                        f_group = 1;
@@ -130,44 +143,36 @@ main(argc, argv)
                case 'i':
                        f_inode = 1;
                        break;
                case 'i':
                        f_inode = 1;
                        break;
-               case 'l':
-                       f_longform = 1;
-                       f_number = f_singlecol = 0;
-                       statfcn = lstat;
-                       if (!f_accesstime && !f_statustime)
-                               f_modtime = 1;
+               case 'k':
+                       f_kblocks = 1;
                        break;
                case 'q':
                        f_nonprint = 1;
                        break;
                case 'q':
                        f_nonprint = 1;
-                       lengthfcn = prablelen;
                        break;
                case 'r':
                        f_reversesort = 1;
                        break;
                case 's':
                        f_size = 1;
                        break;
                case 'r':
                        f_reversesort = 1;
                        break;
                case 's':
                        f_size = 1;
-                       statfcn = lstat;
                        break;
                case 't':
                        f_timesort = 1;
                        break;
                case 't':
                        f_timesort = 1;
-                       if (!f_accesstime && !f_statustime)
-                               f_modtime = 1;
-                       break;
-               case 'u':
-                       f_modtime = f_statustime = 0;
-                       f_accesstime = 1;
                        break;
                default:
                case '?':
                        usage();
                }
        }
                        break;
                default:
                case '?':
                        usage();
                }
        }
+       argc -= optind;
+       argv += optind;
 
 
-       /* -f turns off -l, -t, -s, -r, turns on -a */
-       if (f_specialdir) {
-               f_longform = f_timesort = f_size = f_reversesort = 0;
-               f_listall = f_listalways = 1;
-       }
+       /* -d turns off -R */
+       if (f_listdir)
+               f_recursive = 0;
+
+       /* if need to stat files */
+       f_needstat = f_longform || f_recursive || f_timesort ||
+           f_size || f_type;
 
        /* select a sort function */
        if (f_reversesort) {
 
        /* select a sort function */
        if (f_reversesort) {
@@ -175,580 +180,281 @@ main(argc, argv)
                        sortfcn = revnamecmp;
                else if (f_accesstime)
                        sortfcn = revacccmp;
                        sortfcn = revnamecmp;
                else if (f_accesstime)
                        sortfcn = revacccmp;
-               else if (f_modtime)
-                       sortfcn = revmodcmp;
                else if (f_statustime)
                        sortfcn = revstatcmp;
                else if (f_statustime)
                        sortfcn = revstatcmp;
+               else /* use modification time */
+                       sortfcn = revmodcmp;
        } else {
                if (!f_timesort)
                        sortfcn = namecmp;
                else if (f_accesstime)
                        sortfcn = acccmp;
        } else {
                if (!f_timesort)
                        sortfcn = namecmp;
                else if (f_accesstime)
                        sortfcn = acccmp;
-               else if (f_modtime)
-                       sortfcn = modcmp;
                else if (f_statustime)
                        sortfcn = statcmp;
                else if (f_statustime)
                        sortfcn = statcmp;
+               else /* use modification time */
+                       sortfcn = modcmp;
        }
 
        }
 
-       if (argc > optind)
-               lsdir(argc - optind, &argv[optind]);
-       else {
-               char *defname = ".";
+       /* select a print function */
+       if (f_singlecol)
+               printfcn = printscol;
+       else if (f_longform)
+               printfcn = printlong;
+       else
+               printfcn = printcol;
 
 
-               lsdir(1, &defname);
+       if (!argc) {
+               argc = 1;
+               argv[0] = ".";
+               argv[1] = NULL;
        }
        }
+       doargs(argc, argv);
        exit(0);
 }
 
        exit(0);
 }
 
-/*
- * list the files in a directory
- */
-extern int errno;
+static char path[MAXPATHLEN + 1];
+static char *endofpath = path;
 
 
-lsdir(argc, argv)
-       int argc;               /* count of file names passed */
-       char **argv;            /* array of file names */
+doargs(argc, argv)
+       int argc;
+       char **argv;
 {
 {
-       static char curpath[MAXPATHLEN + 1];
-       register char *p;
-       LS *stats;
-       int curname, entries, i, needstat, neednewline;
-       char *emalloc();
-
-       stats = (LS *)emalloc((u_int)argc * (sizeof(LS)));
-
-       needstat = f_firsttime || f_longform || f_recursive || f_timesort ||
-           f_size || f_inode || f_type;
-       neednewline = !f_firsttime;
-
-       for (entries = curname = 0; curname < argc; ++curname) {
-               if (!f_firsttime) {
-                       /*
-                        * check for .xxx files.  Note can't get listalways
-                        * without listall being set.
-                        */
-                       p = argv[curname];
-                       if (p[0] == '.') {
-                               if (!f_listall)
-                                       continue;
-                               /* and now for listalways: . and .. */
-                               if ((!p[1] || p[1] == '.' && !p[2]) &&
-                                   !f_listalways)
-                                       continue;
-                       }
-               }
-               if (needstat) {
-                       if (statfcn(argv[curname], &stats[entries].lstat)) {
-                               (void)fprintf(stderr, "ls: %s: %s\n",
-                                   argv[curname], strerror(errno));
+       register LS *dstatp, *rstatp;
+       register int cnt, dircnt, maxlen, regcnt;
+       LS *dstats, *rstats;
+       struct stat sb;
+       int (*statfcn)(), stat(), lstat();
+       char top[MAXPATHLEN + 1];
+       u_long blocks;
+
+       /*
+        * walk through the operands, building separate arrays of LS
+        * structures for directory and non-directory files.
+        */
+       dstats = rstats = NULL;
+       statfcn = (f_longform || f_listdir) && !f_ignorelink ? lstat : stat;
+       for (dircnt = regcnt = 0; *argv; ++argv) {
+               if (statfcn(*argv, &sb)) {
+                       if (statfcn != stat || lstat(*argv, &sb)) {
+                               (void)fprintf(stderr, "ls: %s: %s\n", *argv,
+                                   strerror(errno));
                                if (errno == ENOENT)
                                        continue;
                                exit(1);
                        }
                                if (errno == ENOENT)
                                        continue;
                                exit(1);
                        }
-                       if (!S_ISDIR(stats[entries].lstat.st_mode))
-                               neednewline = 1;
                }
                }
-               stats[entries++].name = argv[curname];
-       }
-       if (!entries)
-               return;
-
-       if (entries > 1 && !f_specialdir)
-               qsort((char *)stats, entries, sizeof(LS), sortfcn);
-
-       prindir(stats, entries);
-
-       if ((f_firsttime && !f_listdir) || f_recursive) {
-               for (i = 0; i < entries; ++i) {
-                       /* recurse on directories */
-                       if (S_ISDIR(stats[i].lstat.st_mode)) {
-                               /* don't recurse on . or .. */
-                               p = stats[i].name;
-                               if (p[0] == '.' && (!p[1] ||
-                                   p[1] == '.' && !p[2]) &&
-                                   !f_firsttime)
-                                       continue;
-                               if (chdir(p)) {
-                                       (void)fprintf(stderr, "ls: %s: %s\n",
-                                           p, strerror(errno));
-                                       break;
-                               }
-                               if (entries > 1 ||
-                                   !f_firsttime || f_recursive) {
-                                       /* add current name to path */
-                                       p = rindex(curpath, '\0');
-                                       if (p != curpath && p[-1] != '/')
-                                               *p++ = '/';
-                                       (void)strcpy(p, stats[i].name);
-                                       if (entries > 1 || !f_firsttime) {
-                                               if (neednewline)
-                                                       (void)putchar('\n');
-                                               (void)printf("%s:\n",
-                                                   curpath);
-                                       }
-                               }
-                               recursedir(&stats[i]);
-                               if (entries > 1 || !f_firsttime)
-                                       *p = '\0';
-                               if (chdir("..")) {
-                                       (void)fprintf(stderr, "ls: ..: %s\n",
-                                           strerror(errno));
-                                       exit(1);
-                               }
-                       }
+               if (S_ISDIR(sb.st_mode) && !f_listdir) {
+                       if (!dstats)
+                               dstatp = dstats = (LS *)emalloc((u_int)argc *
+                                   (sizeof(LS)));
+                       dstatp->name = *argv;
+                       dstatp->lstat = sb;
+                       ++dstatp;
+                       ++dircnt;
                }
                }
-               f_firsttime = 0;
-       }
-       free((char *)stats);
-}
-
-/*
- * set up the call to lsdir (lsdir(count, argv-type array))
- * mutually recursive with lsdir
- */
-recursedir(orig)
-       LS *orig;
-{
-       DIR *dirp;
-       struct dirent *entry;
-       int blocksub, vsub, oldfirsttimeflag;
-       char **argv, *argvblock, *emalloc();
-
-       blocksub = vsub = 0;
-
-       if ((dirp = opendir(".")) == NULL) {
-               (void)fprintf(stderr, "ls: %s: %s\n",
-                   orig->name, strerror(errno));
-               return;
-       }
-       /* wildly overestimate dynamic amount of memory needed */
-       argvblock = emalloc((u_int)orig->lstat.st_size);
-       argv = (char **)emalloc((u_int)orig->lstat.st_size);
-
-       while ((entry = readdir(dirp)) != NULL) {
-               argv[vsub++] = strncpy(&argvblock[blocksub], entry->d_name,
-                   (int)entry->d_namlen);
-               blocksub += entry->d_namlen;
-               argvblock[blocksub++] = '\0';
-       }
-       (void)closedir(dirp);
-
-       oldfirsttimeflag = f_firsttime;
-       f_firsttime = 0;
+               else {
+                       if (!rstats) {
+                               rstatp = rstats = (LS *)emalloc((u_int)argc *
+                                   (sizeof(LS)));
+                               blocks = 0;
+                               maxlen = -1;
+                       }
+                       rstatp->name = *argv;
+                       rstatp->lstat = sb;
 
 
-       lsdir(vsub, argv);
+                       /* save name length for -C format */
+                       rstatp->len = strlen(*argv);
 
 
-       f_firsttime = oldfirsttimeflag;
+                       if (f_nonprint)
+                               prcopy(*argv, *argv, rstatp->len);
 
 
-       free(argvblock);
-       free((char *)argv);
-}
+                       /* calculate number of blocks if -l/-s formats */
+                       if (f_longform || f_size)
+                               blocks += sb.st_blocks;
 
 
-/*
- * print a statted (if necessary), sorted directory by listing option
- */
-prindir(stats, endcount)
-       LS *stats;              /* the statted, sorted directory */
-       register int endcount;
-{
-       register int entry;
-       int i;                  /* subscript to stats */
-       int maxlen;             /* length of longest name string */
-       int colwidth;           /* width of a printing column */
-       int numcols;            /* number of columns */
-       int collength;          /* lines in longest column */
-       int base;               /* subscript for leftmost column */
-       int offset;             /* delta from base to next column */
-       int chcnt;              /* character count printed */
-       long blocks;            /* sum of blocks in longform listing */
-
-       if (f_singlecol) {
-               for (entry = 0; entry < endcount; ++entry)
-                       if (!f_firsttime ||
-                           !S_ISDIR(stats[entry].lstat.st_mode)) {
-                               (void)printaname(&stats[entry]);
-                               (void)putchar('\n');
-                       }
-               return;
-       }
+                       /* save max length if -C format */
+                       if (f_column && maxlen < rstatp->len)
+                               maxlen = rstatp->len;
 
 
-       if (f_longform) {
-               if (!f_firsttime) {
-                       for (i = 0, blocks = 0; i < endcount; ++i)
-                               blocks += stats[i].lstat.st_blocks;
-                       (void)printf("total %ld\n", blocks / 2);
+                       ++rstatp;
+                       ++regcnt;
                }
                }
-               for (i = 0; i < endcount; ++i) {
-                       if (f_inode)
-                               (void)printf("%6lu ", stats[i].lstat.st_ino);
-                       if (f_size)
-                               (void)printf("%4ld ",
-                                   stats[i].lstat.st_blocks / 2);
-                       printperms(stats[i].lstat.st_mode);
-                       (void)printf("%3d ", stats[i].lstat.st_nlink);
-                       printowner(stats[i].lstat.st_uid);
-                       if (f_group)
-                               printgrp(stats[i].lstat.st_gid);
-                       if (S_ISCHR(stats[i].lstat.st_mode) ||
-                           S_ISBLK(stats[i].lstat.st_mode))
-                               (void)printf("%3d,%4d ",
-                                   major(stats[i].lstat.st_rdev),
-                                   minor(stats[i].lstat.st_rdev));
-                       else
-                               (void)printf("%8ld ", stats[i].lstat.st_size);
-                       if (f_accesstime)
-                               printtime(stats[i].lstat.st_atime);
-                       else if (f_statustime)
-                               printtime(stats[i].lstat.st_ctime);
-                       else
-                               printtime(stats[i].lstat.st_mtime);
-                       (void)printf("%s", stats[i].name);
-                       if (f_type)
-                               (void)printtype(stats[i].lstat.st_mode);
-                       if (S_ISLNK(stats[i].lstat.st_mode))
-                               printlink(stats[i].name);
-                       (void)putchar('\n');
-               }
-               return;
        }
        }
-
-       /*
-        * assume tabs every 8 columns WARNING: bad code (hard coded
-        * constants) follows:
-        */
-
-       /* figure out max width */
-       maxlen = -1;
-       for (i = 0; i < endcount; ++i) {
-               if (maxlen < lengthfcn(stats[i].name))
-                       maxlen = lengthfcn(stats[i].name);
+       /* 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;
        }
        }
-
-       /* add fudge factors to max name length */
-       if (f_inode)
-               maxlen += 6;
-       if (f_size)
-               maxlen += 5;
-       if (f_type)
-               maxlen += 1;
-       /* one tab after maxlen */
-       colwidth = (maxlen + 9) & ~0x7;
-       numcols = (80 + colwidth - maxlen) / colwidth;
-       collength = (int)((float)endcount / (float)numcols + 0.999);
-
-       for (base = 0; base < collength; base++) {
-               for (offset = 0, i = 0; i < numcols; ++i, offset += collength) {
-                       if (base + offset >= endcount)
-                               break;
-                       chcnt = printaname(&stats[base + offset]);
-                       if (base + offset + collength < endcount) {
-                               while (chcnt + 8 < colwidth) {
-                                       (void)putchar('\t');
-                                       chcnt += 8;
-                               }
-                               if (chcnt < colwidth)
-                                       (void)putchar('\t');
-                               chcnt = (chcnt + 8) & ~0x7;
+       /* 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)) {
+                               (void)fprintf(stderr, "ls: %s: %s\n",
+                                   top, strerror(errno));
+                               exit(1);
                        }
                }
                        }
                }
-               if (base + offset < endcount)
-                       (void)printaname(&stats[base + offset]);
-               (void)printf("\n");
        }
 }
 
        }
 }
 
-/*
- * print [inode] [size] name
- * return # of characters printed, no trailing characters
- */
-printaname(entry)
-       LS *entry;
-{
-       int chcnt = 0;
-
-       if (f_inode)
-               chcnt += printf("%5lu ", entry->lstat.st_ino);
-       if (f_size)
-               chcnt += printf("%4ld ", entry->lstat.st_blocks / 2);
-       chcnt += printf("%s", entry->name);
-       if (f_type)
-               chcnt += printtype(entry->lstat.st_mode);
-       return(chcnt);
-}
-
-/*
- * print group and user name
- */
-printgrp(gid)
-       gid_t gid;
+displaydir(stats, num)
+       LS *stats;
+       register int num;
 {
 {
-       struct group *groupentry;
-
-       if ((groupentry = getgrgid((int)gid)) == NULL) {
-               /* can't find group, print out number instead */
-               (void)printf("%-9u ", gid);
-               return;
-       }
-       (void)printf("%-9s", groupentry->gr_name);
-       (void)getgrent();               /* to rewind group file */
-}
+       register char *p, *savedpath;
+       LS *lp;
 
 
-printowner(uid)
-       uid_t uid;
-{
-       struct passwd *pwentry;
+       if (num > 1 && !f_nosort) {
+               u_long save1, save2;
 
 
-       if ((pwentry = getpwuid((int)uid)) == NULL) {
-               /* can't find owner, print out number instead */
-               (void)printf("%-9u ", uid);
-               return;
+               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;
        }
        }
-       (void)printf("%-9s", pwentry->pw_name);
-       (void)getpwent();
-}
 
 
-#define        SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
-time_t time();
-
-printtime(ftime)
-       time_t ftime;
-{
-       int i;
-       char *longstring;
-
-       longstring = ctime((long *)&ftime);
-       for (i = 4; i < 11; ++i)
-               (void)putchar(longstring[i]);
-
-       if (ftime + SIXMONTHS > time((time_t *)NULL))
-               for (i = 11; i < 16; ++i)
-                       (void)putchar(longstring[i]);
-       else {
-               (void)putchar(' ');
-               for (i = 20; i < 24; ++i)
-                       (void)putchar(longstring[i]);
+       printfcn(stats, num);
+
+       if (f_recursive) {
+               savedpath = endofpath;
+               for (lp = stats; num--; ++lp) {
+                       if (!S_ISDIR(lp->lstat.st_mode))
+                               continue;
+                       p = lp->name;
+                       if (p[0] == '.' && (!p[1] || p[1] == '.' && !p[2]))
+                               continue;
+                       if (endofpath != path && endofpath[-1] != '/')
+                               *endofpath++ = '/';
+                       for (; *endofpath = *p++; ++endofpath);
+                       f_newline = f_dirname = f_total = 1;
+                       subdir(lp);
+                       *(endofpath = savedpath) = '\0';
+               }
        }
        }
-       (void)putchar(' ');
 }
 
 }
 
-/*
- * act like strlen, but also translate non-printing chars to '?'
- */
-prablelen(cp)
-       char *cp;
+subdir(lp)
+       LS *lp;
 {
 {
-       register int len = 0;
+       LS *stats;
+       int num;
+       char *names;
 
 
-       for (; *cp; ++len, ++cp)
-               if (!isprint(*cp))
-                       *cp = '?';
-       return(len);
-}
+       if (f_newline)
+               (void)putchar('\n');
+       if (f_dirname)
+               (void)printf("%s:\n", path);
 
 
-/*
- * do the permissions printing, passed the mode
- */
-printperms(mode)
-       mode_t mode;
-{
-        /* print type */
-       switch (mode & S_IFMT) {
-       case S_IFDIR:                   /* directory */
-               (void)putchar('d');
-               break;
-       case S_IFCHR:                   /* character special */
-               (void)putchar('c');
-               break;
-       case S_IFBLK:                   /* block special */
-               (void)putchar('b');
-               break;
-       case S_IFREG:                   /* regular */
-               (void)putchar('-');
-               break;
-       case S_IFLNK:                   /* symbolic link */
-               (void)putchar('l');
-               break;
-       case S_IFSOCK:                  /* socket */
-               (void)putchar('s');
-               break;
-#ifdef S_IFIFO
-       case S_IFIFO:                   /* fifo */
-               (void)putchar('p');
-               break;
-#endif
-       default:                        /* unknown */
-               (void)putchar('?');
-               break;
-       }
-       /* usr */
-       if (mode & S_IRUSR)
-               (void)putchar('r');
-       else
-               (void)putchar('-');
-       if (mode & S_IWUSR)
-               (void)putchar('w');
-       else
-               (void)putchar('-');
-       switch (mode & (S_IXUSR | S_ISUID)) {
-       case 0:
-               (void)putchar('-');
-               break;
-       case S_IXUSR:
-               (void)putchar('x');
-               break;
-       case S_ISUID:
-               (void)putchar('S');
-               break;
-       case S_IXUSR | S_ISUID:
-               (void)putchar('s');
-               break;
-       }
-       /* group */
-       if (mode & S_IRGRP)
-               (void)putchar('r');
-       else
-               (void)putchar('-');
-       if (mode & S_IWGRP)
-               (void)putchar('w');
-       else
-               (void)putchar('-');
-       switch (mode & (S_IXGRP | S_ISGID)) {
-       case 0:
-               (void)putchar('-');
-               break;
-       case S_IXGRP:
-               (void)putchar('x');
-               break;
-       case S_ISGID:
-               (void)putchar('S');
-               break;
-       case S_IXGRP | S_ISGID:
-               (void)putchar('s');
-               break;
-       }
-       /* other */
-       if (mode & S_IROTH)
-               (void)putchar('r');
-       else
-               (void)putchar('-');
-       if (mode & S_IWOTH)
-               (void)putchar('w');
-       else
-               (void)putchar('-');
-       switch (mode & (S_IXOTH | S_ISVTX)) {
-       case 0:
-               (void)putchar('-');
-               break;
-       case S_IXOTH:
-               (void)putchar('x');
-               break;
-       case S_ISVTX:
-               (void)putchar('T');
-               break;
-       case S_IXOTH | S_ISVTX:
-               (void)putchar('t');
-               break;
+       if (chdir(lp->name)) {
+               (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
+                    strerror(errno));
+               return;
        }
        }
-}
-
-printtype(mode)
-       mode_t mode;
-{
-       switch(mode & S_IFMT) {
-       case S_IFDIR:
-               (void)putchar('/');
-               return(1);
-       case S_IFLNK:
-               (void)putchar('@');
-               return(1);
-       case S_IFSOCK:
-               (void)putchar('=');
-               return(1);
+       if (num = tabdir(lp, &stats, &names)) {
+               displaydir(stats, num);
+               (void)free((char *)stats);
+               (void)free((char *)names);
        }
        }
-       if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
-               (void)putchar('*');
-               return(1);
+       if (chdir("..")) {
+               (void)fprintf(stderr, "ls: ..: %s\n", strerror(errno));
+               exit(1);
        }
        }
-       return(0);
 }
 
 }
 
-printlink(name)
-       char *name;
+tabdir(lp, s_stats, s_names)
+       LS *lp, **s_stats;
+       char **s_names;
 {
 {
-       char path[MAXPATHLEN + 1];
-       int lnklen;
+       register DIR *dirp;
+       register int cnt, maxentry, maxlen;
+       register char *p, *names;
+       struct dirent *dp;
+       u_long blocks;
+       LS *stats;
 
 
-       if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
-               (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
-               return;
+       if (!(dirp = opendir("."))) {
+               (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
+                   strerror(errno));
+               return(0);
        }
        }
-       path[lnklen] = '\0';
-       (void)printf(" -> %s", path);
-}
-
-namecmp(a, b)
-       LS *a, *b;
-{
-       return(strcmp(a->name, b->name));
-}
-
-revnamecmp(a, b)
-       LS *a, *b;
-{
-       return(strcmp(b->name, a->name));
-}
-
-modcmp(a, b)
-       LS *a, *b;
-{
-       return(a->lstat.st_mtime < b->lstat.st_mtime);
-}
-
-revmodcmp(a, b)
-       LS *a, *b;
-{
-       return(b->lstat.st_mtime < a->lstat.st_mtime);
-}
-
-acccmp(a, b)
-       LS *a, *b;
-{
-       return(a->lstat.st_atime < b->lstat.st_atime);
-}
-
-revacccmp(a, b)
-       LS *a, *b;
-{
-       return(b->lstat.st_atime < a->lstat.st_atime);
-}
-
-statcmp(a, b)
-       LS *a, *b;
-{
-       return(a->lstat.st_ctime < b->lstat.st_ctime);
-}
-
-revstatcmp(a, b)
-       LS *a, *b;
-{
-       return(b->lstat.st_ctime < a->lstat.st_ctime);
-}
-
-char
-*emalloc(size)
-       u_int size;
-{
-       char *retval, *malloc();
-
-       if ((retval = malloc(size)) == NULL) {
-               (void)fprintf(stderr, "ls: out of memory.\n");
-               exit(1);
+       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 = (LS *)realloc((char *)stats,
+                           (u_int)maxentry * sizeof(LS))))
+                               nomem();
+               }
+               if (f_needstat && lstat(dp->d_name, &stats[cnt].lstat)) {
+                       (void)fprintf(stderr, "ls: %s: %s\n",
+                           dp->d_name, strerror(errno));
+                       if (errno == ENOENT)
+                               continue;
+                       exit(1);
+               }
+               stats[cnt].name = names;
+
+               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;
+
+               /* save name length for -C format */
+               stats[cnt].len = dp->d_namlen;
+
+               /* calculate number of blocks if -l/-s formats */
+               if (f_longform || f_size)
+                       blocks += stats[cnt].lstat.st_blocks;
+
+               /* save max length if -C format */
+               if (f_column && maxlen < (int)dp->d_namlen)
+                       maxlen = dp->d_namlen;
+               ++cnt;
        }
        }
-       return(retval);
-}
+       (void)closedir(dirp);
 
 
-usage()
-{
-       (void)fprintf(stderr, "usage: ls [-1ACFLRacdfgilqrstu] [file ...]\n");
-       exit(1);
+       if (cnt) {
+               stats[0].lstat.st_btotal = blocks;
+               stats[0].lstat.st_maxlen = maxlen;
+       } else if (stats) {
+               (void)free((char *)stats);
+               (void)free((char *)names);
+       }
+       return(cnt);
 }
 }