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 static char bad_path[] = "/bad-path/";
20 static char *get_perf_dir(void)
25 size_t strlcpy(char *dest, const char *src, size_t size)
27 size_t ret = strlen(src);
30 size_t len = (ret >= size) ? size - 1 : ret;
31 memcpy(dest, src, len);
38 static char *get_pathname(void)
40 static char pathname_array[4][PATH_MAX];
42 return pathname_array[3 & ++index];
45 static char *cleanup_path(char *path)
48 if (!memcmp(path, "./", 2)) {
56 char *mksnpath(char *buf, size_t n, const char *fmt, ...)
62 len = vsnprintf(buf, n, fmt, args);
65 strlcpy(buf, bad_path, n);
68 return cleanup_path(buf);
71 static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
73 const char *perf_dir = get_perf_dir();
76 len = strlen(perf_dir);
79 memcpy(buf, perf_dir, len);
80 if (len && !is_dir_sep(perf_dir[len-1]))
82 len += vsnprintf(buf + len, n - len, fmt, args);
85 return cleanup_path(buf);
87 strlcpy(buf, bad_path, n);
91 char *perf_snpath(char *buf, size_t n, const char *fmt, ...)
95 (void)perf_vsnpath(buf, n, fmt, args);
100 char *perf_pathdup(const char *fmt, ...)
105 (void)perf_vsnpath(path, sizeof(path), fmt, args);
107 return xstrdup(path);
110 char *mkpath(const char *fmt, ...)
114 char *pathname = get_pathname();
117 len = vsnprintf(pathname, PATH_MAX, fmt, args);
121 return cleanup_path(pathname);
124 char *perf_path(const char *fmt, ...)
126 const char *perf_dir = get_perf_dir();
127 char *pathname = get_pathname();
131 len = strlen(perf_dir);
132 if (len > PATH_MAX-100)
134 memcpy(pathname, perf_dir, len);
135 if (len && perf_dir[len-1] != '/')
136 pathname[len++] = '/';
138 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
142 return cleanup_path(pathname);
146 /* perf_mkstemp() - create tmp file honoring TMPDIR variable */
147 int perf_mkstemp(char *path, size_t len, const char *template)
152 tmp = getenv("TMPDIR");
155 n = snprintf(path, len, "%s/%s", tmp, template);
157 errno = ENAMETOOLONG;
160 return mkstemp(path);
164 const char *make_relative_path(const char *abs, const char *base)
166 static char buf[PATH_MAX + 1];
170 baselen = strlen(base);
171 if (prefixcmp(abs, base))
173 if (abs[baselen] == '/')
175 else if (base[baselen - 1] != '/')
177 strcpy(buf, abs + baselen);
182 * It is okay if dst == src, but they should not overlap otherwise.
184 * Performs the following normalizations on src, storing the result in dst:
185 * - Ensures that components are separated by '/' (Windows only)
186 * - Squashes sequences of '/'.
187 * - Removes "." components.
188 * - Removes ".." components, and the components the precede them.
189 * Returns failure (non-zero) if a ".." component appears as first path
190 * component anytime during the normalization. Otherwise, returns success (0).
192 * Note that this function is purely textual. It does not follow symlinks,
193 * verify the existence of the path, or make any system calls.
195 int normalize_path_copy(char *dst, const char *src)
199 if (has_dos_drive_prefix(src)) {
205 if (is_dir_sep(*src)) {
207 while (is_dir_sep(*src))
215 * A path component that begins with . could be
217 * (1) "." and ends -- ignore and terminate.
218 * (2) "./" -- ignore them, eat slash and continue.
219 * (3) ".." and ends -- strip one and terminate.
220 * (4) "../" -- strip one, eat slash and continue.
226 } else if (is_dir_sep(src[1])) {
229 while (is_dir_sep(*src))
232 } else if (src[1] == '.') {
237 } else if (is_dir_sep(src[2])) {
240 while (is_dir_sep(*src))
247 /* copy up to the next '/', and eat all '/' */
248 while ((c = *src++) != '\0' && !is_dir_sep(c))
252 while (is_dir_sep(c))
261 * dst0..dst is prefix portion, and dst[-1] is '/';
264 dst--; /* go to trailing '/' */
267 /* Windows: dst[-1] cannot be backslash anymore */
268 while (dst0 < dst && dst[-1] != '/')
276 * path = Canonical absolute path
277 * prefix_list = Colon-separated list of absolute paths
279 * Determines, for each path in prefix_list, whether the "prefix" really
280 * is an ancestor directory of path. Returns the length of the longest
281 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
282 * is an ancestor. (Note that this means 0 is returned if prefix_list is
283 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
284 * are not considered to be their own ancestors. path must be in a
285 * canonical form: empty components, or "." or ".." components are not
286 * allowed. prefix_list may be null, which is like "".
288 int longest_ancestor_length(const char *path, const char *prefix_list)
290 char buf[PATH_MAX+1];
291 const char *ceil, *colon;
292 int len, max_len = -1;
294 if (prefix_list == NULL || !strcmp(path, "/"))
297 for (colon = ceil = prefix_list; *colon; ceil = colon+1) {
298 for (colon = ceil; *colon && *colon != PATH_SEP; colon++);
300 if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
302 strlcpy(buf, ceil, len+1);
303 if (normalize_path_copy(buf, buf) < 0)
306 if (len > 0 && buf[len-1] == '/')
309 if (!strncmp(path, buf, len) &&
319 /* strip arbitrary amount of directory separators at end of path */
320 static inline int chomp_trailing_dir_sep(const char *path, int len)
322 while (len && is_dir_sep(path[len - 1]))
328 * If path ends with suffix (complete path components), returns the
329 * part before suffix (sans trailing directory separators).
330 * Otherwise returns NULL.
332 char *strip_path_suffix(const char *path, const char *suffix)
334 int path_len = strlen(path), suffix_len = strlen(suffix);
340 if (is_dir_sep(path[path_len - 1])) {
341 if (!is_dir_sep(suffix[suffix_len - 1]))
343 path_len = chomp_trailing_dir_sep(path, path_len);
344 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
346 else if (path[--path_len] != suffix[--suffix_len])
350 if (path_len && !is_dir_sep(path[path_len - 1]))
352 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));