00001 /* Utility to pick a temporary filename prefix. 00002 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. 00003 00004 This file is part of the libiberty library. 00005 Libiberty is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 Libiberty is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with libiberty; see the file COPYING.LIB. If not, 00017 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. */ 00019 00020 #ifdef HAVE_CONFIG_H 00021 #include "config.h" 00022 #endif 00023 00024 #include <stdio.h> /* May get P_tmpdir. */ 00025 #ifdef HAVE_STDLIB_H 00026 #include <stdlib.h> 00027 #endif 00028 #ifdef HAVE_STRING_H 00029 #include <string.h> 00030 #endif 00031 00032 #include "libiberty.h" 00033 extern char *choose_tmpdir PARAMS ((void)); 00034 00035 /* Name of temporary file. 00036 mktemp requires 6 trailing X's. */ 00037 #define TEMP_FILE "ccXXXXXX" 00038 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) 00039 00040 /* 00041 00042 @deftypefn Extension char* choose_temp_base (void) 00043 00044 Return a prefix for temporary file names or @code{NULL} if unable to 00045 find one. The current directory is chosen if all else fails so the 00046 program is exited if a temporary directory can't be found (@code{mktemp} 00047 fails). The buffer for the result is obtained with @code{xmalloc}. 00048 00049 This function is provided for backwards compatability only. Its use is 00050 not recommended. 00051 00052 @end deftypefn 00053 00054 */ 00055 00056 char * 00057 choose_temp_base () 00058 { 00059 const char *base = choose_tmpdir (); 00060 char *temp_filename; 00061 int len; 00062 00063 len = strlen (base); 00064 temp_filename = xmalloc (len + TEMP_FILE_LEN + 1); 00065 strcpy (temp_filename, base); 00066 strcpy (temp_filename + len, TEMP_FILE); 00067 00068 mktemp (temp_filename); 00069 if (strlen (temp_filename) == 0) 00070 abort (); 00071 return temp_filename; 00072 }
1.5.6