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