Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.17
1.17 ! millert 1: /* $OpenBSD: mktemp.c,v 1.16 2013/03/12 15:07:12 millert Exp $ */
1.1 millert 2:
3: /*
1.16 millert 4: * Copyright (c) 1996, 1997, 2001-2003, 2013
5: * Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 6: *
1.11 millert 7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 millert 10: *
1.13 millert 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 millert 18: */
19:
1.6 millert 20: #include <paths.h>
1.1 millert 21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24: #include <unistd.h>
25: #include <err.h>
26:
1.8 millert 27: __dead void usage(void);
1.1 millert 28:
29: int
1.12 deraadt 30: main(int argc, char *argv[])
1.1 millert 31: {
1.7 millert 32: int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
33: char *cp, *template, *tempfile, *prefix = _PATH_TMP;
1.16 millert 34: size_t len;
1.1 millert 35:
1.6 millert 36: while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
37: switch(ch) {
1.3 millert 38: case 'd':
39: makedir = 1;
1.1 millert 40: break;
1.6 millert 41: case 'p':
42: prefix = optarg;
43: tflag = 1;
44: break;
1.1 millert 45: case 'q':
1.7 millert 46: quiet = 1;
1.1 millert 47: break;
1.6 millert 48: case 't':
49: tflag = 1;
50: break;
1.3 millert 51: case 'u':
52: uflag = 1;
53: break;
1.1 millert 54: default:
55: usage();
56: }
57:
1.7 millert 58: /* If no template specified use a default one (implies -t mode) */
59: switch (argc - optind) {
60: case 1:
61: template = argv[optind];
62: break;
63: case 0:
64: template = "tmp.XXXXXXXXXX";
65: tflag = 1;
66: break;
67: default:
1.1 millert 68: usage();
1.7 millert 69: }
1.1 millert 70:
1.6 millert 71: if (tflag) {
1.7 millert 72: if (strchr(template, '/')) {
73: if (!quiet)
1.9 millert 74: warnx("template must not contain directory "
75: "separators in -t mode");
1.7 millert 76: exit(1);
1.6 millert 77: }
78:
79: cp = getenv("TMPDIR");
80: if (cp != NULL && *cp != '\0')
81: prefix = cp;
1.16 millert 82: len = strlen(prefix);
83: while (len != 0 && prefix[len - 1] == '/')
84: len--;
1.6 millert 85:
1.16 millert 86: if (asprintf(&tempfile, "%.*s/%s", (int)len, prefix, template) < 0)
1.10 millert 87: tempfile = NULL;
1.16 millert 88: } else {
89: len = strlen(template);
90: if (len < 6 || strcmp(&template[len - 6], "XXXXXX")) {
91: if (!quiet) {
1.17 ! millert 92: warnx("insufficient number of Xs in template `%s'",
1.16 millert 93: template);
94: }
95: exit(1);
96: }
1.9 millert 97: tempfile = strdup(template);
1.16 millert 98: }
1.9 millert 99: if (tempfile == NULL) {
100: if (!quiet)
101: warnx("cannot allocate memory");
102: exit(1);
1.1 millert 103: }
104:
1.3 millert 105: if (makedir) {
1.7 millert 106: if (mkdtemp(tempfile) == NULL) {
107: if (!quiet)
108: warn("cannot make temp dir %s", tempfile);
109: exit(1);
1.3 millert 110: }
111:
112: if (uflag)
1.7 millert 113: (void)rmdir(tempfile);
1.3 millert 114: } else {
1.7 millert 115: if ((fd = mkstemp(tempfile)) < 0) {
116: if (!quiet)
117: warn("cannot make temp file %s", tempfile);
118: exit(1);
1.3 millert 119: }
1.7 millert 120: (void)close(fd);
1.3 millert 121:
122: if (uflag)
1.7 millert 123: (void)unlink(tempfile);
1.1 millert 124: }
125:
1.7 millert 126: (void)puts(tempfile);
127: free(tempfile);
1.1 millert 128:
129: exit(0);
1.7 millert 130: }
131:
132: __dead void
1.12 deraadt 133: usage(void)
1.7 millert 134: {
135: extern char *__progname;
136:
137: (void)fprintf(stderr,
1.14 sobrado 138: "usage: %s [-dqtu] [-p directory] [template]\n", __progname);
1.7 millert 139: exit(1);
1.1 millert 140: }