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