]> gcc.gnu.org Git - gcc.git/blame - gcc/choose-temp.c
(expand_call): In target code, move PARALLEL case above
[gcc.git] / gcc / choose-temp.c
CommitLineData
402e1fd1 1/* Utility to pick a temporary filename prefix.
ed24b9f2
DE
2 Copyright (C) 1996 Free Software Foundation, Inc.
3
4This file is part of the libiberty library.
5Libiberty is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public
7License as published by the Free Software Foundation; either
8version 2 of the License, or (at your option) any later version.
9
10Libiberty is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
a7095f97
DE
16License along with libiberty; see the file COPYING.LIB. If not,
17write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ed24b9f2
DE
18Boston, MA 02111-1307, USA. */
19
402e1fd1 20/* This file exports one function: choose_temp_base. */
ed24b9f2 21
18496203
DE
22/* This file lives in at least two places: libiberty and gcc.
23 Don't change one without the other. */
24
9e189a7a 25#ifndef NO_SYS_FILE_H
a7095f97 26#include <sys/types.h>
ed24b9f2
DE
27#include <sys/file.h> /* May get R_OK, etc. on some systems. */
28#endif
29
30#ifndef R_OK
31#define R_OK 4
32#define W_OK 2
33#define X_OK 1
34#endif
35
0980566f
DE
36#include <stdio.h> /* May get P_tmpdir. */
37
ed24b9f2
DE
38#ifdef IN_GCC
39#include "config.h"
40#include "gansidecl.h"
41extern char *xmalloc ();
42#else
43#include "ansidecl.h"
44#include "libiberty.h"
45#if defined (__MSDOS__) || defined (_WIN32)
46#define DIR_SEPARATOR '\\'
47#endif
48#endif
49
50#ifndef DIR_SEPARATOR
51#define DIR_SEPARATOR '/'
52#endif
53
54/* On MSDOS, write temp files in current dir
55 because there's no place else we can expect to use. */
56/* ??? Although the current directory is tried as a last resort,
57 this is left in so that on MSDOS it is prefered to /tmp on the
58 off chance that someone requires this, since that was the previous
59 behaviour. */
60#ifdef __MSDOS__
61#ifndef P_tmpdir
62#define P_tmpdir "."
63#endif
64#endif
65
66/* Name of temporary file.
67 mktemp requires 6 trailing X's. */
68#define TEMP_FILE "ccXXXXXX"
69
70/* Subroutine of choose_temp_base.
71 If BASE is non-NULL, returh it.
72 Otherwise it checks if DIR is a usable directory.
73 If success, DIR is returned.
74 Otherwise NULL is returned. */
75
76static char *
77try (dir, base)
78 char *dir, *base;
79{
80 if (base != 0)
81 return base;
82 if (dir != 0
83 && access (dir, R_OK | W_OK) == 0)
84 return dir;
85 return 0;
86}
87
88/* Return a prefix for temporary file names or NULL if unable to find one.
89 The current directory is chosen if all else fails so the program is
90 exited if a temporary directory can't be found (mktemp fails).
91 The buffer for the result is obtained with xmalloc. */
92
93char *
402e1fd1 94choose_temp_base ()
ed24b9f2
DE
95{
96 char *base = 0;
97 char *temp_filename;
98 int len;
6524f653
DE
99 static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
100 static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
ed24b9f2
DE
101
102#ifndef MPW
103 base = try (getenv ("TMPDIR"), base);
104 base = try (getenv ("TMP"), base);
105 base = try (getenv ("TEMP"), base);
106
107#ifdef P_tmpdir
108 base = try (P_tmpdir, base);
109#endif
110
111 /* Try /usr/tmp, then /tmp. */
ed24b9f2 112 base = try (usrtmp, base);
ed24b9f2
DE
113 base = try (tmp, base);
114
115 /* If all else fails, use the current directory! */
116 if (base == 0)
117 base = ".";
118
119#else /* MPW */
120 base = ":";
121#endif
122
123 len = strlen (base);
124 if (len == 0)
125 abort ();
126 temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
127 + strlen (TEMP_FILE) + 1);
128 strcpy (temp_filename, base);
129
130#ifndef MPW
131 if (temp_filename[len-1] != '/'
132 && temp_filename[len-1] != DIR_SEPARATOR)
133 temp_filename[len++] = DIR_SEPARATOR;
134#else /* MPW */
135 if (temp_filename[len-1] != ':')
136 temp_filename[len++] = ':';
137#endif /* MPW */
138 strcpy (temp_filename + len, TEMP_FILE);
139
140 mktemp (temp_filename);
141 if (strlen (temp_filename) == 0)
142 abort ();
143 return temp_filename;
144}
This page took 0.07206 seconds and 5 git commands to generate.