diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-10-01 17:08:31 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-10-01 17:08:31 +0000 |
commit | 8dee1861c085bad9fb091c6ac52dca6100e5899c (patch) | |
tree | e4c72b3fc67f179f9876d9ff95c2eeddb0dd10f8 /usr.bin | |
parent | 9712a0af728c990877f5890334544693ff3f7f35 (diff) |
Add -t and -d flags to allow script writers to easily take a user's TMPDIR
environment variable into account. This came out of a discussion with
Solar Designer.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mktemp/mktemp.1 | 88 | ||||
-rw-r--r-- | usr.bin/mktemp/mktemp.c | 69 |
2 files changed, 129 insertions, 28 deletions
diff --git a/usr.bin/mktemp/mktemp.1 b/usr.bin/mktemp/mktemp.1 index 4a4a47b1af5..24d1deeab44 100644 --- a/usr.bin/mktemp/mktemp.1 +++ b/usr.bin/mktemp/mktemp.1 @@ -1,6 +1,6 @@ -.\" $OpenBSD: mktemp.1,v 1.18 2000/11/09 17:52:25 aaron Exp $ +.\" $OpenBSD: mktemp.1,v 1.19 2001/10/01 17:08:30 millert Exp $ .\" -.\" Copyright (c) 1996, 2000 Todd C. Miller <Todd.Miller@courtesan.com> +.\" Copyright (c) 1996, 2000, 2001 Todd C. Miller <Todd.Miller@courtesan.com> .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -25,7 +25,7 @@ .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd November 20, 1996 +.Dd September 30, 2001 .Dt MKTEMP 1 .Os .Sh NAME @@ -33,9 +33,8 @@ .Nd make temporary file name (unique) .Sh SYNOPSIS .Nm mktemp -.Op Fl d -.Op Fl q -.Op Fl u +.Op Fl dqtu +.Op Fl p Ar directory .Ar template .Sh DESCRIPTION The @@ -92,10 +91,44 @@ The options are as follows: .Bl -tag -width Ds .It Fl d Make a directory instead of a file. +.It Fl p Ar directory +Use the specified +.Ar directory +as a prefix when generating the temporary file name. +The +.Ar directory +will be overridden by the user's +.Ev TMPDIR +environment variable if it is set. +This option implies the +.Fl t +flag (see below). .It Fl q Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error. +.It Fl t +Generate a temporary file rooted in a temporary directory. +The temporary directory is chosen as follows: +.Bl -bullet +.It +If the user's +.Ev TMPDIR +environment variable is set, the directory contained therein is used. +.It +Otherwise, if the +.Fl p +flag was given the specified directory is used. +.It +If none of the above apply, +.Pa /tmp +is used. +.El +.Pp +In this mode, the +.Ar template +should be a directory component (as opposed to a full path) and thus +should not contain any forward slashes. .It Fl u Operate in .Dq unsafe @@ -126,12 +159,38 @@ TMPFILE=`mktemp /tmp/$CMD.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE .Ed .Pp -In this case, we want the script to catch the error ourselves. +The same fragment with support for a user's +.Ev TMPDIR +environment variable can be written as follows. .Bd -literal -offset indent CMD=`basename $0` -TMPFILE=`mktemp -q /tmp/$CMD.XXXXXXXXXX` +TMPFILE=`mktemp -t $CMD.XXXXXXXXXX` || exit 1 +echo "program output" >> $TMPFILE +.Ed +.Pp +In some cases, it may be desirable to use a default temporary directory +other than +.Pa /tmp . +In this example the temporary file will be created in +.Pa /extra/tmp +unless the user's +.Ev TMPDIR +environment variable specifies otherwise. +.Bd -literal -offset indent +CMD=`basename $0` +TMPFILE=`mktemp -t -p /extra/tmp $CMD.XXXXXXXXXX` || exit 1 +echo "program output" >> $TMPFILE +.Ed +.Pp +In some cases, we want the script to catch the error. +For instance, if we attempt to create two temporary files and +the second one fails we need to remove the first before exiting. +.Bd -literal -offset indent +CMD=`basename $0` +TMP1=`mktemp -t $CMD.1.XXXXXXXXXX` || exit 1 +TMP2=`mktemp -t $CMD.2.XXXXXXXXXX` if [ $? -ne 0 ]; then - echo "$CMD: Can't create temp file, exiting..." + rm -f $TMP1 exit 1 fi .Ed @@ -139,16 +198,23 @@ fi Or perhaps you don't want to exit if .Nm is unable to create the file. -In this case you can protect the part of the script thusly. +In this case you can protect that part of the script thusly. .Bd -literal -offset indent CMD=`basename $0` -TMPFILE=`mktemp /tmp/$CMD.XXXXXXXXXX` && { +TMPFILE=`mktemp -q -t $CMD.XXXXXXXXXX` && { # Safe to use $TMPFILE in this block echo data > $TMPFILE ... rm -f $TMPFILE } .Ed +.Sh ENVIRONMENT +.Bl -tag -width TMPDIR +.It Ev TMPDIR +directory in which to place the temporary file when in +.Fl t +mode +.El .Sh SEE ALSO .Xr mkdtemp 3 , .Xr mkstemp 3 , diff --git a/usr.bin/mktemp/mktemp.c b/usr.bin/mktemp/mktemp.c index 6db055d0e50..3afeb44cdc8 100644 --- a/usr.bin/mktemp/mktemp.c +++ b/usr.bin/mktemp/mktemp.c @@ -1,7 +1,7 @@ -/* $OpenBSD: mktemp.c,v 1.5 1998/06/21 22:14:00 millert Exp $ */ +/* $OpenBSD: mktemp.c,v 1.6 2001/10/01 17:08:30 millert Exp $ */ /* - * Copyright (c) 1996 Todd C. Miller <Todd.Miller@courtesan.com> + * Copyright (c) 1996, 1997, 2001 Todd C. Miller <Todd.Miller@courtesan.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,22 +28,23 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: mktemp.c,v 1.5 1998/06/21 22:14:00 millert Exp $"; +static const char rcsid[] = "$OpenBSD: mktemp.c,v 1.6 2001/10/01 17:08:30 millert Exp $"; #endif /* not lint */ +#include <paths.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <err.h> -extern char *__progname; - void usage() { - (void) fprintf(stderr, "Usage: %s [-d] [-q] [-u] template\n", - __progname); + extern char *__progname; + + (void) fprintf(stderr, + "Usage: %s [-dqtu] [-p prefix] template\n", __progname); exit(1); } @@ -52,21 +53,28 @@ main(argc, argv) int argc; char **argv; { - char *template; - int c, uflag = 0, qflag = 0, makedir = 0; + int ch, uflag = 0, qflag = 0, tflag = 0, makedir = 0; + char *cp, *template, *prefix = _PATH_TMP; + size_t plen; - while ((c = getopt(argc, argv, "dqu")) != -1) - switch(c) { + while ((ch = getopt(argc, argv, "dp:qtu")) != -1) + switch(ch) { case 'd': makedir = 1; break; + case 'p': + prefix = optarg; + tflag = 1; + break; case 'q': qflag = 1; break; + case 't': + tflag = 1; + break; case 'u': uflag = 1; break; - case '?': default: usage(); } @@ -74,11 +82,38 @@ main(argc, argv) if (argc - optind != 1) usage(); - if ((template = strdup(argv[optind])) == NULL) { - if (qflag) - exit(1); - else - errx(1, "Cannot allocate memory"); + if (tflag) { + if (strchr(argv[optind], '/')) { + if (qflag) + exit(1); + else + errx(1, "template must not contain directory separators in -t mode"); + } + + cp = getenv("TMPDIR"); + if (cp != NULL && *cp != '\0') + prefix = cp; + plen = strlen(prefix); + while (plen != 0 && prefix[plen - 1] == '/') + plen--; + + template = (char *)malloc(plen + 1 + strlen(argv[optind]) + 1); + if (template == NULL) { + if (qflag) + exit(1); + else + errx(1, "Cannot allocate memory"); + } + memcpy(template, prefix, plen); + template[plen] = '/'; + strcpy(template + plen + 1, argv[optind]); /* SAFE */ + } else { + if ((template = strdup(argv[optind])) == NULL) { + if (qflag) + exit(1); + else + errx(1, "Cannot allocate memory"); + } } if (makedir) { |