Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.11
1.11 ! millert 1: /* $OpenBSD: mktemp.c,v 1.10 2003/04/25 20:02:02 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.11 ! millert 10: * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
! 11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
! 12: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
! 13: * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
! 15: * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
! 16: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 millert 17: */
18:
19: #ifndef lint
1.11 ! millert 20: static const char rcsid[] = "$OpenBSD: mktemp.c,v 1.10 2003/04/25 20:02:02 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
33: main(argc, argv)
34: int argc;
35: char **argv;
36: {
1.7 millert 37: int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
38: char *cp, *template, *tempfile, *prefix = _PATH_TMP;
1.9 millert 39: int plen;
1.1 millert 40:
1.6 millert 41: while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
42: switch(ch) {
1.3 millert 43: case 'd':
44: makedir = 1;
1.1 millert 45: break;
1.6 millert 46: case 'p':
47: prefix = optarg;
48: tflag = 1;
49: break;
1.1 millert 50: case 'q':
1.7 millert 51: quiet = 1;
1.1 millert 52: break;
1.6 millert 53: case 't':
54: tflag = 1;
55: break;
1.3 millert 56: case 'u':
57: uflag = 1;
58: break;
1.1 millert 59: default:
60: usage();
61: }
62:
1.7 millert 63: /* If no template specified use a default one (implies -t mode) */
64: switch (argc - optind) {
65: case 1:
66: template = argv[optind];
67: break;
68: case 0:
69: template = "tmp.XXXXXXXXXX";
70: tflag = 1;
71: break;
72: default:
1.1 millert 73: usage();
1.7 millert 74: }
1.1 millert 75:
1.6 millert 76: if (tflag) {
1.7 millert 77: if (strchr(template, '/')) {
78: if (!quiet)
1.9 millert 79: warnx("template must not contain directory "
80: "separators in -t mode");
1.7 millert 81: exit(1);
1.6 millert 82: }
83:
84: cp = getenv("TMPDIR");
85: if (cp != NULL && *cp != '\0')
86: prefix = cp;
87: plen = strlen(prefix);
88: while (plen != 0 && prefix[plen - 1] == '/')
89: plen--;
90:
1.10 millert 91: if (asprintf(&tempfile, "%.*s/%s", plen, prefix, template) < 0)
92: tempfile = NULL;
1.9 millert 93: } else
94: tempfile = strdup(template);
95: if (tempfile == NULL) {
96: if (!quiet)
97: warnx("cannot allocate memory");
98: exit(1);
1.1 millert 99: }
100:
1.3 millert 101: if (makedir) {
1.7 millert 102: if (mkdtemp(tempfile) == NULL) {
103: if (!quiet)
104: warn("cannot make temp dir %s", tempfile);
105: exit(1);
1.3 millert 106: }
107:
108: if (uflag)
1.7 millert 109: (void)rmdir(tempfile);
1.3 millert 110: } else {
1.7 millert 111: if ((fd = mkstemp(tempfile)) < 0) {
112: if (!quiet)
113: warn("cannot make temp file %s", tempfile);
114: exit(1);
1.3 millert 115: }
1.7 millert 116: (void)close(fd);
1.3 millert 117:
118: if (uflag)
1.7 millert 119: (void)unlink(tempfile);
1.1 millert 120: }
121:
1.7 millert 122: (void)puts(tempfile);
123: free(tempfile);
1.1 millert 124:
125: exit(0);
1.7 millert 126: }
127:
128: __dead void
129: usage()
130: {
131: extern char *__progname;
132:
133: (void)fprintf(stderr,
134: "Usage: %s [-dqtu] [-p prefix] [template]\n", __progname);
135: exit(1);
1.1 millert 136: }