Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: mktemp.c,v 1.3 1997/06/17 15:34:29 millert Exp $ */
1.1 millert 2:
3: /*
4: * Copyright (c) 1996 Todd C. Miller <Todd.Miller@courtesan.com>
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.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by Todd C. Miller.
18: * 4. The name of the author may not be used to endorse or promote products
19: * derived from this software without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31: */
32:
33: #ifndef lint
1.4 ! millert 34: static char rcsid[] = "$OpenBSD: mktemp.c,v 1.3 1997/06/17 15:34:29 millert Exp $";
1.1 millert 35: #endif /* not lint */
36:
37: #include <stdio.h>
38: #include <stdlib.h>
39: #include <string.h>
40: #include <unistd.h>
41: #include <err.h>
42:
43: extern char *__progname;
44:
45: void
46: usage()
47: {
1.3 millert 48: (void) fprintf(stderr, "Usage: %s [-d] [-q] [-u] template\n",
49: __progname);
1.1 millert 50: exit(1);
51: }
52:
53: int
54: main(argc, argv)
55: int argc;
56: char **argv;
57: {
58: char *template;
1.3 millert 59: int c, uflag = 0, qflag = 0, makedir = 0;
1.1 millert 60:
1.3 millert 61: while ((c = getopt(argc, argv, "dqu")) != -1)
62: switch(c) {
63: case 'd':
64: makedir = 1;
1.1 millert 65: break;
66: case 'q':
67: qflag = 1;
68: break;
1.3 millert 69: case 'u':
70: uflag = 1;
71: break;
1.1 millert 72: case '?':
73: default:
74: usage();
75: }
76:
77: if (argc - optind != 1)
78: usage();
79:
80: if ((template = strdup(argv[optind])) == NULL) {
81: if (qflag)
82: exit(1);
83: else
84: errx(1, "Cannot allocate memory");
85: }
86:
1.3 millert 87: if (makedir) {
1.4 ! millert 88: if (mkdtemp(template) == NULL) {
1.3 millert 89: if (qflag)
90: exit(1);
91: else
92: err(1, "Cannot make temp dir %s", template);
93: }
94:
95: if (uflag)
96: (void) rmdir(template);
97: } else {
98: if (mkstemp(template) < 0) {
99: if (qflag)
100: exit(1);
101: else
102: err(1, "Cannot create temp file %s", template);
103: }
104:
105: if (uflag)
106: (void) unlink(template);
1.1 millert 107: }
108:
109: (void) puts(template);
110: free(template);
111:
112: exit(0);
113: }