* call.c (build_over_call): Mark COMPOUND_EXPRs generated for
[official-gcc.git] / libiberty / choose-temp.c
blob1a475dd6fb93b91cd2cb2d26416a7d779cec13ac
1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996, 1997, 1998 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* This file exports two functions: choose_temp_base and make_temp_file. */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h> /* May get P_tmpdir. */
27 #include <sys/types.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #ifdef HAVE_SYS_FILE_H
38 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
39 #endif
41 #ifndef R_OK
42 #define R_OK 4
43 #define W_OK 2
44 #define X_OK 1
45 #endif
47 #include "libiberty.h"
48 extern int mkstemps ();
50 #ifndef IN_GCC
51 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN__) && ! defined (_UWIN))
52 #define DIR_SEPARATOR '\\'
53 #endif
54 #endif
56 #ifndef DIR_SEPARATOR
57 #define DIR_SEPARATOR '/'
58 #endif
60 /* On MSDOS, write temp files in current dir
61 because there's no place else we can expect to use. */
62 /* ??? Although the current directory is tried as a last resort,
63 this is left in so that on MSDOS it is preferred to /tmp on the
64 off chance that someone requires this, since that was the previous
65 behaviour. */
66 #ifdef __MSDOS__
67 #ifndef P_tmpdir
68 #define P_tmpdir "."
69 #endif
70 #endif
72 /* Name of temporary file.
73 mktemp requires 6 trailing X's. */
74 #define TEMP_FILE "ccXXXXXX"
76 /* Subroutine of choose_temp_base.
77 If BASE is non-NULL, return it.
78 Otherwise it checks if DIR is a usable directory.
79 If success, DIR is returned.
80 Otherwise NULL is returned. */
82 static const char *try PARAMS ((const char *, const char *));
84 static const char *
85 try (dir, base)
86 const char *dir, *base;
88 if (base != 0)
89 return base;
90 if (dir != 0
91 && access (dir, R_OK | W_OK | X_OK) == 0)
92 return dir;
93 return 0;
96 /* Return a prefix for temporary file names or NULL if unable to find one.
97 The current directory is chosen if all else fails so the program is
98 exited if a temporary directory can't be found (mktemp fails).
99 The buffer for the result is obtained with xmalloc.
101 This function is provided for backwards compatability only. It use
102 is not recommended. */
104 char *
105 choose_temp_base ()
107 const char *base = 0;
108 char *temp_filename;
109 int len;
110 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
111 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
113 base = try (getenv ("TMPDIR"), base);
114 base = try (getenv ("TMP"), base);
115 base = try (getenv ("TEMP"), base);
117 #ifdef P_tmpdir
118 base = try (P_tmpdir, base);
119 #endif
121 /* Try /usr/tmp, then /tmp. */
122 base = try (usrtmp, base);
123 base = try (tmp, base);
125 /* If all else fails, use the current directory! */
126 if (base == 0)
127 base = ".";
129 len = strlen (base);
130 temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
131 + strlen (TEMP_FILE) + 1);
132 strcpy (temp_filename, base);
134 if (len != 0
135 && temp_filename[len-1] != '/'
136 && temp_filename[len-1] != DIR_SEPARATOR)
137 temp_filename[len++] = DIR_SEPARATOR;
138 strcpy (temp_filename + len, TEMP_FILE);
140 mktemp (temp_filename);
141 if (strlen (temp_filename) == 0)
142 abort ();
143 return temp_filename;
145 /* Return a temporary file name (as a string) or NULL if unable to create
146 one. */
148 char *
149 make_temp_file (suffix)
150 const char *suffix;
152 const char *base = 0;
153 char *temp_filename;
154 int base_len, suffix_len;
155 int fd;
156 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
157 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
159 base = try (getenv ("TMPDIR"), base);
160 base = try (getenv ("TMP"), base);
161 base = try (getenv ("TEMP"), base);
163 #ifdef P_tmpdir
164 base = try (P_tmpdir, base);
165 #endif
167 /* Try /usr/tmp, then /tmp. */
168 base = try (usrtmp, base);
169 base = try (tmp, base);
171 /* If all else fails, use the current directory! */
172 if (base == 0)
173 base = ".";
175 base_len = strlen (base);
177 if (suffix)
178 suffix_len = strlen (suffix);
179 else
180 suffix_len = 0;
182 temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/
183 + strlen (TEMP_FILE)
184 + suffix_len + 1);
185 strcpy (temp_filename, base);
187 if (base_len != 0
188 && temp_filename[base_len-1] != '/'
189 && temp_filename[base_len-1] != DIR_SEPARATOR)
190 temp_filename[base_len++] = DIR_SEPARATOR;
191 strcpy (temp_filename + base_len, TEMP_FILE);
193 if (suffix)
194 strcat (temp_filename, suffix);
196 fd = mkstemps (temp_filename, suffix_len);
197 /* If mkstemps failed, then something bad is happening. Maybe we should
198 issue a message about a possible security attack in progress? */
199 if (fd == -1)
200 abort ();
201 /* Similarly if we can not close the file. */
202 if (close (fd))
203 abort ();
204 return temp_filename;