read directory
authorLaurent Mazet <mazet@softndesign.org>
Sun, 29 Dec 2024 07:17:00 +0000 (08:17 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 29 Dec 2024 07:17:00 +0000 (08:17 +0100)
fm.c
function.c
type.h

diff --git a/fm.c b/fm.c
index 5bdd955f86aa9260b528961db2a587d87207c0cf..25f737801cacdf4dbc394558d7d63748b3fd1a3a 100644 (file)
--- a/fm.c
+++ b/fm.c
@@ -1,8 +1,8 @@
 /* depend: */
 /* cflags: */
-/* linker: display.o function.o -lcurses */
-/* doslnk: display.o function.o -lpdc~1 */
-/* winlnk: display.o function.o -lpdcurses */
+/* linker: debug.o display.o function.o -lcurses */
+/* doslnk: debug.o display.o function.o -lpdc~1 */
+/* winlnk: debug.o display.o function.o -lpdcurses */
 
 #include <curses.h>
 #include <stdio.h>
@@ -90,38 +90,34 @@ int main (int argc, char *argv[])
     while (!stop) {
         char *ptr = NULL;
 
-        boardwindow (current);
+        //boardwindow (current);
 
         int ch = getch ();
         switch (ch) {
         case KEY_UP:
         case 'i':
-            dir = 0;
             break;
         case KEY_LEFT:
         case 'j':
-            dir = 3;
             break;
         case KEY_DOWN:
         case 'k':
-            dir = 2;
             break;
         case KEY_RIGHT:
         case 'l':
-            dir = 1;
             break;
         case KEY_ESC:
         case 'q':
-            if (askwindow (" Restart (Y/N) ", max (board->xoffset + (board->xsize - savelen) / 2, 1), board->yoffset + (board->ysize - 1) / 2, "Yy", "Nn") == 1) {
+            //if (askwindow (" Restart (Y/N) ", max (board->xoffset + (board->xsize - savelen) / 2, 1), board->yoffset + (board->ysize - 1) / 2, "Yy", "Nn") == 1) {
                 stop = 1;
-            }
+            //}
             break;
         }
     }
 
     endwin ();
 
-    freeboard (board);
+    //freeboard (board);
 
     return 0;
 }
index a99ce6808b549e2e2eccbebe5d2317c073f70c1b..3571da204da7899c2b5765a6b2f2493e42f4040b 100644 (file)
@@ -1,12 +1,17 @@
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
 
 #include "debug.h"
 #include "type.h"
 
 #include "function.h"
 
+#define SEPARATOR "/"
+
 int strmaxlen (char *str, char ch)
 {
     int len = 0;
@@ -23,16 +28,69 @@ int strmaxlen (char *str, char ch)
 
 list_t *alloclist ()
 {
-    list_t *list = CHECKALLOC (list_t);
+    list_t *list = (list_t *) malloc (sizeof (list_t));
+    CHECKALLOC (list);
     list->nb = 0;
     list->tab = NULL;
 
     return list;
 }
 
-list_t *addelement (list_t *list, char *path)
+list_t *addelement (list_t *list, char *dirname, char *filename, type_t type)
 {
+    size_t size = 0;
+
+    if ((type == type_unkn_e) || (type == type_reg_e)) {
+        char *name = (char *) calloc (1, strlen (dirname) + strlen (filename) + 1 + 1);
+        CHECKALLOC (name);
+        strcat (strcat(strcpy (name, dirname), SEPARATOR), filename);
+        
+        struct stat sb;
+        if (lstat (name, &sb) == -1) {
+            VERBOSE (ERROR, fprintf (stderr, "can't stat on file '%s'\n", name));
+            exit (1);
+        }
+
+        switch (sb.st_mode & S_IFMT) {
+        case S_IFBLK:
+            type = type_block_e;
+            break;
+        case S_IFCHR:
+            type = type_char_e;
+            break;
+        case S_IFDIR:
+            type = type_dir_e;
+            break;
+        case S_IFIFO:
+            type = type_pipe_e;
+            break;
+        case S_IFLNK:
+            type = type_symb_e;
+            break;
+        case S_IFREG:
+            type = type_reg_e;
+            break;
+        case S_IFSOCK:
+            type = type_socket_e;
+            break;
+        }
+
+        if (type == type_reg_e) {
+            size = sb.st_size;    
+        }
+
+        free (name);
+    }
 
+    list->nb++;
+    list->tab = (elem_t *) realloc (list->tab, list->nb * sizeof (elem_t));
+    CHECKALLOC (list->tab);
+    elem_t * elem = list->tab + list->nb - 1;
+    elem->name = strdup (filename);
+    elem->size = size;
+    elem->type = type;
+
+    return list;
 }
 
 list_t *exporedir (char *dirname)
@@ -47,8 +105,33 @@ list_t *exporedir (char *dirname)
 
     struct dirent *dp = NULL;
     while ((dp = readdir(dir)) != NULL) {
+        type_t type = type_unkn_e;
+        switch (dp->d_type) {
+        case DT_BLK:
+            type = type_block_e;
+            break;
+        case DT_CHR:
+            type = type_char_e;
+            break;
+        case DT_DIR:
+            type = type_dir_e;
+            break;
+        case DT_FIFO:
+            type = type_pipe_e;
+            break;
+        case DT_LNK:
+            type = type_symb_e;
+            break;
+        case DT_REG:
+            type = type_reg_e;
+            break;
+        case DT_SOCK:
+            type = type_socket_e;
+            break;
+        }
+        addelement (list, dirname, dp->d_name, type);
     }
-    closedir(dirp);
+    closedir(dir);
 
     return list;
 }
diff --git a/type.h b/type.h
index f4b89385e86e87757c93cb2fcf5699ab78fced4a..1375039e5c52c7a55906960f3e55e3193a7a95b1 100644 (file)
--- a/type.h
+++ b/type.h
@@ -2,23 +2,23 @@
 #define __TYPE_H__
 
 typedef enum {
-    unkn_e,
-    block_e,
-    char_e,
-    dir_e,
-    pipe_e,
-    symb_e,
-    reg_e,
-    socket_e
-} type_e;
+    type_unkn_e,
+    type_block_e,
+    type_char_e,
+    type_dir_e,
+    type_pipe_e,
+    type_symb_e,
+    type_reg_e,
+    type_socket_e
+} type_t;
 
 typedef struct {
-    char *gid;
-    unsigned short mode;
+    //char *gid;
+    //unsigned short mode;
     char *name;
     size_t size;
-    type_e type;
-    char *uid;
+    type_t type;
+    //char *uid;
 } elem_t;
 
 typedef struct {