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. */
25 /* $FreeBSD: src/contrib/gcc/choose-temp.c,v 1.3 1999/11/04 10:23:25 obrien Exp $ */
26 /* $DragonFly: src/contrib/gcc/Attic/choose-temp.c,v 1.2 2003/06/17 04:23:59 dillon Exp $ */
32 #include <stdio.h> /* May get P_tmpdir. */
33 #include <sys/types.h>
40 #ifdef HAVE_SYS_FILE_H
41 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
50 #include "libiberty.h"
51 extern int mkstemps ();
54 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN__) && ! defined (_UWIN))
55 #define DIR_SEPARATOR '\\'
60 #define DIR_SEPARATOR '/'
63 /* On MSDOS, write temp files in current dir
64 because there's no place else we can expect to use. */
65 /* ??? Although the current directory is tried as a last resort,
66 this is left in so that on MSDOS it is preferred to /tmp on the
67 off chance that someone requires this, since that was the previous
75 /* Name of temporary file.
76 mktemp requires 6 trailing X's. */
77 #define TEMP_FILE "ccXXXXXX"
79 /* Subroutine of choose_temp_base.
80 If BASE is non-NULL, return it.
81 Otherwise it checks if DIR is a usable directory.
82 If success, DIR is returned.
83 Otherwise NULL is returned. */
92 && access (dir, R_OK | W_OK | X_OK) == 0)
97 /* Return a prefix for temporary file names or NULL if unable to find one.
98 The current directory is chosen if all else fails so the program is
99 exited if a temporary directory can't be found (mktemp fails).
100 The buffer for the result is obtained with xmalloc.
102 This function is provided for backwards compatability only. It use
103 is not recommended. */
111 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
112 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
114 base = try (getenv ("TMPDIR"), base);
115 base = try (getenv ("TMP"), base);
116 base = try (getenv ("TEMP"), base);
118 base = try (tmp, base);
121 base = try (P_tmpdir, base);
124 /* Try /usr/tmp even though it usually doesn't exist on FreeBSD. */
125 base = try (usrtmp, base);
127 /* If all else fails, use the current directory! */
132 temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
133 + strlen (TEMP_FILE) + 1);
134 strcpy (temp_filename, base);
137 && temp_filename[len-1] != '/'
138 && temp_filename[len-1] != DIR_SEPARATOR)
139 temp_filename[len++] = DIR_SEPARATOR;
140 strcpy (temp_filename + len, TEMP_FILE);
142 mktemp (temp_filename);
143 if (strlen (temp_filename) == 0)
145 return temp_filename;
147 /* Return a temporary file name (as a string) or NULL if unable to create
151 make_temp_file (suffix)
156 int base_len, suffix_len;
158 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
159 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
161 base = try (getenv ("TMPDIR"), base);
162 base = try (getenv ("TMP"), base);
163 base = try (getenv ("TEMP"), base);
165 base = try (tmp, base);
168 base = try (P_tmpdir, base);
171 /* Try /usr/tmp even though it usually doesn't exist on FreeBSD. */
172 base = try (usrtmp, base);
174 /* If all else fails, use the current directory! */
178 base_len = strlen (base);
181 suffix_len = strlen (suffix);
185 temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/
188 strcpy (temp_filename, base);
191 && temp_filename[base_len-1] != '/'
192 && temp_filename[base_len-1] != DIR_SEPARATOR)
193 temp_filename[base_len++] = DIR_SEPARATOR;
194 strcpy (temp_filename + base_len, TEMP_FILE);
197 strcat (temp_filename, suffix);
199 fd = mkstemps (temp_filename, suffix_len);
200 /* If mkstemps failed, then something bad is happening. Maybe we should
201 issue a message about a possible security attack in progress? */
204 /* Similarly if we can not close the file. */
207 return temp_filename;