2013-11-12 06:52:28 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-07-04 10:55:31 -04:00
|
|
|
#include <libgen.h>
|
2013-11-12 06:52:28 -05:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-11-13 07:10:49 -05:00
|
|
|
eprintf("usage: %s [-dq] [template]\n", argv0);
|
2013-11-12 06:52:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dflag = 0;
|
2013-11-13 07:10:49 -05:00
|
|
|
static int qflag = 0;
|
2013-11-12 06:52:28 -05:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *template = "tmp.XXXXXXXXXX";
|
2013-11-14 14:46:21 -05:00
|
|
|
char *tmpdir = "/tmp", *p;
|
2014-07-04 10:55:31 -04:00
|
|
|
char path[PATH_MAX], tmp[PATH_MAX];
|
|
|
|
int fd;
|
2013-11-12 06:52:28 -05:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
2013-11-13 07:10:49 -05:00
|
|
|
case 'q':
|
|
|
|
qflag = 1;
|
|
|
|
break;
|
2013-11-12 06:52:28 -05:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
else if (argc == 1)
|
|
|
|
template = argv[0];
|
|
|
|
|
2013-11-14 14:46:21 -05:00
|
|
|
if ((p = getenv("TMPDIR")))
|
|
|
|
tmpdir = p;
|
|
|
|
|
2014-07-04 10:55:31 -04:00
|
|
|
if (strlcpy(tmp, template, sizeof(tmp)) >= sizeof(tmp))
|
2014-01-30 08:54:16 -05:00
|
|
|
eprintf("path too long\n");
|
2014-07-04 10:55:31 -04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2013-11-12 06:52:28 -05:00
|
|
|
if (dflag) {
|
2014-07-04 10:55:31 -04:00
|
|
|
if (!mkdtemp(path)) {
|
2013-11-13 07:10:49 -05:00
|
|
|
if (!qflag)
|
2014-07-04 10:55:31 -04:00
|
|
|
eprintf("mkdtemp %s:", path);
|
2014-10-02 18:46:04 -04:00
|
|
|
exit(1);
|
2013-11-13 07:10:49 -05:00
|
|
|
}
|
2013-11-12 06:52:28 -05:00
|
|
|
} else {
|
2014-07-04 10:55:31 -04:00
|
|
|
if ((fd = mkstemp(path)) < 0) {
|
2013-11-13 07:10:49 -05:00
|
|
|
if (!qflag)
|
2014-07-04 10:55:31 -04:00
|
|
|
eprintf("mkstemp %s:", path);
|
2014-10-02 18:46:04 -04:00
|
|
|
exit(1);
|
2013-11-13 07:10:49 -05:00
|
|
|
}
|
2013-11-12 06:52:28 -05:00
|
|
|
close(fd);
|
|
|
|
}
|
2014-07-04 10:55:31 -04:00
|
|
|
puts(path);
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2013-11-12 06:52:28 -05:00
|
|
|
}
|