]>
Commit | Line | Data |
---|---|---|
c0fcf4e1 A |
1 | /*- |
2 | * Copyright (c) 1994, 1995, 1996, 1998 Peter Wemm <peter@netplex.com.au> | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
24 | * SUCH DAMAGE. | |
25 | * | |
26 | */ | |
27 | ||
28 | /* | |
29 | * This program was originally written long ago, originally for a non | |
30 | * BSD-like OS without mkstemp(). It's been modified over the years | |
31 | * to use mkstemp() rather than the original O_CREAT|O_EXCL/fstat/lstat | |
32 | * etc style hacks. | |
33 | * A cleanup, misc options and mkdtemp() calls were added to try and work | |
34 | * more like the OpenBSD version - which was first to publish the interface. | |
35 | */ | |
36 | ||
37 | #include <err.h> | |
b5fe885e A |
38 | #ifdef __APPLE__ |
39 | #include <limits.h> | |
40 | #endif | |
c0fcf4e1 A |
41 | #include <paths.h> |
42 | #include <stdio.h> | |
43 | #include <stdlib.h> | |
44 | #include <string.h> | |
45 | #include <unistd.h> | |
46 | ||
47 | #ifndef lint | |
48 | static const char rcsid[] = | |
b5fe885e | 49 | "$FreeBSD$"; |
c0fcf4e1 A |
50 | #endif /* not lint */ |
51 | ||
ddb4a88b | 52 | static void usage(void); |
c0fcf4e1 A |
53 | |
54 | int | |
55 | main(int argc, char **argv) | |
56 | { | |
57 | int c, fd, ret; | |
ddb4a88b A |
58 | char *tmpdir; |
59 | const char *prefix; | |
c0fcf4e1 A |
60 | char *name; |
61 | int dflag, qflag, tflag, uflag; | |
b5fe885e A |
62 | #ifdef __APPLE__ |
63 | char tmpbuf[PATH_MAX]; | |
64 | size_t len; | |
65 | #endif | |
c0fcf4e1 A |
66 | |
67 | ret = dflag = qflag = tflag = uflag = 0; | |
68 | prefix = "mktemp"; | |
69 | name = NULL; | |
70 | ||
71 | while ((c = getopt(argc, argv, "dqt:u")) != -1) | |
72 | switch (c) { | |
73 | case 'd': | |
74 | dflag++; | |
75 | break; | |
76 | ||
77 | case 'q': | |
78 | qflag++; | |
79 | break; | |
80 | ||
81 | case 't': | |
82 | prefix = optarg; | |
83 | tflag++; | |
84 | break; | |
85 | ||
86 | case 'u': | |
87 | uflag++; | |
88 | break; | |
89 | ||
90 | default: | |
91 | usage(); | |
92 | } | |
93 | ||
94 | argc -= optind; | |
95 | argv += optind; | |
96 | ||
b5fe885e A |
97 | if (!tflag && argc < 1) { |
98 | tflag = 1; | |
99 | prefix = "tmp"; | |
100 | } | |
101 | ||
c0fcf4e1 | 102 | if (tflag) { |
b5fe885e A |
103 | #ifdef __APPLE__ |
104 | if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmpbuf, sizeof(tmpbuf)) > 0) { | |
105 | tmpdir = tmpbuf; | |
106 | } else { | |
107 | tmpdir = getenv("TMPDIR"); | |
108 | } | |
109 | ||
110 | if (tmpdir == NULL) { | |
111 | tmpdir = _PATH_TMP; | |
112 | } | |
113 | ||
114 | len = strlen(tmpdir); | |
115 | if (len > 0 && tmpdir[len - 1] == '/') { | |
116 | asprintf(&name, "%s%s.XXXXXXXX", tmpdir, prefix); | |
117 | } else { | |
118 | asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix); | |
119 | } | |
120 | #else | |
c0fcf4e1 A |
121 | tmpdir = getenv("TMPDIR"); |
122 | if (tmpdir == NULL) | |
123 | asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP, prefix); | |
b5fe885e A |
124 | else |
125 | asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix); | |
126 | #endif | |
c0fcf4e1 A |
127 | /* if this fails, the program is in big trouble already */ |
128 | if (name == NULL) { | |
129 | if (qflag) | |
130 | return (1); | |
131 | else | |
132 | errx(1, "cannot generate template"); | |
133 | } | |
c0fcf4e1 A |
134 | } |
135 | ||
136 | /* generate all requested files */ | |
137 | while (name != NULL || argc > 0) { | |
138 | if (name == NULL) { | |
139 | name = strdup(argv[0]); | |
140 | argv++; | |
141 | argc--; | |
142 | } | |
ddb4a88b | 143 | |
c0fcf4e1 A |
144 | if (dflag) { |
145 | if (mkdtemp(name) == NULL) { | |
146 | ret = 1; | |
147 | if (!qflag) | |
148 | warn("mkdtemp failed on %s", name); | |
149 | } else { | |
150 | printf("%s\n", name); | |
151 | if (uflag) | |
152 | rmdir(name); | |
153 | } | |
154 | } else { | |
155 | fd = mkstemp(name); | |
156 | if (fd < 0) { | |
157 | ret = 1; | |
158 | if (!qflag) | |
159 | warn("mkstemp failed on %s", name); | |
160 | } else { | |
161 | close(fd); | |
162 | if (uflag) | |
163 | unlink(name); | |
164 | printf("%s\n", name); | |
165 | } | |
166 | } | |
167 | if (name) | |
168 | free(name); | |
169 | name = NULL; | |
170 | } | |
171 | return (ret); | |
172 | } | |
173 | ||
174 | static void | |
b5fe885e | 175 | usage(void) |
c0fcf4e1 A |
176 | { |
177 | fprintf(stderr, | |
178 | "usage: mktemp [-d] [-q] [-t prefix] [-u] template ...\n"); | |
179 | fprintf(stderr, | |
180 | " mktemp [-d] [-q] [-u] -t prefix \n"); | |
181 | exit (1); | |
182 | } |