oops. Got stat test logic wrong
[unix-history] / usr / src / bin / ls / ls.c
index 4296afb..22a75a3 100644 (file)
-#ifndef lint
-static char *sccsid = "@(#)ls.c        4.2 82/03/05";
-#endif
-
 /*
 /*
- * ls
+ * Copyright (c) 1989 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Michael Fischbein.
  *
  *
- * 4.2bsd version for symbolic links and variable length directory entries.
+ * %sccs.include.redist.c%
  */
 
  */
 
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)ls.c       5.48 (Berkeley) %G%";
+#endif /* not lint */
+
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <ndir.h>
+#include <sys/ioctl.h>
+#include <dirent.h>
+#include <string.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <sgtty.h>
-
-#define        kbytes(size)    ((size + 1023) / 1024)
-
-struct afile {
-       char    ftype;          /* file type, e.g. 'd', 'c', 'f' */
-       ino_t   fnum;           /* inode number of file */
-       short   fflags;         /* mode&~S_IFMT, perhaps ISARG */
-       short   fnl;            /* number of links */
-       short   fuid;           /* owner id */
-       short   fgid;           /* group id */
-       long    fsize;          /* file size */
-       time_t  fmtime;         /* time (modify or access or create) */
-       char    *fname;         /* file name */
-       char    *flinkto;       /* symbolic link value */
-};
-
-#define ISARG  0x8000          /* extra ``mode'' */
-
-struct subdirs {
-       char    *sd_name;
-       struct  subdirs *sd_next;
-} *subdirs;
-
-int    aflg, dflg, gflg, lflg, sflg, tflg, uflg, iflg, fflg, cflg, rflg = 1;
-int    qflg, Aflg, Fflg, Rflg, Cflg, hflg;
-
-int    usetabs;
-
-time_t now, sixmonthsago;
-
-char   *dotp = ".";
-
-struct afile *gstat();
-int    fcmp();
-char   *cat(), *savestr();
-char   *fmtentry();
-char   *getname(), *getgroup();
-
-char   *ctime();
-char   *malloc(), *calloc(), *realloc();
-char   *sprintf(), *strcpy(), *strcat();
+#include "ls.h"
+
+int (*sortfcn)(), (*printfcn)();
+int lstat();
+char *emalloc();
+
+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_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 */
+int f_recursive;               /* ls subdirectories also */
+int f_reversesort;             /* reverse whatever sort is used */
+int f_sectime;                 /* print the real time for all files */
+int f_singlecol;               /* use single column output */
+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 */
+
+int (*statfcn)(), stat(), lstat();
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
-       char *argv[];
+       char **argv;
 {
 {
-       int i;
-       struct afile *fp0, *fplast;
-       register struct afile *fp;
-       struct sgttyb sgbuf;
-
-       argc--, argv++;
-       if (getuid() == 0)
-               Aflg++;
-       (void) time(&now); sixmonthsago = now - 6L*30L*24L*60L*60L; now += 60;
+       extern int optind, stat();
+       struct winsize win;
+       int ch;
+       char *p, *getenv();
+       int acccmp(), modcmp(), namecmp(), prcopy(), printcol();
+       int printlong(), printscol(), revacccmp(), revmodcmp(), revnamecmp();
+       int revstatcmp(), statcmp();
+
+       /* terminal defaults to -Cq, non-terminal defaults to -1 */
        if (isatty(1)) {
        if (isatty(1)) {
-
-               qflg = Cflg = 1;
-               (void) gtty(1, &sgbuf);
-               if ((sgbuf.sg_flags & XTABS) == 0)
-                       usetabs = 1;
+               f_nonprint = 1;
+               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
        } else
-               usetabs = 1;
-       while (argc > 0 && **argv == '-') {
-               (*argv)++;
-               while (**argv) switch (*(*argv)++) {
-
-               case 'C':
-                       Cflg = 1; break;
-               case 'q':
-                       qflg = 1; break;
+               f_singlecol = 1;
+
+       /* root is -A automatically */
+       if (!getuid())
+               f_listdot = 1;
+
+       while ((ch = getopt(argc, argv, "1ACFLRTacdfgiklqrstu")) != EOF) {
+               switch (ch) {
+               /*
+                * -1, -C and -l all override each other
+                * so shell aliasing works right
+                */
                case '1':
                case '1':
-                       Cflg = 0; break;
+                       f_singlecol = 1;
+                       f_column = f_longform = 0;
+                       break;
+               case 'C':
+                       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':
+                       f_ignorelink = 1;
+                       break;
+               case 'R':
+                       f_recursive = 1;
+                       break;
                case 'a':
                case 'a':
-                       aflg++; break;
+                       f_listalldot = 1;
+                       /* FALLTHROUGH */
                case 'A':
                case 'A':
-                       Aflg++; break;
-               case 'c':
-                       cflg++; break;
-               case 's':
-                       sflg++; break;
+                       f_listdot = 1;
+                       break;
+               case 'S':
+                       Sflg++; /* fall into... */
                case 'd':
                case 'd':
-                       dflg++; break;
+                       f_listdir = 1;
+                       break;
+               case 'f':
+                       f_nosort = 1;
+                       break;
                case 'g':
                case 'g':
-                       gflg++; break;
-               case 'l':
-                       lflg++; break;
+                       f_group = 1;
+                       break;
+               case 'i':
+                       f_inode = 1;
+                       break;
+               case 'k':
+                       f_kblocks = 1;
+                       break;
+               case 'q':
+                       f_nonprint = 1;
+                       break;
                case 'r':
                case 'r':
-                       rflg = -1; break;
+                       f_reversesort = 1;
+                       break;
+               case 's':
+                       f_size = 1;
+                       break;
+               case 'T':
+                       f_sectime = 1;
+                       break;
                case 't':
                case 't':
-                       tflg++; break;
-               case 'u':
-                       uflg++; break;
-               case 'i':
-                       iflg++; break;
-               case 'f':
-                       fflg++; break;
-               case 'h':
-                       hflg++; break;
-               case 'F':
-                       Fflg++; break;
-               case 'R':
-                       Rflg++; break;
-               }
-               argc--, argv++;
-       }
-       if (fflg) { 
-               aflg++; lflg = 0; sflg = 0; tflg = 0;
-       }
-       if (lflg)
-               Cflg = 0;
-       if (argc == 0) {
-               argc++;
-               argv = &dotp;
-       }
-       fp = (struct afile *)calloc(argc, sizeof (struct afile));
-       if (fp == 0) {
-               fprintf(stderr, "ls: out of memory\n");
-               exit(1);
-       }
-       fp0 = fp;
-       for (i = 0; i < argc; i++) {
-               if (gstat(fp, *argv, 1, (int *)0)) {
-                       fp->fname = *argv;
-                       fp->fflags |= ISARG;
-                       fp++;
+                       f_timesort = 1;
+                       break;
+               default:
+               case '?':
+                       usage();
                }
                }
-               argv++;
-       }
-       fplast = fp;
-       qsort(fp0, fplast - fp0, sizeof (struct afile), fcmp);
-       if (dflg) {
-               formatf(fp0, fplast);
-               exit(0);
        }
        }
-       if (fflg)
-               fp = fp0;
-       else {
-               for (fp = fp0; fp < fplast && fp->ftype != 'd'; fp++)
-                       continue;
-               formatf(fp0, fp);
+       argc -= optind;
+       argv += optind;
+
+       /* -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) {
+               if (!f_timesort)
+                       sortfcn = revnamecmp;
+               else if (f_accesstime)
+                       sortfcn = revacccmp;
+               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_statustime)
+                       sortfcn = statcmp;
+               else /* use modification time */
+                       sortfcn = modcmp;
        }
        }
-       if (fp < fplast) {
-               if (fp > fp0)
-                       printf("\n");
-               for (;;) {
-                       formatd(fp->fname, argc > 1);
-                       while (subdirs) {
-                               struct subdirs *t;
-
-                               t = subdirs; subdirs = t->sd_next;
-                               printf("\n");
-                               formatd(t->sd_name, 1);
-                               cfree(t->sd_name);
-                               cfree((char *)t);
-                       }
-                       if (++fp == fplast)
-                               break;
-                       printf("\n");
-               }
-       }
-       exit(0);
-}
 
 
-formatd(name, title)
-       char *name;
-       int title;
-{
-       register struct afile *fp;
-       register struct subdirs *dp;
-       struct afile *dfp0, *dfplast;
-       int nkb;
+       /* select a print function */
+       if (f_singlecol)
+               printfcn = printscol;
+       else if (f_longform)
+               printfcn = printlong;
+       else
+               printfcn = printcol;
 
 
-       nkb = getdir(name, &dfp0, &dfplast);
-       if (dfp0 == 0)
-               return;
-       if (fflg == 0)
-               qsort(dfp0, dfplast - dfp0, sizeof (struct afile), fcmp);
-       if (title)
-               printf("%s:\n", name);
-       if (lflg || sflg)
-               printf("total %ld\n", nkb);
-       formatf(dfp0, dfplast);
-       if (Rflg)
-               for (fp = dfplast; fp >= dfp0; fp--) {
-                       if (fp->ftype != 'd' ||
-                           !strcmp(fp->fname, ".") ||
-                           !strcmp(fp->fname, ".."))
-                               continue;
-                       dp = (struct subdirs *)malloc(sizeof (struct subdirs));
-                       dp->sd_name = savestr(cat(name, fp->fname));
-                       dp->sd_next = subdirs; subdirs = dp;
-               }
-       for (fp = dfp0; fp < dfplast; fp++) {
-               if ((fp->fflags&ISARG) == 0 && fp->fname)
-                       cfree(fp->fname);
-               if (fp->flinkto)
-                       cfree(fp->flinkto);
-       }
-       cfree((char *)dfp0);
-}
+       /* if -l, -d or -F, and not ignoring the link, use lstat() */
+       statfcn =
+           (f_longform || f_listdir || f_type) && !f_ignorelink ? lstat : stat;
 
 
-getdir(dir, pfp0, pfplast)
-       char *dir;
-       struct afile **pfp0, **pfplast;
-{
-       register struct afile *fp;
-       DIR *dirp;
-       register struct direct *dp;
-       int nkb, nent = 20;
-
-       dirp = opendir(dir);
-       if (dirp == NULL) {
-               *pfp0 = *pfplast = NULL;
-               printf("%s unreadable\n", dir);         /* not stderr! */
-               return (0);
-       }
-       fp = *pfp0 = (struct afile *)calloc(nent, sizeof (struct afile));
-       *pfplast = *pfp0 + nent;
-       nkb = 0;
-       while (dp = readdir(dirp)) {
-               if (dp->d_ino == 0)
-                       continue;
-               if (aflg == 0 && dp->d_name[0]=='.' &&
-                   (Aflg == 0 || dp->d_name[1]==0 ||
-                    dp->d_name[1]=='.' && dp->d_name[2]==0))
-                       continue;
-               if (gstat(fp, cat(dir, dp->d_name), Fflg+Rflg, &nkb) == 0)
-                       continue;
-               fp->fnum = dp->d_ino;
-               fp->fname = savestr(dp->d_name);
-               fp++;
-               if (fp == *pfplast) {
-                       *pfp0 = (struct afile *)realloc((char *)*pfp0,
-                           2 * nent * sizeof (struct afile));
-                       if (*pfp0 == 0) {
-                               fprintf(stderr, "ls: out of memory\n");
-                               exit(1);
-                       }
-                       fp = *pfp0 + nent;
-                       *pfplast = fp + nent;
-                       nent *= 2;
-               }
+       if (!argc) {
+               static char dot[] = ".";
+
+               argc = 1;
+               argv[0] = dot;
+               argv[1] = NULL;
        }
        }
-       closedir(dirp);
-       *pfplast = fp;
-       return (nkb);
+       doargs(argc, argv);
+       exit(0);
 }
 
 }
 
-int    stat(), lstat();
+static char path[MAXPATHLEN + 1];
+static char *endofpath = path;
 
 
-struct afile *
-gstat(fp, file, statarg, pnkb)
-       register struct afile *fp;
-       char *file;
-       int statarg, *pnkb;
+doargs(argc, argv)
+       int argc;
+       char **argv;
 {
 {
-       int (*statf)() = hflg ? lstat : stat;
-       char buf[BUFSIZ]; int cc;
-       static struct afile azerofile;
-
-       *fp = azerofile;
-       fp->fflags = 0;
-       fp->fnum = 0;
-       fp->ftype = '-';
-       if (statarg || sflg || lflg || tflg) {
-               struct stat stb;
-
-               if ((*statf)(file, &stb) < 0) {
-                       fprintf(stderr, "%s not found\n", file);
-                       return (0);
-               }
-               fp->fsize = stb.st_size;
-               switch (stb.st_mode & S_IFMT) {
-
-               case S_IFDIR:
-                       fp->ftype = 'd'; break;
-               case S_IFBLK:
-                       fp->ftype = 'b'; fp->fsize = stb.st_rdev; break;
-               case S_IFCHR:
-                       fp->ftype = 'c'; fp->fsize = stb.st_rdev; break;
-               case S_IFLNK:
-                       fp->ftype = 'l';
-                       if (lflg) {
-                               cc = readlink(file, buf, BUFSIZ);
-                               if (cc >= 0) {
-                                       buf[cc] = 0;
-                                       fp->flinkto = savestr(buf);
-                               }
-                       }
-                       break;
+       register LS *dstatp, *rstatp;
+       register int cnt, dircnt, maxlen, regcnt;
+       LS *dstats, *rstats;
+       struct stat sb;
+       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;
+       for (dircnt = regcnt = 0; *argv; ++argv) {
+               if (statfcn(*argv, &sb) &&
+                   (statfcn == lstat || lstat(*argv, &sb))) {
+                       (void)fprintf(stderr,
+                           "ls: %s: %s\n", *argv, strerror(errno));
+                       if (errno == ENOENT)
+                               continue;
+                       exit(1);
                }
                }
-               fp->fnum = stb.st_ino;
-               fp->fflags = stb.st_mode & ~S_IFMT;
-               fp->fnl = stb.st_nlink;
-               fp->fuid = stb.st_uid;
-               fp->fgid = stb.st_gid;
-               if (uflg)
-                       fp->fmtime = stb.st_atime;
-               else if (cflg)
-                       fp->fmtime = stb.st_ctime;
-               else
-                       fp->fmtime = stb.st_mtime;
-               if (pnkb)
-                       if (fp->ftype != 'b' && fp->ftype != 'c')
-                               *pnkb += kbytes(fp->fsize);
-       }
-       return (fp);
-}
-
-formatf(fp0, fplast)
-       struct afile *fp0, *fplast;
-{
-       register struct afile *fp;
-       int width = 0, w, nentry = fplast - fp0;
-       int i, j, columns, lines;
-       char *cp;
-
-       if (fp0 == fplast)
-               return;
-       if (lflg || Cflg == 0)
-               columns = 1;
-       else {
-               for (fp = fp0; fp < fplast; fp++) {
-                       int len = strlen(fmtentry(fp));
-
-                       if (len > width)
-                               width = len;
+               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;
                }
                }
-               if (usetabs)
-                       width = (width + 8) &~ 7;
-               else
-                       width += 2;
-               columns = 80 / width;
-               if (columns == 0)
-                       columns = 1;
-       }
-       lines = (nentry + columns - 1) / columns;
-       for (i = 0; i < lines; i++) {
-               for (j = 0; j < columns; j++) {
-                       fp = fp0 + j * lines + i;
-                       cp = fmtentry(fp);
-                       printf("%s", cp);
-                       if (fp + lines >= fplast) {
-                               printf("\n");
-                               break;
+               else {
+                       if (!rstats) {
+                               rstatp = rstats = (LS *)emalloc((u_int)argc *
+                                   (sizeof(LS)));
+                               blocks = 0;
+                               maxlen = -1;
                        }
                        }
-                       w = strlen(cp);
-                       while (w < width)
-                               if (usetabs) {
-                                       w = (w + 8) &~ 7;
-                                       putchar('\t');
-                               } else {
-                                       w++;
-                                       putchar(' ');
-                               }
-               }
-       }
-}
-
-fcmp(f1, f2)
-       register struct afile *f1, *f2;
-{
+                       rstatp->name = *argv;
+                       rstatp->lstat = sb;
 
 
-       if (dflg == 0 && fflg == 0) {
-               if ((f1->fflags&ISARG) && f1->ftype == 'd') {
-                       if ((f2->fflags&ISARG) == 0 || f2->ftype != 'd')
-                               return (1);
-               } else {
-                       if ((f2->fflags&ISARG) && f2->ftype == 'd')
-                               return (-1);
-               }
-       }
-       if (tflg) {
-               if (f2->fmtime == f1->fmtime)
-                       return (0);
-               if (f2->fmtime > f1->fmtime)
-                       return (rflg);
-               return (-rflg);
-       }
-       return (rflg * strcmp(f1->fname, f2->fname));
-}
+                       /* save name length for -C format */
+                       rstatp->len = strlen(*argv);
 
 
-char *
-cat(dir, file)
-       char *dir, *file;
-{
-       static char dfile[BUFSIZ];
+                       if (f_nonprint)
+                               prcopy(*argv, *argv, rstatp->len);
 
 
-       if (strlen(dir)+1+strlen(file)+1 > BUFSIZ) {
-               fprintf(stderr, "ls: filename too long\n");
-               exit(1);
-       }
-       if (!strcmp(dir, "") || !strcmp(dir, ".")) {
-               (void) strcpy(dfile, file);
-               return (dfile);
-       }
-       (void) strcpy(dfile, dir);
-       if (dir[strlen(dir) - 1] != '/' && *file != '/')
-               (void) strcat(dfile, "/");
-       (void) strcat(dfile, file);
-       return (dfile);
-}
+                       /* calculate number of blocks if -l/-s formats */
+                       if (f_longform || f_size)
+                               blocks += sb.st_blocks;
 
 
-char *
-savestr(str)
-       char *str;
-{
-       char *cp = malloc(strlen(str) + 1);
+                       /* save max length if -C format */
+                       if (f_column && maxlen < rstatp->len)
+                               maxlen = rstatp->len;
 
 
-       if (cp == NULL) {
-               fprintf(stderr, "ls: out of memory\n");
-               exit(1);
+                       ++rstatp;
+                       ++regcnt;
+               }
        }
        }
-       (void) strcpy(cp, str);
-       return (cp);
-}
-
-char   *fmtinum(), *fmtsize(), *fmtlstuff(), *fmtmode();
-
-char *
-fmtentry(fp)
-       register struct afile *fp;
-{
-       static char fmtres[BUFSIZ];
-       register char *cp, *dp;
-
-       (void) sprintf(fmtres, "%s%s%s%s",
-           iflg ? fmtinum(fp) : "",
-           sflg ? fmtsize(fp) : "",
-           lflg ? fmtlstuff(fp) : "");
-       dp = &fmtres[strlen(fmtres)];
-       for (cp = fp->fname; *cp; cp++)
-               if (qflg && (*cp < ' ' || *cp >= 0177))
-                       *dp++ = '?';
-               else
-                       *dp++ = *cp;
-       if (Fflg) {
-               if (fp->ftype == 'd')
-                       *dp++ = '/';
-               else if (fp->fflags & 0111)
-                       *dp++ = '*';
+       /* 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;
        }
        }
-       if (lflg && fp->flinkto) {
-               (void) strcpy(dp, " -> "); dp += 4;
-               for (cp = fp->flinkto; *cp; cp++)
-                       if (qflg && (*cp < ' ' || *cp >= 0177))
-                               *dp++ = '?';
-                       else
-                               *dp++ = *cp;
+       /* 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);
+                       }
+               }
        }
        }
-       *dp++ = 0;
-       return (fmtres);
 }
 
 }
 
-char *
-fmtinum(p)
-       register struct afile *p;
+displaydir(stats, num)
+       LS *stats;
+       register int num;
 {
 {
-       static char inumbuf[8];
+       register char *p, *savedpath;
+       LS *lp;
 
 
-       (void) sprintf(inumbuf, "%5d ", p->fnum);
-       return (inumbuf);
-}
-
-char *
-fmtsize(p)
-       register struct afile *p;
-{
-       static char sizebuf[32];
+       if (num > 1 && !f_nosort) {
+               u_long save1, save2;
 
 
-       switch (p->ftype) {
+               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;
+       }
 
 
-       case 'b':
-       case 'c':
-               (void) sprintf(sizebuf, "%4ld ", 0);
-               break;
+       printfcn(stats, num);
 
 
-       default:
-               (void) sprintf(sizebuf, "%4ld ", kbytes(p->fsize));
-               break;
+       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';
+               }
        }
        }
-       return (sizebuf);
 }
 
 }
 
-char *
-fmtlstuff(p)
-       register struct afile *p;
+subdir(lp)
+       LS *lp;
 {
 {
-       static char lstuffbuf[256];
-       char gname[32], uname[32], fsize[32], ftime[32];
-       register char *lp = lstuffbuf;
-
-       /* type mode uname gname fsize ftime */
-/* get uname */
-       { char *cp = getname(p->fuid);
-         if (cp)
-               (void) sprintf(uname, "%-9.9s", cp);
-         else
-               (void) sprintf(uname, "%-9d", p->fuid);
+       LS *stats;
+       int num;
+       char *names;
+
+       if (f_newline)
+               (void)putchar('\n');
+       if (f_dirname)
+               (void)printf("%s:\n", path);
+
+       if (chdir(lp->name)) {
+               (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
+                    strerror(errno));
+               return;
        }
        }
-/* get gname */
-       if (gflg) {
-         char *cp = getgroup(p->fgid);
-         if (cp)
-               (void) sprintf(gname, "%-9.9s", cp);
-         else
-               (void) sprintf(gname, "%-9d", p->fgid);
+       if (num = tabdir(lp, &stats, &names)) {
+               displaydir(stats, num);
+               (void)free((char *)stats);
+               (void)free((char *)names);
        }
        }
-/* get fsize */
-       if (p->ftype == 'b' || p->ftype == 'c')
-               (void) sprintf(fsize, "%3d,%4d",
-                   major(p->fsize), minor(p->fsize));
-       else
-               (void) sprintf(fsize, "%8ld", p->fsize);
-/* get ftime */
-       { char *cp = ctime(&p->fmtime);
-         if ((p->fmtime < sixmonthsago) || (p->fmtime > now))
-               (void) sprintf(ftime, " %-7.7s %-4.4s ", cp+4, cp+20);
-         else
-               (void) sprintf(ftime, " %-12.12s ", cp+4);
+       if (chdir("..")) {
+               (void)fprintf(stderr, "ls: ..: %s\n", strerror(errno));
+               exit(1);
        }
        }
-/* splat */
-       *lp++ = p->ftype;
-       lp = fmtmode(lp, p->fflags);
-       (void) sprintf(lp, "%3d %s%s%s%s",
-           p->fnl, uname, gflg ? gname : "", fsize, ftime);
-       return (lstuffbuf);
 }
 
 }
 
-int    m1[] = { 1, S_IREAD>>0, 'r', '-' };
-int    m2[] = { 1, S_IWRITE>>0, 'w', '-' };
-int    m3[] = { 2, S_ISUID, 's', S_IEXEC>>0, 'x', '-' };
-int    m4[] = { 1, S_IREAD>>3, 'r', '-' };
-int    m5[] = { 1, S_IWRITE>>3, 'w', '-' };
-int    m6[] = { 2, S_ISGID, 's', S_IEXEC>>3, 'x', '-' };
-int    m7[] = { 1, S_IREAD>>6, 'r', '-' };
-int    m8[] = { 1, S_IWRITE>>6, 'w', '-' };
-int    m9[] = { 2, S_ISVTX, 't', S_IEXEC>>6, 'x', '-' };
-
-int    *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
-
-char *
-fmtmode(lp, flags)
-       char *lp;
-       int flags;
+tabdir(lp, s_stats, s_names)
+       LS *lp, **s_stats;
+       char **s_names;
 {
 {
-       int **mp;
-
-       for (mp = &m[0]; mp < &m[sizeof(m)/sizeof(m[0])]; ) {
-               register int *pairp = *mp++;
-               register int n = *pairp++;
-
-               while (--n >= 0 && (flags&*pairp++) == 0)
-                       pairp++;
-               *lp++ = *pairp;
+       register DIR *dirp;
+       register int cnt, maxentry, maxlen;
+       register char *p, *names;
+       struct dirent *dp;
+       u_long blocks;
+       LS *stats;
+
+       if (!(dirp = opendir("."))) {
+               (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
+                   strerror(errno));
+               return(0);
        }
        }
-       return (lp);
-}
-
-/* rest should be done with nameserver or database */
-
-#include <pwd.h>
-#include <grp.h>
-#include <utmp.h>
-
-struct utmp utmp;
-
-#define NUID   2048
-#define NGID   300
-#define        NMAX    (sizeof (utmp.ut_name))
-
-char   names[NUID][NMAX+1];
-char   groups[NGID][NMAX+1];
-
-char *
-getname(uid)
-{
-       register struct passwd *pw;
-       static init;
-       struct passwd *getpwent();
-
-       if (uid >= 0 && uid < NUID && names[uid][0])
-               return (&names[uid][0]);
-       if (init == 2)
-               return (0);
-       if (init == 0)
-               setpwent(), init = 1;
-       while (pw = getpwent()) {
-               if (pw->pw_uid < 0 || pw->pw_uid >= NUID)
-                       continue;
-               if (names[pw->pw_uid][0])
-                       continue;
-               strncpy(names[pw->pw_uid], pw->pw_name, NMAX);
-               if (pw->pw_uid == uid) {
-                       return (&names[uid][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;
                }
                }
-       }
-       init = 2;
-       endpwent();
-       return (0);
-}
-
-char *
-getgroup(gid)
-{
-       register struct group *gr;
-       static init;
-       struct group *getgrent();
-
-       if (gid >= 0 && gid < NGID && groups[gid][0])
-               return (&groups[gid][0]);
-       if (init == 2)
-               return (0);
-       if (init == 0)
-               setgrent(), init = 1;
-       while (gr = getgrent()) {
-               if (gr->gr_gid < 0 || gr->gr_gid >= NGID)
-                       continue;
-               if (groups[gr->gr_gid][0])
+               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 && 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);
+                       (void)fprintf(stderr,
+                           "ls: %s: %s\n", dp->d_name, strerror(errno));
                        continue;
                        continue;
-               strncpy(groups[gr->gr_gid], gr->gr_name, NMAX);
-               if (gr->gr_gid == gid) {
-                       return (&groups[gid][0]);
                }
                }
+               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;
+       }
+       (void)closedir(dirp);
+
+       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);
        }
        }
-       init = 2;
-       endgrent();
-       return (0);
+       return(cnt);
 }
 }