accidentally applied wrong (old) patch, fixing up...
[oweals/busybox.git] / debianutils / mktemp.c
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 "busybox.h"
13 #include <stdio.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18
19 int mktemp_main(int argc, char **argv)
20 {
21         unsigned long flags = getopt32(argc, argv, "dqt");
22         char *chp;
23
24         if (optind + 1 != argc)
25                 bb_show_usage();
26
27         chp = argv[optind];
28
29         if (flags & 4) {
30                 char *dir = getenv("TMPDIR");
31                 if (dir && *dir != '\0')
32                         chp = concat_path_file(dir, chp);
33                 else
34                         chp = concat_path_file("/tmp/", chp);
35         }
36
37         if (flags & 1) {
38                 if (mkdtemp(chp) == NULL)
39                         return EXIT_FAILURE;
40         } else {
41                 if (mkstemp(chp) < 0)
42                         return EXIT_FAILURE;
43         }
44
45         puts(chp);
46
47         return EXIT_SUCCESS;
48 }