libarchive: open_zipped() does not need to check extensions for e.g. gzip
[oweals/busybox.git] / debianutils / mktemp.c
index 5772ad9ee3f7a854eed3b24b4d0d761b40d3c08d..983d7a246595c225e2a3011ae794286926fba9e3 100644 (file)
  * Copyright (C) 2000 by Daniel Jacobowitz
  * Written by Daniel Jacobowitz <dan@debian.org>
  *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
+/* 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]
+ */
+
+//usage:#define mktemp_trivial_usage
+//usage:       "[-dt] [-p DIR] [TEMPLATE]"
+//usage:#define mktemp_full_usage "\n\n"
+//usage:       "Create a temporary file with name based on TEMPLATE and print its name.\n"
+//usage:       "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
+//usage:       "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
+//usage:     "\n       -d      Make directory, not file"
+//usage:     "\n       -q      Fail silently on errors"
+//usage:     "\n       -t      Prepend base directory name to TEMPLATE"
+//usage:     "\n       -p DIR  Use DIR as a base directory (implies -t)"
+//usage:     "\n       -u      Do not create anything; print a name"
+//usage:     "\n"
+//usage:     "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
+//usage:
+//usage:#define mktemp_example_usage
+//usage:       "$ mktemp /tmp/temp.XXXXXX\n"
+//usage:       "/tmp/temp.mWiLjM\n"
+//usage:       "$ ls -la /tmp/temp.mWiLjM\n"
+//usage:       "-rw-------    1 andersen andersen        0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
+
 #include "libbb.h"
 
 int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int mktemp_main(int argc, char **argv)
+int mktemp_main(int argc UNUSED_PARAM, char **argv)
 {
-       unsigned long flags = getopt32(argv, "dqt");
+       const char *path;
        char *chp;
+       unsigned opts;
+       enum {
+               OPT_d = 1 << 0,
+               OPT_q = 1 << 1,
+               OPT_t = 1 << 2,
+               OPT_p = 1 << 3,
+               OPT_u = 1 << 4,
+       };
 
-       if (optind + 1 != argc)
-               bb_show_usage();
+       path = getenv("TMPDIR");
+       if (!path || path[0] == '\0')
+               path = "/tmp";
 
-       chp = argv[optind];
+       opt_complementary = "?1"; /* 1 argument max */
+       opts = getopt32(argv, "dqtp:u", &path);
 
-       if (flags & 4) {
-               char *dir = getenv("TMPDIR");
-               if (dir && *dir != '\0')
-                       chp = concat_path_file(dir, chp);
-               else
-                       chp = concat_path_file("/tmp/", chp);
+       chp = argv[optind];
+       if (!chp) {
+               /* GNU coreutils 8.4:
+                * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
+                */
+               chp = xstrdup("tmp.XXXXXX");
+               opts |= OPT_t;
+       }
+#if 0
+       /* Don't allow directory separator in template */
+       if ((opts & OPT_t) && bb_basename(chp) != chp) {
+               errno = EINVAL;
+               goto error;
        }
+#endif
+       if (opts & (OPT_t|OPT_p))
+               chp = concat_path_file(path, chp);
 
-       if (flags & 1) {
+       if (opts & OPT_u) {
+               chp = mktemp(chp);
+               if (chp[0] == '\0')
+                       goto error;
+       } else if (opts & OPT_d) {
                if (mkdtemp(chp) == NULL)
-                       return EXIT_FAILURE;
+                       goto error;
        } else {
                if (mkstemp(chp) < 0)
-                       return EXIT_FAILURE;
+                       goto error;
        }
-
        puts(chp);
-
        return EXIT_SUCCESS;
+ error:
+       if (opts & OPT_q)
+               return EXIT_FAILURE;
+       /* don't use chp as it gets mangled in case of error */
+       bb_perror_nomsg_and_die();
 }