1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "file-name.h"
26 #include "make-file.h"
27 #include <libpspp/message.h>
28 #include <libpspp/alloc.h>
31 #define _(msgid) gettext (msgid)
33 /* Non ansi compilers may set this */
35 #define P_tmpdir "/tmp"
38 /* Creates a temporary file and stores its name in *FILE_NAME and
39 a file descriptor for it in *FD. Returns success. Caller is
40 responsible for freeing *FILE_NAME. */
42 make_temp_file (int *fd, char **file_name)
44 const char *parent_dir;
46 assert (file_name != NULL);
49 if (getenv ("TMPDIR") != NULL)
50 parent_dir = getenv ("TMPDIR");
52 parent_dir = P_tmpdir;
54 *file_name = xmalloc (strlen (parent_dir) + 32);
55 sprintf (*file_name, "%s/psppXXXXXX", parent_dir);
56 *fd = mkstemp (*file_name);
59 msg (ME, _("%s: Creating temporary file: %s."),
60 *file_name, strerror (errno));
69 /* Creates a temporary file and stores its name in *FILE_NAME and
70 a file stream for it in *FP. Returns success. Caller is
71 responsible for freeing *FILE_NAME and for closing *FP */
73 make_unique_file_stream (FILE **fp, char **file_name)
75 static int serial = 0;
76 const char *parent_dir;
80 Need to check for pre-existing file name.
81 Need also to pass in the directory instead of using /tmp
84 assert (file_name != NULL);
87 if (getenv ("TMPDIR") != NULL)
88 parent_dir = getenv ("TMPDIR");
90 parent_dir = P_tmpdir;
92 *file_name = xmalloc (strlen (parent_dir) + 32);
95 sprintf (*file_name, "%s/pspp%d.png", parent_dir, serial++);
97 *fp = fopen(*file_name, "w");
101 msg (ME, _("%s: Creating file: %s."), *file_name, strerror (errno));