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