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 0807680278b2e6d338e6c56c0f497eb0ad14c43d..a8d7b182ff59b7279173b5a4f6e2e6c79ea48243 100644 (file)
--- a/ls.c
+++ b/ls.c
  * 1. requires lstat (BSD) - how do you do it without?
  */
 
-static const int TERMINAL_WIDTH = 80;          /* use 79 if your terminal has linefold bug */
-static const int COLUMN_WIDTH = 14;            /* default if AUTOWIDTH not defined */
-static const int 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>
@@ -55,12 +57,18 @@ static const int COLUMN_GAP = 2;                    /* includes the file type char, if present */
 #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
@@ -168,15 +176,17 @@ static unsigned int follow_links=FALSE;
 
 static unsigned short column = 0;
 #ifdef BB_FEATURE_AUTOWIDTH
-static unsigned short terminal_width;
-static unsigned short column_width;
-static unsigned short tabstops;
+static unsigned short terminal_width = TERMINAL_WIDTH;
+static unsigned short column_width = COLUMN_WIDTH;
+static unsigned short tabstops = COLUMN_GAP;
+#else
+static unsigned short column_width = COLUMN_WIDTH;
 #endif
 
 static int status = EXIT_SUCCESS;
 
 #ifdef BB_FEATURE_HUMAN_READABLE
-unsigned long ls_disp_hr = KILOBYTE;
+unsigned long ls_disp_hr = 0;
 #endif
 
 static int my_stat(struct dnode *cur)
@@ -236,7 +246,7 @@ static void nexttabstop( void )
                        column++;
                }
        }
-       nexttab= column + column_width + COLUMN_GAP ;
+       nexttab= column + column_width + COLUMN_GAP
 }
 
 /*----------------------------------------------------------------------*/
@@ -427,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 */
@@ -438,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;
@@ -587,7 +610,8 @@ int list_single(struct dnode *dn)
                                break;
                        case LIST_BLOCKS:
 #ifdef BB_FEATURE_HUMAN_READABLE
-                               fprintf(stdout, "%5s ", format(dn->dstat.st_size, ls_disp_hr));
+                               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);
@@ -608,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
@@ -630,12 +648,13 @@ int list_single(struct dnode *dn)
                                        printf("%4d, %3d ", (int)MAJOR(dn->dstat.st_rdev), (int)MINOR(dn->dstat.st_rdev));
                                } else {
 #ifdef BB_FEATURE_HUMAN_READABLE
-                                       fprintf(stdout, "%9s ", format(dn->dstat.st_size, ls_disp_hr));
+                                       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
                                }
@@ -703,6 +722,9 @@ 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;
@@ -715,9 +737,11 @@ extern int ls_main(int argc, char **argv)
        time_fmt= TIME_MOD;
 #endif
 #ifdef BB_FEATURE_AUTOWIDTH
-       terminal_width = TERMINAL_WIDTH;
-       column_width = COLUMN_WIDTH;
-       tabstops = 8;
+       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;
 
@@ -757,7 +781,7 @@ extern int ls_main(int argc, char **argv)
                                style_fmt = STYLE_LONG;
                                list_fmt |= LIST_LONG;
 #ifdef BB_FEATURE_HUMAN_READABLE
-                               ls_disp_hr = 1;
+                               ls_disp_hr = FALSE;
 #endif
                        break;
                        case 'n': list_fmt |= LIST_ID_NUMERIC; break;
@@ -804,11 +828,9 @@ extern int ls_main(int argc, char **argv)
                        case 'w': terminal_width= atoi(optarg); break;
 #endif
 #ifdef BB_FEATURE_HUMAN_READABLE
-                       case 'h': ls_disp_hr = 0; break;
-                       case 'k': ls_disp_hr = KILOBYTE; break;
-#else
-                       case 'k': break;
+                       case 'h': ls_disp_hr = TRUE; break;
 #endif
+                       case 'k': break;
                        default:
                                goto print_usage_message;
                }
@@ -903,9 +925,8 @@ extern int ls_main(int argc, char **argv)
                        showdirs(dnd, dndirs);
                }
        }
-
        return(status);
 
   print_usage_message:
-       usage(ls_usage);
+       show_usage();
 }