X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=path.c;h=b85f087f4390f1235a6b9e9545b3f6dd8457fdd5;hb=3c3852e33b3e40ad64885d845cb92a52c7b15884;hp=d217ef0b7f04d7e021770cb50a7c58c14bc7920d;hpb=98e031f0bb6e857c684e6db24d03d22cfc1a532a;p=git.git diff --git a/path.c b/path.c index d217ef0b..b85f087f 100644 --- a/path.c +++ b/path.c @@ -58,3 +58,34 @@ char *git_path(const char *fmt, ...) return bad_path; return cleanup_path(pathname); } + + +/* git_mkstemp() - create tmp file honoring TMPDIR variable */ +int git_mkstemp(char *path, size_t len, const char *template) +{ + char *env, *pch = path; + + if ((env = getenv("TMPDIR")) == NULL) { + strcpy(pch, "/tmp/"); + len -= 5; + pch += 5; + } else { + size_t n = snprintf(pch, len, "%s/", env); + + len -= n; + pch += n; + } + + safe_strncpy(pch, template, len); + + return mkstemp(path); +} + + +char *safe_strncpy(char *dest, const char *src, size_t n) +{ + strncpy(dest, src, n); + dest[n - 1] = '\0'; + + return dest; +}