oops. Got stat test logic wrong
[unix-history] / usr / src / bin / ls / ls.c
index 382c936..22a75a3 100644 (file)
@@ -15,7 +15,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ls.c       5.42 (Berkeley) %G%";
+static char sccsid[] = "@(#)ls.c       5.48 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -50,6 +50,7 @@ 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_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_singlecol;               /* use single column output */
 int f_size;                    /* list size in short listing */
 int f_statustime;              /* use time of last mode change */
@@ -58,6 +59,8 @@ 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 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;
        char **argv;
 main(argc, argv)
        int argc;
        char **argv;
@@ -87,7 +90,7 @@ main(argc, argv)
        if (!getuid())
                f_listdot = 1;
 
        if (!getuid())
                f_listdot = 1;
 
-       while ((ch = getopt(argc, argv, "1ACFLRacdfgiklqrstu")) != EOF) {
+       while ((ch = getopt(argc, argv, "1ACFLRTacdfgiklqrstu")) != EOF) {
                switch (ch) {
                /*
                 * -1, -C and -l all override each other
                switch (ch) {
                /*
                 * -1, -C and -l all override each other
@@ -155,6 +158,9 @@ main(argc, argv)
                case 's':
                        f_size = 1;
                        break;
                case 's':
                        f_size = 1;
                        break;
+               case 'T':
+                       f_sectime = 1;
+                       break;
                case 't':
                        f_timesort = 1;
                        break;
                case 't':
                        f_timesort = 1;
                        break;
@@ -203,9 +209,15 @@ main(argc, argv)
        else
                printfcn = printcol;
 
        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) {
        if (!argc) {
+               static char dot[] = ".";
+
                argc = 1;
                argc = 1;
-               argv[0] = ".";
+               argv[0] = dot;
                argv[1] = NULL;
        }
        doargs(argc, argv);
                argv[1] = NULL;
        }
        doargs(argc, argv);
@@ -223,7 +235,6 @@ doargs(argc, argv)
        register int cnt, dircnt, maxlen, regcnt;
        LS *dstats, *rstats;
        struct stat sb;
        register int cnt, dircnt, maxlen, regcnt;
        LS *dstats, *rstats;
        struct stat sb;
-       int (*statfcn)(), stat(), lstat();
        char top[MAXPATHLEN + 1];
        u_long blocks;
 
        char top[MAXPATHLEN + 1];
        u_long blocks;
 
@@ -232,16 +243,14 @@ doargs(argc, argv)
         * structures for directory and non-directory files.
         */
        dstats = rstats = NULL;
         * 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) {
        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 (statfcn(*argv, &sb) &&
+                   (statfcn == lstat || lstat(*argv, &sb))) {
+                       (void)fprintf(stderr,
+                           "ls: %s: %s\n", *argv, strerror(errno));
+                       if (errno == ENOENT)
+                               continue;
+                       exit(1);
                }
                if (S_ISDIR(sb.st_mode) && !f_listdir) {
                        if (!dstats)
                }
                if (S_ISDIR(sb.st_mode) && !f_listdir) {
                        if (!dstats)
@@ -413,14 +422,15 @@ tabdir(lp, s_stats, s_names)
                            (u_int)maxentry * sizeof(LS))))
                                nomem();
                }
                            (u_int)maxentry * sizeof(LS))))
                                nomem();
                }
-               if (f_needstat && lstat(dp->d_name, &stats[cnt].lstat)) {
+               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);
                        /*
                         * 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));
+                       (void)fprintf(stderr,
+                           "ls: %s: %s\n", dp->d_name, strerror(errno));
                        continue;
                }
                stats[cnt].name = names;
                        continue;
                }
                stats[cnt].name = names;