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