mktemp: add support for -u

zlib-1.2.6 Makefile uses "mktemp -u".

function                                             old     new   delta
___path_search                                         -     266    +266
mktemp_main                                          165     250     +85
tempnam                                                -      79     +79
packed_usage                                       29189   29176     -13
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 1/1 up/down: 430/-13)           Total: 417 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2012-04-17 16:00:20 +02:00
parent 176bc34475
commit 75e1e7b3d5

View File

@ -41,6 +41,7 @@
////usage: "\n -q Fail silently on errors" - we ignore this opt ////usage: "\n -q Fail silently on errors" - we ignore this opt
//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" //usage: "\n"
//usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp" //usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
//usage: //usage:
@ -63,6 +64,7 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
OPT_q = 1 << 1, OPT_q = 1 << 1,
OPT_t = 1 << 2, OPT_t = 1 << 2,
OPT_p = 1 << 3, OPT_p = 1 << 3,
OPT_u = 1 << 4,
}; };
path = getenv("TMPDIR"); path = getenv("TMPDIR");
@ -71,7 +73,7 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
/* -q is ignored */ /* -q is ignored */
opt_complementary = "?1"; /* 1 argument max */ opt_complementary = "?1"; /* 1 argument max */
opts = getopt32(argv, "dqtp:", &path); opts = getopt32(argv, "dqtp:u", &path);
chp = argv[optind]; chp = argv[optind];
if (!chp) { if (!chp) {
@ -81,6 +83,22 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
chp = xstrdup("tmp.XXXXXX"); chp = xstrdup("tmp.XXXXXX");
opts |= OPT_t; opts |= OPT_t;
} }
if (opts & OPT_u) {
/* Remove (up to) 6 X's */
unsigned len = strlen(chp);
int cnt = len > 6 ? 6 : len;
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;
}
if (opts & (OPT_t|OPT_p)) if (opts & (OPT_t|OPT_p))
chp = concat_path_file(path, chp); chp = concat_path_file(path, chp);
@ -91,8 +109,7 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
if (mkstemp(chp) < 0) if (mkstemp(chp) < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ret:
puts(chp); puts(chp);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }