Thought of another janitorial item for the list.
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index e4b8f69a3190526ec0918a9f04a9060f08c343f6..d8666152eaac194d03c3a7139b7da7264eb374d9 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,16 +57,18 @@ static const int COLUMN_GAP = 2;                    /* includes the file type char, if present */
 #include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
-#ifdef BB_FEATURE_LS_TIMESTAMPS
-#include <time.h>
-#endif
 #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
 
-#ifndef NAJOR
+#ifndef MAJOR
 #define MAJOR(dev) (((dev)>>8)&0xff)
 #define MINOR(dev) ((dev)&0xff)
 #endif
@@ -172,11 +176,11 @@ 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
-# define column_width  COLUMN_WIDTH 
+static unsigned short column_width = COLUMN_WIDTH;
 #endif
 
 static int status = EXIT_SUCCESS;
@@ -433,9 +437,15 @@ 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;
        }
-       ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
 #else
        ncols= TERMINAL_WIDTH;
 #endif
@@ -446,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;
@@ -595,7 +610,7 @@ 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 ", format(dn->dstat.st_blocks>>1, 1));
 #else
 #if _FILE_OFFSET_BITS == 64
                                printf("%4lld ", dn->dstat.st_blocks>>1);
@@ -616,15 +631,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
@@ -641,9 +650,9 @@ int list_single(struct dnode *dn)
                                        fprintf(stdout, "%9s ", format(dn->dstat.st_size, ls_disp_hr));
 #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
                                }
@@ -726,13 +735,12 @@ extern int ls_main(int argc, char **argv)
        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;
+       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
-       tabstops = 8;
        nfiles=0;
 
        /* process options */
@@ -819,10 +827,8 @@ extern int ls_main(int argc, char **argv)
 #endif
 #ifdef BB_FEATURE_HUMAN_READABLE
                        case 'h': ls_disp_hr = 0; break;
-                       case 'k': ls_disp_hr = KILOBYTE; break;
-#else
-                       case 'k': break;
 #endif
+                       case 'k': break;
                        default:
                                goto print_usage_message;
                }
@@ -920,5 +926,5 @@ extern int ls_main(int argc, char **argv)
        return(status);
 
   print_usage_message:
-       usage(ls_usage);
+       show_usage();
 }