wip processchmod
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 16 Jan 2025 16:15:25 +0000 (17:15 +0100)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 16 Jan 2025 16:15:25 +0000 (17:15 +0100)
fm.c
function.c
function.h

diff --git a/fm.c b/fm.c
index cbfb8567ffeaa84507914af899ea7ddd42ec86ec..95a387fa0095acabd84f49308bc233e425fc6623 100644 (file)
--- a/fm.c
+++ b/fm.c
@@ -418,9 +418,9 @@ int main (int argc, char *argv[])
                         }
                         free (src);
                         free (dest);
+                        freelist (list[panel]);
+                        list[panel] = NULL;
                     }
-                    freelist (list[panel]);
-                    list[panel] = NULL;
                     free (string);
                 }
                 break;
@@ -432,6 +432,36 @@ int main (int argc, char *argv[])
                 break;
             case ALT_V: /* view */
                 break;
+            case ALT_X: /* chmod */
+                string = getwindow ("Change to mode", winscreen, 4, 4, "0644");
+                if (string) {
+                    int mode = strtol (string, NULL, 8);
+                    if ((mode <= 0)  || (mode > 0777)) {
+                        char *octal = itoo (mode);
+                        char *buf = strdupcat ("Can't change file(s) to mode 0", (octal && *octal) ? octal : "000" , NULL);
+                        msgwindow (buf, winscreen, 0);
+                        getch ();
+                        free (octal);
+                        free (buf);
+                    } else {
+                        for (i = 0; i < list[panel]->nb; i++) {
+                            current = list[panel]->tab + i;
+                            if (current->selected) {
+                                char *src = strdupcat (dirname[panel], SEPARATOR, current->name, NULL);
+                                char *msg = processchmod (src, mode);
+                                if (msg) {
+                                    msgwindow (msg, winscreen, 0);
+                                    getch ();
+                                    free (msg);
+                                }
+                                free (src);
+                            }
+                        }
+                        freelist (list[panel]);
+                        list[panel] = NULL;
+                    }
+                    free (string);
+                }
 #ifndef PDCURSES
             }
             break;
index e9405b55c0a4a427665a47a83e1d354845aafa74..7b0a5a6f60e2b8479128759d1f114ef80b39c08a 100644 (file)
@@ -56,6 +56,31 @@ char *strdupcat (char *init, ...)
     return string;
 }
 
+int _log8 (int x)
+{
+    int l = 0;
+    while (x > 0) {
+        x /= 8;
+        l++;
+    }
+    return l;
+}
+
+char *itoo (int x)
+{
+    int n = _log8 (x);
+    char *octal = (char *) calloc (n, 1);
+    CHECKALLOC (octal);
+
+    int l = 0;
+    while (x > 0) {
+        octal[n - 1 - (l++)] = '0' + (x % 8);
+        x /= 8;
+    }
+
+    return octal;
+}
+
 char *newfilename (char *dirname, char *filename)
 {
     char *name = (char *) calloc (strlen (dirname) + strlen (SEPARATOR) + strlen (filename) + 1, 1);
@@ -528,4 +553,49 @@ char *processmove (char *dest, char *src)
     return msg;
 }
 
+char *processchmod (char *name, int mode)
+{
+    char *msg = NULL;
+
+    struct stat sb;
+    if (STAT (name, &sb) == 0) {
+        char *octal = itoo (mode);
+        if ((sb.st_mode & S_IFMT) == S_IFDIR) {
+            DIR *dir = opendir (name);
+
+            if (dir != NULL) {
+                struct dirent *dp = NULL;
+                while (((dp = readdir (dir)) != NULL) && (msg == NULL)) {
+                    if ((strcmp (dp->d_name, ".") == 0) || (strcmp (dp->d_name, "..") == 0)) {
+                        continue;
+                    }
+                    char *next = strdupcat (name, SEPARATOR, dp->d_name, NULL);
+                    msg = processchmod (next, mode);
+                    free (next);
+                }
+                closedir (dir);
+                if (msg == NULL) {
+                    VERBOSE (DEBUG, fprintf (stderr, "chmod '%s' to 0%o\n", name, mode));
+                    if (chmod (name, mode) != 0) {
+                        msg = strdupcat ("Can't change mode ", octal, " for directory '", name, "'");
+                    }
+                }
+            } else {
+                msg = strdupcat ("Can't open directory '", name, "'");
+            }
+        } else {
+            VERBOSE (DEBUG, fprintf (stderr, "chmod  '%s' to 0%o\n", name, mode));
+            if (chmod (name, mode) != 0) {
+                msg = strdupcat ("Can't change mode ", octal, " for file '", name, "'");
+            }
+        }
+        free (octal);
+    } else {
+        VERBOSE (ERROR, fprintf (stderr, "can't stat on file '%s'\n", name));
+        exit (1);
+    }
+
+    return msg;
+}
+
 /* vim: set ts=4 sw=4 et: */
index 01f7b167e32d5fbd94e3a603004e755e4dd150c4..8fcfaab621e76603ab14d1efd553b61bba1a3be6 100644 (file)
@@ -29,6 +29,8 @@ int strmaxlen (char *str, char ch);
 
 char *strdupcat (char *init, ...);
 
+char *itoo (int x);
+
 char *newfilename (char *dirname, char *filename);
 
 list_t *alloclist (void);
@@ -55,6 +57,8 @@ char *processmkdir (char *name);
 
 char *processmove (char *dest, char *src);
 
+char *processchmod (char *name, int mode);
+
 #endif /* __FUNCTION_H__ */
 
 /* vim: set ts=4 sw=4 et: */