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