tin  2.6.1
About: TIN is a threaded NNTP and spool based UseNet newsreader.
  Fossies Dox: tin-2.6.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

my_tmpfile.c
Go to the documentation of this file.
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-2022 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 */
56int
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)
112 return fd;
113}
pid_t process_id
Definition: init.c:125
constext txt_cannot_create_uniq_name[]
Definition: lang.c:122
static char buf[16]
Definition: langinfo.c:50
int my_tmpfile(char *filename, size_t name_size, const char *base_dir)
Definition: my_tmpfile.c:57
int errno
const char * get_host_name(void)
Definition: header.c:52
void error_message(unsigned int sdelay, const char *fmt,...)
Definition: screen.c:224
void joinpath(char *result, size_t result_size, const char *dir, const char *file)
Definition: joinpath.c:50
void wait_message(unsigned int sdelay, const char *fmt,...)
Definition: screen.c:133
#define strerror(n)
Definition: proto.h:700
#define TMPDIR
Definition: tin.h:2170
#define S_IRWXG
Definition: tin.h:2189
#define MIN(a, b)
Definition: tin.h:811
#define S_IRWXO
Definition: tin.h:2194
#define S_IRUSR
Definition: tin.h:2185
#define _(Text)
Definition: tin.h:94
#define PATH_LEN
Definition: tin.h:843
#define snprintf
Definition: tin.h:2464
#define S_IWUSR
Definition: tin.h:2186