| @@ -1,239 +1,239 @@ | | | @@ -1,239 +1,239 @@ |
1 | /* Utility to pick a temporary filename prefix. | | 1 | /* Utility to pick a temporary filename prefix. |
2 | Copyright (C) 1996-2022 Free Software Foundation, Inc. | | 2 | Copyright (C) 1996-2022 Free Software Foundation, Inc. |
3 | | | 3 | |
4 | This file is part of the libiberty library. | | 4 | This file is part of the libiberty library. |
5 | Libiberty is free software; you can redistribute it and/or | | 5 | Libiberty is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public | | 6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either | | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | | 8 | version 2 of the License, or (at your option) any later version. |
9 | | | 9 | |
10 | Libiberty is distributed in the hope that it will be useful, | | 10 | Libiberty is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | | 13 | Library General Public License for more details. |
14 | | | 14 | |
15 | You should have received a copy of the GNU Library General Public | | 15 | You should have received a copy of the GNU Library General Public |
16 | License along with libiberty; see the file COPYING.LIB. If not, | | 16 | License along with libiberty; see the file COPYING.LIB. If not, |
17 | write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | | 17 | write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
18 | Boston, MA 02110-1301, USA. */ | | 18 | Boston, MA 02110-1301, USA. */ |
19 | | | 19 | |
20 | #ifdef HAVE_CONFIG_H | | 20 | #ifdef HAVE_CONFIG_H |
21 | #include "config.h" | | 21 | #include "config.h" |
22 | #endif | | 22 | #endif |
23 | | | 23 | |
24 | #include <stdio.h> /* May get P_tmpdir. */ | | 24 | #include <stdio.h> /* May get P_tmpdir. */ |
25 | #include <sys/types.h> | | 25 | #include <sys/types.h> |
26 | #include <errno.h> | | 26 | #include <errno.h> |
27 | #ifdef HAVE_UNISTD_H | | 27 | #ifdef HAVE_UNISTD_H |
28 | #include <unistd.h> | | 28 | #include <unistd.h> |
29 | #endif | | 29 | #endif |
30 | #ifdef HAVE_STDLIB_H | | 30 | #ifdef HAVE_STDLIB_H |
31 | #include <stdlib.h> | | 31 | #include <stdlib.h> |
32 | #endif | | 32 | #endif |
33 | #ifdef HAVE_STRING_H | | 33 | #ifdef HAVE_STRING_H |
34 | #include <string.h> | | 34 | #include <string.h> |
35 | #endif | | 35 | #endif |
36 | #ifdef HAVE_SYS_FILE_H | | 36 | #ifdef HAVE_SYS_FILE_H |
37 | #include <sys/file.h> /* May get R_OK, etc. on some systems. */ | | 37 | #include <sys/file.h> /* May get R_OK, etc. on some systems. */ |
38 | #endif | | 38 | #endif |
39 | #if defined(_WIN32) && !defined(__CYGWIN__) | | 39 | #if defined(_WIN32) && !defined(__CYGWIN__) |
40 | #include <windows.h> | | 40 | #include <windows.h> |
41 | #endif | | 41 | #endif |
42 | #if HAVE_SYS_STAT_H | | 42 | #if HAVE_SYS_STAT_H |
43 | #include <sys/stat.h> | | 43 | #include <sys/stat.h> |
44 | #endif | | 44 | #endif |
45 | | | 45 | |
46 | | | 46 | |
47 | #ifndef R_OK | | 47 | #ifndef R_OK |
48 | #define R_OK 4 | | 48 | #define R_OK 4 |
49 | #define W_OK 2 | | 49 | #define W_OK 2 |
50 | #define X_OK 1 | | 50 | #define X_OK 1 |
51 | #endif | | 51 | #endif |
52 | | | 52 | |
53 | #include "libiberty.h" | | 53 | #include "libiberty.h" |
54 | extern int mkstemps (char *, int); | | 54 | extern int mkstemps (char *, int); |
55 | | | 55 | |
56 | /* '/' works just fine on MS-DOS based systems. */ | | 56 | /* '/' works just fine on MS-DOS based systems. */ |
57 | #ifndef DIR_SEPARATOR | | 57 | #ifndef DIR_SEPARATOR |
58 | #define DIR_SEPARATOR '/' | | 58 | #define DIR_SEPARATOR '/' |
59 | #endif | | 59 | #endif |
60 | | | 60 | |
61 | /* Name of temporary file. | | 61 | /* Name of temporary file. |
62 | mktemp requires 6 trailing X's. */ | | 62 | mktemp requires 6 trailing X's. */ |
63 | #define TEMP_FILE "XXXXXX" | | 63 | #define TEMP_FILE "XXXXXX" |
64 | #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) | | 64 | #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) |
65 | | | 65 | |
66 | #if !defined(_WIN32) || defined(__CYGWIN__) | | 66 | #if !defined(_WIN32) || defined(__CYGWIN__) |
67 | | | 67 | |
68 | /* Subroutine of choose_tmpdir. | | 68 | /* Subroutine of choose_tmpdir. |
69 | If BASE is non-NULL, return it. | | 69 | If BASE is non-NULL, return it. |
70 | Otherwise it checks if DIR is a usable directory. | | 70 | Otherwise it checks if DIR is a usable directory. |
71 | If success, DIR is returned. | | 71 | If success, DIR is returned. |
72 | Otherwise NULL is returned. */ | | 72 | Otherwise NULL is returned. */ |
73 | | | 73 | |
74 | static inline const char *try_dir (const char *, const char *); | | 74 | static inline const char *try_dir (const char *, const char *); |
75 | | | 75 | |
76 | static inline const char * | | 76 | static inline const char * |
77 | try_dir (const char *dir, const char *base) | | 77 | try_dir (const char *dir, const char *base) |
78 | { | | 78 | { |
79 | if (base != 0) | | 79 | if (base != 0) |
80 | return base; | | 80 | return base; |
81 | if (dir != 0 | | 81 | if (dir != 0 |
82 | && access (dir, R_OK | W_OK | X_OK) == 0) | | 82 | && access (dir, R_OK | W_OK | X_OK) == 0) |
83 | { | | 83 | { |
84 | /* Check to make sure dir is actually a directory. */ | | 84 | /* Check to make sure dir is actually a directory. */ |
85 | #ifdef S_ISDIR | | 85 | #ifdef S_ISDIR |
86 | struct stat s; | | 86 | struct stat s; |
87 | if (stat (dir, &s)) | | 87 | if (stat (dir, &s)) |
88 | return NULL; | | 88 | return NULL; |
89 | if (!S_ISDIR (s.st_mode)) | | 89 | if (!S_ISDIR (s.st_mode)) |
90 | return NULL; | | 90 | return NULL; |
91 | #endif | | 91 | #endif |
92 | return dir; | | 92 | return dir; |
93 | } | | 93 | } |
94 | return 0; | | 94 | return 0; |
95 | } | | 95 | } |
96 | | | 96 | |
97 | static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; | | 97 | static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; |
98 | static const char vartmp[] = | | 98 | static const char vartmp[] = |
99 | { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; | | 99 | { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; |
100 | | | 100 | |
101 | #endif | | 101 | #endif |
102 | | | 102 | |
103 | static char *memoized_tmpdir; | | 103 | static char *memoized_tmpdir; |
104 | | | 104 | |
105 | /* | | 105 | /* |
106 | | | 106 | |
107 | @deftypefn Replacement const char* choose_tmpdir () | | 107 | @deftypefn Replacement const char* choose_tmpdir () |
108 | | | 108 | |
109 | Returns a pointer to a directory path suitable for creating temporary | | 109 | Returns a pointer to a directory path suitable for creating temporary |
110 | files in. | | 110 | files in. |
111 | | | 111 | |
112 | @end deftypefn | | 112 | @end deftypefn |
113 | | | 113 | |
114 | */ | | 114 | */ |
115 | | | 115 | |
116 | const char * | | 116 | const char * |
117 | choose_tmpdir (void) | | 117 | choose_tmpdir (void) |
118 | { | | 118 | { |
119 | if (!memoized_tmpdir) | | 119 | if (!memoized_tmpdir) |
120 | { | | 120 | { |
121 | #if !defined(_WIN32) || defined(__CYGWIN__) | | 121 | #if !defined(_WIN32) || defined(__CYGWIN__) |
122 | const char *base = 0; | | 122 | const char *base = 0; |
123 | char *tmpdir; | | 123 | char *tmpdir; |
124 | unsigned int len; | | 124 | unsigned int len; |
125 | | | 125 | |
126 | #ifdef VMS | | 126 | #ifdef VMS |
127 | /* Try VMS standard temp logical. */ | | 127 | /* Try VMS standard temp logical. */ |
128 | base = try_dir ("/sys$scratch", base); | | 128 | base = try_dir ("/sys$scratch", base); |
129 | #else | | 129 | #else |
130 | base = try_dir (getenv ("TMPDIR"), base); | | 130 | base = try_dir (getenv ("TMPDIR"), base); |
131 | base = try_dir (getenv ("TMP"), base); | | 131 | base = try_dir (getenv ("TMP"), base); |
132 | base = try_dir (getenv ("TEMP"), base); | | 132 | base = try_dir (getenv ("TEMP"), base); |
133 | #endif | | 133 | #endif |
134 | | | 134 | |
135 | #ifdef P_tmpdir | | 135 | #ifdef P_tmpdir |
136 | /* We really want a directory name here as if concatenated with say \dir | | 136 | /* We really want a directory name here as if concatenated with say \dir |
137 | we do not end up with a double \\ which defines an UNC path. */ | | 137 | we do not end up with a double \\ which defines an UNC path. */ |
138 | if (strcmp (P_tmpdir, "\\") == 0) | | 138 | if (strcmp (P_tmpdir, "\\") == 0) |
139 | base = try_dir ("\\.", base); | | 139 | base = try_dir ("\\.", base); |
140 | else | | 140 | else |
141 | base = try_dir (P_tmpdir, base); | | 141 | base = try_dir (P_tmpdir, base); |
142 | #endif | | 142 | #endif |
143 | | | 143 | |
144 | /* Try /tmp, then /var/tmp. */ | | 144 | /* Try /tmp, then /var/tmp. */ |
145 | base = try_dir (tmp, base); | | 145 | base = try_dir (tmp, base); |
146 | base = try_dir (usrtmp, base); | | 146 | base = try_dir (vartmp, base); |
147 | | | 147 | |
148 | /* If all else fails, use the current directory! */ | | 148 | /* If all else fails, use the current directory! */ |
149 | if (base == 0) | | 149 | if (base == 0) |
150 | base = "."; | | 150 | base = "."; |
151 | /* Append DIR_SEPARATOR to the directory we've chosen | | 151 | /* Append DIR_SEPARATOR to the directory we've chosen |
152 | and return it. */ | | 152 | and return it. */ |
153 | len = strlen (base); | | 153 | len = strlen (base); |
154 | tmpdir = XNEWVEC (char, len + 2); | | 154 | tmpdir = XNEWVEC (char, len + 2); |
155 | strcpy (tmpdir, base); | | 155 | strcpy (tmpdir, base); |
156 | tmpdir[len] = DIR_SEPARATOR; | | 156 | tmpdir[len] = DIR_SEPARATOR; |
157 | tmpdir[len+1] = '\0'; | | 157 | tmpdir[len+1] = '\0'; |
158 | memoized_tmpdir = tmpdir; | | 158 | memoized_tmpdir = tmpdir; |
159 | #else /* defined(_WIN32) && !defined(__CYGWIN__) */ | | 159 | #else /* defined(_WIN32) && !defined(__CYGWIN__) */ |
160 | DWORD len; | | 160 | DWORD len; |
161 | | | 161 | |
162 | /* Figure out how much space we need. */ | | 162 | /* Figure out how much space we need. */ |
163 | len = GetTempPath(0, NULL); | | 163 | len = GetTempPath(0, NULL); |
164 | if (len) | | 164 | if (len) |
165 | { | | 165 | { |
166 | memoized_tmpdir = XNEWVEC (char, len); | | 166 | memoized_tmpdir = XNEWVEC (char, len); |
167 | if (!GetTempPath(len, memoized_tmpdir)) | | 167 | if (!GetTempPath(len, memoized_tmpdir)) |
168 | { | | 168 | { |
169 | XDELETEVEC (memoized_tmpdir); | | 169 | XDELETEVEC (memoized_tmpdir); |
170 | memoized_tmpdir = NULL; | | 170 | memoized_tmpdir = NULL; |
171 | } | | 171 | } |
172 | } | | 172 | } |
173 | if (!memoized_tmpdir) | | 173 | if (!memoized_tmpdir) |
174 | /* If all else fails, use the current directory. */ | | 174 | /* If all else fails, use the current directory. */ |
175 | memoized_tmpdir = xstrdup (".\\"); | | 175 | memoized_tmpdir = xstrdup (".\\"); |
176 | #endif /* defined(_WIN32) && !defined(__CYGWIN__) */ | | 176 | #endif /* defined(_WIN32) && !defined(__CYGWIN__) */ |
177 | } | | 177 | } |
178 | | | 178 | |
179 | return memoized_tmpdir; | | 179 | return memoized_tmpdir; |
180 | } | | 180 | } |
181 | | | 181 | |
182 | /* | | 182 | /* |
183 | | | 183 | |
184 | @deftypefn Replacement char* make_temp_file (const char *@var{suffix}) | | 184 | @deftypefn Replacement char* make_temp_file (const char *@var{suffix}) |
185 | | | 185 | |
186 | Return a temporary file name (as a string) or @code{NULL} if unable to | | 186 | Return a temporary file name (as a string) or @code{NULL} if unable to |
187 | create one. @var{suffix} is a suffix to append to the file name. The | | 187 | create one. @var{suffix} is a suffix to append to the file name. The |
188 | string is @code{malloc}ed, and the temporary file has been created. | | 188 | string is @code{malloc}ed, and the temporary file has been created. |
189 | | | 189 | |
190 | @end deftypefn | | 190 | @end deftypefn |
191 | | | 191 | |
192 | */ | | 192 | */ |
193 | | | 193 | |
194 | char * | | 194 | char * |
195 | make_temp_file_with_prefix (const char *prefix, const char *suffix) | | 195 | make_temp_file_with_prefix (const char *prefix, const char *suffix) |
196 | { | | 196 | { |
197 | const char *base = choose_tmpdir (); | | 197 | const char *base = choose_tmpdir (); |
198 | char *temp_filename; | | 198 | char *temp_filename; |
199 | int base_len, suffix_len, prefix_len; | | 199 | int base_len, suffix_len, prefix_len; |
200 | int fd; | | 200 | int fd; |
201 | | | 201 | |
202 | if (prefix == 0) | | 202 | if (prefix == 0) |
203 | prefix = "cc"; | | 203 | prefix = "cc"; |
204 | | | 204 | |
205 | if (suffix == 0) | | 205 | if (suffix == 0) |
206 | suffix = ""; | | 206 | suffix = ""; |
207 | | | 207 | |
208 | base_len = strlen (base); | | 208 | base_len = strlen (base); |
209 | prefix_len = strlen (prefix); | | 209 | prefix_len = strlen (prefix); |
210 | suffix_len = strlen (suffix); | | 210 | suffix_len = strlen (suffix); |
211 | | | 211 | |
212 | temp_filename = XNEWVEC (char, base_len | | 212 | temp_filename = XNEWVEC (char, base_len |
213 | + TEMP_FILE_LEN | | 213 | + TEMP_FILE_LEN |
214 | + suffix_len | | 214 | + suffix_len |
215 | + prefix_len + 1); | | 215 | + prefix_len + 1); |
216 | strcpy (temp_filename, base); | | 216 | strcpy (temp_filename, base); |
217 | strcpy (temp_filename + base_len, prefix); | | 217 | strcpy (temp_filename + base_len, prefix); |
218 | strcpy (temp_filename + base_len + prefix_len, TEMP_FILE); | | 218 | strcpy (temp_filename + base_len + prefix_len, TEMP_FILE); |
219 | strcpy (temp_filename + base_len + prefix_len + TEMP_FILE_LEN, suffix); | | 219 | strcpy (temp_filename + base_len + prefix_len + TEMP_FILE_LEN, suffix); |
220 | | | 220 | |
221 | fd = mkstemps (temp_filename, suffix_len); | | 221 | fd = mkstemps (temp_filename, suffix_len); |
222 | /* Mkstemps failed. It may be EPERM, ENOSPC etc. */ | | 222 | /* Mkstemps failed. It may be EPERM, ENOSPC etc. */ |
223 | if (fd == -1) | | 223 | if (fd == -1) |
224 | { | | 224 | { |
225 | fprintf (stderr, "Cannot create temporary file in %s: %s\n", | | 225 | fprintf (stderr, "Cannot create temporary file in %s: %s\n", |
226 | base, strerror (errno)); | | 226 | base, strerror (errno)); |
227 | abort (); | | 227 | abort (); |
228 | } | | 228 | } |
229 | /* We abort on failed close out of sheer paranoia. */ | | 229 | /* We abort on failed close out of sheer paranoia. */ |
230 | if (close (fd)) | | 230 | if (close (fd)) |
231 | abort (); | | 231 | abort (); |
232 | return temp_filename; | | 232 | return temp_filename; |
233 | } | | 233 | } |
234 | | | 234 | |
235 | char * | | 235 | char * |
236 | make_temp_file (const char *suffix) | | 236 | make_temp_file (const char *suffix) |
237 | { | | 237 | { |
238 | return make_temp_file_with_prefix (NULL, suffix); | | 238 | return make_temp_file_with_prefix (NULL, suffix); |
239 | } | | 239 | } |