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