| 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 two functions: choose_temp_base and make_temp_file. */ |
| 21 | |
| 22 | /* This file lives in at least two places: libiberty and gcc. |
| 23 | Don't change one without the other. */ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include "config.h" |
| 27 | #endif |
| 28 | |
| 29 | #include <stdio.h> /* May get P_tmpdir. */ |
| 30 | #include <sys/types.h> |
| 31 | #ifdef HAVE_UNISTD_H |
| 32 | #include <unistd.h> |
| 33 | #endif |
| 34 | #ifdef HAVE_STDLIB_H |
| 35 | #include <stdlib.h> |
| 36 | #endif |
| 37 | #ifdef HAVE_STRING_H |
| 38 | #include <string.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_SYS_FILE_H |
| 41 | #include <sys/file.h> /* May get R_OK, etc. on some systems. */ |
| 42 | #endif |
| 43 | |
| 44 | #ifndef R_OK |
| 45 | #define R_OK 4 |
| 46 | #define W_OK 2 |
| 47 | #define X_OK 1 |
| 48 | #endif |
| 49 | |
| 50 | #include "libiberty.h" |
| 51 | extern int mkstemps (); |
| 52 | |
| 53 | #ifndef IN_GCC |
| 54 | #if defined (__MSDOS__) || defined (_WIN32) |
| 55 | #define DIR_SEPARATOR '\\' |
| 56 | #endif |
| 57 | #endif |
| 58 | |
| 59 | #ifndef DIR_SEPARATOR |
| 60 | #define DIR_SEPARATOR '/' |
| 61 | #endif |
| 62 | |
| 63 | /* On MSDOS, write temp files in current dir |
| 64 | because there's no place else we can expect to use. */ |
| 65 | /* ??? Although the current directory is tried as a last resort, |
| 66 | this is left in so that on MSDOS it is preferred to /tmp on the |
| 67 | off chance that someone requires this, since that was the previous |
| 68 | behaviour. */ |
| 69 | #ifdef __MSDOS__ |
| 70 | #ifndef P_tmpdir |
| 71 | #define P_tmpdir "." |
| 72 | #endif |
| 73 | #endif |
| 74 | |
| 75 | /* Name of temporary file. |
| 76 | mktemp requires 6 trailing X's. */ |
| 77 | #define TEMP_FILE "ccXXXXXX" |
| 78 | |
| 79 | /* Subroutine of choose_temp_base. |
| 80 | If BASE is non-NULL, return it. |
| 81 | Otherwise it checks if DIR is a usable directory. |
| 82 | If success, DIR is returned. |
| 83 | Otherwise NULL is returned. */ |
| 84 | |
| 85 | static char * |
| 86 | try (dir, base) |
| 87 | char *dir, *base; |
| 88 | { |
| 89 | if (base != 0) |
| 90 | return base; |
| 91 | if (dir != 0 |
| 92 | && access (dir, R_OK | W_OK | X_OK) == 0) |
| 93 | return dir; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* Return a prefix for temporary file names or NULL if unable to find one. |
| 98 | The current directory is chosen if all else fails so the program is |
| 99 | exited if a temporary directory can't be found (mktemp fails). |
| 100 | The buffer for the result is obtained with xmalloc. |
| 101 | |
| 102 | This function is provided for backwards compatability only. It use |
| 103 | is not recommended. */ |
| 104 | |
| 105 | char * |
| 106 | choose_temp_base () |
| 107 | { |
| 108 | char *base = 0; |
| 109 | char *temp_filename; |
| 110 | int len; |
| 111 | static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; |
| 112 | static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; |
| 113 | |
| 114 | base = try (getenv ("TMPDIR"), base); |
| 115 | base = try (getenv ("TMP"), base); |
| 116 | base = try (getenv ("TEMP"), base); |
| 117 | |
| 118 | #ifdef P_tmpdir |
| 119 | base = try (P_tmpdir, base); |
| 120 | #endif |
| 121 | |
| 122 | /* Try /usr/tmp, then /tmp. */ |
| 123 | base = try (usrtmp, base); |
| 124 | base = try (tmp, base); |
| 125 | |
| 126 | /* If all else fails, use the current directory! */ |
| 127 | if (base == 0) |
| 128 | base = "."; |
| 129 | |
| 130 | len = strlen (base); |
| 131 | temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/ |
| 132 | + strlen (TEMP_FILE) + 1); |
| 133 | strcpy (temp_filename, base); |
| 134 | |
| 135 | if (len != 0 |
| 136 | && temp_filename[len-1] != '/' |
| 137 | && temp_filename[len-1] != DIR_SEPARATOR) |
| 138 | temp_filename[len++] = DIR_SEPARATOR; |
| 139 | strcpy (temp_filename + len, TEMP_FILE); |
| 140 | |
| 141 | mktemp (temp_filename); |
| 142 | if (strlen (temp_filename) == 0) |
| 143 | abort (); |
| 144 | return temp_filename; |
| 145 | } |
| 146 | /* Return a temporary file name (as a string) or NULL if unable to create |
| 147 | one. */ |
| 148 | |
| 149 | char * |
| 150 | make_temp_file (suffix) |
| 151 | char *suffix; |
| 152 | { |
| 153 | char *base = 0; |
| 154 | char *temp_filename; |
| 155 | int base_len, suffix_len; |
| 156 | int fd; |
| 157 | static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; |
| 158 | static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; |
| 159 | |
| 160 | base = try (getenv ("TMPDIR"), base); |
| 161 | base = try (getenv ("TMP"), base); |
| 162 | base = try (getenv ("TEMP"), base); |
| 163 | |
| 164 | #ifdef P_tmpdir |
| 165 | base = try (P_tmpdir, base); |
| 166 | #endif |
| 167 | |
| 168 | /* Try /usr/tmp, then /tmp. */ |
| 169 | base = try (usrtmp, base); |
| 170 | base = try (tmp, base); |
| 171 | |
| 172 | /* If all else fails, use the current directory! */ |
| 173 | if (base == 0) |
| 174 | base = "."; |
| 175 | |
| 176 | base_len = strlen (base); |
| 177 | |
| 178 | if (suffix) |
| 179 | suffix_len = strlen (suffix); |
| 180 | else |
| 181 | suffix_len = 0; |
| 182 | |
| 183 | temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/ |
| 184 | + strlen (TEMP_FILE) |
| 185 | + suffix_len + 1); |
| 186 | strcpy (temp_filename, base); |
| 187 | |
| 188 | if (base_len != 0 |
| 189 | && temp_filename[base_len-1] != '/' |
| 190 | && temp_filename[base_len-1] != DIR_SEPARATOR) |
| 191 | temp_filename[base_len++] = DIR_SEPARATOR; |
| 192 | strcpy (temp_filename + base_len, TEMP_FILE); |
| 193 | |
| 194 | if (suffix) |
| 195 | strcat (temp_filename, suffix); |
| 196 | |
| 197 | fd = mkstemps (temp_filename, suffix_len); |
| 198 | /* If mkstemps failed, then something bad is happening. Maybe we should |
| 199 | issue a message about a possible security attack in progress? */ |
| 200 | if (fd == -1) |
| 201 | abort (); |
| 202 | /* Similarly if we can not close the file. */ |
| 203 | if (close (fd)) |
| 204 | abort (); |
| 205 | return temp_filename; |
| 206 | } |