1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996, 1997, 1998, 2001, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
25 #include <stdio.h> /* May get P_tmpdir. */
26 #include <sys/types.h>
37 #ifdef HAVE_SYS_FILE_H
38 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
40 #if defined(_WIN32) && !defined(__CYGWIN__)
50 #include "libiberty.h"
51 extern int mkstemps (char *, int);
53 /* '/' works just fine on MS-DOS based systems. */
55 #define DIR_SEPARATOR '/'
58 /* Name of temporary file.
59 mktemp requires 6 trailing X's. */
60 #define TEMP_FILE "ccXXXXXX"
61 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
63 #if !defined(_WIN32) || defined(__CYGWIN__)
65 /* Subroutine of choose_tmpdir.
66 If BASE is non-NULL, return it.
67 Otherwise it checks if DIR is a usable directory.
68 If success, DIR is returned.
69 Otherwise NULL is returned. */
71 static inline const char *try_dir (const char *, const char *);
73 static inline const char *
74 try_dir (const char *dir
, const char *base
)
79 && access (dir
, R_OK
| W_OK
| X_OK
) == 0)
84 static const char tmp
[] = { DIR_SEPARATOR
, 't', 'm', 'p', 0 };
85 static const char usrtmp
[] =
86 { DIR_SEPARATOR
, 'u', 's', 'r', DIR_SEPARATOR
, 't', 'm', 'p', 0 };
87 static const char vartmp
[] =
88 { DIR_SEPARATOR
, 'v', 'a', 'r', DIR_SEPARATOR
, 't', 'm', 'p', 0 };
92 static char *memoized_tmpdir
;
96 @deftypefn Replacement char* choose_tmpdir ()
98 Returns a pointer to a directory path suitable for creating temporary
108 if (!memoized_tmpdir
)
110 #if !defined(_WIN32) || defined(__CYGWIN__)
111 const char *base
= 0;
116 /* Try VMS standard temp logical. */
117 base
= try_dir ("/sys$scratch", base
);
119 base
= try_dir (getenv ("TMPDIR"), base
);
120 base
= try_dir (getenv ("TMP"), base
);
121 base
= try_dir (getenv ("TEMP"), base
);
125 /* We really want a directory name here as if concatenated with say \dir
126 we do not end up with a double \\ which defines an UNC path. */
127 if (strcmp (P_tmpdir
, "\\") == 0)
128 base
= try_dir ("\\.", base
);
130 base
= try_dir (P_tmpdir
, base
);
133 /* Try /var/tmp, /usr/tmp, then /tmp. */
134 base
= try_dir (vartmp
, base
);
135 base
= try_dir (usrtmp
, base
);
136 base
= try_dir (tmp
, base
);
138 /* If all else fails, use the current directory! */
141 /* Append DIR_SEPARATOR to the directory we've chosen
144 tmpdir
= XNEWVEC (char, len
+ 2);
145 strcpy (tmpdir
, base
);
146 tmpdir
[len
] = DIR_SEPARATOR
;
147 tmpdir
[len
+1] = '\0';
148 memoized_tmpdir
= tmpdir
;
149 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
152 /* Figure out how much space we need. */
153 len
= GetTempPath(0, NULL
);
156 memoized_tmpdir
= XNEWVEC (char, len
);
157 if (!GetTempPath(len
, memoized_tmpdir
))
159 XDELETEVEC (memoized_tmpdir
);
160 memoized_tmpdir
= NULL
;
163 if (!memoized_tmpdir
)
164 /* If all else fails, use the current directory. */
165 memoized_tmpdir
= xstrdup (".\\");
166 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
169 return memoized_tmpdir
;
174 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
176 Return a temporary file name (as a string) or @code{NULL} if unable to
177 create one. @var{suffix} is a suffix to append to the file name. The
178 string is @code{malloc}ed, and the temporary file has been created.
185 make_temp_file (const char *suffix
)
187 const char *base
= choose_tmpdir ();
189 int base_len
, suffix_len
;
195 base_len
= strlen (base
);
196 suffix_len
= strlen (suffix
);
198 temp_filename
= XNEWVEC (char, base_len
201 strcpy (temp_filename
, base
);
202 strcpy (temp_filename
+ base_len
, TEMP_FILE
);
203 strcpy (temp_filename
+ base_len
+ TEMP_FILE_LEN
, suffix
);
205 fd
= mkstemps (temp_filename
, suffix_len
);
206 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
209 fprintf (stderr
, "Cannot create temporary file in %s: %s\n",
210 base
, strerror (errno
));
213 /* We abort on failed close out of sheer paranoia. */
216 return temp_filename
;