Mercurial > hg > CbC > CbC_gcc
annotate libiberty/make-temp-file.c @ 158:494b0b89df80 default tip
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 18:13:55 +0900 |
parents | 1830386684a0 |
children |
rev | line source |
---|---|
0 | 1 /* Utility to pick a temporary filename prefix. |
145 | 2 Copyright (C) 1996-2020 Free Software Foundation, Inc. |
0 | 3 |
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. | |
9 | |
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. | |
14 | |
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. */ | |
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> | |
26 #include <errno.h> | |
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 #if defined(_WIN32) && !defined(__CYGWIN__) | |
40 #include <windows.h> | |
41 #endif | |
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" | |
50 extern int mkstemps (char *, int); | |
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. */ | |
131 | 59 #define TEMP_FILE "XXXXXX" |
0 | 60 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) |
61 | |
62 #if !defined(_WIN32) || defined(__CYGWIN__) | |
63 | |
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 | |
70 static inline const char *try_dir (const char *, const char *); | |
71 | |
72 static inline const char * | |
73 try_dir (const char *dir, const char *base) | |
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 | |
83 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; | |
84 static const char usrtmp[] = | |
85 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; | |
86 static const char vartmp[] = | |
87 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; | |
88 | |
89 #endif | |
90 | |
91 static char *memoized_tmpdir; | |
92 | |
93 /* | |
94 | |
111 | 95 @deftypefn Replacement const char* choose_tmpdir () |
0 | 96 |
97 Returns a pointer to a directory path suitable for creating temporary | |
98 files in. | |
99 | |
100 @end deftypefn | |
101 | |
102 */ | |
103 | |
111 | 104 const char * |
0 | 105 choose_tmpdir (void) |
106 { | |
107 if (!memoized_tmpdir) | |
108 { | |
109 #if !defined(_WIN32) || defined(__CYGWIN__) | |
110 const char *base = 0; | |
111 char *tmpdir; | |
112 unsigned int len; | |
113 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
114 #ifdef VMS |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
115 /* Try VMS standard temp logical. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
116 base = try_dir ("/sys$scratch", base); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
117 #else |
0 | 118 base = try_dir (getenv ("TMPDIR"), base); |
119 base = try_dir (getenv ("TMP"), base); | |
120 base = try_dir (getenv ("TEMP"), base); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
121 #endif |
0 | 122 |
123 #ifdef P_tmpdir | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
124 /* We really want a directory name here as if concatenated with say \dir |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
125 we do not end up with a double \\ which defines an UNC path. */ |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
126 if (strcmp (P_tmpdir, "\\") == 0) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
127 base = try_dir ("\\.", base); |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
128 else |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
129 base = try_dir (P_tmpdir, base); |
0 | 130 #endif |
131 | |
132 /* Try /var/tmp, /usr/tmp, then /tmp. */ | |
133 base = try_dir (vartmp, base); | |
134 base = try_dir (usrtmp, base); | |
135 base = try_dir (tmp, base); | |
136 | |
137 /* If all else fails, use the current directory! */ | |
138 if (base == 0) | |
139 base = "."; | |
140 /* Append DIR_SEPARATOR to the directory we've chosen | |
141 and return it. */ | |
142 len = strlen (base); | |
143 tmpdir = XNEWVEC (char, len + 2); | |
144 strcpy (tmpdir, base); | |
145 tmpdir[len] = DIR_SEPARATOR; | |
146 tmpdir[len+1] = '\0'; | |
147 memoized_tmpdir = tmpdir; | |
148 #else /* defined(_WIN32) && !defined(__CYGWIN__) */ | |
149 DWORD len; | |
150 | |
151 /* Figure out how much space we need. */ | |
152 len = GetTempPath(0, NULL); | |
153 if (len) | |
154 { | |
155 memoized_tmpdir = XNEWVEC (char, len); | |
156 if (!GetTempPath(len, memoized_tmpdir)) | |
157 { | |
158 XDELETEVEC (memoized_tmpdir); | |
159 memoized_tmpdir = NULL; | |
160 } | |
161 } | |
162 if (!memoized_tmpdir) | |
163 /* If all else fails, use the current directory. */ | |
164 memoized_tmpdir = xstrdup (".\\"); | |
165 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */ | |
166 } | |
167 | |
168 return memoized_tmpdir; | |
169 } | |
170 | |
171 /* | |
172 | |
173 @deftypefn Replacement char* make_temp_file (const char *@var{suffix}) | |
174 | |
175 Return a temporary file name (as a string) or @code{NULL} if unable to | |
176 create one. @var{suffix} is a suffix to append to the file name. The | |
177 string is @code{malloc}ed, and the temporary file has been created. | |
178 | |
179 @end deftypefn | |
180 | |
181 */ | |
182 | |
183 char * | |
131 | 184 make_temp_file_with_prefix (const char *prefix, const char *suffix) |
0 | 185 { |
186 const char *base = choose_tmpdir (); | |
187 char *temp_filename; | |
131 | 188 int base_len, suffix_len, prefix_len; |
0 | 189 int fd; |
190 | |
131 | 191 if (prefix == 0) |
192 prefix = "cc"; | |
193 | |
0 | 194 if (suffix == 0) |
195 suffix = ""; | |
196 | |
197 base_len = strlen (base); | |
131 | 198 prefix_len = strlen (prefix); |
0 | 199 suffix_len = strlen (suffix); |
200 | |
201 temp_filename = XNEWVEC (char, base_len | |
202 + TEMP_FILE_LEN | |
131 | 203 + suffix_len |
204 + prefix_len + 1); | |
0 | 205 strcpy (temp_filename, base); |
131 | 206 strcpy (temp_filename + base_len, prefix); |
207 strcpy (temp_filename + base_len + prefix_len, TEMP_FILE); | |
208 strcpy (temp_filename + base_len + prefix_len + TEMP_FILE_LEN, suffix); | |
0 | 209 |
210 fd = mkstemps (temp_filename, suffix_len); | |
211 /* Mkstemps failed. It may be EPERM, ENOSPC etc. */ | |
212 if (fd == -1) | |
213 { | |
214 fprintf (stderr, "Cannot create temporary file in %s: %s\n", | |
215 base, strerror (errno)); | |
216 abort (); | |
217 } | |
218 /* We abort on failed close out of sheer paranoia. */ | |
219 if (close (fd)) | |
220 abort (); | |
221 return temp_filename; | |
222 } | |
131 | 223 |
224 char * | |
225 make_temp_file (const char *suffix) | |
226 { | |
227 return make_temp_file_with_prefix (NULL, suffix); | |
228 } |