Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: mktemp.c,v 1.5 1998/06/21 22:14:00 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.6 ! millert 31: static const char rcsid[] = "$OpenBSD: mktemp.c,v 1.5 1998/06/21 22:14:00 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:
41: void
42: usage()
43: {
1.6 ! millert 44: extern char *__progname;
! 45:
! 46: (void) fprintf(stderr,
! 47: "Usage: %s [-dqtu] [-p prefix] template\n", __progname);
1.1 millert 48: exit(1);
49: }
50:
51: int
52: main(argc, argv)
53: int argc;
54: char **argv;
55: {
1.6 ! millert 56: int ch, uflag = 0, qflag = 0, tflag = 0, makedir = 0;
! 57: char *cp, *template, *prefix = _PATH_TMP;
! 58: size_t plen;
1.1 millert 59:
1.6 ! millert 60: while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
! 61: switch(ch) {
1.3 millert 62: case 'd':
63: makedir = 1;
1.1 millert 64: break;
1.6 ! millert 65: case 'p':
! 66: prefix = optarg;
! 67: tflag = 1;
! 68: break;
1.1 millert 69: case 'q':
70: qflag = 1;
71: break;
1.6 ! millert 72: case 't':
! 73: tflag = 1;
! 74: break;
1.3 millert 75: case 'u':
76: uflag = 1;
77: break;
1.1 millert 78: default:
79: usage();
80: }
81:
82: if (argc - optind != 1)
83: usage();
84:
1.6 ! millert 85: if (tflag) {
! 86: if (strchr(argv[optind], '/')) {
! 87: if (qflag)
! 88: exit(1);
! 89: else
! 90: errx(1, "template must not contain directory separators in -t mode");
! 91: }
! 92:
! 93: cp = getenv("TMPDIR");
! 94: if (cp != NULL && *cp != '\0')
! 95: prefix = cp;
! 96: plen = strlen(prefix);
! 97: while (plen != 0 && prefix[plen - 1] == '/')
! 98: plen--;
! 99:
! 100: template = (char *)malloc(plen + 1 + strlen(argv[optind]) + 1);
! 101: if (template == NULL) {
! 102: if (qflag)
! 103: exit(1);
! 104: else
! 105: errx(1, "Cannot allocate memory");
! 106: }
! 107: memcpy(template, prefix, plen);
! 108: template[plen] = '/';
! 109: strcpy(template + plen + 1, argv[optind]); /* SAFE */
! 110: } else {
! 111: if ((template = strdup(argv[optind])) == NULL) {
! 112: if (qflag)
! 113: exit(1);
! 114: else
! 115: errx(1, "Cannot allocate memory");
! 116: }
1.1 millert 117: }
118:
1.3 millert 119: if (makedir) {
1.4 millert 120: if (mkdtemp(template) == NULL) {
1.3 millert 121: if (qflag)
122: exit(1);
123: else
124: err(1, "Cannot make temp dir %s", template);
125: }
126:
127: if (uflag)
128: (void) rmdir(template);
129: } else {
130: if (mkstemp(template) < 0) {
131: if (qflag)
132: exit(1);
133: else
134: err(1, "Cannot create temp file %s", template);
135: }
136:
137: if (uflag)
138: (void) unlink(template);
1.1 millert 139: }
140:
141: (void) puts(template);
142: free(template);
143:
144: exit(0);
145: }