Merge branch 'for-4.8-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[deliverable/linux.git] / tools / perf / util / path.c
CommitLineData
07800601
IM
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"
175729fc
ACM
14#include "util.h"
15#include <limits.h>
07800601
IM
16
17static char bad_path[] = "/bad-path/";
18/*
814b3f51 19 * One hack:
07800601 20 */
07800601
IM
21static char *get_pathname(void)
22{
23 static char pathname_array[4][PATH_MAX];
83a0944f
IM
24 static int idx;
25
26 return pathname_array[3 & ++idx];
07800601
IM
27}
28
29static char *cleanup_path(char *path)
30{
31 /* Clean it up */
32 if (!memcmp(path, "./", 2)) {
33 path += 2;
34 while (*path == '/')
35 path++;
36 }
37 return path;
38}
39
07800601
IM
40char *mkpath(const char *fmt, ...)
41{
42 va_list args;
43 unsigned len;
44 char *pathname = get_pathname();
45
46 va_start(args, fmt);
47 len = vsnprintf(pathname, PATH_MAX, fmt, args);
48 va_end(args);
49 if (len >= PATH_MAX)
50 return bad_path;
51 return cleanup_path(pathname);
52}
This page took 0.339909 seconds and 5 git commands to generate.