"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.6.2/src/my_tmpfile.c" (9 Dec 2022, 3661 Bytes) of package /linux/misc/tin-2.6.2.tar.xz:


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.6.1_vs_2.6.2.

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