1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996-2022 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__)
53 #include "libiberty.h"
54 extern int mkstemps (char *, int);
56 /* '/' works just fine on MS-DOS based systems. */
58 #define DIR_SEPARATOR '/'
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)
66 #if !defined(_WIN32) || defined(__CYGWIN__)
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. */
74 static inline const char *try_dir (const char *, const char *);
76 static inline const char *
77 try_dir (const char *dir
, const char *base
)
82 && access (dir
, R_OK
| W_OK
| X_OK
) == 0)
84 /* Check to make sure dir is actually a directory. */
89 if (!S_ISDIR (s
.st_mode
))
97 static const char tmp
[] = { DIR_SEPARATOR
, 't', 'm', 'p', 0 };
98 static const char vartmp
[] =
99 { DIR_SEPARATOR
, 'v', 'a', 'r', DIR_SEPARATOR
, 't', 'm', 'p', 0 };
103 static char *memoized_tmpdir
;
107 @deftypefn Replacement const char* choose_tmpdir ()
109 Returns a pointer to a directory path suitable for creating temporary
119 if (!memoized_tmpdir
)
121 #if !defined(_WIN32) || defined(__CYGWIN__)
122 const char *base
= 0;
127 /* Try VMS standard temp logical. */
128 base
= try_dir ("/sys$scratch", base
);
130 base
= try_dir (getenv ("TMPDIR"), base
);
131 base
= try_dir (getenv ("TMP"), base
);
132 base
= try_dir (getenv ("TEMP"), base
);
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. */
138 if (strcmp (P_tmpdir
, "\\") == 0)
139 base
= try_dir ("\\.", base
);
141 base
= try_dir (P_tmpdir
, base
);
144 /* Try /var/tmp, then /tmp. */
145 base
= try_dir (vartmp
, base
);
146 base
= try_dir (tmp
, base
);
148 /* If all else fails, use the current directory! */
151 /* Append DIR_SEPARATOR to the directory we've chosen
154 tmpdir
= XNEWVEC (char, len
+ 2);
155 strcpy (tmpdir
, base
);
156 tmpdir
[len
] = DIR_SEPARATOR
;
157 tmpdir
[len
+1] = '\0';
158 memoized_tmpdir
= tmpdir
;
159 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
162 /* Figure out how much space we need. */
163 len
= GetTempPath(0, NULL
);
166 memoized_tmpdir
= XNEWVEC (char, len
);
167 if (!GetTempPath(len
, memoized_tmpdir
))
169 XDELETEVEC (memoized_tmpdir
);
170 memoized_tmpdir
= NULL
;
173 if (!memoized_tmpdir
)
174 /* If all else fails, use the current directory. */
175 memoized_tmpdir
= xstrdup (".\\");
176 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
179 return memoized_tmpdir
;
184 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
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
188 string is @code{malloc}ed, and the temporary file has been created.
195 make_temp_file_with_prefix (const char *prefix
, const char *suffix
)
197 const char *base
= choose_tmpdir ();
199 int base_len
, suffix_len
, prefix_len
;
208 base_len
= strlen (base
);
209 prefix_len
= strlen (prefix
);
210 suffix_len
= strlen (suffix
);
212 temp_filename
= XNEWVEC (char, base_len
216 strcpy (temp_filename
, base
);
217 strcpy (temp_filename
+ base_len
, prefix
);
218 strcpy (temp_filename
+ base_len
+ prefix_len
, TEMP_FILE
);
219 strcpy (temp_filename
+ base_len
+ prefix_len
+ TEMP_FILE_LEN
, suffix
);
221 fd
= mkstemps (temp_filename
, suffix_len
);
222 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
225 fprintf (stderr
, "Cannot create temporary file in %s: %s\n",
226 base
, strerror (errno
));
229 /* We abort on failed close out of sheer paranoia. */
232 return temp_filename
;
236 make_temp_file (const char *suffix
)
238 return make_temp_file_with_prefix (NULL
, suffix
);