"Fossies" - the Fresh Open Source Software Archive

Member "amavisd-milter-1.7.2/compat/mkdtemp.c" (6 Jan 2019, 3991 Bytes) of package /linux/privat/amavisd-milter-1.7.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* OPENBSD ORIGINAL: lib/libc/stdio/mktemp.c */
    2 
    3 /*
    4  * Copyright (c) 1987, 1993
    5  *  The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 #if defined(LIBC_SCCS) && !defined(lint)
   33 static char rcsid[] = "$OpenBSD: mktemp.c,v 1.18 2004/09/28 18:12:44 otto Exp $";
   34 #endif /* LIBC_SCCS and not lint */
   35 
   36 #include <sys/types.h>
   37 #include <sys/stat.h>
   38 #include <fcntl.h>
   39 #include <errno.h>
   40 #include <stdio.h>
   41 #include <stdlib.h>
   42 #include <ctype.h>
   43 #include <unistd.h>
   44 
   45 static int _gettemp(char *, int *, int, int);
   46 
   47 char *
   48 mkdtemp(char *path)
   49 {
   50     return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
   51 }
   52 
   53 static int
   54 _gettemp(char *path, int *doopen, int domkdir, int slen)
   55 {
   56     char *start, *trv, *suffp;
   57     struct stat sbuf;
   58     int rval;
   59     pid_t pid;
   60 
   61     if (doopen && domkdir) {
   62         errno = EINVAL;
   63         return(0);
   64     }
   65 
   66     for (trv = path; *trv; ++trv)
   67         ;
   68     trv -= slen;
   69     suffp = trv;
   70     --trv;
   71     if (trv < path) {
   72         errno = EINVAL;
   73         return (0);
   74     }
   75     pid = getpid();
   76     while (trv >= path && *trv == 'X' && pid != 0) {
   77         *trv-- = (pid % 10) + '0';
   78         pid /= 10;
   79     }
   80     while (trv >= path && *trv == 'X') {
   81         char c;
   82 
   83 #ifdef HAVE_ARC4RANDOM
   84         pid = (arc4random() & 0xffff) % (26+26);
   85 #else
   86         pid = (random() & 0xffff) % (26+26);
   87 #endif
   88         if (pid < 26)
   89             c = pid + 'A';
   90         else
   91             c = (pid - 26) + 'a';
   92         *trv-- = c;
   93     }
   94     start = trv + 1;
   95 
   96     /*
   97      * check the target directory; if you have six X's and it
   98      * doesn't exist this runs for a *very* long time.
   99      */
  100     if (doopen || domkdir) {
  101         for (;; --trv) {
  102             if (trv <= path)
  103                 break;
  104             if (*trv == '/') {
  105                 *trv = '\0';
  106                 rval = stat(path, &sbuf);
  107                 *trv = '/';
  108                 if (rval != 0)
  109                     return(0);
  110                 if (!S_ISDIR(sbuf.st_mode)) {
  111                     errno = ENOTDIR;
  112                     return(0);
  113                 }
  114                 break;
  115             }
  116         }
  117     }
  118 
  119     for (;;) {
  120         if (doopen) {
  121             if ((*doopen =
  122                 open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
  123                 return(1);
  124             if (errno != EEXIST)
  125                 return(0);
  126         } else if (domkdir) {
  127             if (mkdir(path, 0700) == 0)
  128                 return(1);
  129             if (errno != EEXIST)
  130                 return(0);
  131         } else if (lstat(path, &sbuf))
  132             return(errno == ENOENT ? 1 : 0);
  133 
  134         /* tricky little algorithm for backward compatibility */
  135         for (trv = start;;) {
  136             if (!*trv)
  137                 return (0);
  138             if (*trv == 'Z') {
  139                 if (trv == suffp)
  140                     return (0);
  141                 *trv++ = 'a';
  142             } else {
  143                 if (isdigit(*trv))
  144                     *trv = 'a';
  145                 else if (*trv == 'z')   /* inc from z to A */
  146                     *trv = 'A';
  147                 else {
  148                     if (trv == suffp)
  149                         return (0);
  150                     ++*trv;
  151                 }
  152                 break;
  153             }
  154         }
  155     }
  156     /*NOTREACHED*/
  157 }