1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "file-name.h"
27 #include "make-file.h"
28 #include <libpspp/message.h>
29 #include <libpspp/alloc.h>
32 #define _(msgid) gettext (msgid)
34 /* Non ansi compilers may set this */
36 #define P_tmpdir "/tmp"
39 /* Creates a temporary file and stores its name in *FILE_NAME and
40 a file descriptor for it in *FD. Returns success. Caller is
41 responsible for freeing *FILE_NAME. */
43 make_temp_file (int *fd, char **file_name)
45 const char *parent_dir;
47 assert (file_name != NULL);
50 if (getenv ("TMPDIR") != NULL)
51 parent_dir = getenv ("TMPDIR");
53 parent_dir = P_tmpdir;
55 *file_name = xmalloc (strlen (parent_dir) + 32);
56 sprintf (*file_name, "%s/psppXXXXXX", parent_dir);
57 *fd = mkstemp (*file_name);
60 msg (ME, _("%s: Creating temporary file: %s."),
61 *file_name, strerror (errno));
70 /* Creates a temporary file and stores its name in *FILE_NAME and
71 a file stream for it in *FP. Returns success. Caller is
72 responsible for freeing *FILE_NAME and for closing *FP */
74 make_unique_file_stream (FILE **fp, char **file_name)
76 static int serial = 0;
77 const char *parent_dir;
81 Need to check for pre-existing file name.
82 Need also to pass in the directory instead of using /tmp
85 assert (file_name != NULL);
88 if (getenv ("TMPDIR") != NULL)
89 parent_dir = getenv ("TMPDIR");
91 parent_dir = P_tmpdir;
93 *file_name = xmalloc (strlen (parent_dir) + 32);
96 sprintf (*file_name, "%s/pspp%d.png", parent_dir, serial++);
98 *fp = fopen(*file_name, "w");
102 msg (ME, _("%s: Creating file: %s."), *file_name, strerror (errno));