More stuff
[oweals/busybox.git] / ls.c
diff --git a/ls.c b/ls.c
index 2566beea0667fde23f79fef2ed442ac201e42415..a8e7e048a5831068e254c5fae5d61bd4cfe2096d 100644 (file)
--- a/ls.c
+++ b/ls.c
@@ -1,4 +1,137 @@
+/*
+ * 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
+ *
+ */
+
+// I started writing a newer small one, but it isn't done yet....
+//  -Erik
+#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, FALSE, 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:
+               usage (ls_usage);
+       }
+       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
+
+
 /*
  * tiny-ls.c version 0.1.0: A minimalist 'ls'
  * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
  * 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 (libc6 uses 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> 
 
 /* The 9 mode bits to test */
 
-static const umode_t MBIT[] = {
+static const mode_t MBIT[] = {
   S_IRUSR, S_IWUSR, S_IXUSR,
   S_IRGRP, S_IWGRP, S_IXGRP,
   S_IROTH, S_IWOTH, S_IXOTH
@@ -101,7 +232,7 @@ static const umode_t MBIT[] = {
 
 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
 
-static const umode_t SBIT[] = {
+static const mode_t SBIT[] = {
   0, 0, S_ISUID,
   0, 0, S_ISGID,
   0, 0, S_ISVTX
@@ -146,7 +277,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;
@@ -192,7 +323,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';
@@ -211,14 +342,14 @@ static char append_char(umode_t mode)
 
 static void list_single(const char *name, struct stat *info)
 {
-       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; 
+               mode_t mode = info->st_mode; 
                int i;
                
                scratch[0] = TYPECHAR(mode);
@@ -436,11 +567,11 @@ direrr:
        closedir(dir);  
 listerr:
        newline();
-       name_and_error(name);
+       perror(name);
        return 1;
 }
 
-const char     ls_usage[] = "Usage: ls [-1a"
+const char     ls_usage[] = "ls [-1a"
 #ifdef FEATURE_TIMESTAMPS
        "c"
 #endif
@@ -465,7 +596,7 @@ const char  ls_usage[] = "Usage: ls [-1a"
        "] [filenames...]\n";
 
 extern int
-ls_main(struct FileInfo * not_used, int argc, char * * argv)
+ls_main(int argc, char * * argv)
 {
        int argi=1, i;
        
@@ -534,9 +665,11 @@ ls_main(struct FileInfo * not_used, int argc, char * * argv)
        while (argi < argc)
                i |= list_item(argv[argi++]);
        newline();
-       return i;
+       exit( i);
 
 print_usage_message:
-       usage(ls_usage);
-       return 1;
+       usage (ls_usage);
+       exit( FALSE);
 }
+
+#endif