From 1221c4a697a5d128948e3fa3d2d7412b6a231851 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 29 Dec 2024 08:17:00 +0100 Subject: [PATCH] read directory --- fm.c | 18 +++++------ function.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- type.h | 26 ++++++++-------- 3 files changed, 106 insertions(+), 27 deletions(-) diff --git a/fm.c b/fm.c index 5bdd955..25f7378 100644 --- 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 #include @@ -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; } diff --git a/function.c b/function.c index a99ce68..3571da2 100644 --- a/function.c +++ b/function.c @@ -1,12 +1,17 @@ +#include #include #include #include +#include +#include #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 f4b8938..1375039 100644 --- 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 { -- 2.30.2