summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2024-03-01 21:50:41 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2024-03-01 21:50:41 +0000
commit0460045a2c41b854addf06253a1e1109baed9e22 (patch)
tree33a98407ef3bac2842ec6674e14fe96ff765c7be /usr.bin
parent5e1c66d66cc28f1eb05dc9127b4f0d62400b54f9 (diff)
mktemp(1): add suffix support for compatibility with the GNU version
It is now possible to use templates where the Xs are not at the end, like "foo.XXXXXX.bar". If there are multiple runs of Xs, the last one is used. OK deraadt@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mktemp/mktemp.120
-rw-r--r--usr.bin/mktemp/mktemp.c31
2 files changed, 35 insertions, 16 deletions
diff --git a/usr.bin/mktemp/mktemp.1 b/usr.bin/mktemp/mktemp.1
index 20cd5ec5abd..277ba765d29 100644
--- a/usr.bin/mktemp/mktemp.1
+++ b/usr.bin/mktemp/mktemp.1
@@ -1,6 +1,6 @@
-.\" $OpenBSD: mktemp.1,v 1.31 2022/03/31 17:27:25 naddy Exp $
+.\" $OpenBSD: mktemp.1,v 1.32 2024/03/01 21:50:40 millert Exp $
.\"
-.\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013
+.\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013, 2024
.\" Todd C. Miller <millert@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
@@ -15,7 +15,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: March 31 2022 $
+.Dd $Mdocdate: March 1 2024 $
.Dt MKTEMP 1
.Os
.Sh NAME
@@ -36,9 +36,15 @@ The
.Ar template
may be any filename with at least six
.Ql X Ns s
-appended
-to it, for example
-.Pa /tmp/tfile.XXXXXXXXXX .
+in the last component of the filename, for example
+.Pa /tmp/tfile.XXXXXXXXXX
+or
+.Pa /tmp/editor.XXXXXXXXXX.txt .
+If there is more than one run of
+.Ql X Ns s
+in the
+.Ar template ,
+the last one will be used.
If no
.Ar template
is specified, a default of
@@ -47,7 +53,7 @@ is used and the
.Fl t
flag is implied (see below).
.Pp
-The trailing
+The final
.Ql X Ns s
are replaced with a unique digit and letter combination.
The name chosen depends both on the number of
diff --git a/usr.bin/mktemp/mktemp.c b/usr.bin/mktemp/mktemp.c
index 188fac101ad..f4ccbe0cadc 100644
--- a/usr.bin/mktemp/mktemp.c
+++ b/usr.bin/mktemp/mktemp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mktemp.c,v 1.25 2019/06/28 05:35:34 deraadt Exp $ */
+/* $OpenBSD: mktemp.c,v 1.26 2024/03/01 21:50:40 millert Exp $ */
/*
* Copyright (c) 1996, 1997, 2001-2003, 2013
@@ -35,8 +35,8 @@ int
main(int argc, char *argv[])
{
int ch, fd, uflag = 0, tflag = 0, makedir = 0;
- char *cp, *template, *tempfile, *prefix = _PATH_TMP;
- size_t len;
+ char *base, *cp, *template, *tempfile, *prefix = _PATH_TMP;
+ size_t len, suffixlen = 0;
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
@@ -76,13 +76,26 @@ main(int argc, char *argv[])
usage();
}
- len = strlen(template);
- if (len < 6 || strcmp(&template[len - 6], "XXXXXX")) {
+ base = strrchr(template, '/');
+ if (base != NULL)
+ base++;
+ else
+ base = template;
+ len = strlen(base);
+ if (len > 0 && base[len - 1] != 'X') {
+ /* Check for suffix, e.g. /tmp/XXXXXX.foo in last component. */
+ for (suffixlen = 0; suffixlen < len; suffixlen++) {
+ if (base[len - suffixlen - 1] == 'X')
+ break;
+ }
+ }
+ if (len - suffixlen < 6 ||
+ strncmp(&base[len - suffixlen - 6], "XXXXXX", 6)) {
fatalx("insufficient number of Xs in template `%s'",
template);
}
if (tflag) {
- if (strchr(template, '/')) {
+ if (base != template) {
fatalx("template must not contain directory "
"separators in -t mode");
}
@@ -103,12 +116,12 @@ main(int argc, char *argv[])
fatalx("cannot allocate memory");
if (makedir) {
- if (mkdtemp(tempfile) == NULL)
+ if (mkdtemps(tempfile, suffixlen) == NULL)
fatal("cannot make temp dir %s", tempfile);
if (uflag)
(void)rmdir(tempfile);
} else {
- if ((fd = mkstemp(tempfile)) == -1)
+ if ((fd = mkstemps(tempfile, suffixlen)) == -1)
fatal("cannot make temp file %s", tempfile);
(void)close(fd);
if (uflag)
@@ -118,7 +131,7 @@ main(int argc, char *argv[])
(void)puts(tempfile);
free(tempfile);
- exit(EXIT_SUCCESS);
+ return EXIT_SUCCESS;
}
__dead void