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