ls patches

From: Jeremy Fitzhardinge <jeremy_at_nospam.org>
Date: Thu Nov 03 1994 - 04:35:48 PST

Here's my patches to ls. They arn't optimal; files get stat()ed
more than they need to and the output could be nicer, but I didn't
want to rewrite it.

        J

--- 1.4 1993/05/03 21:32:15
+++ ls.c 1994/10/30 15:13:24
@@ -9,6 +9,8 @@
 #include <sys/param.h>
 #include <sys/fs.h>
 #include <sys/perm.h>
+#include <sys/stat.h>
+#include <string.h>
 #include <fcntl.h>
 
 static int ndir; /* # dirs being displayed */
@@ -16,6 +18,13 @@
 
 static int lflag = 0; /* -l flag */
 
+static int sort(const void *v1, const void *v2)
+{
+ const char *s1 = *(const char **)v1;
+ const char *s2 = *(const char **)v2;
+ return strcmp(s1, s2);
+}
+
 /*
  * prcols()
  * Print array of strings in columnar fashion
@@ -36,6 +45,7 @@
                 }
         }
 
+ qsort((void *)v, nelem, sizeof(char *), sort);
         /*
          * Calculate how many columns that makes, and how many
          * entries will end up in each column.
@@ -269,12 +279,33 @@
         struct dirent *de;
         char **v = 0;
         int nelem;
+ struct stat st;
 
- /*
- * Prefix with name of dir if multiples
- */
- if (ndir > 1) {
- printf("%s:\n", path);
+ if (stat(path, &st)) {
+ perror("stat failed");
+ return;
+ }
+ if (!S_ISDIR(st.st_mode)) {
+ if (lflag) {
+ struct dirent de;
+
+ de.d_namlen = strlen(path);
+ strcpy(de.d_name, path);
+ ls_l(&de);
+ } else {
+ char *v[2];
+ v[0] = path;
+ v[1] = 0;
+ prcols(v);
+ }
+ return;
+ } else {
+ /*
+ * Prefix with name of dir if multiples
+ */
+ if (ndir > 1) {
+ printf("%s:\n", path);
+ }
         }
 
         /*
Received on Thu Nov 3 03:14:45 1994

This archive was generated by hypermail 2.1.8 : Thu Sep 22 2005 - 15:12:10 PDT