1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996, 1997, 1998, 2001 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. */
24 #include <stdio.h> /* May get P_tmpdir. */
25 #include <sys/types.h>
36 #ifdef HAVE_SYS_FILE_H
37 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
46 #include "libiberty.h"
47 extern int mkstemps (char *, int);
49 /* '/' works just fine on MS-DOS based systems. */
51 #define DIR_SEPARATOR '/'
54 /* Name of temporary file.
55 mktemp requires 6 trailing X's. */
56 #define TEMP_FILE "ccXXXXXX"
57 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
59 /* Subroutine of choose_tmpdir.
60 If BASE is non-NULL, return it.
61 Otherwise it checks if DIR is a usable directory.
62 If success, DIR is returned.
63 Otherwise NULL is returned. */
65 static inline const char *try_dir (const char *, const char *);
67 static inline const char *
68 try_dir (const char *dir, const char *base)
73 && access (dir, R_OK | W_OK | X_OK) == 0)
78 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
79 static const char usrtmp[] =
80 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
81 static const char vartmp[] =
82 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
84 static char *memoized_tmpdir;
88 @deftypefn Replacement char* choose_tmpdir ()
90 Returns a pointer to a directory path suitable for creating temporary
100 const char *base = 0;
105 return memoized_tmpdir;
107 base = try_dir (getenv ("TMPDIR"), base);
108 base = try_dir (getenv ("TMP"), base);
109 base = try_dir (getenv ("TEMP"), base);
112 base = try_dir (P_tmpdir, base);
115 /* Try /var/tmp, /usr/tmp, then /tmp. */
116 base = try_dir (vartmp, base);
117 base = try_dir (usrtmp, base);
118 base = try_dir (tmp, base);
120 /* If all else fails, use the current directory! */
124 /* Append DIR_SEPARATOR to the directory we've chosen
127 tmpdir = XNEWVEC (char, len + 2);
128 strcpy (tmpdir, base);
129 tmpdir[len] = DIR_SEPARATOR;
130 tmpdir[len+1] = '\0';
132 memoized_tmpdir = tmpdir;
138 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
140 Return a temporary file name (as a string) or @code{NULL} if unable to
141 create one. @var{suffix} is a suffix to append to the file name. The
142 string is @code{malloc}ed, and the temporary file has been created.
149 make_temp_file (const char *suffix)
151 const char *base = choose_tmpdir ();
153 int base_len, suffix_len;
159 base_len = strlen (base);
160 suffix_len = strlen (suffix);
162 temp_filename = XNEWVEC (char, base_len
165 strcpy (temp_filename, base);
166 strcpy (temp_filename + base_len, TEMP_FILE);
167 strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
169 fd = mkstemps (temp_filename, suffix_len);
170 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
173 fprintf (stderr, "Cannot create temporary file in %s: %s\n",
174 base, strerror (errno));
177 /* We abort on failed close out of sheer paranoia. */
180 return temp_filename;