use bb_fflush_stdout_and_exit instead of exit
[oweals/busybox.git] / coreutils / ls.c
index 18cffb13eda88e519a93e37a67ab2f22b0484066..4e21454cef9af6d5e4dd91b38b2843c85d20a968 100644 (file)
@@ -61,6 +61,7 @@ enum {
 #include <signal.h>
 #include <termios.h>
 #include <sys/ioctl.h>
+#include <sys/sysmacros.h>     /* major() and minor() */
 #include "busybox.h"
 #ifdef CONFIG_SELINUX
 #include <fs_secure.h>
@@ -72,11 +73,6 @@ enum {
 #include <time.h>
 #endif
 
-#ifndef MAJOR
-#define MAJOR(dev) (((dev)>>8)&0xff)
-#define MINOR(dev) ((dev)&0xff)
-#endif
-
 /* what is the overall style of the listing */
 #define STYLE_AUTO      (0)
 #define STYLE_COLUMNS   (1U<<21)       /* fill columns */
@@ -315,7 +311,7 @@ static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
                if (S_ISDIR(dn[i]->dstat.st_mode)
                        && (notsubdirs
                                || ((dn[i]->name[0] != '.')
-                                       || (dn[i]->name[1] 
+                                       || (dn[i]->name[1]
                                                && ((dn[i]->name[1] != '.')
                                                        || dn[i]->name[2])))))
                        dirs++;
@@ -532,7 +528,7 @@ static void showfiles(struct dnode **dn, int nfiles)
 }
 
 /*----------------------------------------------------------------------*/
-static void showdirs(struct dnode **dn, int ndirs)
+static void showdirs(struct dnode **dn, int ndirs, int first)
 {
        int i, nfiles;
        struct dnode **subdnp;
@@ -547,7 +543,10 @@ static void showdirs(struct dnode **dn, int ndirs)
 
        for (i = 0; i < ndirs; i++) {
                if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
-                       printf("\n%s:\n", dn[i]->fullname);
+                       if (!first)
+                               printf("\n");
+                       first = 0;
+                       printf("%s:\n", dn[i]->fullname);
                }
                subdnp = list_dir(dn[i]->fullname);
                nfiles = countfiles(subdnp);
@@ -566,7 +565,7 @@ static void showdirs(struct dnode **dn, int ndirs)
 #ifdef CONFIG_FEATURE_LS_SORTFILES
                                        shellsort(dnd, dndirs);
 #endif
-                                       showdirs(dnd, dndirs);
+                                       showdirs(dnd, dndirs, 0);
                                        free(dnd);      /* free the array of dnode pointers to the dirs */
                                }
                        }
@@ -684,9 +683,9 @@ static int list_single(struct dnode *dn)
                        break;
                case LIST_ID_NAME:
 #ifdef CONFIG_FEATURE_LS_USERNAME
-                       my_getpwuid(scratch, dn->dstat.st_uid);
+                       my_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch));
                        printf("%-8.8s ", scratch);
-                       my_getgrgid(scratch, dn->dstat.st_gid);
+                       my_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch));
                        printf("%-8.8s", scratch);
                        column += 17;
                        break;
@@ -697,8 +696,8 @@ static int list_single(struct dnode *dn)
                case LIST_SIZE:
                case LIST_DEV:
                        if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) {
-                               column += printf("%4d, %3d ", (int) MAJOR(dn->dstat.st_rdev),
-                                          (int) MINOR(dn->dstat.st_rdev));
+                               column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev),
+                                          (int) minor(dn->dstat.st_rdev));
                        } else {
 #ifdef CONFIG_FEATURE_HUMAN_READABLE
                                if (all_fmt & LS_DISP_HR) {
@@ -717,21 +716,21 @@ static int list_single(struct dnode *dn)
                        break;
 #ifdef CONFIG_FEATURE_LS_TIMESTAMPS
                case LIST_FULLTIME:
+                       printf("%24.24s ", filetime);
+                       column += 25;
+                       break;
                case LIST_DATE_TIME:
-                       if (all_fmt & LIST_FULLTIME) {
-                               printf("%24.24s ", filetime);
-                               column += 25;
-                               break;
-                       }
-                       age = time(NULL) - ttime;
-                       printf("%6.6s ", filetime + 4);
-                       if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
-                               /* hh:mm if less than 6 months old */
-                               printf("%5.5s ", filetime + 11);
-                       } else {
-                               printf(" %4.4s ", filetime + 20);
+                       if ((all_fmt & LIST_FULLTIME) == 0) {
+                               age = time(NULL) - ttime;
+                               printf("%6.6s ", filetime + 4);
+                               if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
+                                       /* hh:mm if less than 6 months old */
+                                       printf("%5.5s ", filetime + 11);
+                               } else {
+                                       printf(" %4.4s ", filetime + 20);
+                               }
+                               column += 13;
                        }
-                       column += 13;
                        break;
 #endif
 #ifdef CONFIG_SELINUX
@@ -875,7 +874,7 @@ static const char ls_options[]="Cadil1gnsxAk" \
        LS_STR_SELINUX \
        LS_STR_AUTOWIDTH;
 
-#define LIST_MASK_TRIGGER      LIST_SHORT
+#define LIST_MASK_TRIGGER      0
 #define STYLE_MASK_TRIGGER     STYLE_MASK
 #define SORT_MASK_TRIGGER      SORT_MASK
 #define DISP_MASK_TRIGGER      DISP_ROWS
@@ -917,10 +916,10 @@ static const unsigned opt_flags[] = {
 # endif
 #endif
 #ifdef CONFIG_FEATURE_LS_SORTFILES
-       SORT_ORDER_REVERSE,             /* r */
        SORT_SIZE,                      /* S */
+       SORT_EXT,                       /* X */
+       SORT_ORDER_REVERSE,             /* r */
        SORT_VERSION,                   /* v */
-       SORT_EXT,                       /* v */
 #endif
 #ifdef CONFIG_FEATURE_LS_FILETYPES
        LIST_FILETYPE | LIST_EXEC,      /* F */
@@ -938,6 +937,7 @@ static const unsigned opt_flags[] = {
 #ifdef CONFIG_SELINUX
        LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */
 #endif
+       (1U<<31)
 };
 
 
@@ -978,13 +978,13 @@ extern int ls_main(int argc, char **argv)
 
 #ifdef CONFIG_FEATURE_AUTOWIDTH
        /* Obtain the terminal width.  */
-       get_terminal_width_height(0, &terminal_width, NULL);
+       get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
        /* Go one less... */
        terminal_width--;
 #endif
 
 #ifdef CONFIG_FEATURE_LS_COLOR
-       if (isatty(fileno(stdout)))
+       if (isatty(STDOUT_FILENO))
                show_color = 1;
 #endif
 
@@ -1000,8 +1000,7 @@ extern int ls_main(int argc, char **argv)
 #else
        opt = bb_getopt_ulflags(argc, argv, ls_options);
 #endif
-       /* 16 = maximum options minus tabsize and screewn width */
-       for (i = 0; i < 16; i++) {
+       for (i = 0; opt_flags[i] != (1U<<31); i++) {
                if (opt & (1 << i)) {
                        unsigned int flags = opt_flags[i];
                        if (flags & LIST_MASK_TRIGGER) {
@@ -1052,14 +1051,14 @@ extern int ls_main(int argc, char **argv)
        if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
                all_fmt &= ~LIST_ID_NAME;       /* don't list names if numeric uid */
 #endif
-                       
+
        /* choose a display format */
        if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
 #if STYLE_AUTO != 0
                all_fmt = (all_fmt & ~STYLE_MASK)
-                               | (isatty(fileno(stdout)) ? STYLE_COLUMNS : STYLE_SINGLE);
+                               | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
 #else
-               all_fmt |= (isatty(fileno(stdout)) ? STYLE_COLUMNS : STYLE_SINGLE);
+               all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
 #endif
 
        /*
@@ -1107,7 +1106,6 @@ extern int ls_main(int argc, char **argv)
                cur = cur->next;
        }
 
-
        if (all_fmt & DISP_NOLIST) {
 #ifdef CONFIG_FEATURE_LS_SORTFILES
                shellsort(dnp, nfiles);
@@ -1129,7 +1127,7 @@ extern int ls_main(int argc, char **argv)
 #ifdef CONFIG_FEATURE_LS_SORTFILES
                        shellsort(dnd, dndirs);
 #endif
-                       showdirs(dnd, dndirs);
+                       showdirs(dnd, dndirs, dnfiles == 0);
                }
        }
        return (status);