-.\" $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
.\" 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
.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
.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
-/* $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
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");
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");
}
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)
(void)puts(tempfile);
free(tempfile);
- exit(EXIT_SUCCESS);
+ return EXIT_SUCCESS;
}
__dead void