choose-temp.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Utility to pick a temporary filename prefix.
  2. Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Libiberty is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libiberty is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libiberty; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <stdio.h> /* May get P_tmpdir. */
  20. #include <sys/types.h>
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. #include <string.h>
  29. #endif
  30. #include "libiberty.h"
  31. /* Name of temporary file.
  32. mktemp requires 6 trailing X's. */
  33. #define TEMP_FILE "ccXXXXXX"
  34. #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
  35. /*
  36. @deftypefn Extension char* choose_temp_base (void)
  37. Return a prefix for temporary file names or @code{NULL} if unable to
  38. find one. The current directory is chosen if all else fails so the
  39. program is exited if a temporary directory can't be found (@code{mktemp}
  40. fails). The buffer for the result is obtained with @code{xmalloc}.
  41. This function is provided for backwards compatibility only. Its use is
  42. not recommended.
  43. @end deftypefn
  44. */
  45. char *
  46. choose_temp_base (void)
  47. {
  48. const char *base = choose_tmpdir ();
  49. char *temp_filename;
  50. int len;
  51. len = strlen (base);
  52. temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
  53. strcpy (temp_filename, base);
  54. strcpy (temp_filename + len, TEMP_FILE);
  55. if (mktemp (temp_filename) == 0)
  56. abort ();
  57. return temp_filename;
  58. }