Sat Aug 15 20:02:28 2009 UTC ()
add -p <tmpdir> option to override $TMPDIR from the command line like linux
has.


(christos)
diff -r1.17 -r1.18 src/usr.bin/mktemp/mktemp.1
diff -r1.10 -r1.11 src/usr.bin/mktemp/mktemp.c

cvs diff -r1.17 -r1.18 src/usr.bin/mktemp/mktemp.1 (expand / switch to unified diff)

--- src/usr.bin/mktemp/mktemp.1 2006/09/19 19:01:44 1.17
+++ src/usr.bin/mktemp/mktemp.1 2009/08/15 20:02:28 1.18
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1.\" $NetBSD: mktemp.1,v 1.17 2006/09/19 19:01:44 wiz Exp $ 1.\" $NetBSD: mktemp.1,v 1.18 2009/08/15 20:02:28 christos Exp $
2.\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 2.\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
3.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $ 3.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
4.\" 4.\"
5.\" Copyright (c) 1989, 1991, 1993 5.\" Copyright (c) 1989, 1991, 1993
6.\" The Regents of the University of California. All rights reserved. 6.\" The Regents of the University of California. All rights reserved.
7.\" 7.\"
8.\" Redistribution and use in source and binary forms, with or without 8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions 9.\" modification, are permitted provided that the following conditions
10.\" are met: 10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright 11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer. 12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 14.\" notice, this list of conditions and the following disclaimer in the
@@ -21,35 +21,36 @@ @@ -21,35 +21,36 @@
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 32.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
33.\" 33.\"
34.Dd January 12, 2006 34.Dd August 15, 2009
35.Dt MKTEMP 1 35.Dt MKTEMP 1
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm mktemp 38.Nm mktemp
39.Nd make temporary file name (unique) 39.Nd make temporary file name (unique)
40.Sh SYNOPSIS 40.Sh SYNOPSIS
41.Nm mktemp 41.Nm mktemp
42.Op Fl dqu 42.Op Fl dqu
 43.Op Fl p Ar tmpdir
43.Bro 44.Bro
44.Fl t Ar prefix 45.Fl t Ar prefix
45.No | 46.No |
46.Ar template ... 47.Ar template ...
47.Brc 48.Brc
48.Sh DESCRIPTION 49.Sh DESCRIPTION
49The 50The
50.Nm 51.Nm
51utility takes each of the given file name templates and overwrites a 52utility takes each of the given file name templates and overwrites a
52portion of it to create a file name. 53portion of it to create a file name.
53This file name is unique and suitable for use by the application. 54This file name is unique and suitable for use by the application.
54The template may be any file name with some number of 55The template may be any file name with some number of
55.Ql X Ns s 56.Ql X Ns s
@@ -79,26 +80,29 @@ flag is given) and the filename is print @@ -79,26 +80,29 @@ flag is given) and the filename is print
79If the 80If the
80.Fl t Ar prefix 81.Fl t Ar prefix
81option is given, 82option is given,
82.Nm 83.Nm
83will generate a template string based on the 84will generate a template string based on the
84.Ar prefix 85.Ar prefix
85and the 86and the
86.Ev TMPDIR 87.Ev TMPDIR
87environment variable, if set. 88environment variable, if set.
88The default location if 89The default location if
89.Ev TMPDIR 90.Ev TMPDIR
90is not set is 91is not set is
91.Pa /tmp . 92.Pa /tmp .
 93The default location of the temporary directory can be overriden with the
 94.Fl p Ar tmpdir
 95option.
92The template string created will consist of the 96The template string created will consist of the
93.Ar prefix 97.Ar prefix
94followed by a 98followed by a
95.So . Sc 99.So . Sc
96and an eight character unique letter combination. 100and an eight character unique letter combination.
97.Ql X Ns s 101.Ql X Ns s
98in the 102in the
99.Ar prefix 103.Ar prefix
100string will be treated as literal. 104string will be treated as literal.
101If an additional 105If an additional
102.Ar template 106.Ar template
103argument is passed, a second file will be created. 107argument is passed, a second file will be created.
104Care should be taken to ensure that it is appropriate to use an 108Care should be taken to ensure that it is appropriate to use an

cvs diff -r1.10 -r1.11 src/usr.bin/mktemp/mktemp.c (expand / switch to unified diff)

--- src/usr.bin/mktemp/mktemp.c 2007/12/15 19:44:52 1.10
+++ src/usr.bin/mktemp/mktemp.c 2009/08/15 20:02:28 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: mktemp.c,v 1.10 2007/12/15 19:44:52 perry Exp $ */ 1/* $NetBSD: mktemp.c,v 1.11 2009/08/15 20:02:28 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1994, 1995, 1996, 1998 Peter Wemm <peter@netplex.com.au> 4 * Copyright (c) 1994, 1995, 1996, 1998 Peter Wemm <peter@netplex.com.au>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -40,93 +40,103 @@ @@ -40,93 +40,103 @@
40#include "nbtool_config.h" 40#include "nbtool_config.h"
41#endif 41#endif
42 42
43#include <sys/cdefs.h> 43#include <sys/cdefs.h>
44#include <sys/types.h> 44#include <sys/types.h>
45#include <err.h> 45#include <err.h>
46#include <paths.h> 46#include <paths.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <stdlib.h> 48#include <stdlib.h>
49#include <string.h> 49#include <string.h>
50#include <unistd.h> 50#include <unistd.h>
51 51
52#if defined(__RCSID) && !defined(__lint) 52#if defined(__RCSID) && !defined(__lint)
53__RCSID("$NetBSD: mktemp.c,v 1.10 2007/12/15 19:44:52 perry Exp $"); 53__RCSID("$NetBSD: mktemp.c,v 1.11 2009/08/15 20:02:28 christos Exp $");
54#endif /* !__lint */ 54#endif /* !__lint */
55 55
56static void usage(void) __dead; 56static void usage(void) __dead;
57 57
58int 58int
59main(int argc, char **argv) 59main(int argc, char **argv)
60{ 60{
61 int c, fd, ret; 61 int c, fd, ret;
62 char *tmpdir; 62 char *tmpdir;
63 const char *prefix; 63 const char *prefix;
64 char *name; 64 char *name;
65 int dflag, qflag, tflag, uflag; 65 int dflag, qflag, tflag, uflag;
66 66
67 setprogname(*argv); 67 setprogname(*argv);
68 ret = dflag = qflag = tflag = uflag = 0; 68 ret = dflag = qflag = tflag = uflag = 0;
 69 tmpdir = NULL;
69 prefix = "mktemp"; 70 prefix = "mktemp";
70 name = NULL; 71 name = NULL;
71 72
72 while ((c = getopt(argc, argv, "dqt:u")) != -1) 73 while ((c = getopt(argc, argv, "dp:qt:u")) != -1)
73 switch (c) { 74 switch (c) {
74 case 'd': 75 case 'd':
75 dflag++; 76 dflag++;
76 break; 77 break;
77 78
 79 case 'p':
 80 tmpdir = optarg;
 81 break;
 82
78 case 'q': 83 case 'q':
79 qflag++; 84 qflag++;
80 break; 85 break;
81 86
82 case 't': 87 case 't':
83 prefix = optarg; 88 prefix = optarg;
84 tflag++; 89 tflag++;
85 break; 90 break;
86 91
87 case 'u': 92 case 'u':
88 uflag++; 93 uflag++;
89 break; 94 break;
90 95
91 default: 96 default:
92 usage(); 97 usage();
93 } 98 }
94 99
95 argc -= optind; 100 argc -= optind;
96 argv += optind; 101 argv += optind;
97 102
98 if (tflag) { 103 if (tflag) {
99 tmpdir = getenv("TMPDIR"); 104 if (tmpdir == NULL)
 105 tmpdir = getenv("TMPDIR");
100 if (tmpdir == NULL) 106 if (tmpdir == NULL)
101 (void)asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP, 107 (void)asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP,
102 prefix); 108 prefix);
103 else 109 else
104 (void)asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix); 110 (void)asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix);
105 /* if this fails, the program is in big trouble already */ 111 /* if this fails, the program is in big trouble already */
106 if (name == NULL) { 112 if (name == NULL) {
107 if (qflag) 113 if (qflag)
108 return 1; 114 return 1;
109 else 115 else
110 errx(1, "Cannot generate template"); 116 errx(1, "Cannot generate template");
111 } 117 }
112 } else if (argc < 1) { 118 } else if (argc < 1) {
113 usage(); 119 usage();
114 } 120 }
115  121
116 /* generate all requested files */ 122 /* generate all requested files */
117 while (name != NULL || argc > 0) { 123 while (name != NULL || argc > 0) {
118 if (name == NULL) { 124 if (name == NULL) {
119 name = strdup(argv[0]); 125 if (tmpdir)
 126 (void)asprintf(&name, "%s/%s",
 127 tmpdir, argv[0]);
 128 else
 129 name = strdup(argv[0]);
120 argv++; 130 argv++;
121 argc--; 131 argc--;
122 } 132 }
123 133
124 if (dflag) { 134 if (dflag) {
125 if (mkdtemp(name) == NULL) { 135 if (mkdtemp(name) == NULL) {
126 ret = 1; 136 ret = 1;
127 if (!qflag) 137 if (!qflag)
128 warn("mkdtemp failed on %s", name); 138 warn("mkdtemp failed on %s", name);
129 } else { 139 } else {
130 (void)printf("%s\n", name); 140 (void)printf("%s\n", name);
131 if (uflag) 141 if (uflag)
132 (void)rmdir(name); 142 (void)rmdir(name);
@@ -145,17 +155,17 @@ main(int argc, char **argv) @@ -145,17 +155,17 @@ main(int argc, char **argv)
145 } 155 }
146 } 156 }
147 if (name) 157 if (name)
148 free(name); 158 free(name);
149 name = NULL; 159 name = NULL;
150 } 160 }
151 return ret; 161 return ret;
152} 162}
153 163
154static void 164static void
155usage(void) 165usage(void)
156{ 166{
157 (void)fprintf(stderr, 167 (void)fprintf(stderr,
158 "Usage: %s [-dqu] {-t prefix | template ...}\n", 168 "Usage: %s [-dqu] [-p <tmpdir>] {-t prefix | template ...}\n",
159 getprogname()); 169 getprogname());
160 exit (1); 170 exit (1);
161} 171}