10Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 20Sstevel@tonic-gate static const char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93"; 3*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: mktemp.c,v 1.2 2005/04/27 04:56:11 sra Exp $"; 40Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate /* 70Sstevel@tonic-gate * Copyright (c) 1987, 1993 80Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 90Sstevel@tonic-gate * 100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 110Sstevel@tonic-gate * modification, are permitted provided that the following conditions 120Sstevel@tonic-gate * are met: 130Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 170Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 180Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 190Sstevel@tonic-gate * must display the following acknowledgement: 200Sstevel@tonic-gate * This product includes software developed by the University of 210Sstevel@tonic-gate * California, Berkeley and its contributors. 220Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 230Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 240Sstevel@tonic-gate * without specific prior written permission. 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 270Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 280Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 290Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 300Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 310Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 320Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 330Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 340Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 350Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 360Sstevel@tonic-gate * SUCH DAMAGE. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation. 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 430Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 440Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that 450Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or 460Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without 470Sstevel@tonic-gate * specific, written prior permission. 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 500Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 510Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 520Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 530Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 540Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 550Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 560Sstevel@tonic-gate * SOFTWARE. 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include "port_before.h" 600Sstevel@tonic-gate 610Sstevel@tonic-gate #include <sys/types.h> 620Sstevel@tonic-gate #include <sys/stat.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate #include <ctype.h> 650Sstevel@tonic-gate #include <errno.h> 660Sstevel@tonic-gate #include <fcntl.h> 670Sstevel@tonic-gate #include <stdio.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate #include "port_after.h" 700Sstevel@tonic-gate 710Sstevel@tonic-gate #if (!defined(NEED_MKTEMP)) && (!defined(NEED_MKSTEMP)) 720Sstevel@tonic-gate int __mktemp_unneeded__; 730Sstevel@tonic-gate #else 740Sstevel@tonic-gate 750Sstevel@tonic-gate static int gettemp(char *path, int *doopen); 760Sstevel@tonic-gate 770Sstevel@tonic-gate #ifdef NEED_MKSTEMP 780Sstevel@tonic-gate mkstemp(char *path) { 790Sstevel@tonic-gate int fd; 800Sstevel@tonic-gate 810Sstevel@tonic-gate return (gettemp(path, &fd) ? fd : -1); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate #endif 840Sstevel@tonic-gate 850Sstevel@tonic-gate #ifdef NEED_MKTEMP 860Sstevel@tonic-gate char * 870Sstevel@tonic-gate mktemp(char *path) { 880Sstevel@tonic-gate return(gettemp(path, (int *)NULL) ? path : (char *)NULL); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate #endif 910Sstevel@tonic-gate 920Sstevel@tonic-gate static int 930Sstevel@tonic-gate gettemp(char *path, int *doopen) { 940Sstevel@tonic-gate char *start, *trv; 950Sstevel@tonic-gate struct stat sbuf; 960Sstevel@tonic-gate u_int pid; 970Sstevel@tonic-gate 980Sstevel@tonic-gate pid = getpid(); 99*11038SRao.Shoaib@Sun.COM for (trv = path; *trv; ++trv); /*%< extra X's get set to 0's */ 1000Sstevel@tonic-gate while (*--trv == 'X') { 1010Sstevel@tonic-gate *trv = (pid % 10) + '0'; 1020Sstevel@tonic-gate pid /= 10; 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* 1060Sstevel@tonic-gate * check the target directory; if you have six X's and it 1070Sstevel@tonic-gate * doesn't exist this runs for a *very* long time. 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate for (start = trv + 1;; --trv) { 1100Sstevel@tonic-gate if (trv <= path) 1110Sstevel@tonic-gate break; 1120Sstevel@tonic-gate if (*trv == '/') { 1130Sstevel@tonic-gate *trv = '\0'; 1140Sstevel@tonic-gate if (stat(path, &sbuf)) 1150Sstevel@tonic-gate return(0); 1160Sstevel@tonic-gate if (!S_ISDIR(sbuf.st_mode)) { 1170Sstevel@tonic-gate errno = ENOTDIR; 1180Sstevel@tonic-gate return(0); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate *trv = '/'; 1210Sstevel@tonic-gate break; 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate for (;;) { 1260Sstevel@tonic-gate if (doopen) { 1270Sstevel@tonic-gate if ((*doopen = 1280Sstevel@tonic-gate open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) 1290Sstevel@tonic-gate return(1); 1300Sstevel@tonic-gate if (errno != EEXIST) 1310Sstevel@tonic-gate return(0); 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate else if (stat(path, &sbuf)) 1340Sstevel@tonic-gate return(errno == ENOENT ? 1 : 0); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* tricky little algorithm for backward compatibility */ 1370Sstevel@tonic-gate for (trv = start;;) { 1380Sstevel@tonic-gate if (!*trv) 1390Sstevel@tonic-gate return(0); 1400Sstevel@tonic-gate if (*trv == 'z') 1410Sstevel@tonic-gate *trv++ = 'a'; 1420Sstevel@tonic-gate else { 1430Sstevel@tonic-gate if (isdigit(*trv)) 1440Sstevel@tonic-gate *trv = 'a'; 1450Sstevel@tonic-gate else 1460Sstevel@tonic-gate ++*trv; 1470Sstevel@tonic-gate break; 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate /*NOTREACHED*/ 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate #endif /*NEED_MKTEMP*/ 155*11038SRao.Shoaib@Sun.COM 156*11038SRao.Shoaib@Sun.COM /*! \file */ 157