Commit | Line | Data |
---|---|---|
1dffcc66 | 1 | /* Utility to pick a temporary filename prefix. |
250d07de | 2 | Copyright (C) 1996-2021 Free Software Foundation, Inc. |
1dffcc66 DD |
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, | |
979c05d3 NC |
17 | write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
18 | Boston, MA 02110-1301, USA. */ | |
1dffcc66 DD |
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> | |
f562800d | 26 | #include <errno.h> |
1dffcc66 DD |
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 | |
3ca747ab DD |
39 | #if defined(_WIN32) && !defined(__CYGWIN__) |
40 | #include <windows.h> | |
41 | #endif | |
1dffcc66 DD |
42 | |
43 | #ifndef R_OK | |
44 | #define R_OK 4 | |
45 | #define W_OK 2 | |
46 | #define X_OK 1 | |
47 | #endif | |
48 | ||
49 | #include "libiberty.h" | |
49b1fae4 | 50 | extern int mkstemps (char *, int); |
1dffcc66 DD |
51 | |
52 | /* '/' works just fine on MS-DOS based systems. */ | |
53 | #ifndef DIR_SEPARATOR | |
54 | #define DIR_SEPARATOR '/' | |
55 | #endif | |
56 | ||
57 | /* Name of temporary file. | |
58 | mktemp requires 6 trailing X's. */ | |
50320b1d | 59 | #define TEMP_FILE "XXXXXX" |
1dffcc66 DD |
60 | #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) |
61 | ||
3ca747ab DD |
62 | #if !defined(_WIN32) || defined(__CYGWIN__) |
63 | ||
1dffcc66 DD |
64 | /* Subroutine of choose_tmpdir. |
65 | If BASE is non-NULL, return it. | |
66 | Otherwise it checks if DIR is a usable directory. | |
67 | If success, DIR is returned. | |
68 | Otherwise NULL is returned. */ | |
69 | ||
abf6a75b | 70 | static inline const char *try_dir (const char *, const char *); |
1dffcc66 | 71 | |
d9697354 | 72 | static inline const char * |
abf6a75b | 73 | try_dir (const char *dir, const char *base) |
1dffcc66 DD |
74 | { |
75 | if (base != 0) | |
76 | return base; | |
77 | if (dir != 0 | |
78 | && access (dir, R_OK | W_OK | X_OK) == 0) | |
79 | return dir; | |
80 | return 0; | |
81 | } | |
82 | ||
83 | static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; | |
1dffcc66 DD |
84 | static const char vartmp[] = |
85 | { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; | |
86 | ||
3ca747ab DD |
87 | #endif |
88 | ||
1dffcc66 DD |
89 | static char *memoized_tmpdir; |
90 | ||
ba19b94f DD |
91 | /* |
92 | ||
abdef8eb | 93 | @deftypefn Replacement const char* choose_tmpdir () |
ba19b94f DD |
94 | |
95 | Returns a pointer to a directory path suitable for creating temporary | |
96 | files in. | |
97 | ||
98 | @end deftypefn | |
99 | ||
100 | */ | |
101 | ||
abdef8eb | 102 | const char * |
49b1fae4 | 103 | choose_tmpdir (void) |
1dffcc66 | 104 | { |
3ca747ab DD |
105 | if (!memoized_tmpdir) |
106 | { | |
107 | #if !defined(_WIN32) || defined(__CYGWIN__) | |
108 | const char *base = 0; | |
109 | char *tmpdir; | |
110 | unsigned int len; | |
111 | ||
b0832eea DD |
112 | #ifdef VMS |
113 | /* Try VMS standard temp logical. */ | |
114 | base = try_dir ("/sys$scratch", base); | |
115 | #else | |
3ca747ab DD |
116 | base = try_dir (getenv ("TMPDIR"), base); |
117 | base = try_dir (getenv ("TMP"), base); | |
118 | base = try_dir (getenv ("TEMP"), base); | |
b0832eea | 119 | #endif |
3ca747ab | 120 | |
1dffcc66 | 121 | #ifdef P_tmpdir |
ae30dc00 DD |
122 | /* We really want a directory name here as if concatenated with say \dir |
123 | we do not end up with a double \\ which defines an UNC path. */ | |
124 | if (strcmp (P_tmpdir, "\\") == 0) | |
125 | base = try_dir ("\\.", base); | |
126 | else | |
127 | base = try_dir (P_tmpdir, base); | |
1dffcc66 DD |
128 | #endif |
129 | ||
20c4b12e | 130 | /* Try /var/tmp, then /tmp. */ |
3ca747ab | 131 | base = try_dir (vartmp, base); |
3ca747ab DD |
132 | base = try_dir (tmp, base); |
133 | ||
134 | /* If all else fails, use the current directory! */ | |
135 | if (base == 0) | |
136 | base = "."; | |
137 | /* Append DIR_SEPARATOR to the directory we've chosen | |
138 | and return it. */ | |
139 | len = strlen (base); | |
140 | tmpdir = XNEWVEC (char, len + 2); | |
141 | strcpy (tmpdir, base); | |
142 | tmpdir[len] = DIR_SEPARATOR; | |
143 | tmpdir[len+1] = '\0'; | |
144 | memoized_tmpdir = tmpdir; | |
145 | #else /* defined(_WIN32) && !defined(__CYGWIN__) */ | |
146 | DWORD len; | |
147 | ||
148 | /* Figure out how much space we need. */ | |
149 | len = GetTempPath(0, NULL); | |
150 | if (len) | |
151 | { | |
152 | memoized_tmpdir = XNEWVEC (char, len); | |
153 | if (!GetTempPath(len, memoized_tmpdir)) | |
154 | { | |
155 | XDELETEVEC (memoized_tmpdir); | |
156 | memoized_tmpdir = NULL; | |
157 | } | |
158 | } | |
159 | if (!memoized_tmpdir) | |
160 | /* If all else fails, use the current directory. */ | |
161 | memoized_tmpdir = xstrdup (".\\"); | |
162 | #endif /* defined(_WIN32) && !defined(__CYGWIN__) */ | |
163 | } | |
164 | ||
165 | return memoized_tmpdir; | |
1dffcc66 DD |
166 | } |
167 | ||
ba19b94f DD |
168 | /* |
169 | ||
170 | @deftypefn Replacement char* make_temp_file (const char *@var{suffix}) | |
171 | ||
172 | Return a temporary file name (as a string) or @code{NULL} if unable to | |
173 | create one. @var{suffix} is a suffix to append to the file name. The | |
5d852400 | 174 | string is @code{malloc}ed, and the temporary file has been created. |
ba19b94f DD |
175 | |
176 | @end deftypefn | |
177 | ||
178 | */ | |
1dffcc66 DD |
179 | |
180 | char * | |
50320b1d | 181 | make_temp_file_with_prefix (const char *prefix, const char *suffix) |
1dffcc66 DD |
182 | { |
183 | const char *base = choose_tmpdir (); | |
184 | char *temp_filename; | |
50320b1d | 185 | int base_len, suffix_len, prefix_len; |
1dffcc66 DD |
186 | int fd; |
187 | ||
50320b1d | 188 | if (prefix == 0) |
189 | prefix = "cc"; | |
190 | ||
1dffcc66 DD |
191 | if (suffix == 0) |
192 | suffix = ""; | |
193 | ||
194 | base_len = strlen (base); | |
50320b1d | 195 | prefix_len = strlen (prefix); |
1dffcc66 DD |
196 | suffix_len = strlen (suffix); |
197 | ||
abf6a75b | 198 | temp_filename = XNEWVEC (char, base_len |
1dffcc66 | 199 | + TEMP_FILE_LEN |
50320b1d | 200 | + suffix_len |
201 | + prefix_len + 1); | |
1dffcc66 | 202 | strcpy (temp_filename, base); |
50320b1d | 203 | strcpy (temp_filename + base_len, prefix); |
204 | strcpy (temp_filename + base_len + prefix_len, TEMP_FILE); | |
205 | strcpy (temp_filename + base_len + prefix_len + TEMP_FILE_LEN, suffix); | |
1dffcc66 DD |
206 | |
207 | fd = mkstemps (temp_filename, suffix_len); | |
f562800d | 208 | /* Mkstemps failed. It may be EPERM, ENOSPC etc. */ |
1dffcc66 | 209 | if (fd == -1) |
f562800d DD |
210 | { |
211 | fprintf (stderr, "Cannot create temporary file in %s: %s\n", | |
212 | base, strerror (errno)); | |
213 | abort (); | |
214 | } | |
215 | /* We abort on failed close out of sheer paranoia. */ | |
1dffcc66 DD |
216 | if (close (fd)) |
217 | abort (); | |
218 | return temp_filename; | |
219 | } | |
50320b1d | 220 | |
221 | char * | |
222 | make_temp_file (const char *suffix) | |
223 | { | |
224 | return make_temp_file_with_prefix (NULL, suffix); | |
225 | } |