mktemp: fix mktemp -u temp.XXXXXX returning garbage when TMPDIR is set

Use mktemp instead of tempnam for compatibility with real mktemp.
Don't let mktemp fail silently, print some simple error messages.
Don't ignore -q.

Signed-off-by: Tito Ragusa <farmatito@tiscali.it>
Signed-off-by: Ron Yorston <rmy@tigress.co.uk>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2012-10-08 11:47:22 +02:00 committed by Denys Vlasenko
parent 8dc6d1a813
commit eab343e7e1

View File

@ -38,7 +38,7 @@
//usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n" //usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
//usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n" //usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
//usage: "\n -d Make directory, not file" //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 -t Prepend base directory name to TEMPLATE"
//usage: "\n -p DIR Use DIR as a base directory (implies -t)" //usage: "\n -p DIR Use DIR as a base directory (implies -t)"
//usage: "\n -u Do not create anything; print a name" //usage: "\n -u Do not create anything; print a name"
@ -71,7 +71,6 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
if (!path || path[0] == '\0') if (!path || path[0] == '\0')
path = "/tmp"; path = "/tmp";
/* -q is ignored */
opt_complementary = "?1"; /* 1 argument max */ opt_complementary = "?1"; /* 1 argument max */
opts = getopt32(argv, "dqtp:u", &path); opts = getopt32(argv, "dqtp:u", &path);
@ -83,33 +82,32 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
chp = xstrdup("tmp.XXXXXX"); chp = xstrdup("tmp.XXXXXX");
opts |= OPT_t; opts |= OPT_t;
} }
#if 0
if (opts & OPT_u) { /* Don't allow directory separator in template */
/* Remove (up to) 6 X's */ if ((opts & OPT_t) && bb_basename(chp) != chp) {
unsigned len = strlen(chp); errno = EINVAL;
int cnt = len > 6 ? 6 : len; goto error;
while (--cnt >= 0 && chp[--len] == 'X')
chp[len] = '\0';
chp = tempnam(opts & (OPT_t|OPT_p) ? path : "./", chp);
if (!chp)
return EXIT_FAILURE;
if (!(opts & (OPT_t|OPT_p)))
chp += 2;
goto ret;
} }
#endif
if (opts & (OPT_t|OPT_p)) if (opts & (OPT_t|OPT_p))
chp = concat_path_file(path, chp); 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) if (mkdtemp(chp) == NULL)
return EXIT_FAILURE; goto error;
} else { } else {
if (mkstemp(chp) < 0) if (mkstemp(chp) < 0)
return EXIT_FAILURE; goto error;
} }
ret:
puts(chp); puts(chp);
return EXIT_SUCCESS; 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();
} }