1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Functions for temporary files that honor $TMPDIR. */
21 #include "libpspp/temp-file.h"
25 #include "gl/clean-temp.h"
26 #include "gl/xvasprintf.h"
28 /* Creates and returns a new temporary file that will be removed automatically
29 when the process exits. The file is opened in mode "wb+". To close the
30 file before the process exits, use close_temp_file() to ensure that it gets
33 Returns NULL if creating the temporary file fails.
35 This is similar to tmpfile(), except:
37 - It honors the $TMPDIR environment variable.
39 - The file will not be automatically deleted upon close. You have to call
40 close_temp_file() if you want it to be deleted before the process exits.
43 create_temp_file (void)
46 static struct temp_dir *temp_dir;
52 temp_dir = create_temp_dir ("pspp", NULL, true);
57 file_name = xasprintf ("%s/%d", temp_dir->dir_name, idx++);
58 stream = fopen_temp (file_name, "wb+");
64 /* Closes and removes a temporary file created by create_temp_file(). */
66 close_temp_file (FILE *file)