Home | History | Annotate | Line # | Download | only in stdio
gettemp.c revision 1.14.20.2
      1  1.14.20.2      yamt /*	$NetBSD: gettemp.c,v 1.14.20.2 2014/05/22 11:36:54 yamt 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.12     lukem #if HAVE_NBTOOL_CONFIG_H
     33       1.12     lukem #include "nbtool_config.h"
     34        1.8   thorpej #endif
     35        1.8   thorpej 
     36       1.12     lukem #if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
     37        1.8   thorpej 
     38        1.1   mycroft #include <sys/cdefs.h>
     39        1.1   mycroft #if defined(LIBC_SCCS) && !defined(lint)
     40        1.1   mycroft #if 0
     41        1.1   mycroft static char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
     42        1.1   mycroft #else
     43  1.14.20.2      yamt __RCSID("$NetBSD: gettemp.c,v 1.14.20.2 2014/05/22 11:36:54 yamt Exp $");
     44        1.1   mycroft #endif
     45        1.1   mycroft #endif /* LIBC_SCCS and not lint */
     46        1.1   mycroft 
     47        1.1   mycroft #include <sys/types.h>
     48        1.1   mycroft #include <sys/stat.h>
     49        1.4     lukem 
     50        1.4     lukem #include <assert.h>
     51        1.4     lukem #include <ctype.h>
     52        1.4     lukem #include <errno.h>
     53        1.1   mycroft #include <fcntl.h>
     54        1.1   mycroft #include <stdio.h>
     55        1.1   mycroft #include <stdlib.h>
     56        1.1   mycroft #include <unistd.h>
     57        1.9   thorpej 
     58       1.12     lukem #if HAVE_NBTOOL_CONFIG_H
     59       1.14       apb #define	GETTEMP		__nbcompat_gettemp
     60        1.8   thorpej #else
     61       1.11       jmc #include "reentrant.h"
     62        1.1   mycroft #include "local.h"
     63        1.8   thorpej #define	GETTEMP		__gettemp
     64        1.8   thorpej #endif
     65        1.1   mycroft 
     66        1.1   mycroft int
     67  1.14.20.1      yamt GETTEMP(char *path, int *doopen, int domkdir)
     68        1.1   mycroft {
     69        1.1   mycroft 	char *start, *trv;
     70        1.1   mycroft 	struct stat sbuf;
     71        1.1   mycroft 	u_int pid;
     72        1.1   mycroft 
     73        1.1   mycroft 	/* To guarantee multiple calls generate unique names even if
     74        1.1   mycroft 	   the file is not created. 676 different possibilities with 7
     75        1.1   mycroft 	   or more X's, 26 with 6 or less. */
     76        1.1   mycroft 	static char xtra[2] = "aa";
     77        1.1   mycroft 	int xcnt = 0;
     78        1.4     lukem 
     79        1.4     lukem 	_DIAGASSERT(path != NULL);
     80        1.4     lukem 	/* doopen may be NULL */
     81        1.1   mycroft 
     82        1.1   mycroft 	pid = getpid();
     83        1.1   mycroft 
     84        1.1   mycroft 	/* Move to end of path and count trailing X's. */
     85        1.1   mycroft 	for (trv = path; *trv; ++trv)
     86        1.1   mycroft 		if (*trv == 'X')
     87        1.1   mycroft 			xcnt++;
     88        1.1   mycroft 		else
     89        1.1   mycroft 			xcnt = 0;
     90        1.1   mycroft 
     91        1.1   mycroft 	/* Use at least one from xtra.  Use 2 if more than 6 X's. */
     92  1.14.20.2      yamt 	if (xcnt > 0) {
     93        1.1   mycroft 		*--trv = xtra[0];
     94  1.14.20.2      yamt 		xcnt--;
     95  1.14.20.2      yamt 	}
     96  1.14.20.2      yamt 	if (xcnt > 5) {
     97        1.1   mycroft 		*--trv = xtra[1];
     98  1.14.20.2      yamt 		xcnt--;
     99  1.14.20.2      yamt 	}
    100        1.1   mycroft 
    101        1.1   mycroft 	/* Set remaining X's to pid digits with 0's to the left. */
    102  1.14.20.2      yamt 	for (; xcnt > 0; xcnt--) {
    103  1.14.20.2      yamt 		*--trv = (pid % 10) + '0';
    104        1.1   mycroft 		pid /= 10;
    105        1.1   mycroft 	}
    106        1.1   mycroft 
    107        1.1   mycroft 	/* update xtra for next call. */
    108        1.1   mycroft 	if (xtra[0] != 'z')
    109        1.1   mycroft 		xtra[0]++;
    110        1.1   mycroft 	else {
    111        1.1   mycroft 		xtra[0] = 'a';
    112        1.1   mycroft 		if (xtra[1] != 'z')
    113        1.1   mycroft 			xtra[1]++;
    114        1.1   mycroft 		else
    115        1.1   mycroft 			xtra[1] = 'a';
    116        1.1   mycroft 	}
    117        1.1   mycroft 
    118        1.1   mycroft 	/*
    119        1.1   mycroft 	 * check the target directory; if you have six X's and it
    120        1.1   mycroft 	 * doesn't exist this runs for a *very* long time.
    121        1.1   mycroft 	 */
    122        1.1   mycroft 	for (start = trv + 1;; --trv) {
    123        1.1   mycroft 		if (trv <= path)
    124        1.1   mycroft 			break;
    125        1.1   mycroft 		if (*trv == '/') {
    126  1.14.20.2      yamt 			int e;
    127        1.1   mycroft 			*trv = '\0';
    128  1.14.20.2      yamt 			e = stat(path, &sbuf);
    129  1.14.20.2      yamt 			*trv = '/';
    130  1.14.20.2      yamt 			if (e == -1)
    131  1.14.20.2      yamt 				return doopen == NULL && !domkdir;
    132        1.1   mycroft 			if (!S_ISDIR(sbuf.st_mode)) {
    133        1.1   mycroft 				errno = ENOTDIR;
    134  1.14.20.2      yamt 				return doopen == NULL && !domkdir;
    135        1.1   mycroft 			}
    136        1.1   mycroft 			break;
    137        1.1   mycroft 		}
    138        1.1   mycroft 	}
    139        1.1   mycroft 
    140        1.1   mycroft 	for (;;) {
    141        1.1   mycroft 		if (doopen) {
    142        1.1   mycroft 			if ((*doopen =
    143       1.13  uebayasi 			    open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)
    144  1.14.20.1      yamt 				return 1;
    145        1.1   mycroft 			if (errno != EEXIST)
    146  1.14.20.1      yamt 				return 0;
    147        1.2   mycroft 		} else if (domkdir) {
    148        1.2   mycroft 			if (mkdir(path, 0700) >= 0)
    149  1.14.20.1      yamt 				return 1;
    150        1.2   mycroft 			if (errno != EEXIST)
    151  1.14.20.1      yamt 				return 0;
    152        1.2   mycroft 		} else if (lstat(path, &sbuf))
    153  1.14.20.1      yamt 			return errno == ENOENT ? 1 : 0;
    154        1.1   mycroft 
    155        1.1   mycroft 		/* tricky little algorithm for backward compatibility */
    156        1.1   mycroft 		for (trv = start;;) {
    157        1.1   mycroft 			if (!*trv)
    158  1.14.20.1      yamt 				return 0;
    159        1.1   mycroft 			if (*trv == 'z')
    160        1.1   mycroft 				*trv++ = 'a';
    161        1.1   mycroft 			else {
    162        1.3  christos 				if (isdigit((unsigned char)*trv))
    163        1.1   mycroft 					*trv = 'a';
    164        1.1   mycroft 				else
    165        1.1   mycroft 					++*trv;
    166        1.1   mycroft 				break;
    167        1.1   mycroft 			}
    168        1.1   mycroft 		}
    169        1.1   mycroft 	}
    170        1.1   mycroft 	/*NOTREACHED*/
    171        1.1   mycroft }
    172        1.8   thorpej 
    173       1.12     lukem #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */
    174