]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/make-temp-file.c
1 /* Utility to pick a temporary filename prefix.
2 Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
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.
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.
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. */
24 #include <stdio.h> /* May get P_tmpdir. */
25 #include <sys/types.h>
35 #ifdef HAVE_SYS_FILE_H
36 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
45 #include "libiberty.h"
46 extern int mkstemps
PARAMS ((char *, int));
49 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN__) && ! defined (_UWIN))
50 #define DIR_SEPARATOR '\\'
55 #define DIR_SEPARATOR '/'
58 /* On MSDOS, write temp files in current dir
59 because there's no place else we can expect to use. */
60 /* ??? Although the current directory is tried as a last resort,
61 this is left in so that on MSDOS it is preferred to /tmp on the
62 off chance that someone requires this, since that was the previous
70 /* Name of temporary file.
71 mktemp requires 6 trailing X's. */
72 #define TEMP_FILE "ccXXXXXX"
73 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
75 /* Subroutine of choose_tmpdir.
76 If BASE is non-NULL, return it.
77 Otherwise it checks if DIR is a usable directory.
78 If success, DIR is returned.
79 Otherwise NULL is returned. */
81 static const char *try PARAMS ((const char *, const char *));
85 const char *dir
, *base
;
90 && access (dir
, R_OK
| W_OK
| X_OK
) == 0)
95 static char tmp
[] = { DIR_SEPARATOR
, 't', 'm', 'p', 0 };
96 static char usrtmp
[] =
97 { DIR_SEPARATOR
, 'u', 's', 'r', DIR_SEPARATOR
, 't', 'm', 'p', 0 };
99 static char *memoized_tmpdir
;
104 const char *base
= 0;
109 return memoized_tmpdir
;
111 base
= try (getenv ("TMPDIR"), base
);
112 base
= try (getenv ("TMP"), base
);
113 base
= try (getenv ("TEMP"), base
);
116 base
= try (P_tmpdir
, base
);
119 /* Try /usr/tmp, then /tmp. */
120 base
= try (usrtmp
, base
);
121 base
= try (tmp
, base
);
123 /* If all else fails, use the current directory! */
127 /* Append DIR_SEPARATOR to the directory we've chosen
130 tmpdir
= xmalloc (len
+ 2);
131 strcpy (tmpdir
, base
);
132 tmpdir
[len
] = DIR_SEPARATOR
;
133 tmpdir
[len
+1] = '\0';
135 memoized_tmpdir
= tmpdir
;
139 /* Return a temporary file name (as a string) or NULL if unable to create
140 one. SUFFIX is a suffix to append to the file name. The string is
141 malloced, and the temporary file has been created. */
144 make_temp_file (suffix
)
147 const char *base
= choose_tmpdir ();
149 int base_len
, suffix_len
;
155 base_len
= strlen (base
);
156 suffix_len
= strlen (suffix
);
158 temp_filename
= xmalloc (base_len
161 strcpy (temp_filename
, base
);
162 strcpy (temp_filename
+ base_len
, TEMP_FILE
);
163 strcpy (temp_filename
+ base_len
+ TEMP_FILE_LEN
, suffix
);
165 fd
= mkstemps (temp_filename
, suffix_len
);
166 /* If mkstemps failed, then something bad is happening. Maybe we should
167 issue a message about a possible security attack in progress? */
170 /* Similarly if we can not close the file. */
173 return temp_filename
;