133965Sjdp/* Utility to pick a temporary filename prefix.
238889Sjdp   Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
333965Sjdp
433965SjdpThis file is part of the libiberty library.
533965SjdpLibiberty is free software; you can redistribute it and/or
633965Sjdpmodify it under the terms of the GNU Library General Public
733965SjdpLicense as published by the Free Software Foundation; either
833965Sjdpversion 2 of the License, or (at your option) any later version.
933965Sjdp
1033965SjdpLibiberty is distributed in the hope that it will be useful,
1133965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1233965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1333965SjdpLibrary General Public License for more details.
1433965Sjdp
1533965SjdpYou should have received a copy of the GNU Library General Public
1633965SjdpLicense along with libiberty; see the file COPYING.LIB.  If not,
17218822Sdimwrite to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18218822SdimBoston, MA 02110-1301, USA.  */
1933965Sjdp
2060484Sobrien#ifdef HAVE_CONFIG_H
2138889Sjdp#include "config.h"
2238889Sjdp#endif
2338889Sjdp
2460484Sobrien#include <stdio.h>	/* May get P_tmpdir.  */
2560484Sobrien#ifdef HAVE_STDLIB_H
2660484Sobrien#include <stdlib.h>
2760484Sobrien#endif
2860484Sobrien#ifdef HAVE_STRING_H
2960484Sobrien#include <string.h>
3060484Sobrien#endif
3133965Sjdp
3260484Sobrien#include "libiberty.h"
33218822Sdimextern char *choose_tmpdir (void);
3433965Sjdp
3533965Sjdp/* Name of temporary file.
3633965Sjdp   mktemp requires 6 trailing X's.  */
3733965Sjdp#define TEMP_FILE "ccXXXXXX"
3889857Sobrien#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
3933965Sjdp
4089857Sobrien/*
4133965Sjdp
4289857Sobrien@deftypefn Extension char* choose_temp_base (void)
4377298Sobrien
4489857SobrienReturn a prefix for temporary file names or @code{NULL} if unable to
4589857Sobrienfind one.  The current directory is chosen if all else fails so the
4689857Sobrienprogram is exited if a temporary directory can't be found (@code{mktemp}
4789857Sobrienfails).  The buffer for the result is obtained with @code{xmalloc}.
4833965Sjdp
49218822SdimThis function is provided for backwards compatibility only.  Its use is
5089857Sobriennot recommended.
5133965Sjdp
5289857Sobrien@end deftypefn
5360484Sobrien
5489857Sobrien*/
5589857Sobrien
5633965Sjdpchar *
57218822Sdimchoose_temp_base (void)
5833965Sjdp{
5989857Sobrien  const char *base = choose_tmpdir ();
6033965Sjdp  char *temp_filename;
6133965Sjdp  int len;
6233965Sjdp
6333965Sjdp  len = strlen (base);
64218822Sdim  temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
6533965Sjdp  strcpy (temp_filename, base);
6633965Sjdp  strcpy (temp_filename + len, TEMP_FILE);
6733965Sjdp
68218822Sdim  if (mktemp (temp_filename) == 0)
6933965Sjdp    abort ();
7033965Sjdp  return temp_filename;
7133965Sjdp}
72