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