1 /* $NetBSD: mktemp.c,v 1.7 2005/07/30 16:19:09 christos 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/types.h> 44 #include <err.h> 45 #include <paths.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #if defined(__RCSID) && !defined(__lint) 52 __RCSID("$NetBSD: mktemp.c,v 1.7 2005/07/30 16:19:09 christos Exp $"); 53 #endif /* !__lint */ 54 55 static void usage(void) __attribute__((__noreturn__)); 56 57 int 58 main(int argc, char **argv) 59 { 60 int c, fd, ret; 61 char *tmpdir; 62 const char *prefix; 63 char *name; 64 int dflag, qflag, tflag, uflag; 65 66 setprogname(*argv); 67 ret = dflag = qflag = tflag = uflag = 0; 68 prefix = "mktemp"; 69 name = NULL; 70 71 while ((c = getopt(argc, argv, "dqt:u")) != -1) 72 switch (c) { 73 case 'd': 74 dflag++; 75 break; 76 77 case 'q': 78 qflag++; 79 break; 80 81 case 't': 82 prefix = optarg; 83 tflag++; 84 break; 85 86 case 'u': 87 uflag++; 88 break; 89 90 default: 91 usage(); 92 } 93 94 argc -= optind; 95 argv += optind; 96 97 if (tflag) { 98 tmpdir = getenv("TMPDIR"); 99 if (tmpdir == NULL) 100 (void)asprintf(&name, "%s%s.XXXXXXXX", _PATH_TMP, 101 prefix); 102 else 103 (void)asprintf(&name, "%s/%s.XXXXXXXX", tmpdir, prefix); 104 /* if this fails, the program is in big trouble already */ 105 if (name == NULL) { 106 if (qflag) 107 return 1; 108 else 109 errx(1, "Cannot generate template"); 110 } 111 } else if (argc < 1) { 112 usage(); 113 } 114 115 /* generate all requested files */ 116 while (name != NULL || argc > 0) { 117 if (name == NULL) { 118 name = strdup(argv[0]); 119 argv++; 120 argc--; 121 } 122 123 if (dflag) { 124 if (mkdtemp(name) == NULL) { 125 ret = 1; 126 if (!qflag) 127 warn("mkdtemp failed on %s", name); 128 } else { 129 (void)printf("%s\n", name); 130 if (uflag) 131 (void)rmdir(name); 132 } 133 } else { 134 fd = mkstemp(name); 135 if (fd < 0) { 136 ret = 1; 137 if (!qflag) 138 warn("mkstemp failed on %s", name); 139 } else { 140 (void)close(fd); 141 if (uflag) 142 (void)unlink(name); 143 (void)printf("%s\n", name); 144 } 145 } 146 if (name) 147 free(name); 148 name = NULL; 149 } 150 return ret; 151 } 152 153 static void 154 usage(void) 155 { 156 (void)fprintf(stderr, 157 "Usage: %s [-d] [-q] [-t prefix] [-u] [template ...]\n", 158 getprogname()); 159 exit (1); 160 } 161