choose-temp.c revision 259694
1130803Smarcel/* Utility to pick a temporary filename prefix. 2130803Smarcel Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. 3130803Smarcel 4130803SmarcelThis file is part of the libiberty library. 5130803SmarcelLibiberty is free software; you can redistribute it and/or 6130803Smarcelmodify it under the terms of the GNU Library General Public 7130803SmarcelLicense as published by the Free Software Foundation; either 8130803Smarcelversion 2 of the License, or (at your option) any later version. 9130803Smarcel 10130803SmarcelLibiberty is distributed in the hope that it will be useful, 11130803Smarcelbut WITHOUT ANY WARRANTY; without even the implied warranty of 12130803SmarcelMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13130803SmarcelLibrary General Public License for more details. 14130803Smarcel 15130803SmarcelYou should have received a copy of the GNU Library General Public 16130803SmarcelLicense along with libiberty; see the file COPYING.LIB. If not, 17130803Smarcelwrite to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 18130803SmarcelBoston, MA 02110-1301, USA. */ 19130803Smarcel 20130803Smarcel#ifdef HAVE_CONFIG_H 21130803Smarcel#include "config.h" 22130803Smarcel#endif 23130803Smarcel 24130803Smarcel#include <stdio.h> /* May get P_tmpdir. */ 25130803Smarcel#ifdef HAVE_STDLIB_H 26130803Smarcel#include <stdlib.h> 27130803Smarcel#endif 28130803Smarcel#ifdef HAVE_STRING_H 29130803Smarcel#include <string.h> 30251858Semaste#endif 31251858Semaste 32130803Smarcel#include "libiberty.h" 33130803Smarcelextern char *choose_tmpdir (void); 34130803Smarcel 35130803Smarcel/* Name of temporary file. 36130803Smarcel mktemp requires 6 trailing X's. */ 37130803Smarcel#define TEMP_FILE "ccXXXXXX" 38130803Smarcel#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) 39130803Smarcel 40130803Smarcel/* 41130803Smarcel 42130803Smarcel@deftypefn Extension char* choose_temp_base (void) 43130803Smarcel 44130803SmarcelReturn a prefix for temporary file names or @code{NULL} if unable to 45130803Smarcelfind one. The current directory is chosen if all else fails so the 46130803Smarcelprogram is exited if a temporary directory can't be found (@code{mktemp} 47130803Smarcelfails). The buffer for the result is obtained with @code{xmalloc}. 48130803Smarcel 49130803SmarcelThis function is provided for backwards compatability only. Its use is 50130803Smarcelnot recommended. 51130803Smarcel 52130803Smarcel@end deftypefn 53130803Smarcel 54251858Semaste*/ 55251858Semaste 56130803Smarcelchar * 57130803Smarcelchoose_temp_base (void) 58130803Smarcel{ 59130803Smarcel const char *base = choose_tmpdir (); 60130803Smarcel char *temp_filename; 61130803Smarcel int len; 62130803Smarcel 63130803Smarcel len = strlen (base); 64130803Smarcel temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1); 65130803Smarcel strcpy (temp_filename, base); 66130803Smarcel strcpy (temp_filename + len, TEMP_FILE); 67130803Smarcel 68130803Smarcel if (mktemp (temp_filename) == 0) 69130803Smarcel abort (); 70130803Smarcel return temp_filename; 71130803Smarcel} 72130803Smarcel