]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/gcc/make-temp-file.c
Merge FreeBSD modifications into gcc 3.2.1-prerelease:
[FreeBSD/FreeBSD.git] / contrib / gcc / 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 /* $FreeBSD$ */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>      /* May get P_tmpdir.  */
27 #include <sys/types.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #ifdef HAVE_SYS_FILE_H
38 #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
39 #endif
40
41 #ifndef R_OK
42 #define R_OK 4
43 #define W_OK 2
44 #define X_OK 1
45 #endif
46
47 #include "libiberty.h"
48 extern int mkstemps PARAMS ((char *, int));
49
50 /* '/' works just fine on MS-DOS based systems.  */
51 #ifndef DIR_SEPARATOR
52 #define DIR_SEPARATOR '/'
53 #endif
54
55 /* Name of temporary file.
56    mktemp requires 6 trailing X's.  */
57 #define TEMP_FILE "ccXXXXXX"
58 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
59
60 /* Subroutine of choose_tmpdir.
61    If BASE is non-NULL, return it.
62    Otherwise it checks if DIR is a usable directory.
63    If success, DIR is returned.
64    Otherwise NULL is returned.  */
65
66 static inline const char *try PARAMS ((const char *, const char *));
67
68 static inline const char *
69 try (dir, base)
70      const char *dir, *base;
71 {
72   if (base != 0)
73     return base;
74   if (dir != 0
75       && access (dir, R_OK | W_OK | X_OK) == 0)
76     return dir;
77   return 0;
78 }
79
80 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
81 static const char usrtmp[] =
82 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
83 static const char vartmp[] =
84 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
85
86 static char *memoized_tmpdir;
87
88 /*
89
90 @deftypefn Replacement char* choose_tmpdir ()
91
92 Returns a pointer to a directory path suitable for creating temporary
93 files in.
94
95 @end deftypefn
96
97 */
98
99 char *
100 choose_tmpdir ()
101 {
102   const char *base = 0;
103   char *tmpdir;
104   unsigned int len;
105
106   if (memoized_tmpdir)
107     return memoized_tmpdir;
108
109   base = try (getenv ("TMPDIR"), base);
110   base = try (getenv ("TMP"), base);
111   base = try (getenv ("TEMP"), base);
112
113 #ifdef P_tmpdir
114   base = try (P_tmpdir, base);
115 #endif
116
117   /* Try /tmp, /var/tmp, then /usr/tmp.  */
118   base = try (tmp, base);
119   base = try (vartmp, base);
120   base = try (usrtmp, base);
121  
122   /* If all else fails, use the current directory!  */
123   if (base == 0)
124     base = ".";
125
126   /* Append DIR_SEPARATOR to the directory we've chosen
127      and return it.  */
128   len = strlen (base);
129   tmpdir = xmalloc (len + 2);
130   strcpy (tmpdir, base);
131   tmpdir[len] = DIR_SEPARATOR;
132   tmpdir[len+1] = '\0';
133
134   memoized_tmpdir = tmpdir;
135   return tmpdir;
136 }
137
138 /*
139
140 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
141
142 Return a temporary file name (as a string) or @code{NULL} if unable to
143 create one.  @var{suffix} is a suffix to append to the file name.  The
144 string is @code{malloc}ed, and the temporary file has been created.
145
146 @end deftypefn
147
148 */
149
150 char *
151 make_temp_file (suffix)
152      const char *suffix;
153 {
154   const char *base = choose_tmpdir ();
155   char *temp_filename;
156   int base_len, suffix_len;
157   int fd;
158
159   if (suffix == 0)
160     suffix = "";
161
162   base_len = strlen (base);
163   suffix_len = strlen (suffix);
164
165   temp_filename = xmalloc (base_len
166                            + TEMP_FILE_LEN
167                            + suffix_len + 1);
168   strcpy (temp_filename, base);
169   strcpy (temp_filename + base_len, TEMP_FILE);
170   strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
171
172   fd = mkstemps (temp_filename, suffix_len);
173   /* If mkstemps failed, then something bad is happening.  Maybe we should
174      issue a message about a possible security attack in progress?  */
175   if (fd == -1)
176     abort ();
177   /* Similarly if we can not close the file.  */
178   if (close (fd))
179     abort ();
180   return temp_filename;
181 }