OSDN Git Service

* choose-temp.c: Always include libiberty.h. Avoid redundancies.
[pf3gnuchains/gcc-fork.git] / libiberty / choose-temp.c
1 /* Utility to pick a temporary filename prefix.
2    Copyright (C) 1996, 1997, 1998 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 /* This file exports two functions: choose_temp_base and make_temp_file.  */
21
22 /* This file lives in at least two places: libiberty and gcc.
23    Don't change one without the other.  */
24
25 #if defined (IN_GCC) || defined (HAVE_CONFIG_H)
26 #include "config.h"
27 #endif
28
29 #ifdef IN_GCC
30 #include "system.h"
31 #else
32
33 /* If we are in gcc, system.h has handled everything.  When not in
34    gcc, if we have a config.h we assume that HAVE_SYS_FILE_H tells us
35    whether to include sys/file.h.  However, libiberty does not have a
36    config.h, and instead arranges to define NO_SYS_FILE_H on the
37    command line when there is no sys/file.h.  */
38
39 #if defined (HAVE_CONFIG_H) ? defined (HAVE_SYS_FILE_H) : ! defined (NO_SYS_FILE_H)
40 #include <sys/types.h>
41 #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
42 #endif
43
44 #ifndef R_OK
45 #define R_OK 4
46 #define W_OK 2
47 #define X_OK 1
48 #endif
49
50 #include <stdio.h>      /* May get P_tmpdir.  */
51 #endif /* IN_GCC */
52
53 #include "libiberty.h"
54 #ifdef IN_GCC
55 extern int mkstemps ();
56 #else
57 #if defined (__MSDOS__) || defined (_WIN32)
58 #define DIR_SEPARATOR '\\'
59 #endif
60 #endif
61
62 #ifndef DIR_SEPARATOR
63 #define DIR_SEPARATOR '/'
64 #endif
65
66 /* On MSDOS, write temp files in current dir
67    because there's no place else we can expect to use.  */
68 /* ??? Although the current directory is tried as a last resort,
69    this is left in so that on MSDOS it is preferred to /tmp on the
70    off chance that someone requires this, since that was the previous
71    behaviour.  */
72 #ifdef __MSDOS__
73 #ifndef P_tmpdir
74 #define P_tmpdir "."
75 #endif
76 #endif
77
78 /* Name of temporary file.
79    mktemp requires 6 trailing X's.  */
80 #define TEMP_FILE "ccXXXXXX"
81
82 /* Subroutine of choose_temp_base.
83    If BASE is non-NULL, return it.
84    Otherwise it checks if DIR is a usable directory.
85    If success, DIR is returned.
86    Otherwise NULL is returned.  */
87
88 static char *
89 try (dir, base)
90      char *dir, *base;
91 {
92   if (base != 0)
93     return base;
94   if (dir != 0
95       && access (dir, R_OK | W_OK | X_OK) == 0)
96     return dir;
97   return 0;
98 }
99
100 /* Return a prefix for temporary file names or NULL if unable to find one.
101    The current directory is chosen if all else fails so the program is
102    exited if a temporary directory can't be found (mktemp fails).
103    The buffer for the result is obtained with xmalloc. 
104
105    This function is provided for backwards compatability only.  It use
106    is not recommended.  */
107
108 char *
109 choose_temp_base ()
110 {
111   char *base = 0;
112   char *temp_filename;
113   int len;
114   static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
115   static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
116
117   base = try (getenv ("TMPDIR"), base);
118   base = try (getenv ("TMP"), base);
119   base = try (getenv ("TEMP"), base);
120
121 #ifdef P_tmpdir
122   base = try (P_tmpdir, base);
123 #endif
124
125   /* Try /usr/tmp, then /tmp.  */
126   base = try (usrtmp, base);
127   base = try (tmp, base);
128  
129   /* If all else fails, use the current directory!  */
130   if (base == 0)
131     base = ".";
132
133   len = strlen (base);
134   temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
135                            + strlen (TEMP_FILE) + 1);
136   strcpy (temp_filename, base);
137
138   if (len != 0
139       && temp_filename[len-1] != '/'
140       && temp_filename[len-1] != DIR_SEPARATOR)
141     temp_filename[len++] = DIR_SEPARATOR;
142   strcpy (temp_filename + len, TEMP_FILE);
143
144   mktemp (temp_filename);
145   if (strlen (temp_filename) == 0)
146     abort ();
147   return temp_filename;
148 }
149 /* Return a temporary file name (as a string) or NULL if unable to create
150    one.  */
151
152 char *
153 make_temp_file (suffix)
154      char *suffix;
155 {
156   char *base = 0;
157   char *temp_filename;
158   int base_len, suffix_len;
159   int fd;
160   static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
161   static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
162
163   base = try (getenv ("TMPDIR"), base);
164   base = try (getenv ("TMP"), base);
165   base = try (getenv ("TEMP"), base);
166
167 #ifdef P_tmpdir
168   base = try (P_tmpdir, base);
169 #endif
170
171   /* Try /usr/tmp, then /tmp.  */
172   base = try (usrtmp, base);
173   base = try (tmp, base);
174  
175   /* If all else fails, use the current directory!  */
176   if (base == 0)
177     base = ".";
178
179   base_len = strlen (base);
180
181   if (suffix)
182     suffix_len = strlen (suffix);
183   else
184     suffix_len = 0;
185
186   temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/
187                            + strlen (TEMP_FILE)
188                            + suffix_len + 1);
189   strcpy (temp_filename, base);
190
191   if (base_len != 0
192       && temp_filename[base_len-1] != '/'
193       && temp_filename[base_len-1] != DIR_SEPARATOR)
194     temp_filename[base_len++] = DIR_SEPARATOR;
195   strcpy (temp_filename + base_len, TEMP_FILE);
196
197   if (suffix)
198     strcat (temp_filename, suffix);
199
200   fd = mkstemps (temp_filename, suffix_len);
201   /* If mkstemps failed, then something bad is happening.  Maybe we should
202      issue a message about a possible security attack in progress?  */
203   if (fd == -1)
204     abort ();
205   /* Similarly if we can not close the file.  */
206   if (close (fd))
207     abort ();
208   return temp_filename;
209 }