]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/make-temp-file.c
* zh_CN.po: Update.
[thirdparty/gcc.git] / libiberty / make-temp-file.c
CommitLineData
33437dc7
ZW
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,
ee58dffd
NC
17write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18Boston, MA 02110-1301, USA. */
33437dc7
ZW
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>
a23eb008 26#include <errno.h>
33437dc7
ZW
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
39
40#ifndef R_OK
41#define R_OK 4
42#define W_OK 2
43#define X_OK 1
44#endif
45
46#include "libiberty.h"
6da879de 47extern int mkstemps (char *, int);
33437dc7 48
49ee944b 49/* '/' works just fine on MS-DOS based systems. */
33437dc7
ZW
50#ifndef DIR_SEPARATOR
51#define DIR_SEPARATOR '/'
52#endif
53
33437dc7
ZW
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)
58
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. */
64
d7cf8390 65static inline const char *try_dir (const char *, const char *);
33437dc7 66
d02af173 67static inline const char *
d7cf8390 68try_dir (const char *dir, const char *base)
33437dc7
ZW
69{
70 if (base != 0)
71 return base;
72 if (dir != 0
73 && access (dir, R_OK | W_OK | X_OK) == 0)
74 return dir;
75 return 0;
76}
77
49ee944b
ZW
78static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
79static const char usrtmp[] =
33437dc7 80{ DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
49ee944b
ZW
81static const char vartmp[] =
82{ DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
33437dc7
ZW
83
84static char *memoized_tmpdir;
85
aac04c15
DD
86/*
87
88@deftypefn Replacement char* choose_tmpdir ()
89
90Returns a pointer to a directory path suitable for creating temporary
91files in.
92
93@end deftypefn
94
95*/
96
33437dc7 97char *
6da879de 98choose_tmpdir (void)
33437dc7
ZW
99{
100 const char *base = 0;
101 char *tmpdir;
102 unsigned int len;
103
104 if (memoized_tmpdir)
105 return memoized_tmpdir;
106
d7cf8390
GDR
107 base = try_dir (getenv ("TMPDIR"), base);
108 base = try_dir (getenv ("TMP"), base);
109 base = try_dir (getenv ("TEMP"), base);
33437dc7
ZW
110
111#ifdef P_tmpdir
d7cf8390 112 base = try_dir (P_tmpdir, base);
33437dc7
ZW
113#endif
114
49ee944b 115 /* Try /var/tmp, /usr/tmp, then /tmp. */
d7cf8390
GDR
116 base = try_dir (vartmp, base);
117 base = try_dir (usrtmp, base);
118 base = try_dir (tmp, base);
33437dc7
ZW
119
120 /* If all else fails, use the current directory! */
121 if (base == 0)
122 base = ".";
123
124 /* Append DIR_SEPARATOR to the directory we've chosen
125 and return it. */
126 len = strlen (base);
d7cf8390 127 tmpdir = XNEWVEC (char, len + 2);
33437dc7
ZW
128 strcpy (tmpdir, base);
129 tmpdir[len] = DIR_SEPARATOR;
130 tmpdir[len+1] = '\0';
131
132 memoized_tmpdir = tmpdir;
133 return tmpdir;
134}
135
aac04c15
DD
136/*
137
138@deftypefn Replacement char* make_temp_file (const char *@var{suffix})
139
140Return a temporary file name (as a string) or @code{NULL} if unable to
141create one. @var{suffix} is a suffix to append to the file name. The
5bed56d9 142string is @code{malloc}ed, and the temporary file has been created.
aac04c15
DD
143
144@end deftypefn
145
146*/
33437dc7
ZW
147
148char *
6da879de 149make_temp_file (const char *suffix)
33437dc7
ZW
150{
151 const char *base = choose_tmpdir ();
152 char *temp_filename;
153 int base_len, suffix_len;
154 int fd;
155
156 if (suffix == 0)
157 suffix = "";
158
159 base_len = strlen (base);
160 suffix_len = strlen (suffix);
161
d7cf8390 162 temp_filename = XNEWVEC (char, base_len
33437dc7
ZW
163 + TEMP_FILE_LEN
164 + suffix_len + 1);
165 strcpy (temp_filename, base);
166 strcpy (temp_filename + base_len, TEMP_FILE);
167 strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
168
169 fd = mkstemps (temp_filename, suffix_len);
a23eb008 170 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
33437dc7 171 if (fd == -1)
a23eb008
DV
172 {
173 fprintf (stderr, "Cannot create temporary file in %s: %s\n",
174 base, strerror (errno));
175 abort ();
176 }
177 /* We abort on failed close out of sheer paranoia. */
33437dc7
ZW
178 if (close (fd))
179 abort ();
180 return temp_filename;
181}