1/* vi: set sw=4 ts=4: */
2/*
3 * Mini mktemp implementation for busybox
4 *
5 *
6 * Copyright (C) 2000 by Daniel Jacobowitz
7 * Written by Daniel Jacobowitz <dan@debian.org>
8 *
9 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
10 */
11
12#include "libbb.h"
13
14int mktemp_main(int argc, char **argv);
15int mktemp_main(int argc, char **argv)
16{
17	unsigned long flags = getopt32(argv, "dqt");
18	char *chp;
19
20	if (optind + 1 != argc)
21		bb_show_usage();
22
23	chp = argv[optind];
24
25	if (flags & 4) {
26		char *dir = getenv("TMPDIR");
27		if (dir && *dir != '\0')
28			chp = concat_path_file(dir, chp);
29		else
30			chp = concat_path_file("/tmp/", chp);
31	}
32
33	if (flags & 1) {
34		if (mkdtemp(chp) == NULL)
35			return EXIT_FAILURE;
36	} else {
37		if (mkstemp(chp) < 0)
38			return EXIT_FAILURE;
39	}
40
41	puts(chp);
42
43	return EXIT_SUCCESS;
44}
45