1 /* $NetBSD: mktemp.c,v 1.10 2007/12/15 19:44:52 perry Exp $ */ 2 3 /*- 4 * Copyright (c) 1994, 1995, 1996, 1998 Peter Wemm <peter@netplex.com.au> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 /* 31 * This program was originally written long ago, originally for a non 32 * BSD-like OS without mkstemp(). It's been modified over the years 33 * to use mkstemp() rather than the original O_CREAT|O_EXCL/fstat/lstat 34 * etc style hacks. 35 * A cleanup, misc options and mkdtemp() calls were added to try and work 36 * more like the OpenBSD version - which was first to publish the interface. 37 */ 38 39 #if HAVE_NBTOOL_CONFIG_H 40 #include "nbtool_config.h" 41 #endif 42 43 #include <sys/cdefs.h> 44 #include <sys/types.h> 45 #include <err.h> 46 #include <paths.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #if defined(__RCSID) && !defined(__lint) 53 __RCSID("$NetBSD: mktemp.c,v 1.10 2007/12/15 19:44:52 perry Exp $"); 54 #endif /* !__lint */ 55 56 static void usage(void) __dead; 57 58 int 59 main(int argc, char **argv) 60 { 61 int c, fd, ret; 62 char *tmpdir; 63 const char *prefix; 64 char *name; 65 int dflag, qflag, tflag, uflag; 66 67 setprogname(*argv); 68 ret = dflag = qflag = tflag = uflag = 0; 69 prefix = "mktemp"; 70 name = NULL; 71 72 while ((c = getopt(argc, argv, "dqt:u")) != -1) 73 switch (c) { 74 case 'd': 75 dflag++; 76 break; 77 78 case 'q': 79 qflag++; 80 break; 81 82 case 't': 83 prefix = optarg; 84 tflag++; 85 break; 86 87 case 'u': 88 uflag++; 89 break; 90 91 default: 92 usage(); 93 } 94 95 argc -= optind; 96 argv += optind; 97 98 if (tflag) { 99 tmpdir = getenv("TMPDIR"); 100 if (tmpdir == NULL) 101 (void)asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP, 102 prefix); 103 else 104 (void)asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix); 105 /* if this fails, the program is in big trouble already */ 106 if (name == NULL) { 107 if (qflag) 108 return 1; 109 else 110 errx(1, "Cannot generate template"); 111 } 112 } else if (argc < 1) { 113 usage(); 114 } 115 116 /* generate all requested files */ 117 while (name != NULL || argc > 0) { 118 if (name == NULL) { 119 name = strdup(argv[0]); 120 argv++; 121 argc--; 122 } 123 124 if (dflag) { 125 if (mkdtemp(name) == NULL) { 126 ret = 1; 127 if (!qflag) 128 warn("mkdtemp failed on %s", name); 129 } else { 130 (void)printf("%s\n", name); 131 if (uflag) 132 (void)rmdir(name); 133 } 134 } else { 135 fd = mkstemp(name); 136 if (fd < 0) { 137 ret = 1; 138 if (!qflag) 139 warn("mkstemp failed on %s", name); 140 } else { 141 (void)close(fd); 142 if (uflag) 143 (void)unlink(name); 144 (void)printf("%s\n", name); 145 } 146 } 147 if (name) 148 free(name); 149 name = NULL; 150 } 151 return ret; 152 } 153 154 static void 155 usage(void) 156 { 157 (void)fprintf(stderr, 158 "Usage: %s [-dqu] {-t prefix | template ...}\n", 159 getprogname()); 160 exit (1); 161 } 162