8b471f0f5f0d982e32ee704333d9455132a0fe9b
[pspp-builds.git] / src / mkfile.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include "mkfile.h"
28 #include "error.h"
29 #include "alloc.h"
30
31 /* Creates a temporary file and stores its name in *FILENAME and
32    a file descriptor for it in *FD.  Returns success.  Caller is
33    responsible for freeing *FILENAME. */
34 int
35 make_temp_file (int *fd, char **filename)
36 {
37   const char *parent_dir;
38
39   assert (filename != NULL);
40   assert (fd != NULL);
41
42   if (getenv ("TMPDIR") != NULL)
43     parent_dir = getenv ("TMPDIR");
44   else
45     parent_dir = P_tmpdir;
46
47   *filename = xmalloc (strlen (parent_dir) + 32);
48   sprintf (*filename, "%s%cpsppXXXXXX", parent_dir, DIR_SEPARATOR);
49   *fd = mkstemp (*filename);
50   if (*fd < 0)
51     {
52       msg (FE, _("%s: Creating temporary file: %s."),
53            *filename, strerror (errno));
54       free (*filename);
55       *filename = NULL;
56       return 0;
57     }
58   return 1;
59 }
60
61
62 /* Creates a temporary file and stores its name in *FILENAME and
63    a file stream for it in *FP.  Returns success.  Caller is
64    responsible for freeing *FILENAME and for closing *FP */
65 int
66 make_unique_file_stream (FILE **fp, char **filename)
67 {
68   static int serial = 0;
69   const char *parent_dir;
70
71
72   /* FIXME: 
73      Need to check for pre-existing file name.
74      Need also to pass in the directory instead of using /tmp 
75   */
76
77   assert (filename != NULL);
78   assert (fp != NULL);
79
80   if (getenv ("TMPDIR") != NULL)
81     parent_dir = getenv ("TMPDIR");
82   else
83     parent_dir = P_tmpdir;
84
85   *filename = xmalloc (strlen (parent_dir) + 32);
86
87
88   sprintf (*filename, "%s%cpspp%d.png", parent_dir, DIR_SEPARATOR, serial++);
89
90   *fp = fopen(*filename, "w");
91
92   if (! *fp )
93     {
94       msg (FE, _("%s: Creating file: %s."), *filename, strerror (errno));
95       free (*filename);
96       *filename = NULL;
97       return 0;
98     }
99
100   return 1;
101 }
102
103
104
105