OSDN Git Service

* pa/pa.c (basereg_operand): Never accept a CONST_INT.
[pf3gnuchains/gcc-fork.git] / gcc / choose-temp.c
1 /* Utility to pick a temporary filename prefix.
2    Copyright (C) 1996 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 one function: choose_temp_base.  */
21
22 #if ! defined (_WIN32) && ! defined (NO_SYS_FILE_H)
23 #include <sys/types.h>
24 #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
25 #endif
26
27 #ifndef R_OK
28 #define R_OK 4
29 #define W_OK 2
30 #define X_OK 1
31 #endif
32
33 #include <stdio.h>      /* May get P_tmpdir.  */
34
35 #ifdef IN_GCC
36 #include "config.h"
37 #include "gansidecl.h"
38 extern char *xmalloc ();
39 #else
40 #include "ansidecl.h"
41 #include "libiberty.h"
42 #if defined (__MSDOS__) || defined (_WIN32)
43 #define DIR_SEPARATOR '\\'
44 #endif
45 #endif
46
47 #ifndef DIR_SEPARATOR
48 #define DIR_SEPARATOR '/'
49 #endif
50
51 /* On MSDOS, write temp files in current dir
52    because there's no place else we can expect to use.  */
53 /* ??? Although the current directory is tried as a last resort,
54    this is left in so that on MSDOS it is prefered to /tmp on the
55    off chance that someone requires this, since that was the previous
56    behaviour.  */
57 #ifdef __MSDOS__
58 #ifndef P_tmpdir
59 #define P_tmpdir "."
60 #endif
61 #endif
62
63 /* Name of temporary file.
64    mktemp requires 6 trailing X's.  */
65 #define TEMP_FILE "ccXXXXXX"
66
67 /* Subroutine of choose_temp_base.
68    If BASE is non-NULL, returh it.
69    Otherwise it checks if DIR is a usable directory.
70    If success, DIR is returned.
71    Otherwise NULL is returned.  */
72
73 static char *
74 try (dir, base)
75      char *dir, *base;
76 {
77   if (base != 0)
78     return base;
79   if (dir != 0
80       && access (dir, R_OK | W_OK) == 0)
81     return dir;
82   return 0;
83 }
84
85 /* Return a prefix for temporary file names or NULL if unable to find one.
86    The current directory is chosen if all else fails so the program is
87    exited if a temporary directory can't be found (mktemp fails).
88    The buffer for the result is obtained with xmalloc.  */
89
90 char *
91 choose_temp_base ()
92 {
93   char *base = 0;
94   char *temp_filename;
95   int len;
96   static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
97   static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
98
99 #ifndef MPW
100   base = try (getenv ("TMPDIR"), base);
101   base = try (getenv ("TMP"), base);
102   base = try (getenv ("TEMP"), base);
103
104 #ifdef P_tmpdir
105   base = try (P_tmpdir, base);
106 #endif
107
108   /* Try /usr/tmp, then /tmp.  */
109   base = try (usrtmp, base);
110   base = try (tmp, base);
111  
112   /* If all else fails, use the current directory!  */
113   if (base == 0)
114     base = ".";
115
116 #else /* MPW */
117   base = ":";
118 #endif
119
120   len = strlen (base);
121   if (len == 0)
122     abort ();
123   temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
124                            + strlen (TEMP_FILE) + 1);
125   strcpy (temp_filename, base);
126
127 #ifndef MPW
128   if (temp_filename[len-1] != '/'
129       && temp_filename[len-1] != DIR_SEPARATOR)
130     temp_filename[len++] = DIR_SEPARATOR;
131 #else /* MPW */
132   if (temp_filename[len-1] != ':')
133     temp_filename[len++] = ':';
134 #endif /* MPW */
135   strcpy (temp_filename + len, TEMP_FILE);
136
137   mktemp (temp_filename);
138   if (strlen (temp_filename) == 0)
139     abort ();
140   return temp_filename;
141 }