1*fae548d3Szrj /* Utility to pick a temporary filename prefix.
2*fae548d3Szrj Copyright (C) 1996-2020 Free Software Foundation, Inc.
3*fae548d3Szrj
4*fae548d3Szrj This file is part of the libiberty library.
5*fae548d3Szrj Libiberty is free software; you can redistribute it and/or
6*fae548d3Szrj modify it under the terms of the GNU Library General Public
7*fae548d3Szrj License as published by the Free Software Foundation; either
8*fae548d3Szrj version 2 of the License, or (at your option) any later version.
9*fae548d3Szrj
10*fae548d3Szrj Libiberty is distributed in the hope that it will be useful,
11*fae548d3Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
12*fae548d3Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*fae548d3Szrj Library General Public License for more details.
14*fae548d3Szrj
15*fae548d3Szrj You should have received a copy of the GNU Library General Public
16*fae548d3Szrj License along with libiberty; see the file COPYING.LIB. If not,
17*fae548d3Szrj write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*fae548d3Szrj Boston, MA 02110-1301, USA. */
19*fae548d3Szrj
20*fae548d3Szrj #ifdef HAVE_CONFIG_H
21*fae548d3Szrj #include "config.h"
22*fae548d3Szrj #endif
23*fae548d3Szrj
24*fae548d3Szrj #include <stdio.h> /* May get P_tmpdir. */
25*fae548d3Szrj #include <sys/types.h>
26*fae548d3Szrj #ifdef HAVE_UNISTD_H
27*fae548d3Szrj #include <unistd.h>
28*fae548d3Szrj #endif
29*fae548d3Szrj #ifdef HAVE_STDLIB_H
30*fae548d3Szrj #include <stdlib.h>
31*fae548d3Szrj #endif
32*fae548d3Szrj #ifdef HAVE_STRING_H
33*fae548d3Szrj #include <string.h>
34*fae548d3Szrj #endif
35*fae548d3Szrj
36*fae548d3Szrj #include "libiberty.h"
37*fae548d3Szrj
38*fae548d3Szrj /* Name of temporary file.
39*fae548d3Szrj mktemp requires 6 trailing X's. */
40*fae548d3Szrj #define TEMP_FILE "ccXXXXXX"
41*fae548d3Szrj #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
42*fae548d3Szrj
43*fae548d3Szrj /*
44*fae548d3Szrj
45*fae548d3Szrj @deftypefn Extension char* choose_temp_base (void)
46*fae548d3Szrj
47*fae548d3Szrj Return a prefix for temporary file names or @code{NULL} if unable to
48*fae548d3Szrj find one. The current directory is chosen if all else fails so the
49*fae548d3Szrj program is exited if a temporary directory can't be found (@code{mktemp}
50*fae548d3Szrj fails). The buffer for the result is obtained with @code{xmalloc}.
51*fae548d3Szrj
52*fae548d3Szrj This function is provided for backwards compatibility only. Its use is
53*fae548d3Szrj not recommended.
54*fae548d3Szrj
55*fae548d3Szrj @end deftypefn
56*fae548d3Szrj
57*fae548d3Szrj */
58*fae548d3Szrj
59*fae548d3Szrj char *
choose_temp_base(void)60*fae548d3Szrj choose_temp_base (void)
61*fae548d3Szrj {
62*fae548d3Szrj const char *base = choose_tmpdir ();
63*fae548d3Szrj char *temp_filename;
64*fae548d3Szrj int len;
65*fae548d3Szrj
66*fae548d3Szrj len = strlen (base);
67*fae548d3Szrj temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
68*fae548d3Szrj strcpy (temp_filename, base);
69*fae548d3Szrj strcpy (temp_filename + len, TEMP_FILE);
70*fae548d3Szrj
71*fae548d3Szrj if (mktemp (temp_filename) == 0)
72*fae548d3Szrj abort ();
73*fae548d3Szrj return temp_filename;
74*fae548d3Szrj }
75