Home | History | Annotate | Download | only in Stdio
      1 /** @file
      2   Internal function to generate temporary file name for tmpnam.
      3 
      4   Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
      5   This program and the accompanying materials are licensed and made available
      6   under the terms and conditions of the BSD License that accompanies this
      7   distribution.  The full text of the license may be found at
      8   http://opensource.org/licenses/bsd-license.php.
      9 
     10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13   Copyright (c) 1987, 1993
     14    The Regents of the University of California.  All rights reserved.
     15 
     16   Redistribution and use in source and binary forms, with or without
     17   modification, are permitted provided that the following conditions
     18   are met:
     19     1. Redistributions of source code must retain the above copyright
     20        notice, this list of conditions and the following disclaimer.
     21     2. Redistributions in binary form must reproduce the above copyright
     22        notice, this list of conditions and the following disclaimer in the
     23        documentation and/or other materials provided with the distribution.
     24     3. Neither the name of the University nor the names of its contributors
     25        may be used to endorse or promote products derived from this software
     26        without specific prior written permission.
     27 
     28   THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   SUCH DAMAGE.
     39 
     40   mktemp.c  8.1 (Berkeley) 6/4/93
     41   NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp
     42 **/
     43 #include  <LibConfig.h>
     44 
     45 #if HAVE_NBTOOL_CONFIG_H
     46 #include "nbtool_config.h"
     47 #endif
     48 
     49 #if !defined(HAVE_NBTOOL_CONFIG_H) || !defined(HAVE_MKSTEMP) || !defined(HAVE_MKDTEMP)
     50 
     51 #include <sys/EfiCdefs.h>
     52 
     53 #include <sys/types.h>
     54 #include <sys/stat.h>
     55 
     56 #include <assert.h>
     57 #include <ctype.h>
     58 #include <errno.h>
     59 #include <fcntl.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include  <unistd.h>
     63 
     64 #if HAVE_NBTOOL_CONFIG_H
     65 #define GETTEMP   gettemp
     66 #else
     67 #include "reentrant.h"
     68 #include "local.h"
     69 #define GETTEMP   __gettemp
     70 #endif
     71 
     72 int
     73 GETTEMP(
     74   char *path,
     75   int *doopen,
     76   int domkdir
     77   )
     78 {
     79   char *start, *trv;
     80   struct stat sbuf;
     81 
     82   /* To guarantee multiple calls generate unique names even if
     83      the file is not created. 676 different possibilities with 7
     84      or more X's, 26 with 6 or less. */
     85   static char xtra[] = "aa";
     86   int xcnt = 0;
     87 
     88   _DIAGASSERT(path != NULL);
     89   /* doopen may be NULL */
     90 
     91   /* Move to end of path and count trailing X's. */
     92   for (trv = path; *trv; ++trv) {
     93     if (*trv == 'X') {
     94       xcnt++;
     95     }
     96     else {
     97       xcnt = 0;
     98     }
     99   }
    100 
    101   /* Use at least one from xtra.  Use 2 if more than 6 X's. */
    102   if (*(trv - 1) == 'X')
    103     *--trv = xtra[0];
    104   if (xcnt > 6 && *(trv - 1) == 'X')
    105     *--trv = xtra[1];
    106 
    107   /* Set remaining X's to 0's. */
    108   while (*--trv == 'X') {
    109     *trv = '0';
    110   }
    111 
    112   /* update xtra for next call. */
    113   if (xtra[0] != 'z')
    114     xtra[0]++;
    115   else {
    116     xtra[0] = 'a';
    117     if (xtra[1] != 'z')
    118       xtra[1]++;
    119     else
    120       xtra[1] = 'a';
    121   }
    122 
    123   /*
    124    * check the target directory; if you have six X's and it
    125    * doesn't exist this runs for a *very* long time.
    126    */
    127   for (start = trv + 1;; --trv) {
    128     if (trv <= path)
    129       break;
    130     if (*trv == '/') {
    131       *trv = '\0';
    132       if (stat(path, &sbuf))
    133         return (0);
    134       if (!S_ISDIR(sbuf.st_mode)) {
    135         errno = ENOTDIR;
    136         return (0);
    137       }
    138       *trv = '/';
    139       break;
    140     }
    141   }
    142 
    143   for (;;) {
    144     if (doopen) {
    145       if ((*doopen =
    146           open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)
    147         return (1);
    148       if (errno != EEXIST)
    149         return (0);
    150     } else if (domkdir) {
    151       if (mkdir(path, 0700) >= 0)
    152         return (1);
    153       if (errno != EEXIST)
    154         return (0);
    155     } else if (lstat(path, &sbuf))
    156       return (errno == ENOENT ? 1 : 0);
    157 
    158     /* tricky little algorithm for backward compatibility */
    159     for (trv = start;;) {
    160       if (!*trv)
    161         return (0);
    162       if (*trv == 'z') {
    163         *trv++ = 'a';
    164       }
    165       else {
    166         if (isdigit((unsigned char)*trv))
    167           *trv = 'a';
    168         else
    169           ++*trv;
    170         break;
    171       }
    172     }
    173   }
    174   /*NOTREACHED*/
    175 }
    176 
    177 #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */
    178