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)  

tmpfile.c
Go to the documentation of this file.
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)
35static 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
52FILE *
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# if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
61 char *p;
62# endif /* !HAVE_MKSTEMP && HAVE_MKTEMP */
63
64 (void) memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1);
65 (void) memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER));
66
67 /* TODO: use portable signal blocking/unblocking */
68 sigfillset(&set);
69 (void) sigprocmask(SIG_BLOCK, &set, &oset);
70
71# ifdef HAVE_MKSTEMP
72 fd = mkstemp(buf);
73# else
74# ifdef HAVE_MKTEMP
75 p = mktemp(buf);
76 fd = open(p, (O_WRONLY|O_CREAT|O_EXCL), (mode_t) (S_IRUSR|S_IWUSR));
77# endif /* HAVE_MKTEMP */
78# endif /* HAVE_MKSTEMP */
79
80 if (fd != -1) {
81 mode_t u;
82
83 (void) unlink(buf);
84 u = umask(0);
85 (void) umask(u);
86# ifdef HAVE_FCHMOD
87 (void) fchmod(fd, (S_IRUGO|S_IWUGO) & ~u);
88# else
89# if defined(HAVE_CHMOD) && !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
90 fchmod(p, (S_IRUGO|S_IWUGO) & ~u);
91# endif /* HAVE_CHMOD && !HAVE_MKSTEMP && HAVE_MKTEMP */
92# endif /* HAVE_FCHMOD */
93 }
94
95 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
96
97 if (fd == -1)
98 return NULL;
99
100 if ((fp = fdopen(fd, "w+")) == NULL) {
101 sverrno = errno;
102 (void) close(fd);
103 errno = sverrno;
104 return NULL;
105 }
106 return fp;
107}
108#endif /* !HAVE_TMPFILE */
static char buf[16]
Definition: langinfo.c:50
int errno
#define _PATH_TMP
Definition: tin.h:404
#define S_IWUGO
Definition: tin.h:2211
#define unlink(file)
Definition: tin.h:387
#define S_IRUSR
Definition: tin.h:2185
#define S_IRUGO
Definition: tin.h:2210
#define S_IWUSR
Definition: tin.h:2186
FILE * tmpfile(void)
Definition: tmpfile.c:53
#define TRAILER
Definition: tmpfile.c:50