prettyness police
[unix-history] / usr / src / bin / ls / ls.c
index 4451a52..0c49aa4 100644 (file)
@@ -1,6 +1,6 @@
 /*
 /*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Michael Fischbein.
  *
  * This code is derived from software contributed to Berkeley by
  * Michael Fischbein.
@@ -9,34 +9,32 @@
  */
 
 #ifndef lint
  */
 
 #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\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ls.c       5.63 (Berkeley) %G%";
+static char sccsid[] = "@(#)ls.c       8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+
 #include <dirent.h>
 #include <dirent.h>
-#include <unistd.h>
+#include <err.h>
+#include <errno.h>
 #include <fts.h>
 #include <fts.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
-#include <stdio.h>
+#include <unistd.h>
+
 #include "ls.h"
 #include "extern.h"
 
 #include "ls.h"
 #include "extern.h"
 
-char   *getbsize __P((char *, int *, long *));
-char   *group_from_gid __P((u_int, int));
-char   *user_from_uid __P((u_int, int));
-
 static void     display __P((FTSENT *, FTSENT *));
 static void     display __P((FTSENT *, FTSENT *));
-static char    *flags_from_fid __P((u_long));
 static int      mastercmp __P((const FTSENT **, const FTSENT **));
 static void     traverse __P((int, char **, int));
 
 static int      mastercmp __P((const FTSENT **, const FTSENT **));
 static void     traverse __P((int, char **, int));
 
@@ -81,7 +79,7 @@ main(argc, argv)
        if (isatty(STDOUT_FILENO)) {
                if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
                    !win.ws_col) {
        if (isatty(STDOUT_FILENO)) {
                if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
                    !win.ws_col) {
-                       if (p = getenv("COLUMNS"))
+                       if ((p = getenv("COLUMNS")) != NULL)
                                termwidth = atoi(p);
                }
                else
                                termwidth = atoi(p);
                }
                else
@@ -95,7 +93,7 @@ main(argc, argv)
                f_listdot = 1;
 
        fts_options = FTS_PHYSICAL;
                f_listdot = 1;
 
        fts_options = FTS_PHYSICAL;
-       while ((ch = getopt(argc, argv, "1ACFLRTacdfgikloqrstu")) != EOF) {
+       while ((ch = getopt(argc, argv, "1ACFLRTacdfgiloqrstu")) != EOF) {
                switch (ch) {
                /*
                 * The -1, -C and -l options all override each other so shell
                switch (ch) {
                /*
                 * The -1, -C and -l options all override each other so shell
@@ -153,9 +151,6 @@ main(argc, argv)
                case 'i':
                        f_inode = 1;
                        break;
                case 'i':
                        f_inode = 1;
                        break;
-               case 'k':               /* Delete before 4.4BSD. */
-                       (void)fprintf(stderr, "ls: -k no longer supported\n");
-                       break;
                case 'o':
                        f_flags = 1;
                        break;
                case 'o':
                        f_flags = 1;
                        break;
@@ -198,7 +193,7 @@ main(argc, argv)
 
        /* If -l or -s, figure out block size. */
        if (f_longform || f_size) {
 
        /* If -l or -s, figure out block size. */
        if (f_longform || f_size) {
-               (void)getbsize("ls", &notused, &blocksize);
+               (void)getbsize(&notused, &blocksize);
                blocksize /= 512;
        }
 
                blocksize /= 512;
        }
 
@@ -251,13 +246,13 @@ traverse(argc, argv, options)
        int argc, options;
        char *argv[];
 {
        int argc, options;
        char *argv[];
 {
-       register FTS *ftsp;
-       register FTSENT *p;
+       FTS *ftsp;
+       FTSENT *p, *chp;
        int ch_options;
 
        if ((ftsp =
            fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
        int ch_options;
 
        if ((ftsp =
            fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
-               err(1, "fts_open: %s", strerror(errno));
+               err(1, NULL);
 
        display(NULL, fts_children(ftsp, 0));
        if (f_listdir)
 
        display(NULL, fts_children(ftsp, 0));
        if (f_listdir)
@@ -269,15 +264,15 @@ traverse(argc, argv, options)
         */
        ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
 
         */
        ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
 
-       while (p = fts_read(ftsp))
-               switch(p->fts_info) {
+       while ((p = fts_read(ftsp)) != NULL)
+               switch (p->fts_info) {
                case FTS_DC:
                case FTS_DC:
-                       err(0, "%s: directory causes a cycle", p->fts_name);
+                       warnx("%s: directory causes a cycle", p->fts_name);
                        break;
                case FTS_DNR:
                case FTS_ERR:
                        break;
                case FTS_DNR:
                case FTS_ERR:
-                       err(0, "%s: %s",
-                           p->fts_name, strerror(p->fts_errno));
+                       errno = p->fts_errno;
+                       warn("%s", p->fts_name);
                        break;
                case FTS_D:
                        if (p->fts_level != FTS_ROOTLEVEL &&
                        break;
                case FTS_D:
                        if (p->fts_level != FTS_ROOTLEVEL &&
@@ -296,9 +291,10 @@ traverse(argc, argv, options)
                                output = 1;
                        }
 
                                output = 1;
                        }
 
-                       display(p, fts_children(ftsp, ch_options));
+                       chp = fts_children(ftsp, ch_options);
+                       display(p, chp);
 
 
-                       if (!f_recursive)
+                       if (!f_recursive && chp != NULL)
                                (void)fts_set(ftsp, p, FTS_SKIP);
                        break;
                }
                                (void)fts_set(ftsp, p, FTS_SKIP);
                        break;
                }
@@ -306,22 +302,21 @@ traverse(argc, argv, options)
 }
 
 /*
 }
 
 /*
- * Display() takes a linked list of FTSENT structures passes the list along
- * with any other necessary information to the print function (printfcn()).
- * points to the parent directory of the display list.
+ * 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
 display(p, list)
  */
 static void
 display(p, list)
-       register FTSENT *p;
-       FTSENT *list;
+       FTSENT *p, *list;
 {
 {
-       register FTSENT *cur;
        struct stat *sp;
        DISPLAY d;
        struct stat *sp;
        DISPLAY d;
+       FTSENT *cur;
        NAMES *np;
        NAMES *np;
-       u_long btotal, flen, glen, ulen;
-       u_long maxblock, maxgroup, maxinode, maxlen, maxnlink, maxsize;
-       u_long maxuser, maxflags;
+       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 */
 
        int entries, needstats;
        char *user, *group, *flags, buf[20];    /* 32 bits == 10 digits */
 
@@ -337,12 +332,14 @@ display(p, list)
 
        needstats = f_inode || f_longform || f_size;
        flen = 0;
 
        needstats = f_inode || f_longform || f_size;
        flen = 0;
-       btotal = maxblock = maxinode = maxlen = maxnlink = maxsize = 0;
+       btotal = maxblock = maxinode = maxlen = maxnlink = 0;
+       bcfile = 0;
        maxuser = maxgroup = maxflags = 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) {
        for (cur = list, entries = 0; cur; cur = cur->fts_link) {
                if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
-                       err(0, "%s: %s",
-                           cur->fts_name, strerror(cur->fts_errno));
+                       errno = cur->fts_errno;
+                       warn("%s", cur->fts_name);
                        cur->fts_number = NO_PRINT;
                        continue;
                }
                        cur->fts_number = NO_PRINT;
                        continue;
                }
@@ -388,7 +385,8 @@ display(p, list)
                                if ((glen = strlen(group)) > maxgroup)
                                        maxgroup = glen;
                                if (f_flags) {
                                if ((glen = strlen(group)) > maxgroup)
                                        maxgroup = glen;
                                if (f_flags) {
-                                       flags = flags_from_fid(sp->st_flags);
+                                       flags =
+                                           flags_to_string(sp->st_flags, "-");
                                        if ((flen = strlen(flags)) > maxflags)
                                                maxflags = flen;
                                } else
                                        if ((flen = strlen(flags)) > maxflags)
                                                maxflags = flen;
                                } else
@@ -396,13 +394,17 @@ display(p, list)
 
                                if ((np = malloc(sizeof(NAMES) +
                                    ulen + glen + flen + 3)) == NULL)
 
                                if ((np = malloc(sizeof(NAMES) +
                                    ulen + glen + flen + 3)) == NULL)
-                                       err(1, "%s", strerror(errno));
+                                       err(1, NULL);
 
                                np->user = &np->data[0];
                                (void)strcpy(np->user, user);
                                np->group = &np->data[ulen + 1];
                                (void)strcpy(np->group, group);
 
 
                                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);
                                if (f_flags) {
                                        np->flags = &np->data[ulen + glen + 2];
                                        (void)strcpy(np->flags, flags);
@@ -420,13 +422,18 @@ display(p, list)
        d.entries = entries;
        d.maxlen = maxlen;
        if (needstats) {
        d.entries = entries;
        d.maxlen = maxlen;
        if (needstats) {
+               d.bcfile = bcfile;
                d.btotal = btotal;
                d.btotal = btotal;
-               d.s_block = snprintf(buf, sizeof(buf), "%d", maxblock);
+               (void)snprintf(buf, sizeof(buf), "%lu", maxblock);
+               d.s_block = strlen(buf);
                d.s_flags = maxflags;
                d.s_group = maxgroup;
                d.s_flags = maxflags;
                d.s_group = maxgroup;
-               d.s_inode = snprintf(buf, sizeof(buf), "%d", maxinode);
-               d.s_nlink = snprintf(buf, sizeof(buf), "%d", maxnlink);
-               d.s_size = snprintf(buf, sizeof(buf), "%d", maxsize);
+               (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;
        }
 
                d.s_user = maxuser;
        }
 
@@ -448,7 +455,7 @@ static int
 mastercmp(a, b)
        const FTSENT **a, **b;
 {
 mastercmp(a, b)
        const FTSENT **a, **b;
 {
-       register int a_info, b_info;
+       int a_info, b_info;
 
        a_info = (*a)->fts_info;
        if (a_info == FTS_ERR)
 
        a_info = (*a)->fts_info;
        if (a_info == FTS_ERR)
@@ -473,35 +480,3 @@ mastercmp(a, b)
        else
                return (sortfcn(*a, *b));
 }
        else
                return (sortfcn(*a, *b));
 }
-
-static char *
-flags_from_fid(flags)
-       u_long flags;
-{
-       static char buf[20];
-       register int comma;
-       register char *p;
-
-       p = buf;
-       if (flags & ARCHIVED) {
-               (void)strcpy(p, "arch");
-               p += sizeof("arch") - 1;
-               comma = 1;
-       } else
-               comma = 0;
-       if (flags & NODUMP) {
-               if (comma++)
-                       *p++ = ',';
-               (void)strcpy(p, "nodump");
-               p += sizeof("nodump") - 1;
-       }
-       if (flags & IMMUTABLE) {
-               if (comma++)
-                       *p++ = ',';
-               (void)strcpy(p, "nochg");
-               p += sizeof("nochg") - 1;
-       }
-       if (!comma)
-               (void)strcpy(p, "-");
-       return (buf);
-}