Home | History | Annotate | Line # | Download | only in libiberty
      1 /* Utility to pick a temporary filename prefix.
      2    Copyright (C) 1996-2025 Free Software Foundation, Inc.
      3 
      4 This file is part of the libiberty library.
      5 Libiberty is free software; you can redistribute it and/or
      6 modify it under the terms of the GNU Library General Public
      7 License as published by the Free Software Foundation; either
      8 version 2 of the License, or (at your option) any later version.
      9 
     10 Libiberty is distributed in the hope that it will be useful,
     11 but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13 Library General Public License for more details.
     14 
     15 You should have received a copy of the GNU Library General Public
     16 License along with libiberty; see the file COPYING.LIB.  If not,
     17 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
     18 Boston, MA 02110-1301, USA.  */
     19 
     20 #ifdef HAVE_CONFIG_H
     21 #include "config.h"
     22 #endif
     23 
     24 #include <stdio.h>	/* May get P_tmpdir.  */
     25 #include <sys/types.h>
     26 #include <errno.h>
     27 #ifdef HAVE_UNISTD_H
     28 #include <unistd.h>
     29 #endif
     30 #ifdef HAVE_STDLIB_H
     31 #include <stdlib.h>
     32 #endif
     33 #ifdef HAVE_STRING_H
     34 #include <string.h>
     35 #endif
     36 #ifdef HAVE_SYS_FILE_H
     37 #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
     38 #endif
     39 #if defined(_WIN32) && !defined(__CYGWIN__)
     40 #define WIN32_LEAN_AND_MEAN
     41 #include <windows.h>
     42 #endif
     43 #if HAVE_SYS_STAT_H
     44 #include <sys/stat.h>
     45 #endif
     46 
     47 
     48 #ifndef R_OK
     49 #define R_OK 4
     50 #define W_OK 2
     51 #define X_OK 1
     52 #endif
     53 
     54 #include "libiberty.h"
     55 extern int mkstemps (char *, int);
     56 
     57 /* '/' works just fine on MS-DOS based systems.  */
     58 #ifndef DIR_SEPARATOR
     59 #define DIR_SEPARATOR '/'
     60 #endif
     61 
     62 /* Name of temporary file.
     63    mktemp requires 6 trailing X's.  */
     64 #define TEMP_FILE "XXXXXX"
     65 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
     66 
     67 #if !defined(_WIN32) || defined(__CYGWIN__)
     68 
     69 /* Subroutine of choose_tmpdir.
     70    If BASE is non-NULL, return it.
     71    Otherwise it checks if DIR is a usable directory.
     72    If success, DIR is returned.
     73    Otherwise NULL is returned.  */
     74 
     75 static inline const char *try_dir (const char *, const char *);
     76 
     77 static inline const char *
     78 try_dir (const char *dir, const char *base)
     79 {
     80   if (base != 0)
     81     return base;
     82   if (dir != 0
     83       && access (dir, R_OK | W_OK | X_OK) == 0)
     84     {
     85       /* Check to make sure dir is actually a directory. */
     86 #ifdef S_ISDIR
     87       struct stat s;
     88       if (stat (dir, &s))
     89 	return NULL;
     90       if (!S_ISDIR (s.st_mode))
     91 	return NULL;
     92 #endif
     93       return dir;
     94     }
     95   return 0;
     96 }
     97 
     98 static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
     99 static const char usrtmp[] =
    100 { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
    101 static const char vartmp[] =
    102 { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
    103 
    104 #endif
    105 
    106 static char *memoized_tmpdir;
    107 
    108 /*
    109 
    110 @deftypefn Replacement const char* choose_tmpdir ()
    111 
    112 Returns a pointer to a directory path suitable for creating temporary
    113 files in.
    114 
    115 @end deftypefn
    116 
    117 */
    118 
    119 const char *
    120 choose_tmpdir (void)
    121 {
    122   if (!memoized_tmpdir)
    123     {
    124 #if !defined(_WIN32) || defined(__CYGWIN__)
    125       const char *base = 0;
    126       char *tmpdir;
    127       unsigned int len;
    128 
    129 #ifdef VMS
    130       /* Try VMS standard temp logical.  */
    131       base = try_dir ("/sys$scratch", base);
    132 #else
    133       base = try_dir (getenv ("TMPDIR"), base);
    134       base = try_dir (getenv ("TMP"), base);
    135       base = try_dir (getenv ("TEMP"), base);
    136 #endif
    137 
    138 #ifdef P_tmpdir
    139       /* We really want a directory name here as if concatenated with say \dir
    140 	 we do not end up with a double \\ which defines an UNC path.  */
    141       if (strcmp (P_tmpdir, "\\") == 0)
    142 	base = try_dir ("\\.", base);
    143       else
    144 	base = try_dir (P_tmpdir, base);
    145 #endif
    146 
    147       /* Try /tmp, /var/tmp, then /usr/tmp.  */
    148       base = try_dir (tmp, base);
    149       base = try_dir (vartmp, base);
    150       base = try_dir (usrtmp, base);
    151 
    152       /* If all else fails, use the current directory!  */
    153       if (base == 0)
    154 	base = ".";
    155       /* Append DIR_SEPARATOR to the directory we've chosen
    156 	 and return it.  */
    157       len = strlen (base);
    158       tmpdir = XNEWVEC (char, len + 2);
    159       strcpy (tmpdir, base);
    160       tmpdir[len] = DIR_SEPARATOR;
    161       tmpdir[len+1] = '\0';
    162       memoized_tmpdir = tmpdir;
    163 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
    164       DWORD len;
    165 
    166       /* Figure out how much space we need.  */
    167       len = GetTempPath(0, NULL);
    168       if (len)
    169 	{
    170 	  memoized_tmpdir = XNEWVEC (char, len);
    171 	  if (!GetTempPath(len, memoized_tmpdir))
    172 	    {
    173 	      XDELETEVEC (memoized_tmpdir);
    174 	      memoized_tmpdir = NULL;
    175 	    }
    176 	}
    177       if (!memoized_tmpdir)
    178 	/* If all else fails, use the current directory.  */
    179 	memoized_tmpdir = xstrdup (".\\");
    180 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
    181     }
    182 
    183   return memoized_tmpdir;
    184 }
    185 
    186 /*
    187 
    188 @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
    189 
    190 Return a temporary file name (as a string) or @code{NULL} if unable to
    191 create one.  @var{suffix} is a suffix to append to the file name.  The
    192 string is @code{malloc}ed, and the temporary file has been created.
    193 
    194 @end deftypefn
    195 
    196 */
    197 
    198 char *
    199 make_temp_file_with_prefix (const char *prefix, const char *suffix)
    200 {
    201   const char *base = choose_tmpdir ();
    202   char *temp_filename;
    203   int base_len, suffix_len, prefix_len;
    204   int fd;
    205 
    206   if (prefix == 0)
    207     prefix = "cc";
    208 
    209   if (suffix == 0)
    210     suffix = "";
    211 
    212   base_len = strlen (base);
    213   prefix_len = strlen (prefix);
    214   suffix_len = strlen (suffix);
    215 
    216   temp_filename = XNEWVEC (char, base_len
    217 			   + TEMP_FILE_LEN
    218 			   + suffix_len
    219 			   + prefix_len + 1);
    220   strcpy (temp_filename, base);
    221   strcpy (temp_filename + base_len, prefix);
    222   strcpy (temp_filename + base_len + prefix_len, TEMP_FILE);
    223   strcpy (temp_filename + base_len + prefix_len + TEMP_FILE_LEN, suffix);
    224 
    225   fd = mkstemps (temp_filename, suffix_len);
    226   /* Mkstemps failed.  It may be EPERM, ENOSPC etc.  */
    227   if (fd == -1)
    228     {
    229       fprintf (stderr, "Cannot create temporary file in %s: %s\n",
    230 	       base, strerror (errno));
    231       abort ();
    232     }
    233   /* We abort on failed close out of sheer paranoia.  */
    234   if (close (fd))
    235     abort ();
    236   return temp_filename;
    237 }
    238 
    239 char *
    240 make_temp_file (const char *suffix)
    241 {
    242   return make_temp_file_with_prefix (NULL, suffix);
    243 }
    244