don't exit if stat fails; could reasonably happen under NFS
[unix-history] / usr / src / bin / ls / ls.c
index f58f473..e5ea3b1 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,7 +15,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ls.c       5.33 (Berkeley) %G%";
+static char sccsid[] = "@(#)ls.c       5.40 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -57,6 +47,7 @@ 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_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_singlecol;               /* use single column output */
 int f_recursive;               /* ls subdirectories also */
 int f_reversesort;             /* reverse whatever sort is used */
 int f_singlecol;               /* use single column output */
@@ -96,7 +87,7 @@ main(argc, argv)
        if (!getuid())
                f_listdot = 1;
 
        if (!getuid())
                f_listdot = 1;
 
-       while ((ch = getopt(argc, argv, "1ACFLRacdgiklqrstu")) != EOF) {
+       while ((ch = getopt(argc, argv, "1ACFLRacdfgiklqrstu")) != EOF) {
                switch (ch) {
                /*
                 * -1, -C and -l all override each other
                switch (ch) {
                /*
                 * -1, -C and -l all override each other
@@ -143,6 +134,9 @@ main(argc, argv)
                case 'd':
                        f_listdir = 1;
                        break;
                case 'd':
                        f_listdir = 1;
                        break;
+               case 'f':
+                       f_nosort = 1;
+                       break;
                case 'g':
                        f_group = 1;
                        break;
                case 'g':
                        f_group = 1;
                        break;
@@ -324,7 +318,7 @@ displaydir(stats, num)
        register char *p, *savedpath;
        LS *lp;
 
        register char *p, *savedpath;
        LS *lp;
 
-       if (num > 1) {
+       if (num > 1 && !f_nosort) {
                u_long save1, save2;
 
                save1 = stats[0].lstat.st_btotal;
                u_long save1, save2;
 
                save1 = stats[0].lstat.st_btotal;
@@ -371,10 +365,11 @@ subdir(lp)
                     strerror(errno));
                return;
        }
                     strerror(errno));
                return;
        }
-       if (num = tabdir(lp, &stats, &names))
+       if (num = tabdir(lp, &stats, &names)) {
                displaydir(stats, num);
                displaydir(stats, num);
-       (void)free((char *)stats);
-       (void)free((char *)names);
+               (void)free((char *)stats);
+               (void)free((char *)names);
+       }
        if (chdir("..")) {
                (void)fprintf(stderr, "ls: ..: %s\n", strerror(errno));
                exit(1);
        if (chdir("..")) {
                (void)fprintf(stderr, "ls: ..: %s\n", strerror(errno));
                exit(1);
@@ -392,23 +387,13 @@ tabdir(lp, s_stats, s_names)
        u_long blocks;
        LS *stats;
 
        u_long blocks;
        LS *stats;
 
-       /*
-        * allocate space for array of LS structures and the file names
-        * the name field will point to.  Make it big so we don't have
-        * to realloc often.
-        */
-#define        DEFNUM  256
-       maxentry = DEFNUM;
-       stats = (LS *)emalloc((u_int)DEFNUM * sizeof(LS));
-       names = emalloc((u_int)lp->lstat.st_size);
-
        if (!(dirp = opendir("."))) {
                (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
                    strerror(errno));
                return(0);
        }
        if (!(dirp = opendir("."))) {
                (void)fprintf(stderr, "ls: %s: %s\n", lp->name,
                    strerror(errno));
                return(0);
        }
-       blocks = 0;
-       maxlen = -1;
+       blocks = maxentry = maxlen = 0;
+       stats = NULL;
        for (cnt = 0; dp = readdir(dirp);) {
                /* this does -A and -a */
                p = dp->d_name;
        for (cnt = 0; dp = readdir(dirp);) {
                /* this does -A and -a */
                p = dp->d_name;
@@ -419,17 +404,24 @@ tabdir(lp, s_stats, s_names)
                                continue;
                }
                if (cnt == maxentry) {
                                continue;
                }
                if (cnt == maxentry) {
-                       maxentry *= 2;
-                       if (!(stats = (LS *)realloc((char *)stats,
+                       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)) {
                            (u_int)maxentry * sizeof(LS))))
                                nomem();
                }
                if (f_needstat && 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));
                        (void)fprintf(stderr, "ls: %s: %s\n",
                            dp->d_name, strerror(errno));
-                       if (errno == ENOENT)
-                               continue;
-                       exit(1);
+                       continue;
                }
                stats[cnt].name = names;
 
                }
                stats[cnt].name = names;
 
@@ -458,10 +450,14 @@ tabdir(lp, s_stats, s_names)
                        maxlen = dp->d_namlen;
                ++cnt;
        }
                        maxlen = dp->d_namlen;
                ++cnt;
        }
-       *s_stats = stats;
-       *s_names = names;
-       stats[0].lstat.st_btotal = blocks;
-       stats[0].lstat.st_maxlen = maxlen;
-       closedir(dirp);
+       (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);
+       }
        return(cnt);
 }
        return(cnt);
 }