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