path.c (f26e8817b235d8764363bffcc9cbfc61867371f2) path.c (9a3993d408bc61b839de1a2c6c783477a04860bb)
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"
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"
14#include "util.h"
14#include "path.h"
15#include <linux/kernel.h>
15#include <limits.h>
16#include <limits.h>
17#include <stdio.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <unistd.h>
16
17static char bad_path[] = "/bad-path/";
18/*
19 * One hack:
20 */
21static char *get_pathname(void)
22{
23 static char pathname_array[4][PATH_MAX];

--- 21 unchanged lines hidden (view full) ---

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}
21
22static char bad_path[] = "/bad-path/";
23/*
24 * One hack:
25 */
26static char *get_pathname(void)
27{
28 static char pathname_array[4][PATH_MAX];

--- 21 unchanged lines hidden (view full) ---

50
51 va_start(args, fmt);
52 len = vsnprintf(pathname, PATH_MAX, fmt, args);
53 va_end(args);
54 if (len >= PATH_MAX)
55 return bad_path;
56 return cleanup_path(pathname);
57}
58
59int path__join(char *bf, size_t size, const char *path1, const char *path2)
60{
61 return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
62}
63
64int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3)
65{
66 return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "",
67 path2, path2[0] ? "/" : "", path3);
68}
69
70bool is_regular_file(const char *file)
71{
72 struct stat st;
73
74 if (stat(file, &st))
75 return false;
76
77 return S_ISREG(st.st_mode);
78}