* lib/tmpdir.c (create_tmp_dir): New function definition. * lib/tmpdir.h (create_tmp_dir): New function declaration. --- lib/tmpdir.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/tmpdir.h | 20 +++++++++ 2 files changed, 139 insertions(+)
diff --git a/lib/tmpdir.c b/lib/tmpdir.c index 28ff99f58..b6cd3e526 100644 --- a/lib/tmpdir.c +++ b/lib/tmpdir.c @@ -163,3 +163,122 @@ path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, sprintf (tmpl + dlen, &"/%.*sXXXXXX"[!add_slash], (int) plen, pfx); return 0; } + + +/* Create a unique temporary directory and return its name. + + If BASEDIR is non-null the directory will be a sub-directory of BASEDIR. + Otherwise, if TRY_TMPDIR is non-null the directory will be created in the + first of $TMPDIR, P_tmpdir, /tmp that exists. If none of those directories + exist, the function will fail, and ENOENT will be set in errno. + + TRY_TMPDIR is ignored if BASEDIR is non-null. + + If PFX is non-null, the first 5 bytes of it will be used as a prefix for + the directory name. On failure, NULL will be returned and errno set + accordingly. + + On success, a temporary directory will be created (using mkdtemp from + stdlib.h), and its name returned. + + The caller is responsible for freeing the return value (and removing the + directory if appropriate). */ +char * +create_tmp_dir (const char *basedir, const char *pfx, bool try_tmpdir) +{ + char *dir = NULL; + bool add_slash; + size_t dlen = 0; + size_t plen = 0; + + if (!pfx || !pfx[0]) + { + pfx = "file"; + plen = 4; + } + else + { + plen = strlen (pfx); + if (plen > 5) + plen = 5; + } + + if (basedir != NULL) + { + dir = strdup (basedir); + if (dir == NULL) + { + return NULL; + } + } + + if (dir == NULL) + { + if (try_tmpdir) + { + char *d = __libc_secure_getenv ("TMPDIR"); + if (d != NULL && direxists (d)) + dir = strdup (d); + else if (dir != NULL && direxists (dir)) + /* nothing */ ; + else + dir = NULL; + } + } + + if (dir == NULL) + { +#if defined _WIN32 && ! defined __CYGWIN__ + char dirbuf[PATH_MAX]; + DWORD retval; + + /* Find Windows temporary file directory. + We try this before P_tmpdir because Windows defines P_tmpdir to "\\" + and will therefore try to put all temporary files in the root + directory (unless $TMPDIR is set). */ + retval = GetTempPath (PATH_MAX, dirbuf); + if (retval > 0 && retval < PATH_MAX && direxists (dirbuf)) + dir = strdup (dirbuf); + else +#endif + if (direxists (P_tmpdir)) + dir = strdup (P_tmpdir); + else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp")) + dir = strdup ("/tmp"); + else + { + __set_errno (ENOENT); + return NULL; + } + } + + dlen = strlen (dir); +#ifdef __VMS + add_slash = 0; +#else + add_slash = dlen != 0 && !ISSLASH (dir[dlen - 1]); +#endif + + { + char *d = dir; + /* Make room for "${dir}/${pfx}XXXXXX\0" */ + dir = realloc (d, dlen + add_slash + plen + 6 + 1); + if (dir == NULL) + { + int e = errno; + free (d); + __set_errno (e); + return NULL; + } + } + + if (0 > sprintf (dir + dlen, &"/%.*sXXXXXX"[!add_slash], (int) plen, pfx)) + { + int e = errno; + free (dir); + __set_errno (e); + return NULL; + } + + return mkdtemp (dir); +} diff --git a/lib/tmpdir.h b/lib/tmpdir.h index 4d694a3d9..ea71ef635 100644 --- a/lib/tmpdir.h +++ b/lib/tmpdir.h @@ -24,3 +24,23 @@ doesn't exist, none of the searched dirs exists, or there's not enough space in TMPL. */ extern int path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, bool try_tmpdir); + +/* Create a unique temporary directory and return its name. + + If BASEDIR is non-null the directory will be a sub-directory of BASEDIR. + Otherwise, if TRY_TMPDIR is non-null the directory will be created in the + first of $TMPDIR, P_tmpdir, /tmp that exists. If none of those directories + exist, the function will fail, and ENOENT will be set in errno. + + TRY_TMPDIR is ignored if BASEDIR is non-null. + + If PFX is non-null, the first 5 bytes of it will be used as a prefix for + the directory name. On failure, NULL will be returned and errno set + accordingly. + + On success, a temporary directory will be created (using mkdtemp from + stdlib.h), and its name returned. + + The caller is responsible for freeing the return value (and removing the + directory if appropriate). */ +char *create_tmp_dir (const char *basedir, const char *pfx, bool try_tmpdir); -- 2.20.1