Independent Samples T-Test Dialog: Fix Crash
[pspp.git] / src / data / file-name.c
blob18624fe33080eaad3b05b85eb16812cc18d70392
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include "data/file-name.h"
20 #include "data/file-handle-def.h"
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
29 #include <unistd.h>
31 #include "data/settings.h"
32 #include "libpspp/hash-functions.h"
33 #include "libpspp/message.h"
34 #include "libpspp/i18n.h"
35 #include "libpspp/str.h"
36 #include "libpspp/version.h"
38 #include "gl/dirname.h"
39 #include "gl/intprops.h"
40 #include "gl/minmax.h"
41 #include "gl/relocatable.h"
42 #include "gl/xalloc.h"
43 #include "gl/xmalloca.h"
45 #include "gettext.h"
46 #include "xvasprintf.h"
47 #define _(msgid) gettext (msgid)
50 /* Functions for performing operations on file names. */
53 /* Returns the extension part of FILE_NAME as a malloc()'d string.
54 If FILE_NAME does not have an extension, returns an empty
55 string. */
56 char *
57 fn_extension (const struct file_handle *fh)
59 const char *file_name = fh_get_file_name (fh);
61 const char *extension = strrchr (file_name, '.');
62 if (extension == NULL)
63 extension = "";
64 return xstrdup (extension);
67 /* Find out information about files. */
69 /* Returns true iff NAME specifies an absolute file name. */
70 static bool
71 fn_is_absolute (const char *name)
73 return IS_ABSOLUTE_FILE_NAME (name);
77 /* Searches for a file with name NAME in the directories given in
78 PATH, which is terminated by a null pointer. Returns the full name of the
79 first file found, which the caller is responsible for freeing with free(),
80 or NULL if none is found. */
81 char *
82 fn_search_path (const char *base_name, char **path)
84 size_t i;
86 if (fn_is_absolute (base_name))
87 return xstrdup (base_name);
89 for (i = 0; path[i] != NULL; i++)
91 const char *dir = path[i];
92 char *file;
94 if (!strcmp (dir, "") || !strcmp (dir, "."))
95 file = xstrdup (base_name);
96 else if (ISSLASH (dir[strlen (dir) - 1]))
97 file = xasprintf ("%s%s", dir, base_name);
98 else
99 file = xasprintf ("%s/%s", dir, base_name);
101 struct stat temp;
102 if (((stat (file, &temp) == 0) && (! S_ISDIR (temp.st_mode))))
103 return file;
105 free (file);
108 return NULL;
112 /* Returns true if file with name NAME exists, and that file is not a
113 directory */
114 bool
115 fn_exists (const struct file_handle *fh)
117 const char *name = fh_get_file_name (fh);
118 struct stat temp;
119 if (stat (name, &temp) != 0)
120 return false;
122 return ! S_ISDIR (temp.st_mode);
126 /* Basic file handling. */
128 #if HAVE_POPEN
129 /* Used for giving an error message on a set_safer security
130 violation. */
131 static FILE *
132 safety_violation (const char *fn)
134 msg (SE, _("Not opening pipe file `%s' because %s option set."), fn, "SAFER");
135 errno = EPERM;
136 return NULL;
138 #endif
140 /* File open routine that understands `-' as stdin/stdout and `|cmd'
141 as a pipe to command `cmd'. Returns resultant FILE on success,
142 NULL on failure. If NULL is returned then errno is set to a
143 sensible value. */
144 FILE *
145 fn_open (const struct file_handle *fh, const char *mode)
147 const char *fn = fh_get_file_name (fh);
149 assert (mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a');
151 if (mode[0] == 'r')
153 if (!strcmp (fn, "stdin") || !strcmp (fn, "-"))
154 return stdin;
156 else
158 if (!strcmp (fn, "stdout") || !strcmp (fn, "-"))
159 return stdout;
160 if (!strcmp (fn, "stderr"))
161 return stderr;
164 #if HAVE_POPEN
165 if (fn[0] == '|')
167 if (settings_get_safer_mode ())
168 return safety_violation (fn);
170 return popen (&fn[1], mode[0] == 'r' ? "r" : "w");
172 else if (*fn && fn[strlen (fn) - 1] == '|')
174 char *s;
175 FILE *f;
177 if (settings_get_safer_mode ())
178 return safety_violation (fn);
180 s = xmalloca (strlen (fn));
181 memcpy (s, fn, strlen (fn) - 1);
182 s[strlen (fn) - 1] = 0;
184 f = popen (s, mode[0] == 'r' ? "r" : "w");
186 freea (s);
188 return f;
190 else
191 #endif
193 #if WIN32
195 wchar_t *ss = convert_to_filename_encoding (fn, strlen (fn), fh_get_file_name_encoding (fh));
196 wchar_t *m = (wchar_t *) recode_string ("UTF-16LE", "ASCII", mode, strlen (mode));
197 FILE *fp = _wfopen (ss, m);
198 free (m);
199 free (ss);
200 return fp;
202 #else
203 return fopen (fn, mode);
204 #endif
207 /* Counterpart to fn_open that closes file F with name FN; returns 0
208 on success, EOF on failure. If EOF is returned, errno is set to a
209 sensible value. */
211 fn_close (const struct file_handle *fh, FILE *f)
213 const char *fn = fh_get_file_name (fh);
214 if (fileno (f) == STDIN_FILENO
215 || fileno (f) == STDOUT_FILENO
216 || fileno (f) == STDERR_FILENO)
217 return 0;
218 #if HAVE_POPEN
219 else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|'))
221 pclose (f);
222 return 0;
224 #endif
225 else
226 return fclose (f);
230 #ifdef WIN32
232 /* Apparently windoze users like to see output dumped into their home directory,
233 not the current directory (!) */
234 const char *
235 default_output_path (void)
237 static char *path = NULL;
239 if (path == NULL)
241 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference
242 to HOME, because the user can change HOME. */
243 const char *home_dir = getenv ("HOME");
244 int i;
246 if (home_dir == NULL)
248 const char *home_drive = getenv ("HOMEDRIVE");
249 const char *home_path = getenv ("HOMEPATH");
251 if (home_drive != NULL && home_path != NULL)
252 home_dir = xasprintf ("%s%s",
253 home_drive, home_path);
256 if (home_dir == NULL)
257 home_dir = "c:/users/default"; /* poor default */
259 /* Copy home_dir into path. Add a slash at the end but
260 only if there isn't already one there, because Windows
261 treats // specially. */
262 if (home_dir[0] == '\0'
263 || strchr ("/\\", home_dir[strlen (home_dir) - 1]) == NULL)
264 path = xasprintf ("%s%c", home_dir, '/');
265 else
266 path = xstrdup (home_dir);
268 for(i = 0; i < strlen (path); i++)
269 if (path[i] == '\\') path[i] = '/';
272 return path;
275 const char *
276 default_log_path (void)
278 return default_output_path ();
281 #else
283 /* ... whereas the rest of the world just likes it to be
284 put "here" for easy access. */
285 const char *
286 default_output_path (void)
288 static char current_dir[] = "";
290 return current_dir;
293 const char *
294 default_log_path (void)
296 static char *log_path = NULL;
298 if (!log_path)
300 char *tmp = NULL;
301 const char *state_home = getenv ("XDG_STATE_HOME");
302 if (!state_home)
304 const char *home = getenv ("HOME");
305 state_home = tmp = xasprintf ("%s/.local/state", home ? home : "");
308 log_path = xasprintf ("%s/pspp/", state_home);
310 struct stat s;
311 if (!stat (state_home, &s) && stat (log_path, &s) && errno == ENOENT)
312 mkdir (log_path, 0700);
314 free (tmp);
317 return log_path;
320 #endif