2024-03-01 13:50:40 -08:00
|
|
|
/* $OpenBSD: mktemp.c,v 1.26 2024/03/01 21:50:40 millert Exp $ */
|
1996-11-20 23:59:31 -08:00
|
|
|
|
|
|
|
/*
|
2013-03-12 08:07:12 -07:00
|
|
|
* Copyright (c) 1996, 1997, 2001-2003, 2013
|
2019-01-24 16:19:25 -08:00
|
|
|
* Todd C. Miller <millert@openbsd.org>
|
1996-11-20 23:59:31 -08:00
|
|
|
*
|
2003-06-02 18:52:39 -07:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
1996-11-20 23:59:31 -08:00
|
|
|
*
|
2003-06-17 14:56:23 -07:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1996-11-20 23:59:31 -08:00
|
|
|
*/
|
|
|
|
|
2013-03-14 08:44:15 -07:00
|
|
|
#include <err.h>
|
2001-10-01 10:08:30 -07:00
|
|
|
#include <paths.h>
|
2013-03-14 08:44:15 -07:00
|
|
|
#include <stdarg.h>
|
1996-11-20 23:59:31 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2002-02-16 13:27:05 -08:00
|
|
|
__dead void usage(void);
|
2013-03-14 08:44:15 -07:00
|
|
|
__dead void fatal(const char *, ...) __attribute__((__format__(printf, 1, 2)));
|
|
|
|
__dead void fatalx(const char *, ...) __attribute__((__format__(printf, 1, 2)));
|
|
|
|
|
|
|
|
static int quiet;
|
1996-11-20 23:59:31 -08:00
|
|
|
|
|
|
|
int
|
2003-06-10 15:20:44 -07:00
|
|
|
main(int argc, char *argv[])
|
1996-11-20 23:59:31 -08:00
|
|
|
{
|
2013-03-14 08:44:15 -07:00
|
|
|
int ch, fd, uflag = 0, tflag = 0, makedir = 0;
|
2024-03-01 13:50:40 -08:00
|
|
|
char *base, *cp, *template, *tempfile, *prefix = _PATH_TMP;
|
|
|
|
size_t len, suffixlen = 0;
|
1996-11-20 23:59:31 -08:00
|
|
|
|
2015-10-08 18:37:06 -07:00
|
|
|
if (pledge("stdio rpath wpath cpath", NULL) == -1)
|
|
|
|
err(1, "pledge");
|
2015-10-06 23:43:15 -07:00
|
|
|
|
2001-10-01 10:08:30 -07:00
|
|
|
while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
|
|
|
|
switch(ch) {
|
1997-06-17 08:34:27 -07:00
|
|
|
case 'd':
|
|
|
|
makedir = 1;
|
1996-11-20 23:59:31 -08:00
|
|
|
break;
|
2001-10-01 10:08:30 -07:00
|
|
|
case 'p':
|
|
|
|
prefix = optarg;
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
1996-11-20 23:59:31 -08:00
|
|
|
case 'q':
|
2001-10-10 17:05:55 -07:00
|
|
|
quiet = 1;
|
1996-11-20 23:59:31 -08:00
|
|
|
break;
|
2001-10-01 10:08:30 -07:00
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
1997-06-17 08:34:27 -07:00
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
1996-11-20 23:59:31 -08:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2001-10-10 17:05:55 -07:00
|
|
|
/* If no template specified use a default one (implies -t mode) */
|
|
|
|
switch (argc - optind) {
|
|
|
|
case 1:
|
|
|
|
template = argv[optind];
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
template = "tmp.XXXXXXXXXX";
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
1996-11-20 23:59:31 -08:00
|
|
|
usage();
|
2001-10-10 17:05:55 -07:00
|
|
|
}
|
1996-11-20 23:59:31 -08:00
|
|
|
|
2024-03-01 13:50:40 -08:00
|
|
|
base = strrchr(template, '/');
|
|
|
|
if (base != NULL)
|
|
|
|
base++;
|
|
|
|
else
|
|
|
|
base = template;
|
|
|
|
len = strlen(base);
|
|
|
|
if (len > 0 && base[len - 1] != 'X') {
|
|
|
|
/* Check for suffix, e.g. /tmp/XXXXXX.foo in last component. */
|
|
|
|
for (suffixlen = 0; suffixlen < len; suffixlen++) {
|
|
|
|
if (base[len - suffixlen - 1] == 'X')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len - suffixlen < 6 ||
|
|
|
|
strncmp(&base[len - suffixlen - 6], "XXXXXX", 6)) {
|
2013-08-06 14:56:51 -07:00
|
|
|
fatalx("insufficient number of Xs in template `%s'",
|
|
|
|
template);
|
|
|
|
}
|
2001-10-01 10:08:30 -07:00
|
|
|
if (tflag) {
|
2024-03-01 13:50:40 -08:00
|
|
|
if (base != template) {
|
2013-03-14 08:44:15 -07:00
|
|
|
fatalx("template must not contain directory "
|
|
|
|
"separators in -t mode");
|
2001-10-01 10:08:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cp = getenv("TMPDIR");
|
|
|
|
if (cp != NULL && *cp != '\0')
|
|
|
|
prefix = cp;
|
2013-03-12 08:07:12 -07:00
|
|
|
len = strlen(prefix);
|
|
|
|
while (len != 0 && prefix[len - 1] == '/')
|
|
|
|
len--;
|
2001-10-01 10:08:30 -07:00
|
|
|
|
2019-06-27 11:03:36 -07:00
|
|
|
if (asprintf(&tempfile, "%.*s/%s", (int)len, prefix, template) == -1)
|
2003-04-25 13:02:02 -07:00
|
|
|
tempfile = NULL;
|
2013-08-06 14:56:51 -07:00
|
|
|
} else
|
2003-04-07 12:25:43 -07:00
|
|
|
tempfile = strdup(template);
|
2013-08-06 14:56:51 -07:00
|
|
|
|
2013-03-14 08:44:15 -07:00
|
|
|
if (tempfile == NULL)
|
|
|
|
fatalx("cannot allocate memory");
|
1996-11-20 23:59:31 -08:00
|
|
|
|
1997-06-17 08:34:27 -07:00
|
|
|
if (makedir) {
|
2024-03-01 13:50:40 -08:00
|
|
|
if (mkdtemps(tempfile, suffixlen) == NULL)
|
2013-03-14 08:44:15 -07:00
|
|
|
fatal("cannot make temp dir %s", tempfile);
|
1997-06-17 08:34:27 -07:00
|
|
|
if (uflag)
|
2001-10-10 17:05:55 -07:00
|
|
|
(void)rmdir(tempfile);
|
1997-06-17 08:34:27 -07:00
|
|
|
} else {
|
2024-03-01 13:50:40 -08:00
|
|
|
if ((fd = mkstemps(tempfile, suffixlen)) == -1)
|
2013-03-14 08:44:15 -07:00
|
|
|
fatal("cannot make temp file %s", tempfile);
|
2001-10-10 17:05:55 -07:00
|
|
|
(void)close(fd);
|
1997-06-17 08:34:27 -07:00
|
|
|
if (uflag)
|
2001-10-10 17:05:55 -07:00
|
|
|
(void)unlink(tempfile);
|
1997-06-17 08:34:27 -07:00
|
|
|
}
|
1996-11-20 23:59:31 -08:00
|
|
|
|
2001-10-10 17:05:55 -07:00
|
|
|
(void)puts(tempfile);
|
|
|
|
free(tempfile);
|
1996-11-20 23:59:31 -08:00
|
|
|
|
2024-03-01 13:50:40 -08:00
|
|
|
return EXIT_SUCCESS;
|
2013-03-14 08:44:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
__dead void
|
|
|
|
fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (!quiet) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vwarn(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
__dead void
|
|
|
|
fatalx(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (!quiet) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vwarnx(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
1996-11-20 23:59:31 -08:00
|
|
|
}
|
2001-10-10 17:05:55 -07:00
|
|
|
|
|
|
|
__dead void
|
2003-06-10 15:20:44 -07:00
|
|
|
usage(void)
|
2001-10-10 17:05:55 -07:00
|
|
|
{
|
|
|
|
extern char *__progname;
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
2008-05-26 02:22:30 -07:00
|
|
|
"usage: %s [-dqtu] [-p directory] [template]\n", __progname);
|
2013-03-14 08:44:15 -07:00
|
|
|
exit(EXIT_FAILURE);
|
2001-10-10 17:05:55 -07:00
|
|
|
}
|