mktemp: don't double up on trailing slashes for -t paths

This is a minor cosmetic change; re-organize slightly to set tmpdir to
_PATH_TMP if we didn't otherwise have a tmpdir candidate, then check the
trailing char before appending another slash.

While we're here, remove some bogus whitespace and add a test case for
this change.

Obtained from:	https://github.com/apple-oss-distributions/shell_cmds
Sponsored by:	Klara, Inc.

(cherry picked from commit a6346c02f6)
This commit is contained in:
Kyle Evans committed 2022-11-12 23:37:36 -06:00
1 parent 3069738381
commit fa4ac142ef
2 files changed
+20 -2

No files matched your search

+6 -2
View File
@@ -126,6 +126,7 @@ main(int argc, char **argv)
if (tflag) {
const char *envtmp;
size_t len;
envtmp = NULL;
@@ -137,7 +138,10 @@ main(int argc, char **argv)
if (envtmp != NULL)
tmpdir = envtmp;
if (tmpdir == NULL)
asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP, prefix);
tmpdir = _PATH_TMP;
len = strlen(tmpdir);
if (len > 0 && tmpdir[len - 1] == '/')
asprintf(&name, "%s%s.XXXXXXXX", tmpdir, prefix);
else
asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix);
/* if this fails, the program is in big trouble already */
@@ -148,7 +152,7 @@ main(int argc, char **argv)
errx(1, "cannot generate template");
}
}
/* generate all requested files */
while (name != NULL || argc > 0) {
if (name == NULL) {
+14
View File
@@ -109,10 +109,24 @@ tmpdir_pflag_noarg_body()
atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
}
atf_test_case tmpdir_tflag_oneslash
tmpdir_tflag_oneslash_body()
{
tmpdir="$PWD"
# Provided a trailing slash, we shouldn't end up with two trailing
# slashes.
atf_check -o save:tmpname \
env TMPDIR="$tmpdir/" mktemp -t foo
atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
}
atf_init_test_cases()
{
atf_add_test_case tmpdir_env
atf_add_test_case tmpdir_pflag
atf_add_test_case tmpdir_pflag_dir
atf_add_test_case tmpdir_pflag_noarg
atf_add_test_case tmpdir_tflag_oneslash
}