"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/my_tmpfile.c" (12 Oct 2016, 3558 Bytes) of package /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 "my_tmpfile.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : my_tmpfile.c
    4  *  Author    : Urs Janssen <urs@tin.org>
    5  *  Created   : 2001-03-11
    6  *  Updated   : 2014-05-13
    7  *  Notes     :
    8  *
    9  * Copyright (c) 2001-2017 Urs Janssen <urs@tin.org>
   10  * All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. The name of the author may not be used to endorse or promote
   21  *    products derived from this software without specific prior written
   22  *    permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS
   25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   28  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   30  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35  */
   36 
   37 #ifndef TIN_H
   38 #   include "tin.h"
   39 #endif /* !TIN_H */
   40 
   41 
   42 /*
   43  * my_tmpfile(filename, name_size, base_dir)
   44  *
   45  * try to create a uniq tmp-file descriptor
   46  *
   47  * return codes:
   48  * >0 = file descriptor of tmpfile
   49  *      if need_name is set to true and/or we have to unlink the file
   50  *      ourself filename is set to the name of the tmp file located in
   51  *      base_dir
   52  * -1 = some error occurred
   53  */
   54 int
   55 my_tmpfile(
   56     char *filename,
   57     size_t name_size,
   58     const char *base_dir)
   59 {
   60     int fd = -1;
   61     char buf[PATH_LEN];
   62     mode_t mask;
   63 #if defined(HAVE_MKTEMP) && !defined(HAVE_MKSTEMP)
   64     char *t;
   65 #endif /* HAVE_MKTEMP && !HAVE_MKSTEMP */
   66 #ifdef DEBUG
   67     int sverrno;
   68 #endif /* DEBUG */
   69 
   70     errno = 0;
   71 
   72     if (filename != NULL && name_size > 0) {
   73         if (base_dir) {
   74 #ifdef HAVE_LONG_FILE_NAMES
   75             snprintf(buf, MIN(name_size, (sizeof(buf) - 1)), "tin-%s-%ld-XXXXXX", get_host_name(), (long) process_id);
   76 #else
   77             snprintf(buf, MIN(name_size, (sizeof(buf) - 1)), "tin-XXXXXX");
   78 #endif /* HAVE_LONG_FILE_NAMES */
   79             joinpath(filename, name_size, base_dir, buf);
   80         } else {
   81             snprintf(buf, MIN(name_size, (sizeof(buf) - 1)), "tin_XXXXXX");
   82             joinpath(filename, name_size, TMPDIR, buf);
   83         }
   84         mask = umask((mode_t) (S_IRWXO|S_IRWXG));
   85 #ifdef DEBUG
   86         errno = 0;
   87 #endif /* DEBUG */
   88 #ifdef HAVE_MKSTEMP
   89         fd = mkstemp(filename);
   90 #   ifdef DEBUG
   91         sverrno = errno;
   92         if (fd == -1 && sverrno)
   93             wait_message(5, "HAVE_MKSTEMP %s: %s", filename, strerror(sverrno));
   94 #   endif /* DEBUG */
   95 #else
   96 #   ifdef HAVE_MKTEMP
   97         if ((t = mktemp(filename)) != NULL)
   98             fd = open(t, (O_WRONLY|O_CREAT|O_EXCL), (mode_t) (S_IRUSR|S_IWUSR));
   99 #       ifdef DEBUG
  100         sverrno = errno;
  101         if (sverrno)
  102             wait_message(5, "HAVE_MKTEMP %s: %s", filename, strerror(sverrno));
  103 #       endif /* DEBUG */
  104 #   endif /* HAVE_MKTEMP */
  105 #endif /* HAVE_MKSTEMP */
  106         umask(mask);
  107     }
  108     if (fd == -1)
  109         error_message(2, _(txt_cannot_create_uniq_name));
  110     return fd;
  111 }