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 15 static char bad_path[] = "/bad-path/"; 16 /* 17 * Two hacks: 18 */ 19 20 static const char *get_perf_dir(void) 21 { 22 return "."; 23 } 24 25 static char *get_pathname(void) 26 { 27 static char pathname_array[4][PATH_MAX]; 28 static int idx; 29 30 return pathname_array[3 & ++idx]; 31 } 32 33 static char *cleanup_path(char *path) 34 { 35 /* Clean it up */ 36 if (!memcmp(path, "./", 2)) { 37 path += 2; 38 while (*path == '/') 39 path++; 40 } 41 return path; 42 } 43 44 static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args) 45 { 46 const char *perf_dir = get_perf_dir(); 47 size_t len; 48 49 len = strlen(perf_dir); 50 if (n < len + 1) 51 goto bad; 52 memcpy(buf, perf_dir, len); 53 if (len && !is_dir_sep(perf_dir[len-1])) 54 buf[len++] = '/'; 55 len += vsnprintf(buf + len, n - len, fmt, args); 56 if (len >= n) 57 goto bad; 58 return cleanup_path(buf); 59 bad: 60 strlcpy(buf, bad_path, n); 61 return buf; 62 } 63 64 char *perf_pathdup(const char *fmt, ...) 65 { 66 char path[PATH_MAX]; 67 va_list args; 68 va_start(args, fmt); 69 (void)perf_vsnpath(path, sizeof(path), fmt, args); 70 va_end(args); 71 return xstrdup(path); 72 } 73 74 char *mkpath(const char *fmt, ...) 75 { 76 va_list args; 77 unsigned len; 78 char *pathname = get_pathname(); 79 80 va_start(args, fmt); 81 len = vsnprintf(pathname, PATH_MAX, fmt, args); 82 va_end(args); 83 if (len >= PATH_MAX) 84 return bad_path; 85 return cleanup_path(pathname); 86 } 87 88 char *perf_path(const char *fmt, ...) 89 { 90 const char *perf_dir = get_perf_dir(); 91 char *pathname = get_pathname(); 92 va_list args; 93 unsigned len; 94 95 len = strlen(perf_dir); 96 if (len > PATH_MAX-100) 97 return bad_path; 98 memcpy(pathname, perf_dir, len); 99 if (len && perf_dir[len-1] != '/') 100 pathname[len++] = '/'; 101 va_start(args, fmt); 102 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args); 103 va_end(args); 104 if (len >= PATH_MAX) 105 return bad_path; 106 return cleanup_path(pathname); 107 } 108 109 /* strip arbitrary amount of directory separators at end of path */ 110 static inline int chomp_trailing_dir_sep(const char *path, int len) 111 { 112 while (len && is_dir_sep(path[len - 1])) 113 len--; 114 return len; 115 } 116 117 /* 118 * If path ends with suffix (complete path components), returns the 119 * part before suffix (sans trailing directory separators). 120 * Otherwise returns NULL. 121 */ 122 char *strip_path_suffix(const char *path, const char *suffix) 123 { 124 int path_len = strlen(path), suffix_len = strlen(suffix); 125 126 while (suffix_len) { 127 if (!path_len) 128 return NULL; 129 130 if (is_dir_sep(path[path_len - 1])) { 131 if (!is_dir_sep(suffix[suffix_len - 1])) 132 return NULL; 133 path_len = chomp_trailing_dir_sep(path, path_len); 134 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); 135 } 136 else if (path[--path_len] != suffix[--suffix_len]) 137 return NULL; 138 } 139 140 if (path_len && !is_dir_sep(path[path_len - 1])) 141 return NULL; 142 return strndup(path, chomp_trailing_dir_sep(path, path_len)); 143 } 144