#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
static void
usage(void)
{
(void) fprintf(stderr,
gettext("Usage: mktemp [-dqtu] [-p prefix_dir] [template]\n"));
exit(1);
}
int
main(int argc, char **argv)
{
int opt;
char *prefix = NULL;
boolean_t dounlink = B_FALSE;
boolean_t domkdir = B_FALSE;
boolean_t quiet = B_FALSE;
boolean_t usetmpdir = B_FALSE;
char template[] = "tmp.XXXXXX";
char *tmpl;
(void) setlocale(LC_ALL, "");
#ifndef TEXT_DOMAIN
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
opterr = 0;
while ((opt = getopt(argc, argv, "dqtup:")) != EOF) {
switch (opt) {
case 'd':
domkdir = B_TRUE;
break;
case 'q':
quiet = B_TRUE;
break;
case 'p':
prefix = optarg;
case 't':
usetmpdir = B_TRUE;
break;
case 'u':
dounlink = B_TRUE;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
switch (argc) {
case 0:
tmpl = template;
usetmpdir = B_TRUE;
break;
case 1:
tmpl = argv[0];
break;
default:
usage();
}
if (usetmpdir) {
char *tmp = getenv("TMPDIR");
size_t len;
if (strchr(tmpl, '/') != NULL) {
(void) fprintf(stderr,
gettext("mktemp: template argument specified "
"with -t/-p option must not contain '/'"
"\n"));
return (1);
}
if (tmp != NULL)
prefix = tmp;
else if (prefix == NULL)
prefix = "/tmp";
len = snprintf(NULL, 0, "%s/%s", prefix, tmpl) + 1;
tmp = malloc(len);
if (tmp == NULL) {
perror("malloc");
return (1);
}
(void) snprintf(tmp, len, "%s/%s", prefix, tmpl);
tmpl = tmp;
}
if (domkdir) {
if (mkdtemp(tmpl) == NULL) {
if (!quiet) {
(void) fprintf(stderr,
gettext("mktemp: failed to create "
"directory: %s\n"), tmpl);
}
return (1);
}
if (dounlink)
(void) rmdir(tmpl);
} else {
if (mkstemp(tmpl) < 0) {
if (!quiet) {
(void) fprintf(stderr,
gettext("mktemp: failed to create file: "
"%s\n"), tmpl);
}
return (1);
}
if (dounlink)
(void) unlink(tmpl);
}
(void) puts(tmpl);
return (0);
}