1169695Skan/* Utility to pick a temporary filename prefix.
2169695Skan   Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3169695Skan
4169695SkanThis file is part of the libiberty library.
5169695SkanLibiberty is free software; you can redistribute it and/or
6169695Skanmodify it under the terms of the GNU Library General Public
7169695SkanLicense as published by the Free Software Foundation; either
8169695Skanversion 2 of the License, or (at your option) any later version.
9169695Skan
10169695SkanLibiberty is distributed in the hope that it will be useful,
11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13169695SkanLibrary General Public License for more details.
14169695Skan
15169695SkanYou should have received a copy of the GNU Library General Public
16169695SkanLicense along with libiberty; see the file COPYING.LIB.  If not,
17169695Skanwrite to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18169695SkanBoston, MA 02110-1301, USA.  */
19169695Skan
20169695Skan#ifdef HAVE_CONFIG_H
21169695Skan#include "config.h"
22169695Skan#endif
23169695Skan
24169695Skan#include <stdio.h>	/* May get P_tmpdir.  */
25169695Skan#ifdef HAVE_STDLIB_H
26169695Skan#include <stdlib.h>
27169695Skan#endif
28169695Skan#ifdef HAVE_STRING_H
29169695Skan#include <string.h>
30169695Skan#endif
31169695Skan
32169695Skan#include "libiberty.h"
33169695Skanextern char *choose_tmpdir (void);
34169695Skan
35169695Skan/* Name of temporary file.
36169695Skan   mktemp requires 6 trailing X's.  */
37169695Skan#define TEMP_FILE "ccXXXXXX"
38169695Skan#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
39169695Skan
40169695Skan/*
41169695Skan
42169695Skan@deftypefn Extension char* choose_temp_base (void)
43169695Skan
44169695SkanReturn a prefix for temporary file names or @code{NULL} if unable to
45169695Skanfind one.  The current directory is chosen if all else fails so the
46169695Skanprogram is exited if a temporary directory can't be found (@code{mktemp}
47169695Skanfails).  The buffer for the result is obtained with @code{xmalloc}.
48169695Skan
49169695SkanThis function is provided for backwards compatability only.  Its use is
50169695Skannot recommended.
51169695Skan
52169695Skan@end deftypefn
53169695Skan
54169695Skan*/
55169695Skan
56169695Skanchar *
57169695Skanchoose_temp_base (void)
58169695Skan{
59169695Skan  const char *base = choose_tmpdir ();
60169695Skan  char *temp_filename;
61169695Skan  int len;
62169695Skan
63169695Skan  len = strlen (base);
64169695Skan  temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
65169695Skan  strcpy (temp_filename, base);
66169695Skan  strcpy (temp_filename + len, TEMP_FILE);
67169695Skan
68259694Spfg  if (mktemp (temp_filename) == 0)
69169695Skan    abort ();
70169695Skan  return temp_filename;
71169695Skan}
72