1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 1997-2000 by Sun Microsystems, Inc. 3*7c478bd9Sstevel@tonic-gate * All rights reserved. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 7*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93"; 8*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: mktemp.c,v 8.4 1999/10/13 16:39:21 vixie Exp $"; 9*7c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 10*7c478bd9Sstevel@tonic-gate 11*7c478bd9Sstevel@tonic-gate /* 12*7c478bd9Sstevel@tonic-gate * Copyright (c) 1987, 1993 13*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 14*7c478bd9Sstevel@tonic-gate * 15*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 16*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 17*7c478bd9Sstevel@tonic-gate * are met: 18*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 19*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 20*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 21*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 22*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 23*7c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 24*7c478bd9Sstevel@tonic-gate * must display the following acknowledgement: 25*7c478bd9Sstevel@tonic-gate * This product includes software developed by the University of 26*7c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors. 27*7c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 28*7c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 29*7c478bd9Sstevel@tonic-gate * without specific prior written permission. 30*7c478bd9Sstevel@tonic-gate * 31*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32*7c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34*7c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35*7c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36*7c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37*7c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38*7c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39*7c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40*7c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41*7c478bd9Sstevel@tonic-gate * SUCH DAMAGE. 42*7c478bd9Sstevel@tonic-gate */ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate /* 45*7c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation. 46*7c478bd9Sstevel@tonic-gate * 47*7c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 48*7c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 49*7c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that 50*7c478bd9Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or 51*7c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without 52*7c478bd9Sstevel@tonic-gate * specific, written prior permission. 53*7c478bd9Sstevel@tonic-gate * 54*7c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 55*7c478bd9Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 56*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 57*7c478bd9Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 58*7c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 59*7c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 60*7c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 61*7c478bd9Sstevel@tonic-gate * SOFTWARE. 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate #include "port_before.h" 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 70*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate #include <ctype.h> 73*7c478bd9Sstevel@tonic-gate #include <errno.h> 74*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 75*7c478bd9Sstevel@tonic-gate #include <stdio.h> 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate #include "port_after.h" 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate #if (!defined(NEED_MKTEMP)) && (!defined(NEED_MKSTEMP)) 80*7c478bd9Sstevel@tonic-gate int __mktemp_unneeded__; 81*7c478bd9Sstevel@tonic-gate #else 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate static int gettemp(char *path, int *doopen); 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate #ifdef NEED_MKSTEMP 86*7c478bd9Sstevel@tonic-gate mkstemp(char *path) { 87*7c478bd9Sstevel@tonic-gate int fd; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate return (gettemp(path, &fd) ? fd : -1); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate #endif 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate #ifdef NEED_MKTEMP 94*7c478bd9Sstevel@tonic-gate char * 95*7c478bd9Sstevel@tonic-gate mktemp(char *path) { 96*7c478bd9Sstevel@tonic-gate return(gettemp(path, (int *)NULL) ? path : (char *)NULL); 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate #endif 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate static int 101*7c478bd9Sstevel@tonic-gate gettemp(char *path, int *doopen) { 102*7c478bd9Sstevel@tonic-gate char *start, *trv; 103*7c478bd9Sstevel@tonic-gate struct stat sbuf; 104*7c478bd9Sstevel@tonic-gate u_int pid; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate pid = getpid(); 107*7c478bd9Sstevel@tonic-gate for (trv = path; *trv; ++trv); /* extra X's get set to 0's */ 108*7c478bd9Sstevel@tonic-gate while (*--trv == 'X') { 109*7c478bd9Sstevel@tonic-gate *trv = (pid % 10) + '0'; 110*7c478bd9Sstevel@tonic-gate pid /= 10; 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * check the target directory; if you have six X's and it 115*7c478bd9Sstevel@tonic-gate * doesn't exist this runs for a *very* long time. 116*7c478bd9Sstevel@tonic-gate */ 117*7c478bd9Sstevel@tonic-gate for (start = trv + 1;; --trv) { 118*7c478bd9Sstevel@tonic-gate if (trv <= path) 119*7c478bd9Sstevel@tonic-gate break; 120*7c478bd9Sstevel@tonic-gate if (*trv == '/') { 121*7c478bd9Sstevel@tonic-gate *trv = '\0'; 122*7c478bd9Sstevel@tonic-gate if (stat(path, &sbuf)) 123*7c478bd9Sstevel@tonic-gate return(0); 124*7c478bd9Sstevel@tonic-gate if (!S_ISDIR(sbuf.st_mode)) { 125*7c478bd9Sstevel@tonic-gate errno = ENOTDIR; 126*7c478bd9Sstevel@tonic-gate return(0); 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate *trv = '/'; 129*7c478bd9Sstevel@tonic-gate break; 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate for (;;) { 134*7c478bd9Sstevel@tonic-gate if (doopen) { 135*7c478bd9Sstevel@tonic-gate if ((*doopen = 136*7c478bd9Sstevel@tonic-gate open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) 137*7c478bd9Sstevel@tonic-gate return(1); 138*7c478bd9Sstevel@tonic-gate if (errno != EEXIST) 139*7c478bd9Sstevel@tonic-gate return(0); 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate else if (stat(path, &sbuf)) 142*7c478bd9Sstevel@tonic-gate return(errno == ENOENT ? 1 : 0); 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate /* tricky little algorithm for backward compatibility */ 145*7c478bd9Sstevel@tonic-gate for (trv = start;;) { 146*7c478bd9Sstevel@tonic-gate if (!*trv) 147*7c478bd9Sstevel@tonic-gate return(0); 148*7c478bd9Sstevel@tonic-gate if (*trv == 'z') 149*7c478bd9Sstevel@tonic-gate *trv++ = 'a'; 150*7c478bd9Sstevel@tonic-gate else { 151*7c478bd9Sstevel@tonic-gate if (isdigit(*trv)) 152*7c478bd9Sstevel@tonic-gate *trv = 'a'; 153*7c478bd9Sstevel@tonic-gate else 154*7c478bd9Sstevel@tonic-gate ++*trv; 155*7c478bd9Sstevel@tonic-gate break; 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate #endif /*NEED_MKTEMP*/ 163