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