Home | History | Annotate | Line # | Download | only in stdio
gettemp.c revision 1.15.2.2
      1  1.15.2.1       tls /*	$NetBSD: gettemp.c,v 1.15.2.2 2014/08/20 00:02:16 tls Exp $	*/
      2       1.1   mycroft 
      3       1.1   mycroft /*
      4       1.1   mycroft  * Copyright (c) 1987, 1993
      5       1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1   mycroft  *
      7       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8       1.1   mycroft  * modification, are permitted provided that the following conditions
      9       1.1   mycroft  * are met:
     10       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15      1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1   mycroft  *    may be used to endorse or promote products derived from this software
     17       1.1   mycroft  *    without specific prior written permission.
     18       1.1   mycroft  *
     19       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1   mycroft  * SUCH DAMAGE.
     30       1.1   mycroft  */
     31       1.1   mycroft 
     32  1.15.2.2       tls #include "gettemp.h"
     33       1.8   thorpej 
     34      1.12     lukem #if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
     35       1.8   thorpej 
     36       1.1   mycroft #include <sys/cdefs.h>
     37       1.1   mycroft #if defined(LIBC_SCCS) && !defined(lint)
     38       1.1   mycroft #if 0
     39       1.1   mycroft static char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
     40       1.1   mycroft #else
     41  1.15.2.1       tls __RCSID("$NetBSD: gettemp.c,v 1.15.2.2 2014/08/20 00:02:16 tls Exp $");
     42       1.1   mycroft #endif
     43       1.1   mycroft #endif /* LIBC_SCCS and not lint */
     44       1.1   mycroft 
     45  1.15.2.2       tls #include <sys/param.h>
     46       1.1   mycroft #include <fcntl.h>
     47  1.15.2.2       tls #include <string.h>
     48       1.9   thorpej 
     49  1.15.2.2       tls static const unsigned char padchar[] =
     50  1.15.2.2       tls "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     51       1.1   mycroft 
     52       1.1   mycroft int
     53  1.15.2.2       tls GETTEMP(char *path, int *doopen, int domkdir, int slen, int oflags)
     54       1.1   mycroft {
     55  1.15.2.2       tls 	char *start, *trv, *suffp, *carryp;
     56  1.15.2.2       tls 	char *pad;
     57       1.1   mycroft 	struct stat sbuf;
     58  1.15.2.2       tls 	int rval;
     59  1.15.2.2       tls 	uint32_t r;
     60  1.15.2.2       tls 	char carrybuf[MAXPATHLEN];
     61       1.4     lukem 
     62       1.4     lukem 	_DIAGASSERT(path != NULL);
     63       1.4     lukem 	/* doopen may be NULL */
     64  1.15.2.2       tls 	if ((doopen != NULL && domkdir) || slen < 0 ||
     65  1.15.2.2       tls 	    (oflags & ~(O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC |
     66  1.15.2.2       tls 	    O_CLOEXEC)) != 0) {
     67  1.15.2.2       tls 		errno = EINVAL;
     68  1.15.2.2       tls 		return 0;
     69  1.15.2.2       tls 	}
     70       1.1   mycroft 
     71  1.15.2.2       tls 	for (trv = path; *trv != '\0'; ++trv)
     72  1.15.2.2       tls 		continue;
     73       1.1   mycroft 
     74  1.15.2.2       tls 	if (trv - path >= MAXPATHLEN) {
     75  1.15.2.2       tls 		errno = ENAMETOOLONG;
     76  1.15.2.2       tls 		return 0;
     77       1.1   mycroft 	}
     78  1.15.2.2       tls 	trv -= slen;
     79  1.15.2.2       tls 	suffp = trv;
     80  1.15.2.2       tls 	--trv;
     81  1.15.2.2       tls 	if (trv < path || NULL != strchr(suffp, '/')) {
     82  1.15.2.2       tls 		errno = EINVAL;
     83  1.15.2.2       tls 		return 0;
     84  1.15.2.2       tls 	}
     85  1.15.2.2       tls 
     86  1.15.2.2       tls 	/* Fill space with random characters */
     87  1.15.2.2       tls 	while (trv >= path && *trv == 'X') {
     88  1.15.2.2       tls 		r = arc4random_uniform(sizeof(padchar) - 1);
     89  1.15.2.2       tls 		*trv-- = padchar[r];
     90  1.15.2.2       tls 	}
     91  1.15.2.2       tls 	start = trv + 1;
     92  1.15.2.2       tls 
     93  1.15.2.2       tls 	/* save first combination of random characters */
     94  1.15.2.2       tls 	memcpy(carrybuf, start, suffp - start);
     95       1.1   mycroft 
     96       1.1   mycroft 	/*
     97  1.15.2.2       tls 	 * check the target directory.
     98       1.1   mycroft 	 */
     99  1.15.2.2       tls 	if (doopen != NULL || domkdir) {
    100  1.15.2.2       tls 		for (; trv > path; --trv) {
    101  1.15.2.2       tls 			if (*trv == '/') {
    102  1.15.2.2       tls 				*trv = '\0';
    103  1.15.2.2       tls 				rval = stat(path, &sbuf);
    104  1.15.2.2       tls 				*trv = '/';
    105  1.15.2.2       tls 				if (rval != 0)
    106  1.15.2.2       tls 					return 0;
    107  1.15.2.2       tls 				if (!S_ISDIR(sbuf.st_mode)) {
    108  1.15.2.2       tls 					errno = ENOTDIR;
    109  1.15.2.2       tls 					return 0;
    110  1.15.2.2       tls 				}
    111  1.15.2.2       tls 				break;
    112       1.1   mycroft 			}
    113       1.1   mycroft 		}
    114       1.1   mycroft 	}
    115       1.1   mycroft 
    116       1.1   mycroft 	for (;;) {
    117       1.1   mycroft 		if (doopen) {
    118  1.15.2.2       tls 			if ((*doopen = open(path, O_CREAT|O_EXCL|O_RDWR|oflags,
    119  1.15.2.2       tls 			    0600)) != -1)
    120      1.15  christos 				return 1;
    121       1.1   mycroft 			if (errno != EEXIST)
    122      1.15  christos 				return 0;
    123       1.2   mycroft 		} else if (domkdir) {
    124  1.15.2.2       tls 			if (mkdir(path, 0700) != -1)
    125      1.15  christos 				return 1;
    126       1.2   mycroft 			if (errno != EEXIST)
    127      1.15  christos 				return 0;
    128       1.2   mycroft 		} else if (lstat(path, &sbuf))
    129  1.15.2.2       tls 			return errno == ENOENT;
    130       1.1   mycroft 
    131  1.15.2.2       tls 		/*
    132  1.15.2.2       tls 		 * If we have a collision,
    133  1.15.2.2       tls 		 * cycle through the space of filenames
    134  1.15.2.2       tls 		 */
    135  1.15.2.2       tls 		for (trv = start, carryp = carrybuf;;) {
    136  1.15.2.2       tls 			/* have we tried all possible permutations? */
    137  1.15.2.2       tls 			if (trv == suffp)
    138  1.15.2.2       tls 				return 0; /* yes - exit with EEXIST */
    139  1.15.2.2       tls 			pad = strchr((const char *)padchar, *trv);
    140  1.15.2.2       tls 			if (pad == NULL) {
    141  1.15.2.2       tls 				/* this should never happen */
    142  1.15.2.2       tls 				errno = EIO;
    143      1.15  christos 				return 0;
    144  1.15.2.2       tls 			}
    145  1.15.2.2       tls 			/* increment character */
    146  1.15.2.2       tls 			*trv = (*++pad == '\0') ? padchar[0] : *pad;
    147  1.15.2.2       tls 			/* carry to next position? */
    148  1.15.2.2       tls 			if (*trv == *carryp) {
    149  1.15.2.2       tls 				/* increment position and loop */
    150  1.15.2.2       tls 				++trv;
    151  1.15.2.2       tls 				++carryp;
    152  1.15.2.2       tls 			} else {
    153  1.15.2.2       tls 				/* try with new name */
    154       1.1   mycroft 				break;
    155       1.1   mycroft 			}
    156       1.1   mycroft 		}
    157       1.1   mycroft 	}
    158       1.1   mycroft 	/*NOTREACHED*/
    159       1.1   mycroft }
    160       1.8   thorpej 
    161      1.12     lukem #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */
    162