diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.c | 23 | ||||
-rw-r--r-- | lib/lib.h | 2 |
2 files changed, 25 insertions, 0 deletions
@@ -1002,6 +1002,17 @@ void mode_to_string(mode_t mode, char *buf) *buf = c; } +// dirname() can modify its argument or return a pointer to a constant string +// This always returns a malloc() copy of everyting before last (run of ) '/'. +char *getdirname(char *name) +{ + char *s = xstrdup(name), *ss = strrchr(s, '/'); + + while (*ss && *ss == '/' && s != ss) *ss-- = 0; + + return s; +} + // basename() can modify its argument or return a pointer to a constant string // This just gives after the last '/' or the whole stirng if no / char *getbasename(char *name) @@ -1013,6 +1024,18 @@ char *getbasename(char *name) return name; } +// Is this file under this directory? +int fileunderdir(char *file, char *dir) +{ + char *s1 = xabspath(dir, 1), *s2 = xabspath(file, -1), *ss = s2; + int rc = s1 && s2 && strstart(&ss, s1) && (!s1[1] || s2[strlen(s1)] == '/'); + + free(s1); + free(s2); + + return rc; +} + // Execute a callback for each PID that matches a process name from a list. void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)) { @@ -337,7 +337,9 @@ char *num_to_sig(int sig); mode_t string_to_mode(char *mode_str, mode_t base); void mode_to_string(mode_t mode, char *buf); +char *getdirname(char *name); char *getbasename(char *name); +int fileunderdir(char *file, char *dir); void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)); pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid); |