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