Latest and greatest
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 7a1b0f3d09db190bc1f8b83a9b84ac18ce936b99..f09cbbc22b8089658d0ccc29c05c3e1ccbf318af 100644 (file)
--- a/ls.c
+++ b/ls.c
@@ -1,137 +1,3 @@
-/*
- * Mini ls implementation for busybox
- *
- * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
-
-#if fooBar
-
-#include <stdio.h>
-#include <unistd.h>
-#include <dirent.h>
-#include "internal.h"
-
-
-static const char ls_usage[] = "ls [OPTION]... [FILE]...\n"
-"List information about the FILEs (the current directory by default).\n";
-
-int oneFlag=FALSE;
-int allFlag=FALSE;
-int directoryFlag=FALSE;
-int longFlag=FALSE;
-int typeFlag=FALSE;
-int dereferenceFlag=FALSE;
-int recursiveFlag=FALSE;
-
-static int fileAction(const char *fileName)
-{
-    if ( allFlag==FALSE && ((strcmp(fileName, "..") == 0) 
-               || (strcmp(fileName, ".") == 0)) ) {
-       return( TRUE);
-    }
-    //struct stat statBuf;
-    //if (stat(fileName, &statBuf) > 0) {
-       fprintf(stdout, "%s\n", fileName);
-       return( TRUE);
-    //}
-    //else {
-//     perror(fileName);
-//     return( FALSE);
-//    }
-}
-
-static int dirAction(const char *fileName)
-{
-    DIR *dir;
-    struct dirent *entry;
-    
-    fprintf(stdout, "%s\n", fileName);
-
-    dir = opendir( fileName);
-    if (!dir) {
-       perror("Can't open directory");
-       exit(FALSE);
-    }
-    while ((entry = readdir(dir)) != NULL) {
-       recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, fileAction, dirAction);
-    }
-    return( TRUE);
-}
-
-int ls_main(int argc, char **argv)
-{
-    if (argc <= 1) {
-       char buf[NAME_MAX];
-       getcwd( buf, NAME_MAX);
-       dirAction( buf); 
-    }
-
-    /* peel of the "ls" */
-    argc--;
-    argv++;
-
-    /* Parse any options */
-    while (**argv == '-') {
-       while (*++(*argv)) switch (**argv) {
-           case '1':
-               oneFlag = TRUE;
-               break;
-           case 'a':
-               allFlag = TRUE;
-               break;
-           case 'd':
-               directoryFlag = TRUE;
-               break;
-           case 'l':
-               longFlag = TRUE;
-               break;
-           case 'F':
-               typeFlag = TRUE;
-               break;
-           case 'L':
-               dereferenceFlag = TRUE;
-               break;
-           case 'R':
-               recursiveFlag = TRUE;
-               break;
-           default:
-               fprintf(stderr, "Usage: %s\n", ls_usage);
-               exit( FALSE);
-       }
-       argc--;
-       argv++;
-    }
-    
-    /* Ok, ready to do the deed now */
-    fprintf(stderr, "B\n");
-    while (argc-- > 1) {
-       fprintf(stderr, "C\n");
-       recursiveAction( *argv, recursiveFlag, dereferenceFlag, fileAction, dirAction);
-    }
-    exit(TRUE);
-}
-
-
-
-#else
-
-
-#include "internal.h"
 /*
  * tiny-ls.c version 0.1.0: A minimalist 'ls'
  * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
@@ -174,22 +40,20 @@ int ls_main(int argc, char **argv)
  * 1. requires lstat (BSD) - how do you do it without?
  */
 
-#define FEATURE_USERNAME       /* show username/groupnames (libc6 uses NSS) */
+#define FEATURE_USERNAME       /* show username/groupnames (bypasses libc6 NSS) */
 #define FEATURE_TIMESTAMPS     /* show file timestamps */
 #define FEATURE_AUTOWIDTH      /* calculate terminal & column widths */
 #define FEATURE_FILETYPECHAR   /* enable -p and -F */
 
-#undef OP_BUF_SIZE     1024    /* leave undefined for unbuffered output */
-
 #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 */
+#define HAS_REWINDDIR
 
 /************************************************************************/
 
-#define HAS_REWINDDIR
-
-#if 1 /* FIXME libc 6 */
+#include "internal.h"
+#if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
 # include <linux/types.h> 
 #else
 # include <sys/types.h> 
@@ -200,10 +64,6 @@ int ls_main(int argc, char **argv)
 #include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
-#ifdef FEATURE_USERNAME
-#include <pwd.h>
-#include <grp.h>
-#endif
 #ifdef FEATURE_TIMESTAMPS
 #include <time.h>
 #endif
@@ -219,27 +79,6 @@ int ls_main(int argc, char **argv)
 #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 umode_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 umode_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 */
@@ -279,7 +118,7 @@ static unsigned char time_fmt = TIME_MOD;
 
 static void writenum(long val, short minwidth)
 {
-       char    scratch[20];
+       char    scratch[128];
 
        char *p = scratch + sizeof(scratch);
        short len = 0;
@@ -325,7 +164,7 @@ static void tab(short col)
 }
 
 #ifdef FEATURE_FILETYPECHAR
-static char append_char(umode_t mode)
+static char append_char(mode_t mode)
 {
        if (!(opts & DISP_FTYPE))
                return '\0';
@@ -342,55 +181,45 @@ static char append_char(umode_t mode)
  **
  **/
 
-static void list_single(const char *name, struct stat *info)
+static void list_single(const char *name, struct stat *info, const char *fullname)
 {
-       char scratch[20];
+       char scratch[PATH_MAX];
        short len = strlen(name);
 #ifdef FEATURE_FILETYPECHAR
        char append = append_char(info->st_mode);
 #endif
        
        if (display_fmt == FMT_LONG) {
-               umode_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];
+               mode_t mode = info->st_mode; 
                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
                if (!(opts & DISP_NUMERIC)) {
-                       struct passwd *pw = getpwuid(info->st_uid);
-                       if (pw)
-                               fputs(pw->pw_name, stdout);
+                       scratch[8]='\0';
+                       my_getpwuid( scratch, info->st_uid);
+                       if (*scratch)
+                               fputs(scratch, stdout);
                        else
                                writenum((long)info->st_uid,(short)0);
                } else
 #endif
                writenum((long)info->st_uid,(short)0);
-               tab(24);
+               tab(16);
 #ifdef FEATURE_USERNAME
                if (!(opts & DISP_NUMERIC)) {
-                       struct group *gr = getgrgid(info->st_gid);
-                       if (gr)
-                               fputs(gr->gr_name, stdout);
+                       scratch[8]='\0';
+                       my_getgrgid( scratch, info->st_gid);
+                       if (*scratch)
+                               fputs(scratch, stdout);
                        else
                                writenum((long)info->st_gid,(short)0);
                } else
 #endif
                writenum((long)info->st_gid,(short)0);
-               tab(33);
+               tab(17);
                if (S_ISBLK(mode) || S_ISCHR(mode)) {
                        writenum((long)MAJOR(info->st_rdev),(short)3);
                        fputs(", ", stdout);
@@ -433,12 +262,12 @@ static void list_single(const char *name, struct stat *info)
                wr(name, len);
                if (S_ISLNK(mode)) {
                        wr(" -> ", 4);
-                       len = readlink(name, scratch, sizeof scratch);
+                       len = readlink(fullname, scratch, sizeof scratch);
                        if (len > 0) fwrite(scratch, 1, len, stdout);
 #ifdef FEATURE_FILETYPECHAR
                        /* show type of destination */
                        if (opts & DISP_FTYPE) {
-                               if (!stat(name, info)) {
+                               if (!stat(fullname, info)) {
                                        append = append_char(info->st_mode);
                                        if (append)
                                                fputc(append, stdout);
@@ -508,7 +337,7 @@ static int list_item(const char *name)
        
        if (!S_ISDIR(info.st_mode) || 
            (opts & DIR_NOLIST)) {
-               list_single(name, &info);
+               list_single(name, &info, name);
                return 0;
        }
 
@@ -560,7 +389,7 @@ static int list_item(const char *name)
                strcpy(fnend, entry->d_name);
                if (lstat(fullname, &info))
                        goto direrr; /* (shouldn't fail) */
-               list_single(entry->d_name, &info);
+               list_single(entry->d_name, &info, fullname);
        }
        closedir(dir);
        return 0;
@@ -573,7 +402,7 @@ listerr:
        return 1;
 }
 
-const char     ls_usage[] = "Usage: ls [-1a"
+static const char ls_usage[] = "ls [-1a"
 #ifdef FEATURE_TIMESTAMPS
        "c"
 #endif
@@ -670,8 +499,7 @@ ls_main(int argc, char * * argv)
        exit( i);
 
 print_usage_message:
-       fprintf(stderr, "Usage: %s\n", ls_usage);
+       usage (ls_usage);
        exit( FALSE);
 }
 
-#endif