From dd0208b307a70a9a31ac084b480c352633a94023 Mon Sep 17 00:00:00 2001 From: Kevin Lo Date: Tue, 13 May 2008 09:42:03 +0000 Subject: [PATCH] Improve temporary file handling Obtained from: OpenBSD --- usr.bin/xstr/xstr.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/usr.bin/xstr/xstr.c b/usr.bin/xstr/xstr.c index 7964dd9897a9..c5fcef102026 100644 --- a/usr.bin/xstr/xstr.c +++ b/usr.bin/xstr/xstr.c @@ -100,6 +100,7 @@ int main(int argc, char *argv[]) { int c; + int fdesc; while ((c = getopt(argc, argv, "-cv")) != -1) switch (c) { @@ -122,8 +123,16 @@ main(int argc, char *argv[]) signal(SIGINT, onintr); if (cflg || (argc == 0 && !readstd)) inithash(); - else - strings = mktemp(strdup(_PATH_TMP)); + else { + strings = strdup(_PATH_TMP); + if (strings == NULL) + err(1, "strdup() failed"); + fdesc = mkstemp(strings); + if (fdesc == -1) + err(1, "Unable to create temporary file"); + close(fdesc); + } + while (readstd || argc > 0) { if (freopen("x.c", "w", stdout) == NULL) err(1, "x.c");