git: 3f9e5e59bda0 - main - sort: use mkstemp(3) instead of reinventing it

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 12 Oct 2022 16:02:00 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=3f9e5e59bda05acea409fa3121c835a74672de1c

commit 3f9e5e59bda05acea409fa3121c835a74672de1c
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2022-10-12 15:57:37 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2022-10-12 16:01:57 +0000

    sort: use mkstemp(3) instead of reinventing it
    
    MFC After:      1 week
---
 usr.bin/sort/file.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index ffcd71e9f991..3e5754442ac1 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -195,15 +195,15 @@ file_is_tmp(const char* fn)
 char *
 new_tmp_file_name(void)
 {
-	static size_t tfcounter = 0;
-	static const char *fn = ".bsdsort.";
 	char *ret;
-	size_t sz;
+	int fd;
 
-	sz = strlen(tmpdir) + 1 + strlen(fn) + 32 + 1;
-	ret = sort_malloc(sz);
+	if (asprintf(&ret, "%s/.bsdsort.XXXXXXXXXX", tmpdir) == -1)
+		err(2, "asprintf()");
+	if ((fd = mkstemp(ret)) == -1)
+		err(2, "mkstemp()");
+	close(fd);
 
-	sprintf(ret, "%s/%s%d.%lu", tmpdir, fn, (int) getpid(), (unsigned long)(tfcounter++));
 	tmp_file_atexit(ret);
 	return (ret);
 }