1 // SPDX-License-Identifier: GPL-2.0
3 * I'm tired of doing "vsnprintf()" etc just to open a
4 * file, so here's a "return static buffer with printf"
7 * It's obviously not thread-safe. Sue me. But it's quite
8 * useful for doing things like
10 * f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
12 * which is what it's designed for.
16 #include <linux/kernel.h>
19 #include <sys/types.h>
23 static char bad_path[] = "/bad-path/";
27 static char *get_pathname(void)
29 static char pathname_array[4][PATH_MAX];
32 return pathname_array[3 & ++idx];
35 static char *cleanup_path(char *path)
38 if (!memcmp(path, "./", 2)) {
46 char *mkpath(const char *fmt, ...)
50 char *pathname = get_pathname();
53 len = vsnprintf(pathname, PATH_MAX, fmt, args);
57 return cleanup_path(pathname);
60 int path__join(char *bf, size_t size, const char *path1, const char *path2)
62 return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
65 int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3)
67 return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "",
68 path2, path2[0] ? "/" : "", path3);
71 bool is_regular_file(const char *file)
78 return S_ISREG(st.st_mode);