OSDN Git Service

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