00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 #include "config.h"
00022 #endif
00023
00024 #include <stdio.h>
00025 #include <sys/types.h>
00026 #ifdef HAVE_UNISTD_H
00027 #include <unistd.h>
00028 #endif
00029 #ifdef HAVE_STDLIB_H
00030 #include <stdlib.h>
00031 #endif
00032 #ifdef HAVE_STRING_H
00033 #include <string.h>
00034 #endif
00035 #ifdef HAVE_SYS_FILE_H
00036 #include <sys/file.h>
00037 #endif
00038
00039 #ifndef R_OK
00040 #define R_OK 4
00041 #define W_OK 2
00042 #define X_OK 1
00043 #endif
00044
00045 #include "libiberty.h"
00046 extern int mkstemps PARAMS ((char *, int));
00047
00048
00049 #ifndef DIR_SEPARATOR
00050 #define DIR_SEPARATOR '/'
00051 #endif
00052
00053
00054
00055 #define TEMP_FILE "ccXXXXXX"
00056 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
00057
00058
00059
00060
00061
00062
00063
00064 static inline const char *try PARAMS ((const char *, const char *));
00065
00066 static inline const char *
00067 try (dir, base)
00068 const char *dir, *base;
00069 {
00070 if (base != 0)
00071 return base;
00072 if (dir != 0
00073 && access (dir, R_OK | W_OK | X_OK) == 0)
00074 return dir;
00075 return 0;
00076 }
00077
00078 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
00079 static const char usrtmp[] =
00080 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
00081 static const char vartmp[] =
00082 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
00083
00084 static char *memoized_tmpdir;
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 char *
00098 choose_tmpdir ()
00099 {
00100 const char *base = 0;
00101 char *tmpdir;
00102 unsigned int len;
00103
00104 if (memoized_tmpdir)
00105 return memoized_tmpdir;
00106
00107 base = try (getenv ("TMPDIR"), base);
00108 base = try (getenv ("TMP"), base);
00109 base = try (getenv ("TEMP"), base);
00110
00111 #ifdef P_tmpdir
00112 base = try (P_tmpdir, base);
00113 #endif
00114
00115
00116 base = try (vartmp, base);
00117 base = try (usrtmp, base);
00118 base = try (tmp, base);
00119
00120
00121 if (base == 0)
00122 base = ".";
00123
00124
00125
00126 len = strlen (base);
00127 tmpdir = xmalloc (len + 2);
00128 strcpy (tmpdir, base);
00129 tmpdir[len] = DIR_SEPARATOR;
00130 tmpdir[len+1] = '\0';
00131
00132 memoized_tmpdir = tmpdir;
00133 return tmpdir;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 char *
00149 make_temp_file (suffix)
00150 const char *suffix;
00151 {
00152 const char *base = choose_tmpdir ();
00153 char *temp_filename;
00154 int base_len, suffix_len;
00155 int fd;
00156
00157 if (suffix == 0)
00158 suffix = "";
00159
00160 base_len = strlen (base);
00161 suffix_len = strlen (suffix);
00162
00163 temp_filename = xmalloc (base_len
00164 + TEMP_FILE_LEN
00165 + suffix_len + 1);
00166 strcpy (temp_filename, base);
00167 strcpy (temp_filename + base_len, TEMP_FILE);
00168 strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
00169
00170 fd = mkstemps (temp_filename, suffix_len);
00171
00172
00173 if (fd == -1)
00174 abort ();
00175
00176 if (close (fd))
00177 abort ();
00178 return temp_filename;
00179 }