]> git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/make-temp-file.c
choose-temp.c: Split off make_temp_file...
[thirdparty/gcc.git] / libiberty / make-temp-file.c
1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
35 #ifdef HAVE_SYS_FILE_H
36 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
37 #endif
38
39 #ifndef R_OK
40 #define R_OK 4
41 #define W_OK 2
42 #define X_OK 1
43 #endif
44
45 #include "libiberty.h"
46 extern int mkstemps PARAMS ((char *, int));
47
48 #ifndef IN_GCC
49 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN__) && ! defined (_UWIN))
50 #define DIR_SEPARATOR '\\'
51 #endif
52 #endif
53
54 #ifndef DIR_SEPARATOR
55 #define DIR_SEPARATOR '/'
56 #endif
57
58 /* On MSDOS, write temp files in current dir
59 because there's no place else we can expect to use. */
60 /* ??? Although the current directory is tried as a last resort,
61 this is left in so that on MSDOS it is preferred to /tmp on the
62 off chance that someone requires this, since that was the previous
63 behaviour. */
64 #ifdef __MSDOS__
65 #ifndef P_tmpdir
66 #define P_tmpdir "."
67 #endif
68 #endif
69
70 /* Name of temporary file.
71 mktemp requires 6 trailing X's. */
72 #define TEMP_FILE "ccXXXXXX"
73 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
74
75 /* Subroutine of choose_tmpdir.
76 If BASE is non-NULL, return it.
77 Otherwise it checks if DIR is a usable directory.
78 If success, DIR is returned.
79 Otherwise NULL is returned. */
80
81 static const char *try PARAMS ((const char *, const char *));
82
83 static const char *
84 try (dir, base)
85 const char *dir, *base;
86 {
87 if (base != 0)
88 return base;
89 if (dir != 0
90 && access (dir, R_OK | W_OK | X_OK) == 0)
91 return dir;
92 return 0;
93 }
94
95 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
96 static char usrtmp[] =
97 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
98
99 static char *memoized_tmpdir;
100
101 char *
102 choose_tmpdir ()
103 {
104 const char *base = 0;
105 char *tmpdir;
106 unsigned int len;
107
108 if (memoized_tmpdir)
109 return memoized_tmpdir;
110
111 base = try (getenv ("TMPDIR"), base);
112 base = try (getenv ("TMP"), base);
113 base = try (getenv ("TEMP"), base);
114
115 #ifdef P_tmpdir
116 base = try (P_tmpdir, base);
117 #endif
118
119 /* Try /usr/tmp, then /tmp. */
120 base = try (usrtmp, base);
121 base = try (tmp, base);
122
123 /* If all else fails, use the current directory! */
124 if (base == 0)
125 base = ".";
126
127 /* Append DIR_SEPARATOR to the directory we've chosen
128 and return it. */
129 len = strlen (base);
130 tmpdir = xmalloc (len + 2);
131 strcpy (tmpdir, base);
132 tmpdir[len] = DIR_SEPARATOR;
133 tmpdir[len+1] = '\0';
134
135 memoized_tmpdir = tmpdir;
136 return tmpdir;
137 }
138
139 /* Return a temporary file name (as a string) or NULL if unable to create
140 one. SUFFIX is a suffix to append to the file name. The string is
141 malloced, and the temporary file has been created. */
142
143 char *
144 make_temp_file (suffix)
145 const char *suffix;
146 {
147 const char *base = choose_tmpdir ();
148 char *temp_filename;
149 int base_len, suffix_len;
150 int fd;
151
152 if (suffix == 0)
153 suffix = "";
154
155 base_len = strlen (base);
156 suffix_len = strlen (suffix);
157
158 temp_filename = xmalloc (base_len
159 + TEMP_FILE_LEN
160 + suffix_len + 1);
161 strcpy (temp_filename, base);
162 strcpy (temp_filename + base_len, TEMP_FILE);
163 strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
164
165 fd = mkstemps (temp_filename, suffix_len);
166 /* If mkstemps failed, then something bad is happening. Maybe we should
167 issue a message about a possible security attack in progress? */
168 if (fd == -1)
169 abort ();
170 /* Similarly if we can not close the file. */
171 if (close (fd))
172 abort ();
173 return temp_filename;
174 }