xref: /netbsd-src/external/gpl3/gcc.old/dist/libiberty/choose-temp.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* Utility to pick a temporary filename prefix.
2*8feb0f0bSmrg    Copyright (C) 1996-2020 Free Software Foundation, Inc.
31debfc3dSmrg 
41debfc3dSmrg This file is part of the libiberty library.
51debfc3dSmrg Libiberty is free software; you can redistribute it and/or
61debfc3dSmrg modify it under the terms of the GNU Library General Public
71debfc3dSmrg License as published by the Free Software Foundation; either
81debfc3dSmrg version 2 of the License, or (at your option) any later version.
91debfc3dSmrg 
101debfc3dSmrg Libiberty is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131debfc3dSmrg Library General Public License for more details.
141debfc3dSmrg 
151debfc3dSmrg You should have received a copy of the GNU Library General Public
161debfc3dSmrg License along with libiberty; see the file COPYING.LIB.  If not,
171debfc3dSmrg write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
181debfc3dSmrg Boston, MA 02110-1301, USA.  */
191debfc3dSmrg 
201debfc3dSmrg #ifdef HAVE_CONFIG_H
211debfc3dSmrg #include "config.h"
221debfc3dSmrg #endif
231debfc3dSmrg 
241debfc3dSmrg #include <stdio.h>	/* May get P_tmpdir.  */
251debfc3dSmrg #include <sys/types.h>
261debfc3dSmrg #ifdef HAVE_UNISTD_H
271debfc3dSmrg #include <unistd.h>
281debfc3dSmrg #endif
291debfc3dSmrg #ifdef HAVE_STDLIB_H
301debfc3dSmrg #include <stdlib.h>
311debfc3dSmrg #endif
321debfc3dSmrg #ifdef HAVE_STRING_H
331debfc3dSmrg #include <string.h>
341debfc3dSmrg #endif
351debfc3dSmrg 
361debfc3dSmrg #include "libiberty.h"
371debfc3dSmrg 
381debfc3dSmrg /* Name of temporary file.
391debfc3dSmrg    mktemp requires 6 trailing X's.  */
401debfc3dSmrg #define TEMP_FILE "ccXXXXXX"
411debfc3dSmrg #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
421debfc3dSmrg 
431debfc3dSmrg /*
441debfc3dSmrg 
451debfc3dSmrg @deftypefn Extension char* choose_temp_base (void)
461debfc3dSmrg 
471debfc3dSmrg Return a prefix for temporary file names or @code{NULL} if unable to
481debfc3dSmrg find one.  The current directory is chosen if all else fails so the
491debfc3dSmrg program is exited if a temporary directory can't be found (@code{mktemp}
501debfc3dSmrg fails).  The buffer for the result is obtained with @code{xmalloc}.
511debfc3dSmrg 
521debfc3dSmrg This function is provided for backwards compatibility only.  Its use is
531debfc3dSmrg not recommended.
541debfc3dSmrg 
551debfc3dSmrg @end deftypefn
561debfc3dSmrg 
571debfc3dSmrg */
581debfc3dSmrg 
591debfc3dSmrg char *
choose_temp_base(void)601debfc3dSmrg choose_temp_base (void)
611debfc3dSmrg {
621debfc3dSmrg   const char *base = choose_tmpdir ();
631debfc3dSmrg   char *temp_filename;
641debfc3dSmrg   int len;
651debfc3dSmrg 
661debfc3dSmrg   len = strlen (base);
671debfc3dSmrg   temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
681debfc3dSmrg   strcpy (temp_filename, base);
691debfc3dSmrg   strcpy (temp_filename + len, TEMP_FILE);
701debfc3dSmrg 
711debfc3dSmrg   if (mktemp (temp_filename) == 0)
721debfc3dSmrg     abort ();
731debfc3dSmrg   return temp_filename;
741debfc3dSmrg }
75