Fix some formatting
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 0807680278b2e6d338e6c56c0f497eb0ad14c43d..affa48c1272b2e6e5595d60dfc8553355b8ac3db 100644 (file)
--- a/ls.c
+++ b/ls.c
@@ -59,6 +59,11 @@ static const int COLUMN_GAP = 2;                     /* includes the file type char, if present */
 #include <time.h>
 #endif
 #include <string.h>
+#include <stdlib.h>
+
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/ioctl.h>
 
 #ifndef NAJOR
 #define MAJOR(dev) (((dev)>>8)&0xff)
@@ -171,6 +176,8 @@ static unsigned short column = 0;
 static unsigned short terminal_width;
 static unsigned short column_width;
 static unsigned short tabstops;
+#else
+static unsigned short column_width = COLUMN_WIDTH;
 #endif
 
 static int status = EXIT_SUCCESS;
@@ -236,7 +243,7 @@ static void nexttabstop( void )
                        column++;
                }
        }
-       nexttab= column + column_width + COLUMN_GAP ;
+       nexttab= column + column_width + COLUMN_GAP
 }
 
 /*----------------------------------------------------------------------*/
@@ -429,8 +436,10 @@ void showfiles(struct dnode **dn, int nfiles)
                        ;
                if (column_width < len) column_width= len;
        }
-#endif
        ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
+#else
+       ncols= TERMINAL_WIDTH;
+#endif
        switch (style_fmt) {
                case STYLE_LONG:        /* one record per line, extended info */
                case STYLE_SINGLE:      /* one record per line */
@@ -703,6 +712,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,10 +727,13 @@ 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
+       tabstops = 8;
        nfiles=0;
 
        /* process options */
@@ -903,7 +918,6 @@ extern int ls_main(int argc, char **argv)
                        showdirs(dnd, dndirs);
                }
        }
-
        return(status);
 
   print_usage_message: