unzip: -d should create the dir
[oweals/busybox.git] / coreutils / mktemp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini mktemp implementation for busybox
4  *
5  * Copyright (C) 2000 by Daniel Jacobowitz
6  * Written by Daniel Jacobowitz <dan@debian.org>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 /* Coreutils 6.12 man page says:
11  *        mktemp [OPTION]... [TEMPLATE]
12  * Create a temporary file or directory, safely, and print its name. If
13  * TEMPLATE is not specified, use tmp.XXXXXXXXXX.
14  * -d, --directory
15  *        create a directory, not a file
16  * -q, --quiet
17  *        suppress diagnostics about file/dir-creation failure
18  * -u, --dry-run
19  *        do not create anything; merely print a name (unsafe)
20  * --tmpdir[=DIR]
21  *        interpret TEMPLATE relative to DIR. If DIR is not specified,
22  *        use  $TMPDIR if set, else /tmp.  With this option, TEMPLATE must
23  *        not be an absolute name. Unlike with -t, TEMPLATE may contain
24  *        slashes, but even here, mktemp still creates only the final com-
25  *        ponent.
26  * -p DIR use DIR as a prefix; implies -t [deprecated]
27  * -t     interpret TEMPLATE as a single file name component, relative  to
28  *        a  directory:  $TMPDIR, if set; else the directory specified via
29  *        -p; else /tmp [deprecated]
30  */
31 //config:config MKTEMP
32 //config:       bool "mktemp (4.2 kb)"
33 //config:       default y
34 //config:       help
35 //config:       mktemp is used to create unique temporary files
36
37 //applet:IF_MKTEMP(APPLET_NOEXEC(mktemp, mktemp, BB_DIR_BIN, BB_SUID_DROP, mktemp))
38
39 //kbuild:lib-$(CONFIG_MKTEMP) += mktemp.o
40
41 //usage:#define mktemp_trivial_usage
42 //usage:       "[-dt] [-p DIR] [TEMPLATE]"
43 //usage:#define mktemp_full_usage "\n\n"
44 //usage:       "Create a temporary file with name based on TEMPLATE and print its name.\n"
45 //usage:       "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
46 //usage:       "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
47 //usage:     "\n        -d      Make directory, not file"
48 //usage:     "\n        -q      Fail silently on errors"
49 //usage:     "\n        -t      Prepend base directory name to TEMPLATE"
50 //usage:     "\n        -p DIR  Use DIR as a base directory (implies -t)"
51 //usage:     "\n        -u      Do not create anything; print a name"
52 //usage:     "\n"
53 //usage:     "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
54 //usage:
55 //usage:#define mktemp_example_usage
56 //usage:       "$ mktemp /tmp/temp.XXXXXX\n"
57 //usage:       "/tmp/temp.mWiLjM\n"
58 //usage:       "$ ls -la /tmp/temp.mWiLjM\n"
59 //usage:       "-rw-------    1 andersen andersen        0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
60
61 #include "libbb.h"
62
63 int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64 int mktemp_main(int argc UNUSED_PARAM, char **argv)
65 {
66         const char *path;
67         char *chp;
68         unsigned opts;
69         enum {
70                 OPT_d = 1 << 0,
71                 OPT_q = 1 << 1,
72                 OPT_t = 1 << 2,
73                 OPT_p = 1 << 3,
74                 OPT_u = 1 << 4,
75         };
76
77         path = getenv("TMPDIR");
78         if (!path || path[0] == '\0')
79                 path = "/tmp";
80
81         opts = getopt32(argv, "^" "dqtp:u" "\0" "?1"/*1 arg max*/, &path);
82
83         chp = argv[optind];
84         if (!chp) {
85                 /* GNU coreutils 8.4:
86                  * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
87                  */
88                 chp = xstrdup("tmp.XXXXXX");
89                 opts |= OPT_t;
90         }
91 #if 0
92         /* Don't allow directory separator in template */
93         if ((opts & OPT_t) && bb_basename(chp) != chp) {
94                 errno = EINVAL;
95                 goto error;
96         }
97 #endif
98         if (opts & (OPT_t|OPT_p))
99                 chp = concat_path_file(path, chp);
100
101         if (opts & OPT_u) {
102                 chp = mktemp(chp);
103                 if (chp[0] == '\0')
104                         goto error;
105         } else if (opts & OPT_d) {
106                 if (mkdtemp(chp) == NULL)
107                         goto error;
108         } else {
109                 if (mkstemp(chp) < 0)
110                         goto error;
111         }
112         puts(chp);
113         return EXIT_SUCCESS;
114  error:
115         if (opts & OPT_q)
116                 return EXIT_FAILURE;
117         /* don't use chp as it gets mangled in case of error */
118         bb_perror_nomsg_and_die();
119 }