Remember to delete un-expandable variables, and do a better job of expanding
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index fa3e5424d831a90c22bcebd5e239fa23b5d4c3fc..a8d7b182ff59b7279173b5a4f6e2e6c79ea48243 100644 (file)
--- a/ls.c
+++ b/ls.c
  * 1. requires lstat (BSD) - how do you do it without?
  */
 
-#define TERMINAL_WIDTH 80              /* use 79 if your terminal has linefold bug */
-#define COLUMN_WIDTH   14              /* default if AUTOWIDTH not defined */
-#define COLUMN_GAP     2                       /* includes the file type char, if present */
+enum {
+       TERMINAL_WIDTH = 80,            /* use 79 if terminal has linefold bug */
+       COLUMN_WIDTH = 14,                      /* default if AUTOWIDTH not defined */
+       COLUMN_GAP = 2,                         /* includes the file type char */
+};
+
 
 /************************************************************************/
 
-#include "busybox.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdio.h>
 #include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/ioctl.h>
+#include "busybox.h"
+
 #ifdef BB_FEATURE_LS_TIMESTAMPS
 #include <time.h>
 #endif
-#include <string.h>
 
-#ifndef NAJOR
+#ifndef MAJOR
 #define MAJOR(dev) (((dev)>>8)&0xff)
 #define MINOR(dev) ((dev)&0xff)
 #endif
 
 /* what is the overall style of the listing */
-#define STYLE_AUTO             0
-#define STYLE_LONG             1               /* one record per line, extended info */
-#define STYLE_SINGLE   2               /* one record per line */
-#define STYLE_COLUMNS  3               /* fill columns */
+enum {
+STYLE_AUTO = 0,
+STYLE_LONG = 1,                /* one record per line, extended info */
+STYLE_SINGLE = 2,              /* one record per line */
+STYLE_COLUMNS = 3              /* fill columns */
+};
 
 /* 51306 lrwxrwxrwx  1 root     root         2 May 11 01:43 /bin/view -> vi* */
 /* what file information will be listed */
 
 #ifdef BB_FEATURE_LS_SORTFILES
 /* how will the files be sorted */
-#define SORT_FORWARD    0              /* sort in reverse order */
-#define SORT_REVERSE    1              /* sort in reverse order */
-#define SORT_NAME              2               /* sort by file name */
-#define SORT_SIZE              3               /* sort by file size */
-#define SORT_ATIME             4               /* sort by last access time */
-#define SORT_CTIME             5               /* sort by last change time */
-#define SORT_MTIME             6               /* sort by last modification time */
-#define SORT_VERSION   7               /* sort by version */
-#define SORT_EXT               8               /* sort by file name extension */
-#define SORT_DIR               9               /* sort by file or directory */
+static const int SORT_FORWARD = 0;             /* sort in reverse order */
+static const int SORT_REVERSE = 1;             /* sort in reverse order */
+static const int SORT_NAME = 2;                /* sort by file name */
+static const int SORT_SIZE = 3;                /* sort by file size */
+static const int SORT_ATIME = 4;               /* sort by last access time */
+static const int SORT_CTIME = 5;               /* sort by last change time */
+static const int SORT_MTIME = 6;               /* sort by last modification time */
+static const int SORT_VERSION = 7;             /* sort by version */
+static const int SORT_EXT = 8;         /* sort by file name extension */
+static const int SORT_DIR = 9;         /* sort by file or directory */
 #endif
 
 #ifdef BB_FEATURE_LS_TIMESTAMPS
 /* which of the three times will be used */
-#define TIME_MOD    0
-#define TIME_CHANGE 1
-#define TIME_ACCESS 2
+static const int TIME_MOD = 0;
+static const int TIME_CHANGE = 1;
+static const int TIME_ACCESS = 2;
 #endif
 
 #define LIST_SHORT             (LIST_FILENAME)
                                                LIST_SYMLINK)
 #define LIST_ILONG             (LIST_INO | LIST_LONG)
 
-#define SPLIT_DIR              0
-#define SPLIT_FILE             1
-#define SPLIT_SUBDIR   2
+static const int SPLIT_DIR = 0;
+static const int SPLIT_FILE = 1;
+static const int SPLIT_SUBDIR = 2;
 
 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
 #define TYPECHAR(mode)  ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
@@ -150,15 +160,15 @@ struct dnode **list_dir(char *);
 struct dnode **dnalloc(int);
 int list_single(struct dnode *);
 
-static unsigned int disp_opts= DISP_NORMAL;
-static unsigned int style_fmt= STYLE_AUTO ;
-static unsigned int list_fmt=  LIST_SHORT ;
+static unsigned int disp_opts;
+static unsigned int style_fmt;
+static unsigned int list_fmt;
 #ifdef BB_FEATURE_LS_SORTFILES
-static unsigned int sort_opts= SORT_FORWARD;
-static unsigned int sort_order=        SORT_FORWARD;
+static unsigned int sort_opts;
+static unsigned int sort_order;
 #endif
 #ifdef BB_FEATURE_LS_TIMESTAMPS
-static unsigned int time_fmt=  TIME_MOD;
+static unsigned int time_fmt;
 #endif
 #ifdef BB_FEATURE_LS_FOLLOWLINKS
 static unsigned int follow_links=FALSE;
@@ -168,14 +178,17 @@ static unsigned short column = 0;
 #ifdef BB_FEATURE_AUTOWIDTH
 static unsigned short terminal_width = TERMINAL_WIDTH;
 static unsigned short column_width = COLUMN_WIDTH;
-static unsigned short tabstops = 8;
+static unsigned short tabstops = COLUMN_GAP;
 #else
-#define terminal_width  TERMINAL_WIDTH
-#define column_width    COLUMN_WIDTH
+static unsigned short column_width = COLUMN_WIDTH;
 #endif
 
 static int status = EXIT_SUCCESS;
 
+#ifdef BB_FEATURE_HUMAN_READABLE
+unsigned long ls_disp_hr = 0;
+#endif
+
 static int my_stat(struct dnode *cur)
 {
 #ifdef BB_FEATURE_LS_FOLLOWLINKS
@@ -233,7 +246,7 @@ static void nexttabstop( void )
                        column++;
                }
        }
-       nexttab= column + column_width + COLUMN_GAP ;
+       nexttab= column + column_width + COLUMN_GAP
 }
 
 /*----------------------------------------------------------------------*/
@@ -424,10 +437,18 @@ void showfiles(struct dnode **dn, int nfiles)
                        ((list_fmt & LIST_INO) ? 8 : 0) +
                        ((list_fmt & LIST_BLOCKS) ? 5 : 0)
                        ;
-               if (column_width < len) column_width= len;
+               if (column_width < len) 
+                       column_width= len;
        }
+       if (column_width >= 6)
+               ncols = (int)(terminal_width / (column_width + COLUMN_GAP));
+       else {
+               ncols = 1;
+               column_width = COLUMN_WIDTH;
+       }
+#else
+       ncols= TERMINAL_WIDTH;
 #endif
-       ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
        switch (style_fmt) {
                case STYLE_LONG:        /* one record per line, extended info */
                case STYLE_SINGLE:      /* one record per line */
@@ -435,7 +456,12 @@ void showfiles(struct dnode **dn, int nfiles)
                        break;
        }
 
-       nrows= nfiles / ncols;
+       if (ncols > 1) {
+               nrows = nfiles / ncols;
+       } else {
+               nrows = nfiles;
+               ncols = 1;
+       }
        if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */
 
        if (nrows > nfiles) nrows= nfiles;
@@ -583,10 +609,15 @@ int list_single(struct dnode *dn)
                                column += 8;
                                break;
                        case LIST_BLOCKS:
+#ifdef BB_FEATURE_HUMAN_READABLE
+                               fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1,
+                                                       (ls_disp_hr==TRUE)? 0: 1));
+#else
 #if _FILE_OFFSET_BITS == 64
                                printf("%4lld ", dn->dstat.st_blocks>>1);
 #else
                                printf("%4ld ", dn->dstat.st_blocks>>1);
+#endif
 #endif
                                column += 5;
                                break;
@@ -601,15 +632,9 @@ int list_single(struct dnode *dn)
                        case LIST_ID_NAME:
 #ifdef BB_FEATURE_LS_USERNAME
                                my_getpwuid(scratch, dn->dstat.st_uid);
-                               if (*scratch)
-                                       printf("%-8.8s ", scratch);
-                               else
-                                       printf("%-8d ", dn->dstat.st_uid);
+                               printf("%-8.8s ", scratch);
                                my_getgrgid(scratch, dn->dstat.st_gid);
-                               if (*scratch)
-                                       printf("%-8.8s", scratch);
-                               else
-                                       printf("%-8d", dn->dstat.st_gid);
+                               printf("%-8.8s", scratch);
                                column += 17;
                                break;
 #endif
@@ -622,10 +647,15 @@ int list_single(struct dnode *dn)
                                if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
                                        printf("%4d, %3d ", (int)MAJOR(dn->dstat.st_rdev), (int)MINOR(dn->dstat.st_rdev));
                                } else {
+#ifdef BB_FEATURE_HUMAN_READABLE
+                                       fprintf(stdout, "%9s ", make_human_readable_str(dn->dstat.st_size,
+                                                               (ls_disp_hr==TRUE)? 0: 1));
+#else
 #if _FILE_OFFSET_BITS == 64
-                                       printf("%9lld ", dn->dstat.st_size);
+                                       printf("%9lld ", dn->dstat.st_size>>1);
 #else
-                                       printf("%9ld ", dn->dstat.st_size);
+                                       printf("%9ld ", dn->dstat.st_size>>1);
+#endif
 #endif
                                }
                                column += 10;
@@ -692,15 +722,26 @@ extern int ls_main(int argc, char **argv)
        int opt;
        int oi, ac;
        char **av;
+#ifdef BB_FEATURE_AUTOWIDTH
+       struct winsize win = { 0, 0, 0, 0 };
+#endif
 
        disp_opts= DISP_NORMAL;
        style_fmt= STYLE_AUTO;
        list_fmt=  LIST_SHORT;
 #ifdef BB_FEATURE_LS_SORTFILES
        sort_opts= SORT_NAME;
+       sort_order=     SORT_FORWARD;
 #endif
 #ifdef BB_FEATURE_LS_TIMESTAMPS
        time_fmt= TIME_MOD;
+#endif
+#ifdef BB_FEATURE_AUTOWIDTH
+       ioctl(fileno(stdout), TIOCGWINSZ, &win);
+       if (win.ws_row > 4)
+               column_width = win.ws_row - 2;
+       if (win.ws_col > 0)
+               terminal_width = win.ws_col - 1;
 #endif
        nfiles=0;
 
@@ -724,7 +765,10 @@ extern int ls_main(int argc, char **argv)
 #ifdef BB_FEATURE_LS_FOLLOWLINKS
 "L"
 #endif
-       )) > 0) {
+#ifdef BB_FEATURE_HUMAN_READABLE
+"h"
+#endif
+"k")) > 0) {
                switch (opt) {
                        case '1': style_fmt = STYLE_SINGLE; break;
                        case 'A': disp_opts |= DISP_HIDDEN; break;
@@ -733,7 +777,13 @@ extern int ls_main(int argc, char **argv)
                        case 'd': disp_opts |= DISP_NOLIST; break;
                        case 'g': /* ignore -- for ftp servers */ break;
                        case 'i': list_fmt |= LIST_INO; break;
-                       case 'l': style_fmt = STYLE_LONG; list_fmt |= LIST_LONG; break;
+                       case 'l':
+                               style_fmt = STYLE_LONG;
+                               list_fmt |= LIST_LONG;
+#ifdef BB_FEATURE_HUMAN_READABLE
+                               ls_disp_hr = FALSE;
+#endif
+                       break;
                        case 'n': list_fmt |= LIST_ID_NUMERIC; break;
                        case 's': list_fmt |= LIST_BLOCKS; break;
                        case 'x': disp_opts = DISP_ROWS; break;
@@ -777,6 +827,10 @@ extern int ls_main(int argc, char **argv)
                        case 'T': tabstops= atoi(optarg); break;
                        case 'w': terminal_width= atoi(optarg); break;
 #endif
+#ifdef BB_FEATURE_HUMAN_READABLE
+                       case 'h': ls_disp_hr = TRUE; break;
+#endif
+                       case 'k': break;
                        default:
                                goto print_usage_message;
                }
@@ -871,9 +925,8 @@ extern int ls_main(int argc, char **argv)
                        showdirs(dnd, dndirs);
                }
        }
-
        return(status);
 
   print_usage_message:
-       usage(ls_usage);
+       show_usage();
 }