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 const 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
];
43 return pathname_array
[3 & ++idx
];
46 static char *cleanup_path(char *path
)
49 if (!memcmp(path
, "./", 2)) {
57 char *mksnpath(char *buf
, size_t n
, const char *fmt
, ...)
63 len
= vsnprintf(buf
, n
, fmt
, args
);
66 strlcpy(buf
, bad_path
, n
);
69 return cleanup_path(buf
);
72 static char *perf_vsnpath(char *buf
, size_t n
, const char *fmt
, va_list args
)
74 const char *perf_dir
= get_perf_dir();
77 len
= strlen(perf_dir
);
80 memcpy(buf
, perf_dir
, len
);
81 if (len
&& !is_dir_sep(perf_dir
[len
-1]))
83 len
+= vsnprintf(buf
+ len
, n
- len
, fmt
, args
);
86 return cleanup_path(buf
);
88 strlcpy(buf
, bad_path
, n
);
92 char *perf_snpath(char *buf
, size_t n
, const char *fmt
, ...)
96 (void)perf_vsnpath(buf
, n
, fmt
, args
);
101 char *perf_pathdup(const char *fmt
, ...)
106 (void)perf_vsnpath(path
, sizeof(path
), fmt
, args
);
108 return xstrdup(path
);
111 char *mkpath(const char *fmt
, ...)
115 char *pathname
= get_pathname();
118 len
= vsnprintf(pathname
, PATH_MAX
, fmt
, args
);
122 return cleanup_path(pathname
);
125 char *perf_path(const char *fmt
, ...)
127 const char *perf_dir
= get_perf_dir();
128 char *pathname
= get_pathname();
132 len
= strlen(perf_dir
);
133 if (len
> PATH_MAX
-100)
135 memcpy(pathname
, perf_dir
, len
);
136 if (len
&& perf_dir
[len
-1] != '/')
137 pathname
[len
++] = '/';
139 len
+= vsnprintf(pathname
+ len
, PATH_MAX
- len
, fmt
, args
);
143 return cleanup_path(pathname
);
147 /* perf_mkstemp() - create tmp file honoring TMPDIR variable */
148 int perf_mkstemp(char *path
, size_t len
, const char *template)
153 tmp
= getenv("TMPDIR");
156 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
158 errno
= ENAMETOOLONG
;
161 return mkstemp(path
);
165 const char *make_relative_path(const char *abs_path
, const char *base
)
167 static char buf
[PATH_MAX
+ 1];
173 baselen
= strlen(base
);
174 if (prefixcmp(abs_path
, base
))
176 if (abs_path
[baselen
] == '/')
178 else if (base
[baselen
- 1] != '/')
181 strcpy(buf
, abs_path
+ baselen
);
187 * It is okay if dst == src, but they should not overlap otherwise.
189 * Performs the following normalizations on src, storing the result in dst:
190 * - Ensures that components are separated by '/' (Windows only)
191 * - Squashes sequences of '/'.
192 * - Removes "." components.
193 * - Removes ".." components, and the components the precede them.
194 * Returns failure (non-zero) if a ".." component appears as first path
195 * component anytime during the normalization. Otherwise, returns success (0).
197 * Note that this function is purely textual. It does not follow symlinks,
198 * verify the existence of the path, or make any system calls.
200 int normalize_path_copy(char *dst
, const char *src
)
204 if (has_dos_drive_prefix(src
)) {
210 if (is_dir_sep(*src
)) {
212 while (is_dir_sep(*src
))
220 * A path component that begins with . could be
222 * (1) "." and ends -- ignore and terminate.
223 * (2) "./" -- ignore them, eat slash and continue.
224 * (3) ".." and ends -- strip one and terminate.
225 * (4) "../" -- strip one, eat slash and continue.
231 } else if (is_dir_sep(src
[1])) {
234 while (is_dir_sep(*src
))
237 } else if (src
[1] == '.') {
242 } else if (is_dir_sep(src
[2])) {
245 while (is_dir_sep(*src
))
252 /* copy up to the next '/', and eat all '/' */
253 while ((c
= *src
++) != '\0' && !is_dir_sep(c
))
257 while (is_dir_sep(c
))
266 * dst0..dst is prefix portion, and dst[-1] is '/';
269 dst
--; /* go to trailing '/' */
272 /* Windows: dst[-1] cannot be backslash anymore */
273 while (dst0
< dst
&& dst
[-1] != '/')
281 * path = Canonical absolute path
282 * prefix_list = Colon-separated list of absolute paths
284 * Determines, for each path in prefix_list, whether the "prefix" really
285 * is an ancestor directory of path. Returns the length of the longest
286 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
287 * is an ancestor. (Note that this means 0 is returned if prefix_list is
288 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
289 * are not considered to be their own ancestors. path must be in a
290 * canonical form: empty components, or "." or ".." components are not
291 * allowed. prefix_list may be null, which is like "".
293 int longest_ancestor_length(const char *path
, const char *prefix_list
)
295 char buf
[PATH_MAX
+1];
296 const char *ceil
, *colon
;
297 int len
, max_len
= -1;
299 if (prefix_list
== NULL
|| !strcmp(path
, "/"))
302 for (colon
= ceil
= prefix_list
; *colon
; ceil
= colon
+1) {
303 for (colon
= ceil
; *colon
&& *colon
!= PATH_SEP
; colon
++);
305 if (len
== 0 || len
> PATH_MAX
|| !is_absolute_path(ceil
))
307 strlcpy(buf
, ceil
, len
+1);
308 if (normalize_path_copy(buf
, buf
) < 0)
311 if (len
> 0 && buf
[len
-1] == '/')
314 if (!strncmp(path
, buf
, len
) &&
324 /* strip arbitrary amount of directory separators at end of path */
325 static inline int chomp_trailing_dir_sep(const char *path
, int len
)
327 while (len
&& is_dir_sep(path
[len
- 1]))
333 * If path ends with suffix (complete path components), returns the
334 * part before suffix (sans trailing directory separators).
335 * Otherwise returns NULL.
337 char *strip_path_suffix(const char *path
, const char *suffix
)
339 int path_len
= strlen(path
), suffix_len
= strlen(suffix
);
345 if (is_dir_sep(path
[path_len
- 1])) {
346 if (!is_dir_sep(suffix
[suffix_len
- 1]))
348 path_len
= chomp_trailing_dir_sep(path
, path_len
);
349 suffix_len
= chomp_trailing_dir_sep(suffix
, suffix_len
);
351 else if (path
[--path_len
] != suffix
[--suffix_len
])
355 if (path_len
&& !is_dir_sep(path
[path_len
- 1]))
357 return xstrndup(path
, chomp_trailing_dir_sep(path
, path_len
));