* elf64-ppc.c (struct ppc_link_hash_table): Add do_toc_opt.
[binutils/dougsmingw.git] / libiberty / make-temp-file.c
blob13e192565340266e80d40ff2bc56a8d6179b6325
1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996, 1997, 1998, 2001, 2009 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. */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
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
43 #ifndef R_OK
44 #define R_OK 4
45 #define W_OK 2
46 #define X_OK 1
47 #endif
49 #include "libiberty.h"
50 extern int mkstemps (char *, int);
52 /* '/' works just fine on MS-DOS based systems. */
53 #ifndef DIR_SEPARATOR
54 #define DIR_SEPARATOR '/'
55 #endif
57 /* Name of temporary file.
58 mktemp requires 6 trailing X's. */
59 #define TEMP_FILE "ccXXXXXX"
60 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
62 #if !defined(_WIN32) || defined(__CYGWIN__)
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. */
70 static inline const char *try_dir (const char *, const char *);
72 static inline const char *
73 try_dir (const char *dir, const char *base)
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;
83 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
84 static const char usrtmp[] =
85 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
86 static const char vartmp[] =
87 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
89 #endif
91 static char *memoized_tmpdir;
95 @deftypefn Replacement char* choose_tmpdir ()
97 Returns a pointer to a directory path suitable for creating temporary
98 files in.
100 @end deftypefn
104 char *
105 choose_tmpdir (void)
107 if (!memoized_tmpdir)
109 #if !defined(_WIN32) || defined(__CYGWIN__)
110 const char *base = 0;
111 char *tmpdir;
112 unsigned int len;
114 #ifdef VMS
115 /* Try VMS standard temp logical. */
116 base = try_dir ("/sys$scratch", base);
117 #else
118 base = try_dir (getenv ("TMPDIR"), base);
119 base = try_dir (getenv ("TMP"), base);
120 base = try_dir (getenv ("TEMP"), base);
121 #endif
123 #ifdef P_tmpdir
124 base = try_dir (P_tmpdir, base);
125 #endif
127 /* Try /var/tmp, /usr/tmp, then /tmp. */
128 base = try_dir (vartmp, base);
129 base = try_dir (usrtmp, base);
130 base = try_dir (tmp, base);
132 /* If all else fails, use the current directory! */
133 if (base == 0)
134 base = ".";
135 /* Append DIR_SEPARATOR to the directory we've chosen
136 and return it. */
137 len = strlen (base);
138 tmpdir = XNEWVEC (char, len + 2);
139 strcpy (tmpdir, base);
140 tmpdir[len] = DIR_SEPARATOR;
141 tmpdir[len+1] = '\0';
142 memoized_tmpdir = tmpdir;
143 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
144 DWORD len;
146 /* Figure out how much space we need. */
147 len = GetTempPath(0, NULL);
148 if (len)
150 memoized_tmpdir = XNEWVEC (char, len);
151 if (!GetTempPath(len, memoized_tmpdir))
153 XDELETEVEC (memoized_tmpdir);
154 memoized_tmpdir = NULL;
157 if (!memoized_tmpdir)
158 /* If all else fails, use the current directory. */
159 memoized_tmpdir = xstrdup (".\\");
160 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
163 return memoized_tmpdir;
168 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
170 Return a temporary file name (as a string) or @code{NULL} if unable to
171 create one. @var{suffix} is a suffix to append to the file name. The
172 string is @code{malloc}ed, and the temporary file has been created.
174 @end deftypefn
178 char *
179 make_temp_file (const char *suffix)
181 const char *base = choose_tmpdir ();
182 char *temp_filename;
183 int base_len, suffix_len;
184 int fd;
186 if (suffix == 0)
187 suffix = "";
189 base_len = strlen (base);
190 suffix_len = strlen (suffix);
192 temp_filename = XNEWVEC (char, base_len
193 + TEMP_FILE_LEN
194 + suffix_len + 1);
195 strcpy (temp_filename, base);
196 strcpy (temp_filename + base_len, TEMP_FILE);
197 strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
199 fd = mkstemps (temp_filename, suffix_len);
200 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
201 if (fd == -1)
203 fprintf (stderr, "Cannot create temporary file in %s: %s\n",
204 base, strerror (errno));
205 abort ();
207 /* We abort on failed close out of sheer paranoia. */
208 if (close (fd))
209 abort ();
210 return temp_filename;