1*86d7f5d3SJohn Marino /* Utility to pick a temporary filename prefix.
2*86d7f5d3SJohn Marino Copyright (C) 1996, 1997, 1998, 2001, 2009, 2010
3*86d7f5d3SJohn Marino Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino
5*86d7f5d3SJohn Marino This file is part of the libiberty library.
6*86d7f5d3SJohn Marino Libiberty is free software; you can redistribute it and/or
7*86d7f5d3SJohn Marino modify it under the terms of the GNU Library General Public
8*86d7f5d3SJohn Marino License as published by the Free Software Foundation; either
9*86d7f5d3SJohn Marino version 2 of the License, or (at your option) any later version.
10*86d7f5d3SJohn Marino
11*86d7f5d3SJohn Marino Libiberty is distributed in the hope that it will be useful,
12*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
13*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*86d7f5d3SJohn Marino Library General Public License for more details.
15*86d7f5d3SJohn Marino
16*86d7f5d3SJohn Marino You should have received a copy of the GNU Library General Public
17*86d7f5d3SJohn Marino License along with libiberty; see the file COPYING.LIB. If not,
18*86d7f5d3SJohn Marino write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*86d7f5d3SJohn Marino Boston, MA 02110-1301, USA. */
20*86d7f5d3SJohn Marino
21*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
22*86d7f5d3SJohn Marino #include "config.h"
23*86d7f5d3SJohn Marino #endif
24*86d7f5d3SJohn Marino
25*86d7f5d3SJohn Marino #include <stdio.h> /* May get P_tmpdir. */
26*86d7f5d3SJohn Marino #include <sys/types.h>
27*86d7f5d3SJohn Marino #include <errno.h>
28*86d7f5d3SJohn Marino #ifdef HAVE_UNISTD_H
29*86d7f5d3SJohn Marino #include <unistd.h>
30*86d7f5d3SJohn Marino #endif
31*86d7f5d3SJohn Marino #ifdef HAVE_STDLIB_H
32*86d7f5d3SJohn Marino #include <stdlib.h>
33*86d7f5d3SJohn Marino #endif
34*86d7f5d3SJohn Marino #ifdef HAVE_STRING_H
35*86d7f5d3SJohn Marino #include <string.h>
36*86d7f5d3SJohn Marino #endif
37*86d7f5d3SJohn Marino #ifdef HAVE_SYS_FILE_H
38*86d7f5d3SJohn Marino #include <sys/file.h> /* May get R_OK, etc. on some systems. */
39*86d7f5d3SJohn Marino #endif
40*86d7f5d3SJohn Marino #if defined(_WIN32) && !defined(__CYGWIN__)
41*86d7f5d3SJohn Marino #include <windows.h>
42*86d7f5d3SJohn Marino #endif
43*86d7f5d3SJohn Marino
44*86d7f5d3SJohn Marino #ifndef R_OK
45*86d7f5d3SJohn Marino #define R_OK 4
46*86d7f5d3SJohn Marino #define W_OK 2
47*86d7f5d3SJohn Marino #define X_OK 1
48*86d7f5d3SJohn Marino #endif
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino #include "libiberty.h"
51*86d7f5d3SJohn Marino extern int mkstemps (char *, int);
52*86d7f5d3SJohn Marino
53*86d7f5d3SJohn Marino /* '/' works just fine on MS-DOS based systems. */
54*86d7f5d3SJohn Marino #ifndef DIR_SEPARATOR
55*86d7f5d3SJohn Marino #define DIR_SEPARATOR '/'
56*86d7f5d3SJohn Marino #endif
57*86d7f5d3SJohn Marino
58*86d7f5d3SJohn Marino /* Name of temporary file.
59*86d7f5d3SJohn Marino mktemp requires 6 trailing X's. */
60*86d7f5d3SJohn Marino #define TEMP_FILE "ccXXXXXX"
61*86d7f5d3SJohn Marino #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
62*86d7f5d3SJohn Marino
63*86d7f5d3SJohn Marino #if !defined(_WIN32) || defined(__CYGWIN__)
64*86d7f5d3SJohn Marino
65*86d7f5d3SJohn Marino /* Subroutine of choose_tmpdir.
66*86d7f5d3SJohn Marino If BASE is non-NULL, return it.
67*86d7f5d3SJohn Marino Otherwise it checks if DIR is a usable directory.
68*86d7f5d3SJohn Marino If success, DIR is returned.
69*86d7f5d3SJohn Marino Otherwise NULL is returned. */
70*86d7f5d3SJohn Marino
71*86d7f5d3SJohn Marino static inline const char *try_dir (const char *, const char *);
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino static inline const char *
try_dir(const char * dir,const char * base)74*86d7f5d3SJohn Marino try_dir (const char *dir, const char *base)
75*86d7f5d3SJohn Marino {
76*86d7f5d3SJohn Marino if (base != 0)
77*86d7f5d3SJohn Marino return base;
78*86d7f5d3SJohn Marino if (dir != 0
79*86d7f5d3SJohn Marino && access (dir, R_OK | W_OK | X_OK) == 0)
80*86d7f5d3SJohn Marino return dir;
81*86d7f5d3SJohn Marino return 0;
82*86d7f5d3SJohn Marino }
83*86d7f5d3SJohn Marino
84*86d7f5d3SJohn Marino static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
85*86d7f5d3SJohn Marino static const char usrtmp[] =
86*86d7f5d3SJohn Marino { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
87*86d7f5d3SJohn Marino static const char vartmp[] =
88*86d7f5d3SJohn Marino { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
89*86d7f5d3SJohn Marino
90*86d7f5d3SJohn Marino #endif
91*86d7f5d3SJohn Marino
92*86d7f5d3SJohn Marino static char *memoized_tmpdir;
93*86d7f5d3SJohn Marino
94*86d7f5d3SJohn Marino /*
95*86d7f5d3SJohn Marino
96*86d7f5d3SJohn Marino @deftypefn Replacement char* choose_tmpdir ()
97*86d7f5d3SJohn Marino
98*86d7f5d3SJohn Marino Returns a pointer to a directory path suitable for creating temporary
99*86d7f5d3SJohn Marino files in.
100*86d7f5d3SJohn Marino
101*86d7f5d3SJohn Marino @end deftypefn
102*86d7f5d3SJohn Marino
103*86d7f5d3SJohn Marino */
104*86d7f5d3SJohn Marino
105*86d7f5d3SJohn Marino char *
choose_tmpdir(void)106*86d7f5d3SJohn Marino choose_tmpdir (void)
107*86d7f5d3SJohn Marino {
108*86d7f5d3SJohn Marino if (!memoized_tmpdir)
109*86d7f5d3SJohn Marino {
110*86d7f5d3SJohn Marino #if !defined(_WIN32) || defined(__CYGWIN__)
111*86d7f5d3SJohn Marino const char *base = 0;
112*86d7f5d3SJohn Marino char *tmpdir;
113*86d7f5d3SJohn Marino unsigned int len;
114*86d7f5d3SJohn Marino
115*86d7f5d3SJohn Marino #ifdef VMS
116*86d7f5d3SJohn Marino /* Try VMS standard temp logical. */
117*86d7f5d3SJohn Marino base = try_dir ("/sys$scratch", base);
118*86d7f5d3SJohn Marino #else
119*86d7f5d3SJohn Marino base = try_dir (getenv ("TMPDIR"), base);
120*86d7f5d3SJohn Marino base = try_dir (getenv ("TMP"), base);
121*86d7f5d3SJohn Marino base = try_dir (getenv ("TEMP"), base);
122*86d7f5d3SJohn Marino #endif
123*86d7f5d3SJohn Marino
124*86d7f5d3SJohn Marino #ifdef P_tmpdir
125*86d7f5d3SJohn Marino /* We really want a directory name here as if concatenated with say \dir
126*86d7f5d3SJohn Marino we do not end up with a double \\ which defines an UNC path. */
127*86d7f5d3SJohn Marino if (strcmp (P_tmpdir, "\\") == 0)
128*86d7f5d3SJohn Marino base = try_dir ("\\.", base);
129*86d7f5d3SJohn Marino else
130*86d7f5d3SJohn Marino base = try_dir (P_tmpdir, base);
131*86d7f5d3SJohn Marino #endif
132*86d7f5d3SJohn Marino
133*86d7f5d3SJohn Marino /* Try /var/tmp, /usr/tmp, then /tmp. */
134*86d7f5d3SJohn Marino base = try_dir (vartmp, base);
135*86d7f5d3SJohn Marino base = try_dir (usrtmp, base);
136*86d7f5d3SJohn Marino base = try_dir (tmp, base);
137*86d7f5d3SJohn Marino
138*86d7f5d3SJohn Marino /* If all else fails, use the current directory! */
139*86d7f5d3SJohn Marino if (base == 0)
140*86d7f5d3SJohn Marino base = ".";
141*86d7f5d3SJohn Marino /* Append DIR_SEPARATOR to the directory we've chosen
142*86d7f5d3SJohn Marino and return it. */
143*86d7f5d3SJohn Marino len = strlen (base);
144*86d7f5d3SJohn Marino tmpdir = XNEWVEC (char, len + 2);
145*86d7f5d3SJohn Marino strcpy (tmpdir, base);
146*86d7f5d3SJohn Marino tmpdir[len] = DIR_SEPARATOR;
147*86d7f5d3SJohn Marino tmpdir[len+1] = '\0';
148*86d7f5d3SJohn Marino memoized_tmpdir = tmpdir;
149*86d7f5d3SJohn Marino #else /* defined(_WIN32) && !defined(__CYGWIN__) */
150*86d7f5d3SJohn Marino DWORD len;
151*86d7f5d3SJohn Marino
152*86d7f5d3SJohn Marino /* Figure out how much space we need. */
153*86d7f5d3SJohn Marino len = GetTempPath(0, NULL);
154*86d7f5d3SJohn Marino if (len)
155*86d7f5d3SJohn Marino {
156*86d7f5d3SJohn Marino memoized_tmpdir = XNEWVEC (char, len);
157*86d7f5d3SJohn Marino if (!GetTempPath(len, memoized_tmpdir))
158*86d7f5d3SJohn Marino {
159*86d7f5d3SJohn Marino XDELETEVEC (memoized_tmpdir);
160*86d7f5d3SJohn Marino memoized_tmpdir = NULL;
161*86d7f5d3SJohn Marino }
162*86d7f5d3SJohn Marino }
163*86d7f5d3SJohn Marino if (!memoized_tmpdir)
164*86d7f5d3SJohn Marino /* If all else fails, use the current directory. */
165*86d7f5d3SJohn Marino memoized_tmpdir = xstrdup (".\\");
166*86d7f5d3SJohn Marino #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
167*86d7f5d3SJohn Marino }
168*86d7f5d3SJohn Marino
169*86d7f5d3SJohn Marino return memoized_tmpdir;
170*86d7f5d3SJohn Marino }
171*86d7f5d3SJohn Marino
172*86d7f5d3SJohn Marino /*
173*86d7f5d3SJohn Marino
174*86d7f5d3SJohn Marino @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
175*86d7f5d3SJohn Marino
176*86d7f5d3SJohn Marino Return a temporary file name (as a string) or @code{NULL} if unable to
177*86d7f5d3SJohn Marino create one. @var{suffix} is a suffix to append to the file name. The
178*86d7f5d3SJohn Marino string is @code{malloc}ed, and the temporary file has been created.
179*86d7f5d3SJohn Marino
180*86d7f5d3SJohn Marino @end deftypefn
181*86d7f5d3SJohn Marino
182*86d7f5d3SJohn Marino */
183*86d7f5d3SJohn Marino
184*86d7f5d3SJohn Marino char *
make_temp_file(const char * suffix)185*86d7f5d3SJohn Marino make_temp_file (const char *suffix)
186*86d7f5d3SJohn Marino {
187*86d7f5d3SJohn Marino const char *base = choose_tmpdir ();
188*86d7f5d3SJohn Marino char *temp_filename;
189*86d7f5d3SJohn Marino int base_len, suffix_len;
190*86d7f5d3SJohn Marino int fd;
191*86d7f5d3SJohn Marino
192*86d7f5d3SJohn Marino if (suffix == 0)
193*86d7f5d3SJohn Marino suffix = "";
194*86d7f5d3SJohn Marino
195*86d7f5d3SJohn Marino base_len = strlen (base);
196*86d7f5d3SJohn Marino suffix_len = strlen (suffix);
197*86d7f5d3SJohn Marino
198*86d7f5d3SJohn Marino temp_filename = XNEWVEC (char, base_len
199*86d7f5d3SJohn Marino + TEMP_FILE_LEN
200*86d7f5d3SJohn Marino + suffix_len + 1);
201*86d7f5d3SJohn Marino strcpy (temp_filename, base);
202*86d7f5d3SJohn Marino strcpy (temp_filename + base_len, TEMP_FILE);
203*86d7f5d3SJohn Marino strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
204*86d7f5d3SJohn Marino
205*86d7f5d3SJohn Marino fd = mkstemps (temp_filename, suffix_len);
206*86d7f5d3SJohn Marino /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
207*86d7f5d3SJohn Marino if (fd == -1)
208*86d7f5d3SJohn Marino {
209*86d7f5d3SJohn Marino fprintf (stderr, "Cannot create temporary file in %s: %s\n",
210*86d7f5d3SJohn Marino base, strerror (errno));
211*86d7f5d3SJohn Marino abort ();
212*86d7f5d3SJohn Marino }
213*86d7f5d3SJohn Marino /* We abort on failed close out of sheer paranoia. */
214*86d7f5d3SJohn Marino if (close (fd))
215*86d7f5d3SJohn Marino abort ();
216*86d7f5d3SJohn Marino return temp_filename;
217*86d7f5d3SJohn Marino }
218