xref: /openbsd/gnu/lib/libiberty/src/make-temp-file.c (revision 20fce977)
19588ddcfSespie /* Utility to pick a temporary filename prefix.
29588ddcfSespie    Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
39588ddcfSespie 
49588ddcfSespie This file is part of the libiberty library.
59588ddcfSespie Libiberty is free software; you can redistribute it and/or
69588ddcfSespie modify it under the terms of the GNU Library General Public
79588ddcfSespie License as published by the Free Software Foundation; either
89588ddcfSespie version 2 of the License, or (at your option) any later version.
99588ddcfSespie 
109588ddcfSespie Libiberty is distributed in the hope that it will be useful,
119588ddcfSespie but WITHOUT ANY WARRANTY; without even the implied warranty of
129588ddcfSespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139588ddcfSespie Library General Public License for more details.
149588ddcfSespie 
159588ddcfSespie You should have received a copy of the GNU Library General Public
169588ddcfSespie License along with libiberty; see the file COPYING.LIB.  If not,
17*20fce977Smiod write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*20fce977Smiod Boston, MA 02110-1301, USA.  */
199588ddcfSespie 
209588ddcfSespie #ifdef HAVE_CONFIG_H
219588ddcfSespie #include "config.h"
229588ddcfSespie #endif
239588ddcfSespie 
249588ddcfSespie #include <stdio.h>	/* May get P_tmpdir.  */
259588ddcfSespie #include <sys/types.h>
269588ddcfSespie #ifdef HAVE_UNISTD_H
279588ddcfSespie #include <unistd.h>
289588ddcfSespie #endif
299588ddcfSespie #ifdef HAVE_STDLIB_H
309588ddcfSespie #include <stdlib.h>
319588ddcfSespie #endif
329588ddcfSespie #ifdef HAVE_STRING_H
339588ddcfSespie #include <string.h>
349588ddcfSespie #endif
359588ddcfSespie #ifdef HAVE_SYS_FILE_H
369588ddcfSespie #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
379588ddcfSespie #endif
389588ddcfSespie 
399588ddcfSespie #ifndef R_OK
409588ddcfSespie #define R_OK 4
419588ddcfSespie #define W_OK 2
429588ddcfSespie #define X_OK 1
439588ddcfSespie #endif
449588ddcfSespie 
459588ddcfSespie #include "libiberty.h"
46*20fce977Smiod extern int mkstemps (char *, int);
479588ddcfSespie 
489588ddcfSespie /* '/' works just fine on MS-DOS based systems.  */
499588ddcfSespie #ifndef DIR_SEPARATOR
509588ddcfSespie #define DIR_SEPARATOR '/'
519588ddcfSespie #endif
529588ddcfSespie 
539588ddcfSespie /* Name of temporary file.
549588ddcfSespie    mktemp requires 6 trailing X's.  */
559588ddcfSespie #define TEMP_FILE "ccXXXXXX"
569588ddcfSespie #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
579588ddcfSespie 
589588ddcfSespie /* Subroutine of choose_tmpdir.
599588ddcfSespie    If BASE is non-NULL, return it.
609588ddcfSespie    Otherwise it checks if DIR is a usable directory.
619588ddcfSespie    If success, DIR is returned.
629588ddcfSespie    Otherwise NULL is returned.  */
639588ddcfSespie 
64*20fce977Smiod static inline const char *try_dir (const char *, const char *);
659588ddcfSespie 
669588ddcfSespie static inline const char *
try_dir(const char * dir,const char * base)67*20fce977Smiod try_dir (const char *dir, const char *base)
689588ddcfSespie {
699588ddcfSespie   if (base != 0)
709588ddcfSespie     return base;
719588ddcfSespie   if (dir != 0
729588ddcfSespie       && access (dir, R_OK | W_OK | X_OK) == 0)
739588ddcfSespie     return dir;
749588ddcfSespie   return 0;
759588ddcfSespie }
769588ddcfSespie 
779588ddcfSespie static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
789588ddcfSespie static const char usrtmp[] =
799588ddcfSespie { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
809588ddcfSespie static const char vartmp[] =
819588ddcfSespie { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
829588ddcfSespie 
839588ddcfSespie static char *memoized_tmpdir;
849588ddcfSespie 
859588ddcfSespie /*
869588ddcfSespie 
879588ddcfSespie @deftypefn Replacement char* choose_tmpdir ()
889588ddcfSespie 
899588ddcfSespie Returns a pointer to a directory path suitable for creating temporary
909588ddcfSespie files in.
919588ddcfSespie 
929588ddcfSespie @end deftypefn
939588ddcfSespie 
949588ddcfSespie */
959588ddcfSespie 
969588ddcfSespie char *
choose_tmpdir(void)97*20fce977Smiod choose_tmpdir (void)
989588ddcfSespie {
999588ddcfSespie   const char *base = 0;
1009588ddcfSespie   char *tmpdir;
1019588ddcfSespie   unsigned int len;
1029588ddcfSespie 
1039588ddcfSespie   if (memoized_tmpdir)
1049588ddcfSespie     return memoized_tmpdir;
1059588ddcfSespie 
106*20fce977Smiod   base = try_dir (getenv ("TMPDIR"), base);
107*20fce977Smiod   base = try_dir (getenv ("TMP"), base);
108*20fce977Smiod   base = try_dir (getenv ("TEMP"), base);
1099588ddcfSespie 
1109588ddcfSespie #ifdef P_tmpdir
111*20fce977Smiod   base = try_dir (P_tmpdir, base);
1129588ddcfSespie #endif
1139588ddcfSespie 
1149588ddcfSespie   /* Try /var/tmp, /usr/tmp, then /tmp.  */
115*20fce977Smiod   base = try_dir (vartmp, base);
116*20fce977Smiod   base = try_dir (usrtmp, base);
117*20fce977Smiod   base = try_dir (tmp, base);
1189588ddcfSespie 
1199588ddcfSespie   /* If all else fails, use the current directory!  */
1209588ddcfSespie   if (base == 0)
1219588ddcfSespie     base = ".";
1229588ddcfSespie 
1239588ddcfSespie   /* Append DIR_SEPARATOR to the directory we've chosen
1249588ddcfSespie      and return it.  */
1259588ddcfSespie   len = strlen (base);
126*20fce977Smiod   tmpdir = XNEWVEC (char, len + 2);
1279588ddcfSespie   strcpy (tmpdir, base);
1289588ddcfSespie   tmpdir[len] = DIR_SEPARATOR;
1299588ddcfSespie   tmpdir[len+1] = '\0';
1309588ddcfSespie 
1319588ddcfSespie   memoized_tmpdir = tmpdir;
1329588ddcfSespie   return tmpdir;
1339588ddcfSespie }
1349588ddcfSespie 
1359588ddcfSespie /*
1369588ddcfSespie 
1379588ddcfSespie @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
1389588ddcfSespie 
1399588ddcfSespie Return a temporary file name (as a string) or @code{NULL} if unable to
1409588ddcfSespie create one.  @var{suffix} is a suffix to append to the file name.  The
1419588ddcfSespie string is @code{malloc}ed, and the temporary file has been created.
1429588ddcfSespie 
1439588ddcfSespie @end deftypefn
1449588ddcfSespie 
1459588ddcfSespie */
1469588ddcfSespie 
1479588ddcfSespie char *
make_temp_file(const char * suffix)148*20fce977Smiod make_temp_file (const char *suffix)
1499588ddcfSespie {
1509588ddcfSespie   const char *base = choose_tmpdir ();
1519588ddcfSespie   char *temp_filename;
1529588ddcfSespie   int base_len, suffix_len;
1539588ddcfSespie   int fd;
1549588ddcfSespie 
1559588ddcfSespie   if (suffix == 0)
1569588ddcfSespie     suffix = "";
1579588ddcfSespie 
1589588ddcfSespie   base_len = strlen (base);
1599588ddcfSespie   suffix_len = strlen (suffix);
1609588ddcfSespie 
161*20fce977Smiod   temp_filename = XNEWVEC (char, base_len
1629588ddcfSespie 			   + TEMP_FILE_LEN
1639588ddcfSespie 			   + suffix_len + 1);
1649588ddcfSespie   strcpy (temp_filename, base);
1659588ddcfSespie   strcpy (temp_filename + base_len, TEMP_FILE);
1669588ddcfSespie   strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
1679588ddcfSespie 
1689588ddcfSespie   fd = mkstemps (temp_filename, suffix_len);
1699588ddcfSespie   /* If mkstemps failed, then something bad is happening.  Maybe we should
1709588ddcfSespie      issue a message about a possible security attack in progress?  */
1719588ddcfSespie   if (fd == -1)
1729588ddcfSespie     abort ();
1739588ddcfSespie   /* Similarly if we can not close the file.  */
1749588ddcfSespie   if (close (fd))
1759588ddcfSespie     abort ();
1769588ddcfSespie   return temp_filename;
1779588ddcfSespie }
178