Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/tmpfile.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||||||||
2 | * tmpfile.c - functions to create and safely open temp files for the shell. | - | ||||||||||||||||||
3 | */ | - | ||||||||||||||||||
4 | - | |||||||||||||||||||
5 | /* Copyright (C) 2000-2015 Free Software Foundation, Inc. | - | ||||||||||||||||||
6 | - | |||||||||||||||||||
7 | This file is part of GNU Bash, the Bourne Again SHell. | - | ||||||||||||||||||
8 | - | |||||||||||||||||||
9 | Bash is free software: you can redistribute it and/or modify | - | ||||||||||||||||||
10 | it under the terms of the GNU General Public License as published by | - | ||||||||||||||||||
11 | the Free Software Foundation, either version 3 of the License, or | - | ||||||||||||||||||
12 | (at your option) any later version. | - | ||||||||||||||||||
13 | - | |||||||||||||||||||
14 | Bash is distributed in the hope that it will be useful, | - | ||||||||||||||||||
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||||||||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||||||||
17 | GNU General Public License for more details. | - | ||||||||||||||||||
18 | - | |||||||||||||||||||
19 | You should have received a copy of the GNU General Public License | - | ||||||||||||||||||
20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | - | ||||||||||||||||||
21 | */ | - | ||||||||||||||||||
22 | - | |||||||||||||||||||
23 | #include <config.h> | - | ||||||||||||||||||
24 | - | |||||||||||||||||||
25 | #include <bashtypes.h> | - | ||||||||||||||||||
26 | #include <posixstat.h> | - | ||||||||||||||||||
27 | #include <posixtime.h> | - | ||||||||||||||||||
28 | #include <filecntl.h> | - | ||||||||||||||||||
29 | - | |||||||||||||||||||
30 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||||||||
31 | # include <unistd.h> | - | ||||||||||||||||||
32 | #endif | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include <bashansi.h> | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #include <stdio.h> | - | ||||||||||||||||||
37 | #include <errno.h> | - | ||||||||||||||||||
38 | - | |||||||||||||||||||
39 | #include <shell.h> | - | ||||||||||||||||||
40 | - | |||||||||||||||||||
41 | #ifndef errno | - | ||||||||||||||||||
42 | extern int errno; | - | ||||||||||||||||||
43 | #endif | - | ||||||||||||||||||
44 | - | |||||||||||||||||||
45 | #define BASEOPENFLAGS (O_CREAT | O_TRUNC | O_EXCL | O_BINARY) | - | ||||||||||||||||||
46 | - | |||||||||||||||||||
47 | #define DEFAULT_TMPDIR "." /* bogus default, should be changed */ | - | ||||||||||||||||||
48 | #define DEFAULT_NAMEROOT "shtmp" | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | /* Use ANSI-C rand() interface if random(3) is not available */ | - | ||||||||||||||||||
51 | #if !HAVE_RANDOM | - | ||||||||||||||||||
52 | #define random() rand() | - | ||||||||||||||||||
53 | #endif | - | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | extern pid_t dollar_dollar_pid; | - | ||||||||||||||||||
56 | - | |||||||||||||||||||
57 | static char *get_sys_tmpdir __P((void)); | - | ||||||||||||||||||
58 | static char *get_tmpdir __P((int)); | - | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | static char *sys_tmpdir = (char *)NULL; | - | ||||||||||||||||||
61 | static int ntmpfiles; | - | ||||||||||||||||||
62 | static int tmpnamelen = -1; | - | ||||||||||||||||||
63 | static unsigned long filenum = 1L; | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | static char * | - | ||||||||||||||||||
66 | get_sys_tmpdir () | - | ||||||||||||||||||
67 | { | - | ||||||||||||||||||
68 | if (sys_tmpdir)
| 0 | ||||||||||||||||||
69 | return sys_tmpdir; never executed: return sys_tmpdir; | 0 | ||||||||||||||||||
70 | - | |||||||||||||||||||
71 | #ifdef P_tmpdir | - | ||||||||||||||||||
72 | sys_tmpdir = P_tmpdir; | - | ||||||||||||||||||
73 | if (file_iswdir (sys_tmpdir))
| 0 | ||||||||||||||||||
74 | return sys_tmpdir; never executed: return sys_tmpdir; | 0 | ||||||||||||||||||
75 | #endif | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | sys_tmpdir = "/tmp"; | - | ||||||||||||||||||
78 | if (file_iswdir (sys_tmpdir))
| 0 | ||||||||||||||||||
79 | return sys_tmpdir; never executed: return sys_tmpdir; | 0 | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | sys_tmpdir = "/var/tmp"; | - | ||||||||||||||||||
82 | if (file_iswdir (sys_tmpdir))
| 0 | ||||||||||||||||||
83 | return sys_tmpdir; never executed: return sys_tmpdir; | 0 | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | sys_tmpdir = "/usr/tmp"; | - | ||||||||||||||||||
86 | if (file_iswdir (sys_tmpdir))
| 0 | ||||||||||||||||||
87 | return sys_tmpdir; never executed: return sys_tmpdir; | 0 | ||||||||||||||||||
88 | - | |||||||||||||||||||
89 | sys_tmpdir = DEFAULT_TMPDIR; | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | return sys_tmpdir; never executed: return sys_tmpdir; | 0 | ||||||||||||||||||
92 | } | - | ||||||||||||||||||
93 | - | |||||||||||||||||||
94 | static char * | - | ||||||||||||||||||
95 | get_tmpdir (flags) | - | ||||||||||||||||||
96 | int flags; | - | ||||||||||||||||||
97 | { | - | ||||||||||||||||||
98 | char *tdir; | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
| 0-107 | ||||||||||||||||||
101 | if (tdir && (file_iswdir (tdir) == 0 || strlen (tdir) > PATH_MAX))
| 0-107 | ||||||||||||||||||
102 | tdir = 0; never executed: tdir = 0; | 0 | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | if (tdir == 0)
| 0-107 | ||||||||||||||||||
105 | tdir = get_sys_tmpdir (); never executed: tdir = get_sys_tmpdir (); | 0 | ||||||||||||||||||
106 | - | |||||||||||||||||||
107 | #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX) | - | ||||||||||||||||||
108 | if (tmpnamelen == -1)
| 31-76 | ||||||||||||||||||
109 | tmpnamelen = pathconf (tdir, _PC_NAME_MAX); executed 76 times by 1 test: tmpnamelen = pathconf (tdir, _PC_NAME_MAX ); Executed by:
| 76 | ||||||||||||||||||
110 | #else | - | ||||||||||||||||||
111 | tmpnamelen = 0; | - | ||||||||||||||||||
112 | #endif | - | ||||||||||||||||||
113 | - | |||||||||||||||||||
114 | return tdir; executed 107 times by 1 test: return tdir; Executed by:
| 107 | ||||||||||||||||||
115 | } | - | ||||||||||||||||||
116 | - | |||||||||||||||||||
117 | static void | - | ||||||||||||||||||
118 | sh_seedrand () | - | ||||||||||||||||||
119 | { | - | ||||||||||||||||||
120 | #if HAVE_RANDOM | - | ||||||||||||||||||
121 | int d; | - | ||||||||||||||||||
122 | static int seeded = 0; | - | ||||||||||||||||||
123 | if (seeded == 0)
| 0 | ||||||||||||||||||
124 | { | - | ||||||||||||||||||
125 | struct timeval tv; | - | ||||||||||||||||||
126 | - | |||||||||||||||||||
127 | gettimeofday (&tv, NULL); | - | ||||||||||||||||||
128 | srandom (tv.tv_sec ^ tv.tv_usec ^ (getpid () << 16) ^ (uintptr_t)&d); | - | ||||||||||||||||||
129 | seeded = 1; | - | ||||||||||||||||||
130 | } never executed: end of block | 0 | ||||||||||||||||||
131 | #endif | - | ||||||||||||||||||
132 | } never executed: end of block | 0 | ||||||||||||||||||
133 | - | |||||||||||||||||||
134 | char * | - | ||||||||||||||||||
135 | sh_mktmpname (nameroot, flags) | - | ||||||||||||||||||
136 | char *nameroot; | - | ||||||||||||||||||
137 | int flags; | - | ||||||||||||||||||
138 | { | - | ||||||||||||||||||
139 | char *filename, *tdir, *lroot; | - | ||||||||||||||||||
140 | struct stat sb; | - | ||||||||||||||||||
141 | int r, tdlen; | - | ||||||||||||||||||
142 | static int seeded = 0; | - | ||||||||||||||||||
143 | - | |||||||||||||||||||
144 | filename = (char *)xmalloc (PATH_MAX + 1); | - | ||||||||||||||||||
145 | tdir = get_tmpdir (flags); | - | ||||||||||||||||||
146 | tdlen = strlen (tdir); | - | ||||||||||||||||||
147 | - | |||||||||||||||||||
148 | lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
| 0 | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | #ifdef USE_MKTEMP | - | ||||||||||||||||||
151 | sprintf (filename, "%s/%s.XXXXXX", tdir, lroot); | - | ||||||||||||||||||
152 | if (mktemp (filename) == 0)
| 0 | ||||||||||||||||||
153 | { | - | ||||||||||||||||||
154 | free (filename); | - | ||||||||||||||||||
155 | filename = NULL; | - | ||||||||||||||||||
156 | } never executed: end of block | 0 | ||||||||||||||||||
157 | #else /* !USE_MKTEMP */ | - | ||||||||||||||||||
158 | sh_seedrand (); | - | ||||||||||||||||||
159 | while (1) | - | ||||||||||||||||||
160 | { | - | ||||||||||||||||||
161 | filenum = (filenum << 1) ^ | - | ||||||||||||||||||
162 | (unsigned long) time ((time_t *)0) ^ | - | ||||||||||||||||||
163 | (unsigned long) dollar_dollar_pid ^ | - | ||||||||||||||||||
164 | (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++); | - | ||||||||||||||||||
165 | sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum); | - | ||||||||||||||||||
166 | if (tmpnamelen > 0 && tmpnamelen < 32) | - | ||||||||||||||||||
167 | filename[tdlen + 1 + tmpnamelen] = '\0'; | - | ||||||||||||||||||
168 | # ifdef HAVE_LSTAT | - | ||||||||||||||||||
169 | r = lstat (filename, &sb); | - | ||||||||||||||||||
170 | # else | - | ||||||||||||||||||
171 | r = stat (filename, &sb); | - | ||||||||||||||||||
172 | # endif | - | ||||||||||||||||||
173 | if (r < 0 && errno == ENOENT) | - | ||||||||||||||||||
174 | break; | - | ||||||||||||||||||
175 | } | - | ||||||||||||||||||
176 | #endif /* !USE_MKTEMP */ | - | ||||||||||||||||||
177 | - | |||||||||||||||||||
178 | return filename; never executed: return filename; | 0 | ||||||||||||||||||
179 | } | - | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | int | - | ||||||||||||||||||
182 | sh_mktmpfd (nameroot, flags, namep) | - | ||||||||||||||||||
183 | char *nameroot; | - | ||||||||||||||||||
184 | int flags; | - | ||||||||||||||||||
185 | char **namep; | - | ||||||||||||||||||
186 | { | - | ||||||||||||||||||
187 | char *filename, *tdir, *lroot; | - | ||||||||||||||||||
188 | int fd, tdlen; | - | ||||||||||||||||||
189 | - | |||||||||||||||||||
190 | filename = (char *)xmalloc (PATH_MAX + 1); | - | ||||||||||||||||||
191 | tdir = get_tmpdir (flags); | - | ||||||||||||||||||
192 | tdlen = strlen (tdir); | - | ||||||||||||||||||
193 | - | |||||||||||||||||||
194 | lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
| 0-107 | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | #ifdef USE_MKSTEMP | - | ||||||||||||||||||
197 | sprintf (filename, "%s/%s.XXXXXX", tdir, lroot); | - | ||||||||||||||||||
198 | fd = mkstemp (filename); | - | ||||||||||||||||||
199 | if (fd < 0 || namep == 0)
| 0-107 | ||||||||||||||||||
200 | { | - | ||||||||||||||||||
201 | free (filename); | - | ||||||||||||||||||
202 | filename = NULL; | - | ||||||||||||||||||
203 | } never executed: end of block | 0 | ||||||||||||||||||
204 | if (namep)
| 0-107 | ||||||||||||||||||
205 | *namep = filename; executed 107 times by 1 test: *namep = filename; Executed by:
| 107 | ||||||||||||||||||
206 | return fd; executed 107 times by 1 test: return fd; Executed by:
| 107 | ||||||||||||||||||
207 | #else /* !USE_MKSTEMP */ | - | ||||||||||||||||||
208 | sh_seedrand (); | - | ||||||||||||||||||
209 | do | - | ||||||||||||||||||
210 | { | - | ||||||||||||||||||
211 | filenum = (filenum << 1) ^ | - | ||||||||||||||||||
212 | (unsigned long) time ((time_t *)0) ^ | - | ||||||||||||||||||
213 | (unsigned long) dollar_dollar_pid ^ | - | ||||||||||||||||||
214 | (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++); | - | ||||||||||||||||||
215 | sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum); | - | ||||||||||||||||||
216 | if (tmpnamelen > 0 && tmpnamelen < 32) | - | ||||||||||||||||||
217 | filename[tdlen + 1 + tmpnamelen] = '\0'; | - | ||||||||||||||||||
218 | fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600); | - | ||||||||||||||||||
219 | } | - | ||||||||||||||||||
220 | while (fd < 0 && errno == EEXIST); | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | if (namep) | - | ||||||||||||||||||
223 | *namep = filename; | - | ||||||||||||||||||
224 | else | - | ||||||||||||||||||
225 | free (filename); | - | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | return fd; | - | ||||||||||||||||||
228 | #endif /* !USE_MKSTEMP */ | - | ||||||||||||||||||
229 | } | - | ||||||||||||||||||
230 | - | |||||||||||||||||||
231 | FILE * | - | ||||||||||||||||||
232 | sh_mktmpfp (nameroot, flags, namep) | - | ||||||||||||||||||
233 | char *nameroot; | - | ||||||||||||||||||
234 | int flags; | - | ||||||||||||||||||
235 | char **namep; | - | ||||||||||||||||||
236 | { | - | ||||||||||||||||||
237 | int fd; | - | ||||||||||||||||||
238 | FILE *fp; | - | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | fd = sh_mktmpfd (nameroot, flags, namep); | - | ||||||||||||||||||
241 | if (fd < 0)
| 0-2 | ||||||||||||||||||
242 | return ((FILE *)NULL); never executed: return ((FILE *) ((void *)0) ); | 0 | ||||||||||||||||||
243 | fp = fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w"); | - | ||||||||||||||||||
244 | if (fp == 0)
| 0-2 | ||||||||||||||||||
245 | close (fd); never executed: close (fd); | 0 | ||||||||||||||||||
246 | return fp; executed 2 times by 1 test: return fp; Executed by:
| 2 | ||||||||||||||||||
247 | } | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |