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 /* This file lives in at least two places: libiberty and gcc.
23 Don't change one without the other. */
29 #include <stdio.h> /* May get P_tmpdir. */
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_FILE_H
35 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
44 #include "libiberty.h"
45 extern int mkstemps ();
48 #if defined (__MSDOS__) || defined (_WIN32)
49 #define DIR_SEPARATOR '\\'
54 #define DIR_SEPARATOR '/'
57 /* On MSDOS, write temp files in current dir
58 because there's no place else we can expect to use. */
59 /* ??? Although the current directory is tried as a last resort,
60 this is left in so that on MSDOS it is preferred to /tmp on the
61 off chance that someone requires this, since that was the previous
69 /* Name of temporary file.
70 mktemp requires 6 trailing X's. */
71 #define TEMP_FILE "ccXXXXXX"
73 /* Subroutine of choose_temp_base.
74 If BASE is non-NULL, return it.
75 Otherwise it checks if DIR is a usable directory.
76 If success, DIR is returned.
77 Otherwise NULL is returned. */
86 && access (dir, R_OK | W_OK | X_OK) == 0)
91 /* Return a prefix for temporary file names or NULL if unable to find one.
92 The current directory is chosen if all else fails so the program is
93 exited if a temporary directory can't be found (mktemp fails).
94 The buffer for the result is obtained with xmalloc.
96 This function is provided for backwards compatability only. It use
97 is not recommended. */
105 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
106 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
108 base = try (getenv ("TMPDIR"), base);
109 base = try (getenv ("TMP"), base);
110 base = try (getenv ("TEMP"), base);
113 base = try (P_tmpdir, base);
116 /* Try /usr/tmp, then /tmp. */
117 base = try (usrtmp, base);
118 base = try (tmp, base);
120 /* If all else fails, use the current directory! */
125 temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
126 + strlen (TEMP_FILE) + 1);
127 strcpy (temp_filename, base);
130 && temp_filename[len-1] != '/'
131 && temp_filename[len-1] != DIR_SEPARATOR)
132 temp_filename[len++] = DIR_SEPARATOR;
133 strcpy (temp_filename + len, TEMP_FILE);
135 mktemp (temp_filename);
136 if (strlen (temp_filename) == 0)
138 return temp_filename;
140 /* Return a temporary file name (as a string) or NULL if unable to create
144 make_temp_file (suffix)
149 int base_len, suffix_len;
151 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
152 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
154 base = try (getenv ("TMPDIR"), base);
155 base = try (getenv ("TMP"), base);
156 base = try (getenv ("TEMP"), base);
159 base = try (P_tmpdir, base);
162 /* Try /usr/tmp, then /tmp. */
163 base = try (usrtmp, base);
164 base = try (tmp, base);
166 /* If all else fails, use the current directory! */
170 base_len = strlen (base);
173 suffix_len = strlen (suffix);
177 temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/
180 strcpy (temp_filename, base);
183 && temp_filename[base_len-1] != '/'
184 && temp_filename[base_len-1] != DIR_SEPARATOR)
185 temp_filename[base_len++] = DIR_SEPARATOR;
186 strcpy (temp_filename + base_len, TEMP_FILE);
189 strcat (temp_filename, suffix);
191 fd = mkstemps (temp_filename, suffix_len);
192 /* If mkstemps failed, then something bad is happening. Maybe we should
193 issue a message about a possible security attack in progress? */
196 /* Similarly if we can not close the file. */
199 return temp_filename;