[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] ls: Versteckte Dateien, Mehrere Verzeichnisse listen



+ls: Erkennung & Handhabung versteckter Dateien
+ls: Mehrere Verzeichnisse können zum auflisten angegeben werden
*ls: Umstellung auf getopt für weitere Erweiterungen

Signed-off-by: Alexander Siol <alex@xxxxxxxxxx>
---
 trunk/src/modules/c/shell/cmds/ls.c |  110 +++++++++++++++++++++++++----------
 1 files changed, 78 insertions(+), 32 deletions(-)

diff --git a/trunk/src/modules/c/shell/cmds/ls.c b/trunk/src/modules/c/shell/cmds/ls.c
index 195f769..8cfe7c7 100644
--- a/trunk/src/modules/c/shell/cmds/ls.c
+++ b/trunk/src/modules/c/shell/cmds/ls.c
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2007 The tyndur Project. All rights reserved.
+ * Copyright (c) 2007-2009 The tyndur Project. All rights reserved.
  *
  * This code is derived from software contributed to the tyndur Project
- * by Antoine Kaufmann.
+ * by Antoine Kaufmann, Alexander Siol
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -39,6 +39,8 @@
 #include "dir.h"
 #include "unistd.h"
 #include <lost/config.h>
+#include <getopt.h>
+#include <collections.h>
 
 void ls_display_usage(void);
 
@@ -48,46 +50,89 @@ void ls_display_usage(void);
     int main(int argc, char* argv[])
 #endif
 {
-    if (argc > 2) {
-        ls_display_usage();
-        return -1;
-    }
-    
     char* dir_path;
-    bool free_dir_path;
+    char* cwd_dir = NULL;
     bool success = TRUE;
+    bool show_hidden = FALSE;
+    list_t *dirs = list_create();
+    bool print_dirnames = FALSE;
 
-    if (argc == 1) {
-        //Mit NULL als Parameter wird der buffer mit malloc() erstellt
-        dir_path = getcwd(NULL, 0);
-        free_dir_path = TRUE;
-    } else {
-        dir_path = argv[1];
-        free_dir_path = FALSE;
+    static const struct option long_options[] =
+    {
+        { "all", no_argument,          0, 'a' },
+        { "help", no_argument,         0,   1 },
+        { 0, 0, 0, 0 }
+    };
+
+    optind = 0;
+
+    while (optind < argc) {
+        int result = getopt_long(argc, argv, "a", long_options, NULL);
+        if (result == -1) {
+            break;
+        }
+        switch (result) {
+            case 'a':
+                show_hidden = TRUE;
+                break;
+            case 1:
+                ls_display_usage();
+                return EXIT_SUCCESS;
+            default:
+                break;
+        }
+    }
+
+    while (optind < argc) {
+        list_push(dirs, argv[optind++]);
     }
 
+    if (list_size(dirs) == 0) {
+        cwd_dir = getcwd(NULL, 0);
+        list_push(dirs, cwd_dir);
+    } 
+
+    if (list_size(dirs) > 1) {
+        print_dirnames = TRUE;
+    }
 
-    io_resource_t* dir_res = directory_open(dir_path);
-    if (dir_res != NULL) {
-        io_direntry_t* direntry;
-        while ((direntry = directory_read(dir_res))) {
-            if (direntry->type == IO_DIRENTRY_FILE) {
-                printf("       %s\n", direntry->name);
-            } else {
-                printf(" [DIR] %s\n", direntry->name);
+    while ((dir_path = list_pop(dirs))) {
+        if (print_dirnames) {
+            printf("%s:\n", dir_path);
+        }
+        io_resource_t* dir_res = directory_open(dir_path);
+        if (dir_res != NULL) {
+            io_direntry_t* direntry;
+            while ((direntry = directory_read(dir_res))) {
+                bool entry_hidden = FALSE;
+                if (strncmp(direntry->name, ".", 1) == 0) {
+                    entry_hidden = TRUE;
+                }
+                if (entry_hidden == FALSE || show_hidden == TRUE) {
+                    if (direntry->type == IO_DIRENTRY_FILE) {
+                        printf("       %s\n", direntry->name);
+                    } else {
+                        printf(" [DIR] %s\n", direntry->name);
+                    }
+                }
+                free(direntry);
             }
-           free(direntry);
+    
+            directory_close(dir_res);
+        } else {
+            printf("Konnte '%s' nicht zum lesen oeffnen!\n", dir_path);
+            success = FALSE;
+        }
+        if (list_size(dirs) > 0) {
+            printf("\n");
         }
-
-        directory_close(dir_res);
-    } else {
-        printf("Konnte '%s' nicht zum lesen oeffnen!\n", dir_path);
-        success = FALSE;
     }
 
-    if (free_dir_path == TRUE) {
-        free(dir_path);
+    if (cwd_dir != NULL) {
+        free(cwd_dir);
     }
+
+    list_destroy(dirs);
     
     if (success == TRUE) {
         return EXIT_SUCCESS;
@@ -98,6 +143,7 @@ void ls_display_usage(void);
 
 void ls_display_usage()
 {
-    puts("\nAufruf: ls [Verzeichnis]\n");
+    puts("\nAufruf: ls [-a] [Verzeichnis]\n");
+    puts("Mittels der Option -a werden versteckte Dateien angezeigt.\n");
 }
 
-- 
1.5.6.5