"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/tmpfile.c" (12 Oct 2016, 3070 Bytes) of archive /linux/misc/tin-2.4.1.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. For more information about "tmpfile.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 1990, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Chris Torek.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #if 0
   34 #   if defined(LIBC_SCCS) && !defined(lint)
   35 static char rcsid[] = "$OpenBSD: tmpfile.c,v 1.6 1998/09/18 22:06:49 deraadt Exp $";
   36 #   endif /* LIBC_SCCS and not lint */
   37 #   include <sys/types.h>
   38 #   include <sys/stat.h>
   39 #   include <unistd.h>
   40 #   include <signal.h>
   41 #   include <errno.h>
   42 #   include <stdio.h>
   43 #   include <string.h>
   44 #   include <paths.h>
   45 #else
   46 #   include "tin.h"
   47 #endif /* 0 */
   48 
   49 #ifndef HAVE_TMPFILE
   50 #   define TRAILER "tmp.XXXXXXXXXX"
   51 
   52 FILE *
   53 tmpfile(
   54     void)
   55 {
   56     FILE *fp;
   57     char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
   58     int sverrno, fd = -1;
   59     sigset_t set, oset;
   60 
   61     (void) memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1);
   62     (void) memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER));
   63 
   64     /* TODO: use portable signal blocking/unblocking */
   65     sigfillset(&set);
   66     (void) sigprocmask(SIG_BLOCK, &set, &oset);
   67 
   68 #   ifdef HAVE_MKSTEMP
   69     fd = mkstemp(buf);
   70 #   else
   71 #       ifdef HAVE_MKTEMP
   72     fd = open(mktemp(buf), (O_WRONLY|O_CREAT|O_EXCL), (mode_t) (S_IRUSR|S_IWUSR));
   73 #       endif /* HAVE_MKTEMP */
   74 #   endif /* HAVE_MKSTEMP */
   75 
   76     if (fd != -1) {
   77         mode_t u;
   78 
   79         (void) unlink(buf);
   80         u = umask(0);
   81         (void) umask(u);
   82         (void) fchmod(fd, (S_IRUGO|S_IWUGO) & ~u);
   83     }
   84 
   85     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
   86 
   87     if (fd == -1)
   88         return NULL;
   89 
   90     if ((fp = fdopen(fd, "w+")) == NULL) {
   91         sverrno = errno;
   92         (void) close(fd);
   93         errno = sverrno;
   94         return NULL;
   95     }
   96     return fp;
   97 }
   98 #endif /* !HAVE_TMPFILE */