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