hush: fix multiple redirections of the same fd (bug 227)
[oweals/busybox.git] / debianutils / mktemp.c
index ce5bda515fd14f7348fa77870176eaef3c7ec8b4..0dcb1e8267c6a3b1b874a025678afb55715907cc 100644 (file)
@@ -9,33 +9,53 @@
  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  */
 
-#include "busybox.h"
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-int mktemp_main(int argc, char **argv);
-int mktemp_main(int argc, char **argv)
+/* Coreutils 6.12 man page says:
+ *        mktemp [OPTION]... [TEMPLATE]
+ * Create a temporary file or directory, safely, and print its name. If
+ * TEMPLATE is not specified, use tmp.XXXXXXXXXX.
+ * -d, --directory
+ *        create a directory, not a file
+ * -q, --quiet
+ *        suppress diagnostics about file/dir-creation failure
+ * -u, --dry-run
+ *        do not create anything; merely print a name (unsafe)
+ * --tmpdir[=DIR]
+ *        interpret TEMPLATE relative to DIR. If DIR is not specified,
+ *        use  $TMPDIR if set, else /tmp.  With this option, TEMPLATE must
+ *        not be an absolute name. Unlike with -t, TEMPLATE may contain
+ *        slashes, but even here, mktemp still creates only the final com-
+ *        ponent.
+ * -p DIR use DIR as a prefix; implies -t [deprecated]
+ * -t     interpret TEMPLATE as a single file name component, relative  to
+ *        a  directory:  $TMPDIR, if set; else the directory specified via
+ *        -p; else /tmp [deprecated]
+ */
+
+
+#include "libbb.h"
+
+int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int mktemp_main(int argc UNUSED_PARAM, char **argv)
 {
-       unsigned long flags = getopt32(argc, argv, "dqt");
+       const char *path;
        char *chp;
+       unsigned opt;
 
-       if (optind + 1 != argc)
-               bb_show_usage();
-
-       chp = argv[optind];
+       opt_complementary = "?1"; /* 1 argument max */
+       opt = getopt32(argv, "dqtp:", &path);
+       chp = argv[optind] ? argv[optind] : xstrdup("tmp.XXXXXX");
 
-       if (flags & 4) {
-               char *dir = getenv("TMPDIR");
+       if (opt & (4|8)) { /* -t and/or -p */
+               const char *dir = getenv("TMPDIR");
                if (dir && *dir != '\0')
-                       chp = concat_path_file(dir, chp);
-               else
-                       chp = concat_path_file("/tmp/", chp);
+                       path = dir;
+               else if (!(opt & 8)) /* no -p */
+                       path = "/tmp/";
+               /* else path comes from -p DIR */
+               chp = concat_path_file(path, chp);
        }
 
-       if (flags & 1) {
+       if (opt & 1) { /* -d */
                if (mkdtemp(chp) == NULL)
                        return EXIT_FAILURE;
        } else {