better error management
authorLaurent Mazet <mazet@softndesign.org>
Sun, 19 Jan 2025 22:55:46 +0000 (23:55 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 19 Jan 2025 22:55:46 +0000 (23:55 +0100)
fm.c
function.c

diff --git a/fm.c b/fm.c
index e41f3a3f6db0b5003c4328d1ec2b71b0e264ab94..36329754b6c4b080f4e0f9929abbaaf2e1249ba9 100644 (file)
--- a/fm.c
+++ b/fm.c
@@ -5,6 +5,7 @@
 /* winlnk: debug.o display.o function.o -lpdcurses -lregex */
 
 #include <curses.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -238,13 +239,22 @@ int main (int argc, char *argv[])
             current = list[panel]->tab + windir[panel]->index;
             if ((current->type == type_dir_e) || (current->type == type_ldir_e)) {
                 char *_dirname = newfilename (dirname[panel], current->name);
-                free (dirname[panel]);
-                dirname[panel] = _dirname;
-                index_x[panel] = 0;
-                index_y[panel] = 0;
-                freelist (list[panel]);
-                list[panel] = NULL;
-                page[panel] = 0;
+                DIR *dir = opendir (_dirname);
+                if (dir == NULL) {
+                    char *msg = strdupcat ("Can't open directory '", _dirname, "'", NULL);
+                    errorwindow (msg, winscreen);
+                   free (msg);
+                   free (_dirname);
+                } else {
+                    closedir (dir);
+                    free (dirname[panel]);
+                    dirname[panel] = _dirname;
+                    index_x[panel] = 0;
+                    index_y[panel] = 0;
+                    freelist (list[panel]);
+                    list[panel] = NULL;
+                    page[panel] = 0;
+                }
             }
             break;
         case '\t':
index 5b490e0a432b5602b5be8ea156045f2695c8b6f8..24b980e77499f062492322d8dac73ca1a6edd197 100644 (file)
@@ -101,11 +101,8 @@ list_t *alloclist (void)
 list_t *_addelement (list_t *list, char *dirname, char *filename, type_t type)
 {
     char *name = newfilename (dirname, filename);
-    struct stat sb;
-    if (STAT (name, &sb) == -1) {
-        VERBOSE (ERROR, fprintf (stderr, "can't stat file '%s'\n", name));
-        exit (1);
-    }
+    struct stat sb = {0};
+    int error = (STAT (name, &sb) == -1);
 
     size_t size = 0;
     if (type == type_reg_e) {
@@ -216,34 +213,39 @@ void getinfo (char *dirname, elem_t *elem)
 {
     char *name = newfilename (dirname, elem->name);
 
-    struct stat sb;
-    if (STAT (name, &sb) == -1) {
-        VERBOSE (ERROR, fprintf (stderr, "can't stat file '%s%s%s'\n", dirname, SEPARATOR, elem->name));
-        exit (1);
-    }
+    struct stat sb = {0};
+    int error = (STAT (name, &sb) == -1);
     free (name);
 
 #ifdef WIN32
     elem->uid = strdup ("");
 #else /* Linux*/
-    struct passwd *pwd = getpwuid (sb.st_uid);
-    if (pwd) {
-        elem->uid = strdup (pwd->pw_name);
+    if (!error) {
+        struct passwd *pwd = getpwuid (sb.st_uid);
+        if (pwd) {
+            elem->uid = strdup (pwd->pw_name);
+        } else {
+            elem->uid = (char *) calloc (_log10 (sb.st_uid) + 1, 1);
+            sprintf (elem->uid, "%d", sb.st_uid);
+        }
     } else {
-        elem->uid = (char *) calloc (_log10 (sb.st_uid) + 1, 1);
-        sprintf (elem->uid, "%d", sb.st_uid);
+        elem->uid = strdup ("");
     }
 #endif
 
 #ifdef WIN32
     elem->gid = strdup ("");
 #else /* Linux*/
-    struct group *grp = getgrgid (sb.st_gid);
-    if (grp) {
-        elem->gid = strdup (grp->gr_name);
+    if (!error) {
+        struct group *grp = getgrgid (sb.st_gid);
+        if (grp) {
+            elem->gid = strdup (grp->gr_name);
+        } else {
+            elem->gid = (char *) calloc (_log10 (sb.st_gid) + 1, 1);
+            sprintf (elem->gid, "%d", sb.st_gid);
+        }
     } else {
-        elem->gid = (char *) calloc (_log10 (sb.st_gid) + 1, 1);
-        sprintf (elem->gid, "%d", sb.st_gid);
+        elem->gid = strdup ("");
     }
 #endif