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 +++++++++++++++++++++++ lib/lib.h | 2 ++ 2 files changed, 25 insertions(+) (limited to 'lib') 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)) { diff --git a/lib/lib.h b/lib/lib.h index ccf342cc..353e262d 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -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); -- cgit v1.2.3