aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-04-08 22:37:33 -0500
committerRob Landley <rob@landley.net>2018-04-08 22:37:33 -0500
commit8fdd58a02257baf8f2bca1c086571ea7b7e17365 (patch)
treedaffcd848c786e63372d4caeb4b9181d0b3f914c /lib/lib.c
parent221439164eb6683a5af35540b24b5620e5d8ab19 (diff)
downloadtoybox-8fdd58a02257baf8f2bca1c086571ea7b7e17365.tar.gz
Add cp --parents
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c23
1 files changed, 23 insertions, 0 deletions
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))
{