Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.9
1.9 ! millert 1: /* $OpenBSD: mktemp.c,v 1.8 2002/02/16 21:27:49 millert Exp $ */
1.1 millert 2:
3: /*
1.6 millert 4: * Copyright (c) 1996, 1997, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.5 millert 15: * 3. The name of the author may not be used to endorse or promote products
1.1 millert 16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #ifndef lint
1.9 ! millert 31: static const char rcsid[] = "$OpenBSD: mktemp.c,v 1.8 2002/02/16 21:27:49 millert Exp $";
1.1 millert 32: #endif /* not lint */
33:
1.6 millert 34: #include <paths.h>
1.1 millert 35: #include <stdio.h>
36: #include <stdlib.h>
37: #include <string.h>
38: #include <unistd.h>
39: #include <err.h>
40:
1.8 millert 41: __dead void usage(void);
1.1 millert 42:
43: int
44: main(argc, argv)
45: int argc;
46: char **argv;
47: {
1.7 millert 48: int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
49: char *cp, *template, *tempfile, *prefix = _PATH_TMP;
1.9 ! millert 50: int plen;
1.1 millert 51:
1.6 millert 52: while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
53: switch(ch) {
1.3 millert 54: case 'd':
55: makedir = 1;
1.1 millert 56: break;
1.6 millert 57: case 'p':
58: prefix = optarg;
59: tflag = 1;
60: break;
1.1 millert 61: case 'q':
1.7 millert 62: quiet = 1;
1.1 millert 63: break;
1.6 millert 64: case 't':
65: tflag = 1;
66: break;
1.3 millert 67: case 'u':
68: uflag = 1;
69: break;
1.1 millert 70: default:
71: usage();
72: }
73:
1.7 millert 74: /* If no template specified use a default one (implies -t mode) */
75: switch (argc - optind) {
76: case 1:
77: template = argv[optind];
78: break;
79: case 0:
80: template = "tmp.XXXXXXXXXX";
81: tflag = 1;
82: break;
83: default:
1.1 millert 84: usage();
1.7 millert 85: }
1.1 millert 86:
1.6 millert 87: if (tflag) {
1.7 millert 88: if (strchr(template, '/')) {
89: if (!quiet)
1.9 ! millert 90: warnx("template must not contain directory "
! 91: "separators in -t mode");
1.7 millert 92: exit(1);
1.6 millert 93: }
94:
95: cp = getenv("TMPDIR");
96: if (cp != NULL && *cp != '\0')
97: prefix = cp;
98: plen = strlen(prefix);
99: while (plen != 0 && prefix[plen - 1] == '/')
100: plen--;
101:
1.9 ! millert 102: tempfile = NULL;
! 103: asprintf(&tempfile, "%.*s/%s", plen, prefix, template);
! 104: } else
! 105: tempfile = strdup(template);
! 106: if (tempfile == NULL) {
! 107: if (!quiet)
! 108: warnx("cannot allocate memory");
! 109: exit(1);
1.1 millert 110: }
111:
1.3 millert 112: if (makedir) {
1.7 millert 113: if (mkdtemp(tempfile) == NULL) {
114: if (!quiet)
115: warn("cannot make temp dir %s", tempfile);
116: exit(1);
1.3 millert 117: }
118:
119: if (uflag)
1.7 millert 120: (void)rmdir(tempfile);
1.3 millert 121: } else {
1.7 millert 122: if ((fd = mkstemp(tempfile)) < 0) {
123: if (!quiet)
124: warn("cannot make temp file %s", tempfile);
125: exit(1);
1.3 millert 126: }
1.7 millert 127: (void)close(fd);
1.3 millert 128:
129: if (uflag)
1.7 millert 130: (void)unlink(tempfile);
1.1 millert 131: }
132:
1.7 millert 133: (void)puts(tempfile);
134: free(tempfile);
1.1 millert 135:
136: exit(0);
1.7 millert 137: }
138:
139: __dead void
140: usage()
141: {
142: extern char *__progname;
143:
144: (void)fprintf(stderr,
145: "Usage: %s [-dqtu] [-p prefix] [template]\n", __progname);
146: exit(1);
1.1 millert 147: }