1 /* $MirOS: src/gnu/usr.bin/binutils/libiberty/make-temp-file.c,v 1.4 2005/06/05 21:24:46 tg Exp $ */
2 
3 /* Utility to pick a temporary filename prefix.
4    Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
5 
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11 
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Library General Public License for more details.
16 
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 __RCSID("$MirOS: src/gnu/usr.bin/binutils/libiberty/make-temp-file.c,v 1.4 2005/06/05 21:24:46 tg Exp $");
27 
28 #include <stdio.h>	/* May get P_tmpdir.  */
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_SYS_FILE_H
40 #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
41 #endif
42 
43 #ifndef R_OK
44 #define R_OK 4
45 #define W_OK 2
46 #define X_OK 1
47 #endif
48 
49 #include "libiberty.h"
50 extern int mkstemps (char *, int);
51 
52 /* '/' works just fine on MS-DOS based systems.  */
53 #ifndef DIR_SEPARATOR
54 #define DIR_SEPARATOR '/'
55 #endif
56 
57 /* Name of temporary file.
58    mktemp requires 6 trailing X's.  */
59 #define TEMP_FILE "ccXXXXXX"
60 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
61 
62 /* Subroutine of choose_tmpdir.
63    If BASE is non-NULL, return it.
64    Otherwise it checks if DIR is a usable directory.
65    If success, DIR is returned.
66    Otherwise NULL is returned.  */
67 
68 static inline const char *try_dir (const char *, const char *);
69 
70 static inline const char *
try_dir(const char * dir,const char * base)71 try_dir (const char *dir, const char *base)
72 {
73   if (base != 0)
74     return base;
75   if (dir != 0
76       && access (dir, R_OK | W_OK | X_OK) == 0)
77     return dir;
78   return 0;
79 }
80 
81 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
82 static const char usrtmp[] =
83 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
84 static const char vartmp[] =
85 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
86 
87 static char *memoized_tmpdir;
88 
89 /*
90 
91 @deftypefn Replacement char* choose_tmpdir ()
92 
93 Returns a pointer to a directory path suitable for creating temporary
94 files in.
95 
96 @end deftypefn
97 
98 */
99 
100 char *
choose_tmpdir(void)101 choose_tmpdir (void)
102 {
103   const char *base = 0;
104   char *tmpdir;
105   unsigned int len;
106 
107   if (memoized_tmpdir)
108     return memoized_tmpdir;
109 
110   base = try_dir (getenv ("TMPDIR"), base);
111   base = try_dir (getenv ("TMP"), base);
112   base = try_dir (getenv ("TEMP"), base);
113 
114 #ifdef P_tmpdir
115   base = try_dir (P_tmpdir, base);
116 #endif
117 
118   /* Try /var/tmp, /usr/tmp, then /tmp.  */
119   base = try_dir (vartmp, base);
120   base = try_dir (usrtmp, base);
121   base = try_dir (tmp, base);
122 
123   /* If all else fails, use the current directory!  */
124   if (base == 0)
125     base = ".";
126 
127   /* Append DIR_SEPARATOR to the directory we've chosen
128      and return it.  */
129   len = strlen (base);
130   tmpdir = XNEWVEC (char, len + 2);
131   strlcpy (tmpdir, base, len + 2);
132   tmpdir[len] = DIR_SEPARATOR;
133   tmpdir[len+1] = '\0';
134 
135   memoized_tmpdir = tmpdir;
136   return tmpdir;
137 }
138 
139 /*
140 
141 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
142 
143 Return a temporary file name (as a string) or @code{NULL} if unable to
144 create one.  @var{suffix} is a suffix to append to the file name.  The
145 string is @code{malloc}ed, and the temporary file has been created.
146 
147 @end deftypefn
148 
149 */
150 
151 char *
make_temp_file(const char * suffix)152 make_temp_file (const char *suffix)
153 {
154   const char *base = choose_tmpdir ();
155   char *temp_filename;
156   int base_len, suffix_len;
157   int fd;
158 
159   if (suffix == 0)
160     suffix = "";
161 
162   base_len = strlen (base);
163   suffix_len = strlen (suffix);
164 
165   temp_filename = XNEWVEC (char, base_len
166 			   + TEMP_FILE_LEN
167 			   + suffix_len + 1);
168   strlcpy (temp_filename, base, base_len + TEMP_FILE_LEN + suffix_len + 1);
169   strlcat (temp_filename, TEMP_FILE, base_len + TEMP_FILE_LEN + suffix_len + 1);
170   strlcat (temp_filename, suffix, base_len + TEMP_FILE_LEN + suffix_len + 1);
171 
172   fd = mkstemps (temp_filename, suffix_len);
173   /* If mkstemps failed, then something bad is happening.  Maybe we should
174      issue a message about a possible security attack in progress?  */
175   if (fd == -1)
176     abort ();
177   /* Similarly if we can not close the file.  */
178   if (close (fd))
179     abort ();
180   return temp_filename;
181 }
182