bcma: claim only 14e4:4365 PCI Dell card with SoftMAC BCM43142
[linux/fpc-iii.git] / tools / perf / util / path.c
blob3654d964e49de2af41a30ca915a349563aba4fd1
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.
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.
13 #include "cache.h"
15 static char bad_path[] = "/bad-path/";
17 * Two hacks:
20 static const char *get_perf_dir(void)
22 return ".";
25 static char *get_pathname(void)
27 static char pathname_array[4][PATH_MAX];
28 static int idx;
30 return pathname_array[3 & ++idx];
33 static char *cleanup_path(char *path)
35 /* Clean it up */
36 if (!memcmp(path, "./", 2)) {
37 path += 2;
38 while (*path == '/')
39 path++;
41 return path;
44 static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
46 const char *perf_dir = get_perf_dir();
47 size_t len;
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;
64 char *perf_pathdup(const char *fmt, ...)
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);
74 char *mkpath(const char *fmt, ...)
76 va_list args;
77 unsigned len;
78 char *pathname = get_pathname();
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);
88 char *perf_path(const char *fmt, ...)
90 const char *perf_dir = get_perf_dir();
91 char *pathname = get_pathname();
92 va_list args;
93 unsigned len;
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);
109 /* strip arbitrary amount of directory separators at end of path */
110 static inline int chomp_trailing_dir_sep(const char *path, int len)
112 while (len && is_dir_sep(path[len - 1]))
113 len--;
114 return len;
118 * If path ends with suffix (complete path components), returns the
119 * part before suffix (sans trailing directory separators).
120 * Otherwise returns NULL.
122 char *strip_path_suffix(const char *path, const char *suffix)
124 int path_len = strlen(path), suffix_len = strlen(suffix);
126 while (suffix_len) {
127 if (!path_len)
128 return NULL;
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);
136 else if (path[--path_len] != suffix[--suffix_len])
137 return NULL;
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));