diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index e902a33ca417..b4050937dfc3 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1,1144 +1,1146 @@ # $FreeBSD$ # # Please see the file src/etc/mtree/README before making changes to this file. # /set type=dir uname=root gname=wheel mode=0755 tags=package=tests . bin cat .. chflags .. chmod .. cp .. date .. dd .. echo .. expr .. ln .. ls .. mkdir .. mv .. pax .. pkill .. pwait .. rm .. rmdir .. sh builtins .. errors .. execution .. expansion .. invocation .. parameters .. parser .. set-e .. .. sleep .. test .. .. cddl lib .. sbin .. usr.bin ctfconvert .. ztest .. .. usr.sbin dtrace common aggs .. arithmetic .. arrays .. assocs .. begin .. bitfields .. buffering .. builtinvar .. cg .. clauses .. cpc .. decls .. drops .. dtraceUtil .. end .. env .. enum .. error .. exit .. fbtprovider .. funcs .. grammar .. include .. inline .. io .. ip .. java_api .. json .. lexer .. llquantize .. mdb .. mib .. misc .. multiaggs .. offsetof .. operators .. pid .. plockstat .. pointers .. pragma .. predicates .. preprocessor .. print .. printa .. printf .. privs .. probes .. proc .. profile-n .. providers .. raise .. rates .. safety .. scalars .. sched .. scripting .. sdt .. sizeof .. speculation .. stability .. stack .. stackdepth .. stop .. strlen .. strtoll .. struct .. sugar .. syscall .. sysevent .. tick-n .. trace .. tracemem .. translators .. typedef .. types .. uctf .. union .. usdt .. ustack .. vars .. version .. .. i386 arrays .. funcs .. pid .. ustack .. .. amd64 arrays .. .. .. zfsd .. .. .. etc rc.d .. .. games .. gnu lib .. usr.bin diff .. .. .. lib atf libatf-c detail .. .. libatf-c++ detail .. .. test-programs .. .. csu dynamic .. dynamiclib .. static .. .. googletest gmock .. gmock_main .. gtest .. gtest_main .. .. libarchive .. libbe .. libc c063 .. db .. gen execve .. posix_spawn .. .. hash data .. .. iconv .. inet .. locale .. net getaddrinfo data .. .. .. nss .. regex data .. .. resolv .. rpc .. ssp .. setjmp .. stdio .. stdlib .. string .. sys .. time .. tls dso .. .. termios .. ttyio .. .. libcam .. libcasper services cap_dns .. cap_grp .. cap_pwd .. cap_sysctl .. .. .. libcrypt .. libdevdctl .. libexecinfo .. libkvm .. libmp .. libnv .. libproc .. libregex data .. .. librt .. libsbuf .. libsysdecode .. libthr dlopen .. .. libutil .. libxo .. msun .. .. libexec atf atf-check .. atf-sh .. .. rtld-elf .. tftpd .. .. sbin bectl .. dhclient .. devd .. growfs .. ifconfig .. md5 .. mdconfig .. newfs_msdos .. nvmecontrol .. pfctl files .. .. ping .. route .. .. secure lib .. libexec .. usr.bin .. usr.sbin .. .. share examples tests atf .. googletest .. plain .. tap .. .. .. zoneinfo .. .. sys acl .. aio .. audit .. auditpipe .. capsicum .. cddl zfs bin .. include .. tests acl cifs .. nontrivial .. trivial .. .. atime .. bootfs .. cache .. cachefile .. clean_mirror .. cli_root zfs_upgrade .. zfs_promote .. zfs_clone .. zfs_property .. zfs_destroy .. zpool_create .. zpool_history .. zpool_expand .. zpool_remove .. zfs_mount .. zfs_unshare .. zdb .. zpool_online .. zpool_get .. zpool_export .. zfs_copies .. zfs_get .. zfs .. zpool_clear .. zpool_import blockfiles .. .. zpool .. zpool_offline .. zpool_replace .. zfs_rollback .. zpool_set .. zfs_send .. zfs_set .. zpool_detach .. zfs_diff .. zpool_scrub .. zfs_inherit .. zfs_snapshot .. zfs_share .. zpool_destroy .. zpool_status .. zfs_unmount .. zfs_receive .. zfs_create .. zpool_upgrade blockfiles .. .. zpool_add .. zfs_rename .. zpool_attach .. zfs_reservation .. .. cli_user misc .. zfs_list .. zpool_iostat .. zpool_list .. .. compression .. ctime .. delegate .. devices .. exec .. grow_pool .. grow_replicas .. history .. hotplug .. hotspare .. inheritance .. interop .. inuse .. iscsi .. large_files .. largest_pool .. link_count .. migration .. mmap .. mount .. mv_files .. nestedfs .. no_space .. online_offline .. pool_names .. poolversion .. quota .. redundancy .. refquota .. refreserv .. rename_dirs .. replacement .. reservation .. rootpool .. rsend .. scrub_mirror .. slog .. snapshot .. snapused .. sparse .. threadsappend .. truncate .. txg_integrity .. userquota .. utils_test .. write_dirs .. xattr .. zfsd .. zil .. zinject .. zones .. zvol zvol_ENOSPC .. zvol_cli .. zvol_misc .. zvol_swap .. .. zvol_thrash .. .. .. .. devrandom .. dtrace .. fifo .. file .. fs fusefs .. tmpfs .. .. geom class concat .. eli .. gate .. gpt .. mirror .. multipath .. nop .. part .. raid3 .. shsec .. stripe .. uzip etalon .. .. .. .. kern acct .. execve .. pipe .. .. kqueue libkqueue .. .. mac bsdextended .. portacl .. .. mqueue .. net .. netgraph .. netinet .. netinet6 frag6 .. .. netipsec tunnel .. .. netmap .. netpfil common .. pf ioctl .. .. .. opencrypto .. pjdfstest chflags .. chmod .. chown .. ftruncate .. granular .. link .. mkdir .. mkfifo .. mknod .. open .. rename .. rmdir .. symlink .. truncate .. unlink .. utimensat .. .. posixshm .. sys .. vfs .. vm .. vmm .. .. usr.bin apply .. awk .. basename .. bmake archives fmt_44bsd .. fmt_44bsd_mod .. fmt_oldbsd .. .. basic t0 .. t1 .. t2 .. t3 .. .. execution ellipsis .. empty .. joberr .. plus .. .. shell builtin .. meta .. path .. path_select .. replace .. select .. .. suffixes basic .. src_wild1 .. src_wild2 .. .. syntax directive-t0 .. enl .. funny-targets .. semi .. .. sysmk t0 2 1 .. .. mk .. .. t1 2 1 .. .. mk .. .. t2 2 1 .. .. mk .. .. .. variables modifier_M .. modifier_t .. opt_V .. t0 .. .. .. bsdcat .. calendar .. cmp .. compress .. cpio .. col .. comm .. csplit .. cut .. dc .. diff .. diff3 .. dirname .. du .. file2c .. file .. find .. fold .. getconf .. gh-bc .. grep .. gzip .. head .. hexdump .. ident .. indent .. join .. jot .. lastcomm .. limits .. locale .. m4 .. mkimg .. + mktemp + .. ncal .. opensm .. patch .. pr .. printf .. procstat .. renice .. rs .. sdiff .. sed regress.multitest.out .. .. seq .. soelim .. sort .. split .. stat .. tail .. tar .. timeout .. tr .. truncate .. units .. uudecode .. uuencode .. unifdef .. uniq .. vmstat .. xargs .. xinstall .. xo .. yacc yacc .. .. .. usr.sbin chown .. daemon .. etcupdate .. extattr .. fstyp .. jail .. makefs .. mixer .. newsyslog .. nmtree .. praudit .. pw .. rpcbind .. sa .. .. .. # vim: set expandtab ts=4 sw=4: diff --git a/usr.bin/mktemp/Makefile b/usr.bin/mktemp/Makefile index 0e23063431c2..72e3d7306bb8 100644 --- a/usr.bin/mktemp/Makefile +++ b/usr.bin/mktemp/Makefile @@ -1,7 +1,12 @@ # $FreeBSD$ +.include + PACKAGE= runtime PROG= mktemp +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + .include diff --git a/usr.bin/mktemp/mktemp.1 b/usr.bin/mktemp/mktemp.1 index 469d4f6097d0..5835cf4b1b3f 100644 --- a/usr.bin/mktemp/mktemp.1 +++ b/usr.bin/mktemp/mktemp.1 @@ -1,207 +1,236 @@ .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $ .\" $FreeBSD$ .\" .Dd August 4, 2022 .Dt MKTEMP 1 .Os .Sh NAME .Nm mktemp .Nd make temporary file name (unique) .Sh SYNOPSIS .Nm .Op Fl d +.Op Fl p Ar tmpdir .Op Fl q .Op Fl t Ar prefix .Op Fl u .Ar template ... .Nm .Op Fl d +.Op Fl p Ar tmpdir .Op Fl q .Op Fl u .Fl t Ar prefix .Sh DESCRIPTION The .Nm utility takes each of the given file name templates and overwrites a portion of it to create a file name. This file name is unique and suitable for use by the application. The template may be any file name with some number of .Ql X Ns s appended to it, for example .Pa /tmp/temp.XXXX . The trailing .Ql X Ns s are replaced with the current process number and/or a unique letter combination. The number of unique file names .Nm can return depends on the number of .Ql X Ns s provided; six .Ql X Ns s will result in .Nm selecting 1 of 56800235584 (62 ** 6) possible file names. .Pp If .Nm can successfully generate a unique file name, the file is created with mode 0600 (unless the .Fl u flag is given) and the filename is printed to standard output. .Pp If the .Fl t Ar prefix option is given, .Nm will generate a template string based on the .Ar prefix and the .Ev TMPDIR environment variable if set. -The default location if +If the +.Fl p +option is set, then the given +.Ar tmpdir +will be used if the +.Ev TMPDIR +environment variable is not set. +Finally, +.Pa /tmp +will be used if neither .Ev TMPDIR -is not set is -.Pa /tmp . +or +.Fl p +are set and used. Care should be taken to ensure that it is appropriate to use an environment variable potentially supplied by the user. .Pp If no arguments are passed or if only the .Fl d flag is passed .Nm behaves as if .Fl t Li tmp was supplied. .Pp Any number of temporary files may be created in a single invocation, including one based on the internal template resulting from the .Fl t flag. .Pp The .Nm utility is provided to allow shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that as a temporary file name. This kind of naming scheme is predictable and the race condition it creates is easy for an attacker to win. A safer, though still inferior, approach is to make a temporary directory using the same naming scheme. While this does allow one to guarantee that a temporary file will not be subverted, it still allows a simple denial of service attack. For these reasons it is suggested that .Nm be used instead. .Sh OPTIONS The available options are as follows: .Bl -tag -width indent .It Fl d , Fl -directory Make a directory instead of a file. +.It Fl p Ar tmpdir , Fl -tmpdir Ns Oo = Ns Ar tmpdir Oc +Use +.Ar tmpdir +for the +.Fl t +flag if the +.Ev TMPDIR +environment variable is not set. +Additionally, any provided +.Ar template +arguments will be interpreted relative to the path specified as +.Ar tmpdir . +If +.Ar tmpdir +is either empty or omitted, then the +.Ev TMPDIR +environment variable will be used. .It Fl q , Fl -quiet Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error. .It Fl t Ar prefix Generate a template (using the supplied .Ar prefix and .Ev TMPDIR if set) to create a filename template. .It Fl u , Fl -dry-run Operate in .Dq unsafe mode. The temp file will be unlinked before .Nm exits. This is slightly better than .Xr mktemp 3 but still introduces a race condition. Use of this option is not encouraged. .El .Sh EXIT STATUS .Ex -std .Sh EXAMPLES The following .Xr sh 1 fragment illustrates a simple use of .Nm where the script should quit if it cannot get a safe temporary file. .Bd -literal -offset indent tempfoo=`basename $0` TMPFILE=`mktemp /tmp/${tempfoo}.XXXXXX` || exit 1 echo "program output" >> $TMPFILE .Ed .Pp To allow the use of $TMPDIR: .Bd -literal -offset indent tempfoo=`basename $0` TMPFILE=`mktemp -t ${tempfoo}` || exit 1 echo "program output" >> $TMPFILE .Ed .Pp In this case, we want the script to catch the error itself. .Bd -literal -offset indent tempfoo=`basename $0` TMPFILE=`mktemp -q /tmp/${tempfoo}.XXXXXX` if [ $? -ne 0 ]; then echo "$0: Can't create temp file, exiting..." exit 1 fi .Ed .Sh SEE ALSO .Xr mkdtemp 3 , .Xr mkstemp 3 , .Xr mktemp 3 , .Xr environ 7 .Sh HISTORY A .Nm utility appeared in .Ox 2.1 . This implementation was written independently based on the .Ox man page, and first appeared in .Fx 2.2.7 . This man page is taken from .Ox . diff --git a/usr.bin/mktemp/mktemp.c b/usr.bin/mktemp/mktemp.c index dc92de2961d0..77245d2fa0d3 100644 --- a/usr.bin/mktemp/mktemp.c +++ b/usr.bin/mktemp/mktemp.c @@ -1,166 +1,204 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 1994, 1995, 1996, 1998 Peter Wemm * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* * This program was originally written long ago, originally for a non * BSD-like OS without mkstemp(). It's been modified over the years * to use mkstemp() rather than the original O_CREAT|O_EXCL/fstat/lstat * etc style hacks. * A cleanup, misc options and mkdtemp() calls were added to try and work * more like the OpenBSD version - which was first to publish the interface. */ #include #include #include +#include #include #include #include #include #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ static void usage(void); static const struct option long_opts[] = { {"directory", no_argument, NULL, 'd'}, + {"tmpdir", optional_argument, NULL, 'p'}, {"quiet", no_argument, NULL, 'q'}, {"dry-run", no_argument, NULL, 'u'}, {NULL, no_argument, NULL, 0}, }; int main(int argc, char **argv) { int c, fd, ret; - char *tmpdir; - const char *prefix; + const char *prefix, *tmpdir; char *name; int dflag, qflag, tflag, uflag; + bool prefer_tmpdir; ret = dflag = qflag = tflag = uflag = 0; + prefer_tmpdir = true; prefix = "mktemp"; name = NULL; + tmpdir = NULL; - while ((c = getopt_long(argc, argv, "dqt:u", long_opts, NULL)) != -1) + while ((c = getopt_long(argc, argv, "dp:qt:u", long_opts, NULL)) != -1) switch (c) { case 'd': dflag++; break; + case 'p': + tmpdir = optarg; + if (tmpdir == NULL || *tmpdir == '\0') + tmpdir = getenv("TMPDIR"); + + /* + * We've already done the necessary environment + * fallback, skip the later one. + */ + prefer_tmpdir = false; + break; + case 'q': qflag++; break; case 't': prefix = optarg; tflag++; break; case 'u': uflag++; break; default: usage(); } argc -= optind; argv += optind; if (!tflag && argc < 1) { tflag = 1; prefix = "tmp"; + + /* + * For this implied -t mode, we actually want to swap the usual + * order of precedence: -p, then TMPDIR, then /tmp. + */ + prefer_tmpdir = false; } if (tflag) { - tmpdir = getenv("TMPDIR"); + const char *envtmp; + + envtmp = NULL; + + /* + * $TMPDIR preferred over `-p` if specified, for compatibility. + */ + if (prefer_tmpdir || tmpdir == NULL) + envtmp = getenv("TMPDIR"); + if (envtmp != NULL) + tmpdir = envtmp; if (tmpdir == NULL) asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP, prefix); else asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix); /* if this fails, the program is in big trouble already */ if (name == NULL) { if (qflag) return (1); else errx(1, "cannot generate template"); } } /* generate all requested files */ while (name != NULL || argc > 0) { if (name == NULL) { - name = strdup(argv[0]); + if (!tflag && tmpdir != NULL) + asprintf(&name, "%s/%s", tmpdir, argv[0]); + else + name = strdup(argv[0]); + if (name == NULL) + err(1, "%s", argv[0]); argv++; argc--; } if (dflag) { if (mkdtemp(name) == NULL) { ret = 1; if (!qflag) warn("mkdtemp failed on %s", name); } else { printf("%s\n", name); if (uflag) rmdir(name); } } else { fd = mkstemp(name); if (fd < 0) { ret = 1; if (!qflag) warn("mkstemp failed on %s", name); } else { close(fd); if (uflag) unlink(name); printf("%s\n", name); } } if (name) free(name); name = NULL; } return (ret); } static void usage(void) { fprintf(stderr, - "usage: mktemp [-d] [-q] [-t prefix] [-u] template ...\n"); + "usage: mktemp [-d] [-p tmpdir] [-q] [-t prefix] [-u] template " + "...\n"); fprintf(stderr, - " mktemp [-d] [-q] [-u] -t prefix \n"); + " mktemp [-d] [-p tmpdir] [-q] [-u] -t prefix \n"); exit (1); } diff --git a/usr.bin/mktemp/tests/Makefile b/usr.bin/mktemp/tests/Makefile new file mode 100644 index 000000000000..5141bf0faade --- /dev/null +++ b/usr.bin/mktemp/tests/Makefile @@ -0,0 +1,7 @@ +# $FreeBSD$ + +PACKAGE= tests + +ATF_TESTS_SH+= mktemp_test + +.include diff --git a/usr.bin/mktemp/tests/mktemp_test.sh b/usr.bin/mktemp/tests/mktemp_test.sh new file mode 100755 index 000000000000..c424030fac4f --- /dev/null +++ b/usr.bin/mktemp/tests/mktemp_test.sh @@ -0,0 +1,118 @@ +# +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2022 Klara Systems +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ + +atf_test_case tmpdir_env +tmpdir_env_body() +{ + + tmpdir="$PWD" + + atf_check -o match:"^$tmpdir/foo\..+$" \ + env TMPDIR="$tmpdir" mktemp -t foo +} + +atf_test_case tmpdir_pflag +tmpdir_pflag_body() +{ + + mkdir tmp_p tmp_env + + tmpdir="$PWD/tmp_env" + export TMPDIR="$tmpdir" + + pflag="$PWD/tmp_p" + + # Basic usage: just -p specified + atf_check -o match:"^$pflag/tmp\..+$" \ + env -u TMPDIR mktemp -p "$pflag" + atf_check -o match:"^$pflag/tmp\..+$" \ + env TMPDIR="$tmpdir" mktemp -p "$pflag" + + # -p with a list of names + atf_check -o ignore env -u TMPDIR mktemp -p "$pflag" x y z + atf_check test -f "$pflag/x" + atf_check test -f "$pflag/y" + atf_check test -f "$pflag/z" + + # Checking --tmpdir usage, which should defer to $TMPDIR followed by + # /tmp with no value specified. + atf_check -o match:"^/tmp/foo\..+$" \ + env -u TMPDIR mktemp --tmpdir -t foo + atf_check -o match:"^$tmpdir/foo\..+$" \ + env TMPDIR="$tmpdir" mktemp --tmpdir -t foo + + # Finally, combined -p -t + atf_check -o match:"^$pflag/foo\..+$" \ + env -u TMPDIR mktemp -p "$pflag" -t foo + atf_check -o match:"^$pflag/foo\..+$" \ + env TMPDIR="$tmpdir" mktemp -p "$pflag" -t foo +} + +atf_test_case tmpdir_pflag_dir +tmpdir_pflag_dir_body() +{ + + tmpdir="$PWD" + atf_check -o save:tmpname \ + env -u TMPDIR mktemp -d -p "$tmpdir" -t foo + + # Better diagnostics when using -o match: + cat rather than grep. + atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname + cdir=$(cat tmpname) + + atf_check test -d "$cdir" + + atf_check -o match:"^$tmpdir/footmp$" \ + env -u TMPDIR mktemp -d -p "$tmpdir" footmp + atf_check test -d "$tmpdir/footmp" +} + +atf_test_case tmpdir_pflag_noarg +tmpdir_pflag_noarg_body() +{ + + # Without -t, this time; this introduces $TMPDIR without having to use + # it. + tmpdir="$PWD" + atf_check -o save:tmpname \ + env TMPDIR="$tmpdir" mktemp --tmpdir foo.XXXXXXXX + atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname + + # An empty string gets the same treatment. + atf_check -o save:tmpname \ + env TMPDIR="$tmpdir" mktemp -p '' foo.XXXXXXXX + 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 +}