1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006, 2007 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
21 #include <data/file-name.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/message.h>
34 #include <data/settings.h>
35 #include <libpspp/str.h>
36 #include <libpspp/verbose-msg.h>
37 #include <libpspp/version.h>
40 #define _(msgid) gettext (msgid)
45 #if defined _WIN32 || defined __WIN32__
46 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
52 const char *config_path;
57 config_path = fn_getenv_default ("STAT_CONFIG_PATH", default_config_path);
60 /* Functions for performing operations on file names. */
63 /* Substitutes $variables in SRC, putting the result in DST,
64 properly handling the case where SRC is a substring of DST.
65 Variables are as defined by GETENV. Supports $var and ${var}
66 syntaxes; $$ substitutes as $. */
68 fn_interp_vars (struct substring src, const char *(*getenv) (const char *),
71 struct string dst = DS_EMPTY_INITIALIZER;
74 while ((c = ss_get_char (&src)) != EOF)
76 ds_put_char (&dst, c);
79 if (ss_match_char (&src, '$') || ss_is_empty (src))
80 ds_put_char (&dst, '$');
83 struct substring var_name;
87 if (ss_match_char (&src, '('))
88 ss_get_until (&src, ')', &var_name);
89 else if (ss_match_char (&src, '{'))
90 ss_get_until (&src, '}', &var_name);
92 ss_get_chars (&src, MIN (1, ss_span (src, ss_cstr (CC_ALNUM))),
95 start = ds_length (&dst);
96 ds_put_substring (&dst, var_name);
97 value = getenv (ds_cstr (&dst) + start);
98 ds_truncate (&dst, start);
100 ds_put_cstr (&dst, value);
104 ds_swap (&dst, dst_);
108 /* Searches for a configuration file with name NAME in the path
109 given by PATH, which is environment-interpolated.
110 Directories in PATH are delimited by ':'. Returns the
111 malloc'd full name of the first file found, or NULL if none is
114 fn_search_path (const char *base_name, const char *path_)
117 struct substring dir;
118 struct string file = DS_EMPTY_INITIALIZER;
121 if (fn_is_absolute (base_name))
122 return xstrdup (base_name);
124 /* Interpolate environment variables. */
125 ds_init_cstr (&path, path_);
126 fn_interp_vars (ds_ss (&path), fn_getenv, &path);
128 verbose_msg (2, _("searching for \"%s\" in path \"%s\""),
129 base_name, ds_cstr (&path));
130 while (ds_separate (&path, ss_cstr (":"), &save_idx, &dir))
132 /* Construct file name. */
134 ds_put_substring (&file, dir);
135 if (!ds_is_empty (&file) && !ISSLASH (ds_last (&file)))
136 ds_put_char (&file, '/');
137 ds_put_cstr (&file, base_name);
139 /* Check whether file exists. */
140 if (fn_exists (ds_cstr (&file)))
142 verbose_msg (2, _("...found \"%s\""), ds_cstr (&file));
144 return ds_cstr (&file);
149 verbose_msg (2, _("...not found"));
155 /* Returns the directory part of FILE_NAME, as a malloc()'d
158 fn_dir_name (const char *file_name)
160 return dir_name (file_name);
163 /* Returns the extension part of FILE_NAME as a malloc()'d string.
164 If FILE_NAME does not have an extension, returns an empty
167 fn_extension (const char *file_name)
169 const char *extension = strrchr (file_name, '.');
170 if (extension == NULL)
172 return xstrdup (extension);
175 /* Find out information about files. */
177 /* Returns true iff NAME specifies an absolute file name. */
179 fn_is_absolute (const char *name)
181 return IS_ABSOLUTE_FILE_NAME (name);
184 /* Returns true if FILE_NAME is a virtual file that doesn't
185 really exist on disk, false if it's a real file name. */
187 fn_is_special (const char *file_name)
189 if (!strcmp (file_name, "-") || !strcmp (file_name, "stdin")
190 || !strcmp (file_name, "stdout") || !strcmp (file_name, "stderr")
192 || file_name[0] == '|'
193 || (*file_name && file_name[strlen (file_name) - 1] == '|')
201 /* Returns true if file with name NAME exists. */
203 fn_exists (const char *name)
206 return stat (name, &temp) == 0;
209 /* Environment variables. */
211 /* Simulates $VER and $ARCH environment variables. */
213 fn_getenv (const char *s)
215 if (!strcmp (s, "VER"))
216 return fn_getenv_default ("STAT_VER", bare_version);
217 else if (!strcmp (s, "ARCH"))
218 return fn_getenv_default ("STAT_ARCH", host_system);
223 /* Returns getenv(KEY) if that's non-NULL; else returns DEF. */
225 fn_getenv_default (const char *key, const char *def)
227 const char *value = getenv (key);
228 return value ? value : def;
231 /* Basic file handling. */
234 /* Used for giving an error message on a set_safer security
237 safety_violation (const char *fn)
239 msg (SE, _("Not opening pipe file `%s' because SAFER option set."), fn);
245 /* As a general comment on the following routines, a `sensible value'
246 for errno includes 0 if there is no associated system error. The
247 routines will only set errno to 0 if there is an error in a
248 callback that sets errno to 0; they themselves won't. */
250 /* File open routine that understands `-' as stdin/stdout and `|cmd'
251 as a pipe to command `cmd'. Returns resultant FILE on success,
252 NULL on failure. If NULL is returned then errno is set to a
255 fn_open (const char *fn, const char *mode)
257 assert (mode[0] == 'r' || mode[0] == 'w');
259 if (mode[0] == 'r' && (!strcmp (fn, "stdin") || !strcmp (fn, "-")))
261 else if (mode[0] == 'w' && (!strcmp (fn, "stdout") || !strcmp (fn, "-")))
263 else if (mode[0] == 'w' && !strcmp (fn, "stderr"))
269 if (get_safer_mode ())
270 return safety_violation (fn);
272 return popen (&fn[1], mode);
274 else if (*fn && fn[strlen (fn) - 1] == '|')
279 if (get_safer_mode ())
280 return safety_violation (fn);
282 s = local_alloc (strlen (fn));
283 memcpy (s, fn, strlen (fn) - 1);
284 s[strlen (fn) - 1] = 0;
295 FILE *f = fopen (fn, mode);
297 if (f && mode[0] == 'w')
298 setvbuf (f, NULL, _IOLBF, 0);
304 /* Counterpart to fn_open that closes file F with name FN; returns 0
305 on success, EOF on failure. If EOF is returned, errno is set to a
308 fn_close (const char *fn, FILE *f)
310 if (!strcmp (fn, "-"))
313 else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|'))
323 #if !(defined _WIN32 || defined __WIN32__)
324 /* A file's identity. */
327 dev_t device; /* Device number. */
328 ino_t inode; /* Inode number. */
331 /* Returns a pointer to a dynamically allocated structure whose
332 value can be used to tell whether two files are actually the
333 same file. Returns a null pointer if no information about the
334 file is available, perhaps because it does not exist. The
335 caller is responsible for freeing the structure with
336 fn_free_identity() when finished. */
337 struct file_identity *
338 fn_get_identity (const char *file_name)
342 if (stat (file_name, &s) == 0)
344 struct file_identity *identity = xmalloc (sizeof *identity);
345 identity->device = s.st_dev;
346 identity->inode = s.st_ino;
353 /* Frees IDENTITY obtained from fn_get_identity(). */
355 fn_free_identity (struct file_identity *identity)
360 /* Compares A and B, returning a strcmp()-type result. */
362 fn_compare_file_identities (const struct file_identity *a,
363 const struct file_identity *b)
367 if (a->device != b->device)
368 return a->device < b->device ? -1 : 1;
370 return a->inode < b->inode ? -1 : a->inode > b->inode;
373 /* A file's identity. */
376 char *normalized_file_name; /* File's normalized name. */
379 /* Returns a pointer to a dynamically allocated structure whose
380 value can be used to tell whether two files are actually the
381 same file. Returns a null pointer if no information about the
382 file is available, perhaps because it does not exist. The
383 caller is responsible for freeing the structure with
384 fn_free_identity() when finished. */
385 struct file_identity *
386 fn_get_identity (const char *file_name)
388 struct file_identity *identity = xmalloc (sizeof *identity);
389 char cname[PATH_MAX];
391 if (GetFullPathName (file_name, sizeof cname, cname, NULL))
392 identity->normalized_file_name = xstrdup (cname);
394 identity->normalized_file_name = xstrdup (file_name);
399 /* Frees IDENTITY obtained from fn_get_identity(). */
401 fn_free_identity (struct file_identity *identity)
403 if (identity != NULL)
405 free (identity->normalized_file_name);
410 /* Compares A and B, returning a strcmp()-type result. */
412 fn_compare_file_identities (const struct file_identity *a,
413 const struct file_identity *b)
415 return strcasecmp (a->normalized_file_name, b->normalized_file_name);