Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 1 | /* Utility to pick a temporary filename prefix. |
Alan Modra | 250d07d | 2021-01-01 09:28:58 +1030 | [diff] [blame] | 2 | Copyright (C) 1996-2021 Free Software Foundation, Inc. |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 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, |
Nick Clifton | 979c05d | 2005-05-10 15:33:34 +0000 | [diff] [blame] | 17 | write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
| 18 | Boston, MA 02110-1301, USA. */ |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 19 | |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include "config.h" |
| 22 | #endif |
| 23 | |
| 24 | #include <stdio.h> /* May get P_tmpdir. */ |
Kai Tietz | 074d710 | 2009-09-22 16:33:56 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #ifdef HAVE_UNISTD_H |
| 27 | #include <unistd.h> |
| 28 | #endif |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 29 | #ifdef HAVE_STDLIB_H |
| 30 | #include <stdlib.h> |
| 31 | #endif |
Richard Henderson | 35ca97e | 1999-07-14 17:32:02 +0000 | [diff] [blame] | 32 | #ifdef HAVE_STRING_H |
| 33 | #include <string.h> |
| 34 | #endif |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 35 | |
| 36 | #include "libiberty.h" |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 37 | |
| 38 | /* Name of temporary file. |
| 39 | mktemp requires 6 trailing X's. */ |
| 40 | #define TEMP_FILE "ccXXXXXX" |
DJ Delorie | 1dffcc6 | 2001-03-21 20:12:30 +0000 | [diff] [blame] | 41 | #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1) |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 42 | |
DJ Delorie | ba19b94 | 2001-10-16 02:55:31 +0000 | [diff] [blame] | 43 | /* |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 44 | |
DJ Delorie | 5d85240 | 2001-10-17 22:35:28 +0000 | [diff] [blame] | 45 | @deftypefn Extension char* choose_temp_base (void) |
DJ Delorie | ba19b94 | 2001-10-16 02:55:31 +0000 | [diff] [blame] | 46 | |
| 47 | Return a prefix for temporary file names or @code{NULL} if unable to |
| 48 | find one. The current directory is chosen if all else fails so the |
| 49 | program is exited if a temporary directory can't be found (@code{mktemp} |
| 50 | fails). The buffer for the result is obtained with @code{xmalloc}. |
| 51 | |
DJ Delorie | 6dd7f01 | 2007-01-31 20:25:23 +0000 | [diff] [blame] | 52 | This function is provided for backwards compatibility only. Its use is |
DJ Delorie | ba19b94 | 2001-10-16 02:55:31 +0000 | [diff] [blame] | 53 | not recommended. |
| 54 | |
| 55 | @end deftypefn |
| 56 | |
| 57 | */ |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 58 | |
| 59 | char * |
DJ Delorie | 9334f9c | 2005-03-27 05:28:42 +0000 | [diff] [blame] | 60 | choose_temp_base (void) |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 61 | { |
DJ Delorie | 1dffcc6 | 2001-03-21 20:12:30 +0000 | [diff] [blame] | 62 | const char *base = choose_tmpdir (); |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 63 | char *temp_filename; |
| 64 | int len; |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 65 | |
| 66 | len = strlen (base); |
DJ Delorie | abf6a75 | 2005-05-24 21:01:33 +0000 | [diff] [blame] | 67 | temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1); |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 68 | strcpy (temp_filename, base); |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 69 | strcpy (temp_filename + len, TEMP_FILE); |
| 70 | |
Ben Elliston | cc0732b | 2007-01-12 00:39:10 +0000 | [diff] [blame] | 71 | if (mktemp (temp_filename) == 0) |
Richard Henderson | 252b513 | 1999-05-03 07:29:11 +0000 | [diff] [blame] | 72 | abort (); |
| 73 | return temp_filename; |
| 74 | } |