Simplify pathname building, in which a bug was noted by Larry Doolittle,
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 64e5bf828e3c40ac5f8a29ab35b9ac6d87e40147..d24ba98663f12e2227d6ef4f0c6604a0a7f7b668 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
@@ -148,9 +156,9 @@ struct dnode {                              /* the basic node */
 };
 typedef struct dnode dnode_t;
 
-struct dnode **list_dir(char *);
-struct dnode **dnalloc(int);
-int list_single(struct dnode *);
+static struct dnode **list_dir(char *);
+static struct dnode **dnalloc(int);
+static int list_single(struct dnode *);
 
 static unsigned int disp_opts;
 static unsigned int style_fmt;
@@ -168,17 +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
-# define column_width  COLUMN_WIDTH 
+static unsigned short column_width = COLUMN_WIDTH;
 #endif
 
 static int status = EXIT_SUCCESS;
 
 #ifdef BB_FEATURE_HUMAN_READABLE
-unsigned long ls_disp_hr = KILOBYTE;
+static unsigned long ls_disp_hr = 0;
 #endif
 
 static int my_stat(struct dnode *cur)
@@ -248,7 +256,7 @@ static int is_subdir(struct dnode *dn)
                        strcmp(dn->name, "..") != 0);
 }
 
-int countdirs(struct dnode **dn, int nfiles)
+static int countdirs(struct dnode **dn, int nfiles)
 {
        int i, dirs;
 
@@ -260,7 +268,7 @@ int countdirs(struct dnode **dn, int nfiles)
        return(dirs);
 }
 
-int countsubdirs(struct dnode **dn, int nfiles)
+static int countsubdirs(struct dnode **dn, int nfiles)
 {
        int i, subdirs;
 
@@ -272,7 +280,7 @@ int countsubdirs(struct dnode **dn, int nfiles)
        return subdirs;
 }
 
-int countfiles(struct dnode **dnp)
+static int countfiles(struct dnode **dnp)
 {
        int nfiles;
        struct dnode *cur;
@@ -285,7 +293,7 @@ int countfiles(struct dnode **dnp)
 }
 
 /* get memory to hold an array of pointers */
-struct dnode **dnalloc(int num)
+static struct dnode **dnalloc(int num)
 {
        struct dnode **p;
 
@@ -295,7 +303,7 @@ struct dnode **dnalloc(int num)
        return(p);
 }
 
-void dfree(struct dnode **dnp)
+static void dfree(struct dnode **dnp)
 {
        struct dnode *cur, *next;
 
@@ -311,7 +319,7 @@ void dfree(struct dnode **dnp)
        free(dnp);      /* free the array holding the dnode pointers */
 }
 
-struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
+static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
 {
        int dncnt, i, d;
        struct dnode **dnp;
@@ -351,7 +359,7 @@ struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
 
 /*----------------------------------------------------------------------*/
 #ifdef BB_FEATURE_LS_SORTFILES
-int sortcmp(struct dnode *d1, struct dnode *d2)
+static int sortcmp(struct dnode *d1, struct dnode *d2)
 {
        int cmp, dif;
 
@@ -388,7 +396,7 @@ int sortcmp(struct dnode *d1, struct dnode *d2)
 }
 
 /*----------------------------------------------------------------------*/
-void shellsort(struct dnode **dn, int size)
+static void shellsort(struct dnode **dn, int size)
 {
        struct dnode *temp;
        int gap, i, j;
@@ -412,7 +420,7 @@ void shellsort(struct dnode **dn, int size)
 #endif
 
 /*----------------------------------------------------------------------*/
-void showfiles(struct dnode **dn, int nfiles)
+static void showfiles(struct dnode **dn, int nfiles)
 {
        int i, ncols, nrows, row, nc;
 #ifdef BB_FEATURE_AUTOWIDTH
@@ -429,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
@@ -442,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;
@@ -462,7 +481,7 @@ void showfiles(struct dnode **dn, int nfiles)
 }
 
 /*----------------------------------------------------------------------*/
-void showdirs(struct dnode **dn, int ndirs)
+static void showdirs(struct dnode **dn, int ndirs)
 {
        int i, nfiles;
        struct dnode **subdnp;
@@ -505,7 +524,7 @@ void showdirs(struct dnode **dn, int ndirs)
 }
 
 /*----------------------------------------------------------------------*/
-struct dnode **list_dir(char *path)
+static struct dnode **list_dir(char *path)
 {
        struct dnode *dn, *cur, **dnp;
        struct dirent *entry;
@@ -524,16 +543,16 @@ struct dnode **list_dir(char *path)
        }
        while ((entry = readdir(dir)) != NULL) {
                /* are we going to list the file- it may be . or .. or a hidden file */
-               if ((strcmp(entry->d_name, ".")==0) && !(disp_opts & DISP_DOT)) continue;
-               if ((strcmp(entry->d_name, "..")==0) && !(disp_opts & DISP_DOT)) continue;
-               if ((entry->d_name[0] ==  '.') && !(disp_opts & DISP_HIDDEN)) continue;
+               if ((strcmp(entry->d_name, ".")==0) && !(disp_opts & DISP_DOT))
+                       continue;
+               if ((strcmp(entry->d_name, "..")==0) && !(disp_opts & DISP_DOT))
+                       continue;
+               if ((entry->d_name[0] ==  '.') && !(disp_opts & DISP_HIDDEN))
+                       continue;
                cur= (struct dnode *)xmalloc(sizeof(struct dnode));
-               cur->fullname = xmalloc(strlen(path)+1+strlen(entry->d_name)+1);
-               strcpy(cur->fullname, path);
-               if (cur->fullname[strlen(cur->fullname)-1] != '/')
-                       strcat(cur->fullname, "/");
-               cur->name= cur->fullname + strlen(cur->fullname);
-               strcat(cur->fullname, entry->d_name);
+               cur->fullname = concat_path_file(path, entry->d_name);
+               cur->name = cur->fullname +
+                               (strlen(cur->fullname) - strlen(entry->d_name));
                if (my_stat(cur))
                        continue;
                cur->next= dn;
@@ -556,7 +575,7 @@ struct dnode **list_dir(char *path)
 }
 
 /*----------------------------------------------------------------------*/
-int list_single(struct dnode *dn)
+static int list_single(struct dnode *dn)
 {
        int i, len;
        char scratch[BUFSIZ + 1];
@@ -591,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);
@@ -606,21 +626,15 @@ int list_single(struct dnode *dn)
                                column += 10;
                                break;
                        case LIST_NLINKS:
-                               printf("%4d ", dn->dstat.st_nlink);
+                               printf("%4ld ", (long)dn->dstat.st_nlink);
                                column += 10;
                                break;
                        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
@@ -634,14 +648,18 @@ 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));
-#else
+                                       if (ls_disp_hr==TRUE) {
+                                               fprintf(stdout, "%9s ", make_human_readable_str(
+                                                                       dn->dstat.st_size>>10, 0));
+                                       } else 
+#endif 
+                                       {
 #if _FILE_OFFSET_BITS == 64
-                                       printf("%9lld ", dn->dstat.st_size);
+                                               printf("%9lld ", (long long)dn->dstat.st_size);
 #else
-                                       printf("%9ld ", dn->dstat.st_size);
-#endif
+                                               printf("%9ld ", dn->dstat.st_size);
 #endif
+                                       }
                                }
                                column += 10;
                                break;
@@ -707,6 +725,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,9 +740,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;
 
@@ -761,7 +784,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;
@@ -808,11 +831,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;
                }
@@ -907,9 +928,8 @@ extern int ls_main(int argc, char **argv)
                        showdirs(dnd, dndirs);
                }
        }
-
        return(status);
 
   print_usage_message:
-       usage(ls_usage);
+       show_usage();
 }