damnit.
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 4eb486f87fa5af919b81a9c564b8d8f8d3438dfe..571c962c708e9e715b667570b31164a87ff0c931 100644 (file)
--- a/ls.c
+++ b/ls.c
  * 1. requires lstat (BSD) - how do you do it without?
  */
 
-//#define FEATURE_USERNAME     /* show username/groupnames (libc6 uses NSS) */
-#define FEATURE_TIMESTAMPS     /* show file timestamps */
-#define FEATURE_AUTOWIDTH      /* calculate terminal & column widths */
-#define FEATURE_FILETYPECHAR   /* enable -p and -F */
-
 #define TERMINAL_WIDTH 80      /* use 79 if your terminal has linefold bug */
 #define        COLUMN_WIDTH    14      /* default if AUTOWIDTH not defined */
 #define COLUMN_GAP     2       /* includes the file type char, if present */
 #include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
-#ifdef FEATURE_USERNAME
-#include <pwd.h>
-#include <grp.h>
-#endif
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
 #include <time.h>
 #endif
 
 #define TYPEINDEX(mode)        (((mode) >> 12) & 0x0f)
 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
 #define APPCHAR(mode)  ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
 #endif
 
 #define MINOR(dev) ((dev)&0xff)
 #endif
 
-#define MODE1  "rwxrwxrwx"
-#define MODE0  "---------"
-#define SMODE1 "..s..s..t"
-#define SMODE0 "..S..S..T"
-
-/* The 9 mode bits to test */
-
-static const mode_t MBIT[] = {
-  S_IRUSR, S_IWUSR, S_IXUSR,
-  S_IRGRP, S_IWGRP, S_IXGRP,
-  S_IROTH, S_IWOTH, S_IXOTH
-};
-
-/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
-
-static const mode_t SBIT[] = {
-  0, 0, S_ISUID,
-  0, 0, S_ISGID,
-  0, 0, S_ISVTX
-};
-
 #define FMT_AUTO       0
 #define FMT_LONG       1       /* one record per line, extended info */
 #define FMT_SINGLE     2       /* one record per line */
@@ -128,14 +98,14 @@ static unsigned char       display_fmt = FMT_AUTO;
 static unsigned short  opts = 0;
 static unsigned short  column = 0;
 
-#ifdef FEATURE_AUTOWIDTH
+#ifdef BB_FEATURE_AUTOWIDTH
 static unsigned short terminal_width = 0, column_width = 0;
 #else
 #define terminal_width TERMINAL_WIDTH
 #define column_width   COLUMN_WIDTH
 #endif
 
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
 static unsigned char time_fmt = TIME_MOD;
 #endif
 
@@ -188,7 +158,7 @@ static void tab(short col)
        #undef nspaces
 }
 
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
 static char append_char(mode_t mode)
 {
        if (!(opts & DISP_FTYPE))
@@ -210,51 +180,48 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
 {
        char scratch[PATH_MAX];
        short len = strlen(name);
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
        char append = append_char(info->st_mode);
 #endif
        
        if (display_fmt == FMT_LONG) {
                mode_t mode = info->st_mode; 
-               int i;
-               
-               scratch[0] = TYPECHAR(mode);
-               for (i=0; i<9; i++)
-                       if (mode & SBIT[i])
-                               scratch[i+1] = (mode & MBIT[i])
-                                               ? SMODE1[i]
-                                               : SMODE0[i];
-                       else
-                               scratch[i+1] = (mode & MBIT[i])
-                                               ? MODE1[i]
-                                               : MODE0[i];
                newline();
-               wr(scratch, 10);
+               wr(modeString(mode), 10);
                column=10;
-               writenum((long)info->st_nlink,(short)4);
+               writenum((long)info->st_nlink,(short)5);
                fputs(" ", stdout);
-#ifdef FEATURE_USERNAME
+#ifdef BB_FEATURE_LS_USERNAME
                if (!(opts & DISP_NUMERIC)) {
-                       struct passwd *pw = getpwuid(info->st_uid);
-                       if (pw)
-                               fputs(pw->pw_name, stdout);
-                       else
-                               writenum((long)info->st_uid,(short)0);
+                       scratch[0]='\0';
+                       my_getpwuid( scratch, info->st_uid);
+                       scratch[8]='\0';
+                       if (*scratch)
+                               wr(scratch,8);
+                       else {
+                               writenum((long) info->st_uid,(short)8);
+                               fputs(" ", stdout);
+                       }
                } else
 #endif
-               writenum((long)info->st_uid,(short)0);
-               tab(24);
-#ifdef FEATURE_USERNAME
+               {
+                   writenum((long) info->st_uid,(short)8);
+                   fputs(" ", stdout);
+               }
+               tab(16);
+#ifdef BB_FEATURE_LS_USERNAME
                if (!(opts & DISP_NUMERIC)) {
-                       struct group *gr = getgrgid(info->st_gid);
-                       if (gr)
-                               fputs(gr->gr_name, stdout);
-                       else
-                               writenum((long)info->st_gid,(short)0);
+                       scratch[0]='\0';
+                       my_getgrgid( scratch, info->st_gid);
+                       scratch[8]='\0';
+                       if (*scratch)
+                               wr(scratch,8);
+                       else 
+                               writenum((long) info->st_gid,(short)8);
                } else
 #endif
-               writenum((long)info->st_gid,(short)0);
-               tab(33);
+               writenum((long) info->st_gid,(short)8);
+               tab(17);
                if (S_ISBLK(mode) || S_ISCHR(mode)) {
                        writenum((long)MAJOR(info->st_rdev),(short)3);
                        fputs(", ", stdout);
@@ -263,7 +230,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
                else
                        writenum((long)info->st_size,(short)8);
                fputs(" ", stdout);
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
                {
                        time_t cal;
                        char *string;
@@ -299,7 +266,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
                        wr(" -> ", 4);
                        len = readlink(fullname, scratch, sizeof scratch);
                        if (len > 0) fwrite(scratch, 1, len, stdout);
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
                        /* show type of destination */
                        if (opts & DISP_FTYPE) {
                                if (!stat(fullname, info)) {
@@ -310,7 +277,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
                        }
 #endif
                }
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
                else if (append)
                        wr(&append, 1);
 #endif
@@ -324,7 +291,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
                        newline();
                else {
                        if (nexttab + column_width > terminal_width
-#ifndef FEATURE_AUTOWIDTH
+#ifndef BB_FEATURE_AUTOWIDTH
                        || nexttab + len >= terminal_width
 #endif
                        )
@@ -333,7 +300,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
                                tab(nexttab);
                }
                /* work out where next column starts */
-#ifdef FEATURE_AUTOWIDTH
+#ifdef BB_FEATURE_AUTOWIDTH
                /* we know the calculated width is big enough */
                nexttab = column + column_width + COLUMN_GAP;
 #else
@@ -346,7 +313,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
                /* now write the data */
                wr(name, len);
                column = column + len;
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
                if (append)
                        wr(&append, 1), column++;
 #endif
@@ -387,7 +354,7 @@ static int list_item(const char *name)
        
        dir = opendir(name);
        if (!dir) goto listerr;
-#ifdef FEATURE_AUTOWIDTH
+#ifdef BB_FEATURE_AUTOWIDTH
        column_width = 0;
        while ((entry = readdir(dir)) != NULL) {
                short w = strlen(entry->d_name);
@@ -438,22 +405,22 @@ listerr:
 }
 
 static const char ls_usage[] = "ls [-1a"
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
        "c"
 #endif
        "d"
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
        "e"
 #endif
        "ln"
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
        "p"
 #endif
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
        "u"
 #endif
        "xAC"
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
        "F"
 #endif
 #ifdef FEATURE_RECURSIVE
@@ -486,7 +453,7 @@ ls_main(int argc, char * * argv)
                        case '1':       display_fmt = FMT_SINGLE; break;
                        case 'x':       display_fmt = FMT_ROWS; break;
                        case 'C':       display_fmt = FMT_COLUMNS; break;
-#ifdef FEATURE_FILETYPECHAR
+#ifdef BB_FEATURE_LS_FILETYPES
                        case 'p':       opts |= DISP_FTYPE; break;
                        case 'F':       opts |= DISP_FTYPE|DISP_EXEC; break;
 #endif
@@ -497,7 +464,7 @@ ls_main(int argc, char * * argv)
 #ifdef FEATURE_RECURSIVE
                        case 'R':       opts |= DIR_RECURSE; break;
 #endif
-#ifdef FEATURE_TIMESTAMPS
+#ifdef BB_FEATURE_LS_TIMESTAMPS
                        case 'u':       time_fmt = TIME_ACCESS; break;
                        case 'c':       time_fmt = TIME_CHANGE; break;
                        case 'e':       opts |= DISP_FULLTIME; break;
@@ -510,10 +477,10 @@ ls_main(int argc, char * * argv)
 
        /* choose a display format */
        if (display_fmt == FMT_AUTO)
-               display_fmt = isatty(STDOUT_FILENO) ? FMT_COLUMNS : FMT_SINGLE;
+               display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
        if (argi < argc - 1)
                opts |= DISP_DIRNAME; /* 2 or more items? label directories */
-#ifdef FEATURE_AUTOWIDTH
+#ifdef BB_FEATURE_AUTOWIDTH
        /* could add a -w option and/or TIOCGWINSZ call */
        if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;