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