From 354834114662d369ce96cac5e787623df1cc68f8 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 27 Dec 2007 21:36:33 -0600 Subject: Make patch's file add actually work, including directory creating and understanding zero-context hunks. --- lib/lib.c | 27 +++++++++++++++++++++++++++ lib/lib.h | 1 + 2 files changed, 28 insertions(+) (limited to 'lib') 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. diff --git a/lib/lib.h b/lib/lib.h index 28ff0da8..afc3a8b2 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -73,6 +73,7 @@ void xwrite(int fd, void *buf, size_t len); char *xgetcwd(void); void xstat(char *path, struct stat *st); char *xabspath(char *path); +void xmkpath(char *path, int mode); struct string_list *find_in_path(char *path, char *filename); void utoa_to_buf(unsigned n, char *buf, unsigned buflen); void itoa_to_buf(int n, char *buf, unsigned buflen); -- cgit v1.2.3