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