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/>. */
19 #include "data/file-name.h"
20 #include "data/file-handle-def.h"
31 #include "data/settings.h"
32 #include "libpspp/hash-functions.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
35 #include "libpspp/version.h"
37 #include "gl/dirname.h"
38 #include "gl/dosname.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"
46 #define _(msgid) gettext (msgid)
49 /* Functions for performing operations on file names. */
51 /* Searches for a configuration file with name NAME in the directories given in
52 PATH, which is terminated by a null pointer. Returns the full name of the
53 first file found, which the caller is responsible for freeing with free(),
54 or NULL if none is found. */
56 fn_search_path (const char *base_name, char **path)
60 if (fn_is_absolute (base_name))
61 return xstrdup (base_name);
63 for (i = 0; path[i] != NULL; i++)
65 const char *dir = path[i];
68 if (!strcmp (dir, "") || !strcmp (dir, "."))
69 file = xstrdup (base_name);
70 else if (ISSLASH (dir[strlen (dir) - 1]))
71 file = xasprintf ("%s%s", dir, base_name);
73 file = xasprintf ("%s/%s", dir, base_name);
83 /* Returns the extension part of FILE_NAME as a malloc()'d string.
84 If FILE_NAME does not have an extension, returns an empty
87 fn_extension (const char *file_name)
89 const char *extension = strrchr (file_name, '.');
90 if (extension == NULL)
92 return xstrdup (extension);
95 /* Find out information about files. */
97 /* Returns true iff NAME specifies an absolute file name. */
99 fn_is_absolute (const char *name)
101 return IS_ABSOLUTE_FILE_NAME (name);
104 /* Returns true if file with name NAME exists, and that file is not a
107 fn_exists (const char *name)
110 if ( stat (name, &temp) != 0 )
113 return ! S_ISDIR (temp.st_mode);
117 /* Basic file handling. */
120 /* Used for giving an error message on a set_safer security
123 safety_violation (const char *fn)
125 msg (SE, _("Not opening pipe file `%s' because %s option set."), fn, "SAFER");
131 /* File open routine that understands `-' as stdin/stdout and `|cmd'
132 as a pipe to command `cmd'. Returns resultant FILE on success,
133 NULL on failure. If NULL is returned then errno is set to a
136 fn_open (const char *fn, const char *mode)
138 assert (mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a');
142 if (!strcmp (fn, "stdin") || !strcmp (fn, "-"))
147 if (!strcmp (fn, "stdout") || !strcmp (fn, "-"))
149 if (!strcmp (fn, "stderr"))
156 if (settings_get_safer_mode ())
157 return safety_violation (fn);
159 return popen (&fn[1], mode[0] == 'r' ? "r" : "w");
161 else if (*fn && fn[strlen (fn) - 1] == '|')
166 if (settings_get_safer_mode ())
167 return safety_violation (fn);
169 s = xmalloca (strlen (fn));
170 memcpy (s, fn, strlen (fn) - 1);
171 s[strlen (fn) - 1] = 0;
173 f = popen (s, mode[0] == 'r' ? "r" : "w");
181 return fopen (fn, mode);
184 /* Counterpart to fn_open that closes file F with name FN; returns 0
185 on success, EOF on failure. If EOF is returned, errno is set to a
188 fn_close (const char *fn, FILE *f)
190 if (fileno (f) == STDIN_FILENO
191 || fileno (f) == STDOUT_FILENO
192 || fileno (f) == STDERR_FILENO)
195 else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|'))
208 /* Apparently windoze users like to see output dumped into their home directory,
209 not the current directory (!) */
211 default_output_path (void)
213 static char *path = NULL;
217 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference
218 to HOME, because the user can change HOME. */
220 const char *home_dir = getenv ("HOME");
223 if (home_dir == NULL)
225 const char *home_drive = getenv ("HOMEDRIVE");
226 const char *home_path = getenv ("HOMEPATH");
228 if (home_drive != NULL && home_path != NULL)
229 home_dir = xasprintf ("%s%s",
230 home_drive, home_path);
233 if (home_dir == NULL)
234 home_dir = "c:/users/default"; /* poor default */
236 /* Copy home_dir into path. Add a slash at the end but
237 only if there isn't already one there, because Windows
238 treats // specially. */
239 if (home_dir[0] == '\0'
240 || strchr ("/\\", home_dir[strlen (home_dir) - 1]) == NULL)
241 path = xasprintf ("%s%c", home_dir, '/');
243 path = xstrdup (home_dir);
245 for(i = 0; i < strlen (path); i++)
246 if (path[i] == '\\') path[i] = '/';
254 /* ... whereas the rest of the world just likes it to be
255 put "here" for easy access. */
257 default_output_path (void)
259 static char current_dir[] = "";