198b9484cSchristos /* Utility to pick a temporary filename prefix. 2*7e120ff0Schristos Copyright (C) 1996-2024 Free Software Foundation, Inc. 398b9484cSchristos 498b9484cSchristos This file is part of the libiberty library. 598b9484cSchristos Libiberty is free software; you can redistribute it and/or 698b9484cSchristos modify it under the terms of the GNU Library General Public 798b9484cSchristos License as published by the Free Software Foundation; either 898b9484cSchristos version 2 of the License, or (at your option) any later version. 998b9484cSchristos 1098b9484cSchristos Libiberty is distributed in the hope that it will be useful, 1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1398b9484cSchristos Library General Public License for more details. 1498b9484cSchristos 1598b9484cSchristos You should have received a copy of the GNU Library General Public 1698b9484cSchristos License along with libiberty; see the file COPYING.LIB. If not, 1798b9484cSchristos write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 1898b9484cSchristos Boston, MA 02110-1301, USA. */ 1998b9484cSchristos 2098b9484cSchristos #ifdef HAVE_CONFIG_H 2198b9484cSchristos #include "config.h" 2298b9484cSchristos #endif 2398b9484cSchristos 2498b9484cSchristos #include <stdio.h> /* May get P_tmpdir. */ 2598b9484cSchristos #include <sys/types.h> 2698b9484cSchristos #ifdef HAVE_UNISTD_H 2798b9484cSchristos #include <unistd.h> 2898b9484cSchristos #endif 2998b9484cSchristos #ifdef HAVE_STDLIB_H 3098b9484cSchristos #include <stdlib.h> 3198b9484cSchristos #endif 3298b9484cSchristos #ifdef HAVE_STRING_H 3398b9484cSchristos #include <string.h> 3498b9484cSchristos #endif 3598b9484cSchristos 3698b9484cSchristos #include "libiberty.h" 3798b9484cSchristos 3898b9484cSchristos /* Name of temporary file. 3998b9484cSchristos mktemp requires 6 trailing X's. */ 4098b9484cSchristos #define TEMP_FILE "ccXXXXXX" 4198b9484cSchristos #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) 4298b9484cSchristos 4398b9484cSchristos /* 4498b9484cSchristos 4598b9484cSchristos @deftypefn Extension char* choose_temp_base (void) 4698b9484cSchristos 4798b9484cSchristos Return a prefix for temporary file names or @code{NULL} if unable to 4898b9484cSchristos find one. The current directory is chosen if all else fails so the 4998b9484cSchristos program is exited if a temporary directory can't be found (@code{mktemp} 5098b9484cSchristos fails). The buffer for the result is obtained with @code{xmalloc}. 5198b9484cSchristos 5298b9484cSchristos This function is provided for backwards compatibility only. Its use is 5398b9484cSchristos not recommended. 5498b9484cSchristos 5598b9484cSchristos @end deftypefn 5698b9484cSchristos 5798b9484cSchristos */ 5898b9484cSchristos 5998b9484cSchristos char * 6098b9484cSchristos choose_temp_base (void) 6198b9484cSchristos { 6298b9484cSchristos const char *base = choose_tmpdir (); 6398b9484cSchristos char *temp_filename; 6498b9484cSchristos int len; 6598b9484cSchristos 6698b9484cSchristos len = strlen (base); 6798b9484cSchristos temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1); 6898b9484cSchristos strcpy (temp_filename, base); 6998b9484cSchristos strcpy (temp_filename + len, TEMP_FILE); 7098b9484cSchristos 7198b9484cSchristos if (mktemp (temp_filename) == 0) 7298b9484cSchristos abort (); 7398b9484cSchristos return temp_filename; 7498b9484cSchristos } 75