changeset 491:176667e320f0

Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
author Andre Renaud <andre@bluewatersys.com>
date Tue, 21 Feb 2012 20:48:52 -0600
parents 96a5e66a7dae
children f3169b2492f1
files toys/ls.c
diffstat 1 files changed, 31 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/toys/ls.c	Tue Feb 21 19:45:19 2012 -0600
+++ b/toys/ls.c	Tue Feb 21 20:48:52 2012 -0600
@@ -6,7 +6,7 @@
  *
  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html
 
-USE_LS(NEWTOY(ls, "RlF1a", TOYFLAG_BIN))
+USE_LS(NEWTOY(ls, "nRlF1a", TOYFLAG_BIN))
 
 config LS
 	bool "ls"
@@ -21,6 +21,14 @@
           -l    show full details for each file
 */
 
+/* So that we can do 64-bit stat etc... */
+#define _FILE_OFFSET_BITS 64
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <grp.h>
+#include <pwd.h>
+
 #include "toys.h"
 
 #define FLAG_a 1
@@ -28,6 +36,7 @@
 #define FLAG_F 4
 #define FLAG_l 8
 #define FLAG_R 16
+#define FLAG_n 32
 
 static int dir_filter(const struct dirent *d)
 {
@@ -134,13 +143,25 @@
                 (st.st_mode & S_IXOTH) ? 'x' : '-');
 
             xprintf("%2d ", st.st_nlink);
-            /* FIXME: We're never looking up uid/gid numbers */
-            xprintf("%4d ", st.st_uid);
-            xprintf("%4d ", st.st_gid);
+            if (toys.optflags & FLAG_n) {
+                xprintf("%4d ", st.st_uid);
+                xprintf("%4d ", st.st_gid);
+            } else {
+                struct passwd *pwd = getpwuid(st.st_uid);
+                struct group *grp = getgrgid(st.st_gid);
+                if (!pwd)
+                    xprintf("%4d ", st.st_uid);
+                else
+                    xprintf("%-10s ", pwd->pw_name);
+                if (!grp)
+                    xprintf("%4d ", st.st_gid);
+                else
+                    xprintf("%-10s ", grp->gr_name);
+            }
             if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
                 xprintf("%3d, %3d ", major(st.st_rdev), minor(st.st_rdev));
             else
-                xprintf("%8ld ", st.st_size);
+                xprintf("%12lld ", st.st_size);
 
             localtime_r(&st.st_mtime, &mtime);
 
@@ -199,6 +220,11 @@
 
 void ls_main(void)
 {
+    /* If the output is not a TTY, then just do one-file per line
+     * This makes ls easier to use with other command line tools (grep/awk etc...)
+     */
+    if (!isatty(fileno(stdout)))
+        toys.optflags |= FLAG_1;
     /* Long output must be one-file per line */
     if (toys.optflags & FLAG_l)
         toys.optflags |= FLAG_1;