OSDN Git Service

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