1 /* 2 * I'm tired of doing "vsnprintf()" etc just to open a 3 * file, so here's a "return static buffer with printf" 4 * interface for paths. 5 * 6 * It's obviously not thread-safe. Sue me. But it's quite 7 * useful for doing things like 8 * 9 * f = open(mkpath("%s/%s.perf", base, name), O_RDONLY); 10 * 11 * which is what it's designed for. 12 */ 13 #include "cache.h" 14 #include "path.h" 15 #include <linux/kernel.h> 16 #include <limits.h> 17 #include <stdio.h> 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 #include <unistd.h> 21 22 static char bad_path[] = "/bad-path/"; 23 /* 24 * One hack: 25 */ 26 static char *get_pathname(void) 27 { 28 static char pathname_array[4][PATH_MAX]; 29 static int idx; 30 31 return pathname_array[3 & ++idx]; 32 } 33 34 static char *cleanup_path(char *path) 35 { 36 /* Clean it up */ 37 if (!memcmp(path, "./", 2)) { 38 path += 2; 39 while (*path == '/') 40 path++; 41 } 42 return path; 43 } 44 45 char *mkpath(const char *fmt, ...) 46 { 47 va_list args; 48 unsigned len; 49 char *pathname = get_pathname(); 50 51 va_start(args, fmt); 52 len = vsnprintf(pathname, PATH_MAX, fmt, args); 53 va_end(args); 54 if (len >= PATH_MAX) 55 return bad_path; 56 return cleanup_path(pathname); 57 } 58 59 int path__join(char *bf, size_t size, const char *path1, const char *path2) 60 { 61 return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2); 62 } 63 64 int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3) 65 { 66 return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "", 67 path2, path2[0] ? "/" : "", path3); 68 } 69 70 bool is_regular_file(const char *file) 71 { 72 struct stat st; 73 74 if (stat(file, &st)) 75 return false; 76 77 return S_ISREG(st.st_mode); 78 } 79