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>
33 #define _(msgid) gettext (msgid)
35 /* Non ansi compilers may set this */
37 #define P_tmpdir "/tmp"
40 /* Creates a temporary file and stores its name in *FILE_NAME and
41 a file descriptor for it in *FD. Returns success. Caller is
42 responsible for freeing *FILE_NAME. */
44 make_temp_file (int *fd, char **file_name)
46 const char *parent_dir;
48 assert (file_name != NULL);
51 if (getenv ("TMPDIR") != NULL)
52 parent_dir = getenv ("TMPDIR");
54 parent_dir = P_tmpdir;
56 *file_name = xmalloc (strlen (parent_dir) + 32);
57 sprintf (*file_name, "%s/psppXXXXXX", parent_dir);
58 *fd = mkstemp (*file_name);
61 msg (ME, _("%s: Creating temporary file: %s."),
62 *file_name, strerror (errno));
71 /* Creates a temporary file and stores its name in *FILE_NAME and
72 a file stream for it in *FP. Returns success. Caller is
73 responsible for freeing *FILE_NAME and for closing *FP */
75 make_unique_file_stream (FILE **fp, char **file_name)
77 static int serial = 0;
78 const char *parent_dir;
82 Need to check for pre-existing file name.
83 Need also to pass in the directory instead of using /tmp
86 assert (file_name != NULL);
89 if (getenv ("TMPDIR") != NULL)
90 parent_dir = getenv ("TMPDIR");
92 parent_dir = P_tmpdir;
94 *file_name = xmalloc (strlen (parent_dir) + 32);
97 sprintf (*file_name, "%s/pspp%d.png", parent_dir, serial++);
99 *fp = fopen(*file_name, "w");
103 msg (ME, _("%s: Creating file: %s."), *file_name, strerror (errno));