[hackers] [sbase] Implement -q support for mktemp(1) || sin

From: <git_AT_suckless.org>
Date: Wed, 13 Nov 2013 16:27:45 +0100

commit 56a62c605f1db3076da06bc148a5bca152295209
Author: sin <sin_AT_2f30.org>
Date: Wed Nov 13 12:10:49 2013 +0000

    Implement -q support for mktemp(1)

diff --git a/mktemp.1 b/mktemp.1
index 740350a..b81461b 100644
--- a/mktemp.1
+++ b/mktemp.1
_AT_@ -3,7 +3,7 @@
 mktemp \- make temporary filename
 .SH SYNOPSIS
 .B mktemp
-.RB [ \-d ]
+.RB [ \-dq ]
 .RB [ template ]
 .SH DESCRIPTION
 .B mktemp
_AT_@ -15,6 +15,10 @@ six `Xs' appended to it. If no template is specified a default of
 .TP
 .B \-d
 Make a directory instead of a file
+.TP
+.B \-q
+Fail silently if an error occurs. This is useful if a script
+does not want error output to go to standard error.
 .SH SEE ALSO
 .IR mkdtemp (3),
 .IR mkstemp (3)
diff --git a/mktemp.c b/mktemp.c
index 0989931..f5a0be5 100644
--- a/mktemp.c
+++ b/mktemp.c
_AT_@ -9,10 +9,11 @@
 static void
 usage(void)
 {
- eprintf("usage: %s [-d] [template]
", argv0);
+ eprintf("usage: %s [-dq] [template]
", argv0);
 }
 
 static int dflag = 0;
+static int qflag = 0;
 
 int
 main(int argc, char *argv[])
_AT_@ -26,6 +27,9 @@ main(int argc, char *argv[])
         case 'd':
                 dflag = 1;
                 break;
+ case 'q':
+ qflag = 1;
+ break;
         default:
                 usage();
         } ARGEND;
_AT_@ -37,11 +41,17 @@ main(int argc, char *argv[])
 
         snprintf(tmppath, sizeof(tmppath), "%s/%s", tmpdir, template);
         if (dflag) {
- if (!mkdtemp(tmppath))
- eprintf("mkdtemp %s:", tmppath);
+ if (!mkdtemp(tmppath)) {
+ if (!qflag)
+ eprintf("mkdtemp %s:", tmppath);
+ exit(EXIT_FAILURE);
+ }
         } else {
- if ((fd = mkstemp(tmppath)) < 0)
- eprintf("mkstemp %s:", tmppath);
+ if ((fd = mkstemp(tmppath)) < 0) {
+ if (!qflag)
+ eprintf("mkstemp %s:", tmppath);
+ exit(EXIT_FAILURE);
+ }
                 close(fd);
         }
         puts(tmppath);
Received on Wed Nov 13 2013 - 16:27:45 CET

This archive was generated by hypermail 2.3.0 : Wed Nov 13 2013 - 16:36:15 CET