From 8fdd58a02257baf8f2bca1c086571ea7b7e17365 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 8 Apr 2018 22:37:33 -0500 Subject: Add cp --parents --- lib/lib.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/lib.c') diff --git a/lib/lib.c b/lib/lib.c index 27d14be7..8c46678f 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -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)) { -- cgit v1.2.3