libarchive: open_zipped() does not need to check extensions for e.g. gzip
[oweals/busybox.git] / debianutils / mktemp.c
index 007cb1c5b6e4d8eb2cc1bd632d0f19b5dd11d41b..983d7a246595c225e2a3011ae794286926fba9e3 100644 (file)
 //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" - we ignore this opt
+//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:
@@ -63,15 +64,15 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
                OPT_q = 1 << 1,
                OPT_t = 1 << 2,
                OPT_p = 1 << 3,
+               OPT_u = 1 << 4,
        };
 
        path = getenv("TMPDIR");
        if (!path || path[0] == '\0')
                path = "/tmp";
 
-       /* -q is ignored */
        opt_complementary = "?1"; /* 1 argument max */
-       opts = getopt32(argv, "dqtp:", &path);
+       opts = getopt32(argv, "dqtp:u", &path);
 
        chp = argv[optind];
        if (!chp) {
@@ -81,18 +82,32 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
                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 (opts & OPT_d) {
+       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();
 }