Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.14
1.14 ! sobrado 1: /* $OpenBSD: mktemp.c,v 1.13 2003/06/17 21:56:25 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: *
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:
19: #ifndef lint
1.14 ! sobrado 20: static const char rcsid[] = "$OpenBSD: mktemp.c,v 1.13 2003/06/17 21:56:25 millert Exp $";
1.1 millert 21: #endif /* not lint */
22:
1.6 millert 23: #include <paths.h>
1.1 millert 24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28: #include <err.h>
29:
1.8 millert 30: __dead void usage(void);
1.1 millert 31:
32: int
1.12 deraadt 33: main(int argc, char *argv[])
1.1 millert 34: {
1.7 millert 35: int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
36: char *cp, *template, *tempfile, *prefix = _PATH_TMP;
1.9 millert 37: int plen;
1.1 millert 38:
1.6 millert 39: while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
40: switch(ch) {
1.3 millert 41: case 'd':
42: makedir = 1;
1.1 millert 43: break;
1.6 millert 44: case 'p':
45: prefix = optarg;
46: tflag = 1;
47: break;
1.1 millert 48: case 'q':
1.7 millert 49: quiet = 1;
1.1 millert 50: break;
1.6 millert 51: case 't':
52: tflag = 1;
53: break;
1.3 millert 54: case 'u':
55: uflag = 1;
56: break;
1.1 millert 57: default:
58: usage();
59: }
60:
1.7 millert 61: /* If no template specified use a default one (implies -t mode) */
62: switch (argc - optind) {
63: case 1:
64: template = argv[optind];
65: break;
66: case 0:
67: template = "tmp.XXXXXXXXXX";
68: tflag = 1;
69: break;
70: default:
1.1 millert 71: usage();
1.7 millert 72: }
1.1 millert 73:
1.6 millert 74: if (tflag) {
1.7 millert 75: if (strchr(template, '/')) {
76: if (!quiet)
1.9 millert 77: warnx("template must not contain directory "
78: "separators in -t mode");
1.7 millert 79: exit(1);
1.6 millert 80: }
81:
82: cp = getenv("TMPDIR");
83: if (cp != NULL && *cp != '\0')
84: prefix = cp;
85: plen = strlen(prefix);
86: while (plen != 0 && prefix[plen - 1] == '/')
87: plen--;
88:
1.10 millert 89: if (asprintf(&tempfile, "%.*s/%s", plen, prefix, template) < 0)
90: tempfile = NULL;
1.9 millert 91: } else
92: tempfile = strdup(template);
93: if (tempfile == NULL) {
94: if (!quiet)
95: warnx("cannot allocate memory");
96: exit(1);
1.1 millert 97: }
98:
1.3 millert 99: if (makedir) {
1.7 millert 100: if (mkdtemp(tempfile) == NULL) {
101: if (!quiet)
102: warn("cannot make temp dir %s", tempfile);
103: exit(1);
1.3 millert 104: }
105:
106: if (uflag)
1.7 millert 107: (void)rmdir(tempfile);
1.3 millert 108: } else {
1.7 millert 109: if ((fd = mkstemp(tempfile)) < 0) {
110: if (!quiet)
111: warn("cannot make temp file %s", tempfile);
112: exit(1);
1.3 millert 113: }
1.7 millert 114: (void)close(fd);
1.3 millert 115:
116: if (uflag)
1.7 millert 117: (void)unlink(tempfile);
1.1 millert 118: }
119:
1.7 millert 120: (void)puts(tempfile);
121: free(tempfile);
1.1 millert 122:
123: exit(0);
1.7 millert 124: }
125:
126: __dead void
1.12 deraadt 127: usage(void)
1.7 millert 128: {
129: extern char *__progname;
130:
131: (void)fprintf(stderr,
1.14 ! sobrado 132: "usage: %s [-dqtu] [-p directory] [template]\n", __progname);
1.7 millert 133: exit(1);
1.1 millert 134: }