make-temp-file.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Utility to pick a temporary filename prefix.
  2. Copyright (C) 1996, 1997, 1998, 2001, 2009, 2010
  3. Free Software Foundation, Inc.
  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. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <stdio.h> /* May get P_tmpdir. */
  21. #include <sys/types.h>
  22. #include <errno.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #ifdef HAVE_STDLIB_H
  27. #include <stdlib.h>
  28. #endif
  29. #ifdef HAVE_STRING_H
  30. #include <string.h>
  31. #endif
  32. #ifdef HAVE_SYS_FILE_H
  33. #include <sys/file.h> /* May get R_OK, etc. on some systems. */
  34. #endif
  35. #if defined(_WIN32) && !defined(__CYGWIN__)
  36. #include <windows.h>
  37. #endif
  38. #ifndef R_OK
  39. #define R_OK 4
  40. #define W_OK 2
  41. #define X_OK 1
  42. #endif
  43. #include "libiberty.h"
  44. extern int mkstemps (char *, int);
  45. /* '/' works just fine on MS-DOS based systems. */
  46. #ifndef DIR_SEPARATOR
  47. #define DIR_SEPARATOR '/'
  48. #endif
  49. /* Name of temporary file.
  50. mktemp requires 6 trailing X's. */
  51. #define TEMP_FILE "ccXXXXXX"
  52. #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
  53. #if !defined(_WIN32) || defined(__CYGWIN__)
  54. /* Subroutine of choose_tmpdir.
  55. If BASE is non-NULL, return it.
  56. Otherwise it checks if DIR is a usable directory.
  57. If success, DIR is returned.
  58. Otherwise NULL is returned. */
  59. static inline const char *try_dir (const char *, const char *);
  60. static inline const char *
  61. try_dir (const char *dir, const char *base)
  62. {
  63. if (base != 0)
  64. return base;
  65. if (dir != 0
  66. && access (dir, R_OK | W_OK | X_OK) == 0)
  67. return dir;
  68. return 0;
  69. }
  70. static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
  71. static const char usrtmp[] =
  72. { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
  73. static const char vartmp[] =
  74. { DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
  75. #endif
  76. static char *memoized_tmpdir;
  77. /*
  78. @deftypefn Replacement const char* choose_tmpdir ()
  79. Returns a pointer to a directory path suitable for creating temporary
  80. files in.
  81. @end deftypefn
  82. */
  83. const char *
  84. choose_tmpdir (void)
  85. {
  86. if (!memoized_tmpdir)
  87. {
  88. #if !defined(_WIN32) || defined(__CYGWIN__)
  89. const char *base = 0;
  90. char *tmpdir;
  91. unsigned int len;
  92. #ifdef VMS
  93. /* Try VMS standard temp logical. */
  94. base = try_dir ("/sys$scratch", base);
  95. #else
  96. base = try_dir (getenv ("TMPDIR"), base);
  97. base = try_dir (getenv ("TMP"), base);
  98. base = try_dir (getenv ("TEMP"), base);
  99. #endif
  100. #ifdef P_tmpdir
  101. /* We really want a directory name here as if concatenated with say \dir
  102. we do not end up with a double \\ which defines an UNC path. */
  103. if (strcmp (P_tmpdir, "\\") == 0)
  104. base = try_dir ("\\.", base);
  105. else
  106. base = try_dir (P_tmpdir, base);
  107. #endif
  108. /* Try /var/tmp, /usr/tmp, then /tmp. */
  109. base = try_dir (vartmp, base);
  110. base = try_dir (usrtmp, base);
  111. base = try_dir (tmp, base);
  112. /* If all else fails, use the current directory! */
  113. if (base == 0)
  114. base = ".";
  115. /* Append DIR_SEPARATOR to the directory we've chosen
  116. and return it. */
  117. len = strlen (base);
  118. tmpdir = XNEWVEC (char, len + 2);
  119. strcpy (tmpdir, base);
  120. tmpdir[len] = DIR_SEPARATOR;
  121. tmpdir[len+1] = '\0';
  122. memoized_tmpdir = tmpdir;
  123. #else /* defined(_WIN32) && !defined(__CYGWIN__) */
  124. DWORD len;
  125. /* Figure out how much space we need. */
  126. len = GetTempPath(0, NULL);
  127. if (len)
  128. {
  129. memoized_tmpdir = XNEWVEC (char, len);
  130. if (!GetTempPath(len, memoized_tmpdir))
  131. {
  132. XDELETEVEC (memoized_tmpdir);
  133. memoized_tmpdir = NULL;
  134. }
  135. }
  136. if (!memoized_tmpdir)
  137. /* If all else fails, use the current directory. */
  138. memoized_tmpdir = xstrdup (".\\");
  139. #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
  140. }
  141. return memoized_tmpdir;
  142. }
  143. /*
  144. @deftypefn Replacement char* make_temp_file (const char *@var{suffix})
  145. Return a temporary file name (as a string) or @code{NULL} if unable to
  146. create one. @var{suffix} is a suffix to append to the file name. The
  147. string is @code{malloc}ed, and the temporary file has been created.
  148. @end deftypefn
  149. */
  150. char *
  151. make_temp_file (const char *suffix)
  152. {
  153. const char *base = choose_tmpdir ();
  154. char *temp_filename;
  155. int base_len, suffix_len;
  156. int fd;
  157. if (suffix == 0)
  158. suffix = "";
  159. base_len = strlen (base);
  160. suffix_len = strlen (suffix);
  161. temp_filename = XNEWVEC (char, base_len
  162. + TEMP_FILE_LEN
  163. + suffix_len + 1);
  164. strcpy (temp_filename, base);
  165. strcpy (temp_filename + base_len, TEMP_FILE);
  166. strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
  167. fd = mkstemps (temp_filename, suffix_len);
  168. /* Mkstemps failed. It may be EPERM, ENOSPC etc. */
  169. if (fd == -1)
  170. {
  171. fprintf (stderr, "Cannot create temporary file in %s: %s\n",
  172. base, strerror (errno));
  173. abort ();
  174. }
  175. /* We abort on failed close out of sheer paranoia. */
  176. if (close (fd))
  177. abort ();
  178. return temp_filename;
  179. }