Fix the pwd and group functions. The bb_ stuff was a leftover from
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 215c58bb61dd9d47da12dbe05f1a50bdb9b98a14..a5574a52e87931954cc0b9d0c62cde04fee4ac86 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 */
+};
+
 
 /************************************************************************/
 
@@ -59,12 +62,13 @@ 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
+#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;
@@ -185,31 +189,6 @@ static int status = EXIT_SUCCESS;
 unsigned long ls_disp_hr = KILOBYTE;
 #endif
 
-/* sparc termios is broken -- use old termio handling. */
-#ifdef BB_FEATURE_USE_TERMIOS
-#      if #cpu(sparc)
-#              include <termio.h>
-#              define termios termio
-#              define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
-#              define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
-#      else
-#              include <termios.h>
-#              define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
-#              define getTermSettings(fd,argp) tcgetattr(fd, argp);
-#      endif
-
-FILE *cin;
-
-static struct termios initial_settings, new_settings;
-
-static void gotsig(int sig)
-{
-       setTermSettings(fileno(cin), &initial_settings);
-       putchar('\n');
-       exit(EXIT_FAILURE);
-}
-#endif /* BB_FEATURE_USE_TERMIOS */
-
 static int my_stat(struct dnode *cur)
 {
 #ifdef BB_FEATURE_LS_FOLLOWLINKS
@@ -458,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
@@ -471,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;
@@ -641,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
@@ -736,7 +720,7 @@ extern int ls_main(int argc, char **argv)
        int opt;
        int oi, ac;
        char **av;
-#if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
+#ifdef BB_FEATURE_AUTOWIDTH
        struct winsize win = { 0, 0, 0, 0 };
 #endif
 
@@ -751,32 +735,11 @@ extern int ls_main(int argc, char **argv)
        time_fmt= TIME_MOD;
 #endif
 #ifdef BB_FEATURE_AUTOWIDTH
-#ifdef BB_FEATURE_USE_TERMIOS
-               cin = fopen("/dev/tty", "r");
-               if (!cin)
-                       cin = fopen("/dev/console", "r");
-               getTermSettings(fileno(cin), &initial_settings);
-               new_settings = initial_settings;
-               new_settings.c_cc[VMIN] = 1;
-               new_settings.c_cc[VTIME] = 0;
-               new_settings.c_lflag &= ~ICANON;
-               new_settings.c_lflag &= ~ECHO;
-               setTermSettings(fileno(cin), &new_settings);
-
                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;
-
-               (void) signal(SIGINT, gotsig);
-               (void) signal(SIGQUIT, gotsig);
-               (void) signal(SIGTERM, gotsig);
-#else
-
-       terminal_width = TERMINAL_WIDTH;
-       column_width = COLUMN_WIDTH;
-#endif
 #endif
        tabstops = 8;
        nfiles=0;
@@ -963,10 +926,6 @@ extern int ls_main(int argc, char **argv)
                        showdirs(dnd, dndirs);
                }
        }
-
-#ifdef BB_FEATURE_USE_TERMIOS
-       gotsig(0);
-#endif
        return(status);
 
   print_usage_message: