ce5bda515fd14f7348fa77870176eaef3c7ec8b4
[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 int mktemp_main(int argc, char **argv)
21 {
22         unsigned long flags = getopt32(argc, argv, "dqt");
23         char *chp;
24
25         if (optind + 1 != argc)
26                 bb_show_usage();
27
28         chp = argv[optind];
29
30         if (flags & 4) {
31                 char *dir = getenv("TMPDIR");
32                 if (dir && *dir != '\0')
33                         chp = concat_path_file(dir, chp);
34                 else
35                         chp = concat_path_file("/tmp/", chp);
36         }
37
38         if (flags & 1) {
39                 if (mkdtemp(chp) == NULL)
40                         return EXIT_FAILURE;
41         } else {
42                 if (mkstemp(chp) < 0)
43                         return EXIT_FAILURE;
44         }
45
46         puts(chp);
47
48         return EXIT_SUCCESS;
49 }