2 * I'm tired of doing "vsnprintf()" etc just to open a
3 * file, so here's a "return static buffer with printf"
6 * It's obviously not thread-safe. Sue me. But it's quite
7 * useful for doing things like
9 * f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
11 * which is what it's designed for.
15 #include <linux/kernel.h>
18 #include <sys/types.h>
22 static char bad_path[] = "/bad-path/";
26 static char *get_pathname(void)
28 static char pathname_array[4][PATH_MAX];
31 return pathname_array[3 & ++idx];
34 static char *cleanup_path(char *path)
37 if (!memcmp(path, "./", 2)) {
45 char *mkpath(const char *fmt, ...)
49 char *pathname = get_pathname();
52 len = vsnprintf(pathname, PATH_MAX, fmt, args);
56 return cleanup_path(pathname);
59 int path__join(char *bf, size_t size, const char *path1, const char *path2)
61 return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
64 int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3)
66 return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "",
67 path2, path2[0] ? "/" : "", path3);
70 bool is_regular_file(const char *file)
77 return S_ISREG(st.st_mode);