Changed include paths to be explicitly specified in the #include directive.
[pspp-builds.git] / src / data / make-file.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 #include "filename.h"
27 #include "make-file.h"
28 #include <libpspp/message.h>
29 #include <libpspp/alloc.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 /* Non ansi compilers may set this */
35 #ifndef P_tmpdir
36 #define P_tmpdir "/tmp"
37 #endif
38
39 /* Creates a temporary file and stores its name in *FILENAME and
40    a file descriptor for it in *FD.  Returns success.  Caller is
41    responsible for freeing *FILENAME. */
42 int
43 make_temp_file (int *fd, char **filename)
44 {
45   const char *parent_dir;
46
47   assert (filename != NULL);
48   assert (fd != NULL);
49
50   if (getenv ("TMPDIR") != NULL)
51     parent_dir = getenv ("TMPDIR");
52   else
53     parent_dir = P_tmpdir;
54
55   *filename = xmalloc (strlen (parent_dir) + 32);
56   sprintf (*filename, "%s%cpsppXXXXXX", parent_dir, DIR_SEPARATOR);
57   *fd = mkstemp (*filename);
58   if (*fd < 0)
59     {
60       msg (ME, _("%s: Creating temporary file: %s."),
61            *filename, strerror (errno));
62       free (*filename);
63       *filename = NULL;
64       return 0;
65     }
66   return 1;
67 }
68
69
70 /* Creates a temporary file and stores its name in *FILENAME and
71    a file stream for it in *FP.  Returns success.  Caller is
72    responsible for freeing *FILENAME and for closing *FP */
73 int
74 make_unique_file_stream (FILE **fp, char **filename)
75 {
76   static int serial = 0;
77   const char *parent_dir;
78
79
80   /* FIXME: 
81      Need to check for pre-existing file name.
82      Need also to pass in the directory instead of using /tmp 
83   */
84
85   assert (filename != NULL);
86   assert (fp != NULL);
87
88   if (getenv ("TMPDIR") != NULL)
89     parent_dir = getenv ("TMPDIR");
90   else
91     parent_dir = P_tmpdir;
92
93   *filename = xmalloc (strlen (parent_dir) + 32);
94
95
96   sprintf (*filename, "%s%cpspp%d.png", parent_dir, DIR_SEPARATOR, serial++);
97
98   *fp = fopen(*filename, "w");
99
100   if (! *fp )
101     {
102       msg (ME, _("%s: Creating file: %s."), *filename, strerror (errno));
103       free (*filename);
104       *filename = NULL;
105       return 0;
106     }
107
108   return 1;
109 }
110
111
112
113