aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-12-27 21:36:33 -0600
committerRob Landley <rob@landley.net>2007-12-27 21:36:33 -0600
commit354834114662d369ce96cac5e787623df1cc68f8 (patch)
treee36dce93f8aef65d1770ca04cf4e02f9701dcf21 /lib/lib.c
parentd3236c1fd785b27da00dd704ade7992432dd7644 (diff)
downloadtoybox-354834114662d369ce96cac5e787623df1cc68f8.tar.gz
Make patch's file add actually work, including directory creating and
understanding zero-context hunks.
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 985c580d..6699a685 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -322,6 +322,33 @@ char *xabspath(char *path)
return path;
}
+// Ensure entire path exists.
+// If mode != -1 set permissions on newly created dirs.
+// Requires that path string be writable (for temporary null terminators).
+void xmkpath(char *path, int mode)
+{
+ char *p, old;
+ mode_t mask;
+ int rc;
+ struct stat st;
+
+ for (p = path; ; p++) {
+ if (!*p || *p == '/') {
+ old = *p;
+ *p = rc = 0;
+ if (stat(path, &st) || !S_ISDIR(st.st_mode)) {
+ if (mode != -1) {
+ mask=umask(0);
+ rc = mkdir(path, mode);
+ umask(mask);
+ } else rc = mkdir(path, 0777);
+ }
+ *p = old;
+ if(rc) perror_exit("mkpath '%s'",path);
+ }
+ if (!*p) break;
+ }
+}
// Find all file in a colon-separated path with access type "type" (generally
// X_OK or R_OK). Returns a list of absolute paths to each file found, in
// order.