From 38b42320166194063e087b834222b6192f4c1031 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 16 Jan 2025 17:15:25 +0100 Subject: [PATCH] wip processchmod --- fm.c | 34 ++++++++++++++++++++++++-- function.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ function.h | 4 ++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/fm.c b/fm.c index cbfb856..95a387f 100644 --- 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; diff --git a/function.c b/function.c index e9405b5..7b0a5a6 100644 --- a/function.c +++ b/function.c @@ -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: */ diff --git a/function.h b/function.h index 01f7b16..8fcfaab 100644 --- a/function.h +++ b/function.h @@ -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: */ -- 2.30.2