Only use /tmp if template is a file and not a path

This commit is contained in:
sin 2014-07-04 15:55:31 +01:00
parent 8eea7f74e9
commit f5ac08cc04
1 changed files with 22 additions and 10 deletions

View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <libgen.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -20,8 +21,8 @@ main(int argc, char *argv[])
{ {
char *template = "tmp.XXXXXXXXXX"; char *template = "tmp.XXXXXXXXXX";
char *tmpdir = "/tmp", *p; char *tmpdir = "/tmp", *p;
char tmppath[PATH_MAX]; char path[PATH_MAX], tmp[PATH_MAX];
int fd, r; int fd;
ARGBEGIN { ARGBEGIN {
case 'd': case 'd':
@ -42,24 +43,35 @@ main(int argc, char *argv[])
if ((p = getenv("TMPDIR"))) if ((p = getenv("TMPDIR")))
tmpdir = p; tmpdir = p;
r = snprintf(tmppath, sizeof(tmppath), if (strlcpy(tmp, template, sizeof(tmp)) >= sizeof(tmp))
"%s/%s", tmpdir, template);
if (r >= sizeof(tmppath) || r < 0)
eprintf("path too long\n"); eprintf("path too long\n");
p = dirname(tmp);
if (p[0] != '.') {
if (strlcpy(path, template, sizeof(path)) >= sizeof(path))
eprintf("path too long\n");
} else {
if (strlcpy(path, tmpdir, sizeof(path)) >= sizeof(path))
eprintf("path too long\n");
if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
eprintf("path too long\n");
if (strlcat(path, template, sizeof(path)) >= sizeof(path))
eprintf("path too long\n");
}
if (dflag) { if (dflag) {
if (!mkdtemp(tmppath)) { if (!mkdtemp(path)) {
if (!qflag) if (!qflag)
eprintf("mkdtemp %s:", tmppath); eprintf("mkdtemp %s:", path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } else {
if ((fd = mkstemp(tmppath)) < 0) { if ((fd = mkstemp(path)) < 0) {
if (!qflag) if (!qflag)
eprintf("mkstemp %s:", tmppath); eprintf("mkstemp %s:", path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
close(fd); close(fd);
} }
puts(tmppath); puts(path);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }