Fix some formatting
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 64e5bf828e3c40ac5f8a29ab35b9ac6d87e40147..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)
@@ -172,7 +177,7 @@ static unsigned short terminal_width;
 static unsigned short column_width;
 static unsigned short tabstops;
 #else
-# define column_width  COLUMN_WIDTH 
+static unsigned short column_width = COLUMN_WIDTH;
 #endif
 
 static int status = EXIT_SUCCESS;
@@ -707,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;
@@ -719,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 */
@@ -907,7 +918,6 @@ extern int ls_main(int argc, char **argv)
                        showdirs(dnd, dndirs);
                }
        }
-
        return(status);
 
   print_usage_message: