1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
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.
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.
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. */
24 #include <stdio.h> /* May get P_tmpdir. */
25 #include <sys/types.h>
36 #ifdef HAVE_SYS_FILE_H
37 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
39 #if defined(_WIN32) && !defined(__CYGWIN__)
40 #define WIN32_LEAN_AND_MEAN
54 #include "libiberty.h"
55 extern int mkstemps (char *, int);
57 /* '/' works just fine on MS-DOS based systems. */
59 #define DIR_SEPARATOR '/'
62 /* Name of temporary file.
63 mktemp requires 6 trailing X's. */
64 #define TEMP_FILE "XXXXXX"
65 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
67 #if !defined(_WIN32) || defined(__CYGWIN__)
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. */
75 static inline const char *try_dir (const char *, const char *);
77 static inline const char *
78 try_dir (const char *dir
, const char *base
)
83 && access (dir
, R_OK
| W_OK
| X_OK
) == 0)
85 /* Check to make sure dir is actually a directory. */
90 if (!S_ISDIR (s
.st_mode
))
98 static const char tmp
[] = { DIR_SEPARATOR
, 't', 'm', 'p', 0 };
99 static const char vartmp
[] =
100 { DIR_SEPARATOR
, 'v', 'a', 'r', DIR_SEPARATOR
, 't', 'm', 'p', 0 };
104 static char *memoized_tmpdir
;
108 @deftypefn Replacement const char* choose_tmpdir ()
110 Returns a pointer to a directory path suitable for creating temporary
120 if (!memoized_tmpdir
)
122 #if !defined(_WIN32) || defined(__CYGWIN__)
123 const char *base
= 0;
128 /* Try VMS standard temp logical. */
129 base
= try_dir ("/sys$scratch", base
);
131 base
= try_dir (getenv ("TMPDIR"), base
);
132 base
= try_dir (getenv ("TMP"), base
);
133 base
= try_dir (getenv ("TEMP"), base
);
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
);
142 base
= try_dir (P_tmpdir
, base
);
145 /* Try /var/tmp, then /tmp. */
146 base
= try_dir (vartmp
, base
);
147 base
= try_dir (tmp
, base
);
149 /* If all else fails, use the current directory! */
152 /* Append DIR_SEPARATOR to the directory we've chosen
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__) */
163 /* Figure out how much space we need. */
164 len
= GetTempPath(0, NULL
);
167 memoized_tmpdir
= XNEWVEC (char, len
);
168 if (!GetTempPath(len
, memoized_tmpdir
))
170 XDELETEVEC (memoized_tmpdir
);
171 memoized_tmpdir
= NULL
;
174 if (!memoized_tmpdir
)
175 /* If all else fails, use the current directory. */
176 memoized_tmpdir
= xstrdup (".\\");
177 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
180 return memoized_tmpdir
;
185 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
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
189 string is @code{malloc}ed, and the temporary file has been created.
196 make_temp_file_with_prefix (const char *prefix
, const char *suffix
)
198 const char *base
= choose_tmpdir ();
200 int base_len
, suffix_len
, prefix_len
;
209 base_len
= strlen (base
);
210 prefix_len
= strlen (prefix
);
211 suffix_len
= strlen (suffix
);
213 temp_filename
= XNEWVEC (char, base_len
217 strcpy (temp_filename
, base
);
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
);
222 fd
= mkstemps (temp_filename
, suffix_len
);
223 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
226 fprintf (stderr
, "Cannot create temporary file in %s: %s\n",
227 base
, strerror (errno
));
230 /* We abort on failed close out of sheer paranoia. */
233 return temp_filename
;
237 make_temp_file (const char *suffix
)
239 return make_temp_file_with_prefix (NULL
, suffix
);