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.git", base, name), O_RDONLY);
11 * which is what it's designed for.
15 static char pathname[PATH_MAX];
16 static char bad_path[] = "/bad-path/";
18 static char *cleanup_path(char *path)
21 if (!memcmp(path, "./", 2)) {
29 char *mkpath(const char *fmt, ...)
35 len = vsnprintf(pathname, PATH_MAX, fmt, args);
39 return cleanup_path(pathname);
42 char *git_path(const char *fmt, ...)
48 git_dir = getenv(GIT_DIR_ENVIRONMENT);
49 if (!git_dir) git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
50 len = strlen(git_dir);
51 if (len > PATH_MAX-100)
53 memcpy(pathname, git_dir, len);
54 if (len && git_dir[len-1] != '/')
55 pathname[len++] = '/';
57 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
61 return cleanup_path(pathname);
65 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
66 int git_mkstemp(char *path, size_t len, const char *template)
68 char *env, *pch = path;
70 if ((env = getenv("TMPDIR")) == NULL) {
75 size_t n = snprintf(pch, len, "%s/", env);
81 safe_strncpy(pch, template, len);
87 char *safe_strncpy(char *dest, const char *src, size_t n)
89 strncpy(dest, src, n);