Implement -q support for mktemp(1)

This commit is contained in:
sin 2013-11-13 12:10:49 +00:00
parent b8edf3b4ee
commit 56a62c605f
2 changed files with 20 additions and 6 deletions

@ -3,7 +3,7 @@
mktemp \- make temporary filename mktemp \- make temporary filename
.SH SYNOPSIS .SH SYNOPSIS
.B mktemp .B mktemp
.RB [ \-d ] .RB [ \-dq ]
.RB [ template ] .RB [ template ]
.SH DESCRIPTION .SH DESCRIPTION
.B mktemp .B mktemp
@ -15,6 +15,10 @@ six `Xs' appended to it. If no template is specified a default of
.TP .TP
.B \-d .B \-d
Make a directory instead of a file 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 .SH SEE ALSO
.IR mkdtemp (3), .IR mkdtemp (3),
.IR mkstemp (3) .IR mkstemp (3)

@ -9,10 +9,11 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-d] [template]\n", argv0); eprintf("usage: %s [-dq] [template]\n", argv0);
} }
static int dflag = 0; static int dflag = 0;
static int qflag = 0;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -26,6 +27,9 @@ main(int argc, char *argv[])
case 'd': case 'd':
dflag = 1; dflag = 1;
break; break;
case 'q':
qflag = 1;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -37,11 +41,17 @@ main(int argc, char *argv[])
snprintf(tmppath, sizeof(tmppath), "%s/%s", tmpdir, template); snprintf(tmppath, sizeof(tmppath), "%s/%s", tmpdir, template);
if (dflag) { if (dflag) {
if (!mkdtemp(tmppath)) if (!mkdtemp(tmppath)) {
if (!qflag)
eprintf("mkdtemp %s:", tmppath); eprintf("mkdtemp %s:", tmppath);
exit(EXIT_FAILURE);
}
} else { } else {
if ((fd = mkstemp(tmppath)) < 0) if ((fd = mkstemp(tmppath)) < 0) {
if (!qflag)
eprintf("mkstemp %s:", tmppath); eprintf("mkstemp %s:", tmppath);
exit(EXIT_FAILURE);
}
close(fd); close(fd);
} }
puts(tmppath); puts(tmppath);