1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 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 "file-name.h"
28 #include "canonicalize.h"
33 #include <libpspp/alloc.h>
34 #include <libpspp/message.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37 #include <libpspp/verbose-msg.h>
38 #include <libpspp/version.h>
41 #define _(msgid) gettext (msgid)
48 const char *config_path;
53 config_path = fn_getenv_default ("STAT_CONFIG_PATH", default_config_path);
56 /* Functions for performing operations on file names. */
59 /* Substitutes $variables in SRC, putting the result in DST,
60 properly handling the case where SRC is a substring of DST.
61 Variables are as defined by GETENV. Supports $var and ${var}
62 syntaxes; $$ substitutes as $. */
64 fn_interp_vars (struct substring src, const char *(*getenv) (const char *),
67 struct string dst = DS_EMPTY_INITIALIZER;
70 while ((c = ss_get_char (&src)) != EOF)
72 ds_put_char (&dst, c);
75 if (ss_match_char (&src, '$') || ss_is_empty (src))
76 ds_put_char (&dst, '$');
79 struct substring var_name;
83 if (ss_match_char (&src, '('))
84 ss_get_until (&src, ')', &var_name);
85 else if (ss_match_char (&src, '{'))
86 ss_get_until (&src, '}', &var_name);
88 ss_get_chars (&src, MIN (1, ss_span (src, ss_cstr (CC_ALNUM))),
91 start = ds_length (&dst);
92 ds_put_substring (&dst, var_name);
93 value = getenv (ds_cstr (&dst) + start);
94 ds_truncate (&dst, start);
96 ds_put_cstr (&dst, value);
100 ds_swap (&dst, dst_);
104 /* Searches for a configuration file with name NAME in the path
105 given by PATH, which is environment-interpolated.
106 Directories in PATH are delimited by ':'. Returns the
107 malloc'd full name of the first file found, or NULL if none is
110 fn_search_path (const char *base_name, const char *path_)
113 struct substring dir;
114 struct string file = DS_EMPTY_INITIALIZER;
117 if (fn_is_absolute (base_name))
118 return xstrdup (base_name);
120 /* Interpolate environment variables. */
121 ds_init_cstr (&path, path_);
122 fn_interp_vars (ds_ss (&path), fn_getenv, &path);
124 verbose_msg (2, _("searching for \"%s\" in path \"%s\""),
125 base_name, ds_cstr (&path));
126 while (ds_separate (&path, ss_cstr (":"), &save_idx, &dir))
128 /* Construct file name. */
130 ds_put_substring (&file, dir);
131 if (!ds_is_empty (&file) && ds_last (&file) != '/')
132 ds_put_char (&file, '/');
133 ds_put_cstr (&file, base_name);
135 /* Check whether file exists. */
136 if (fn_exists (ds_cstr (&file)))
138 verbose_msg (2, _("...found \"%s\""), ds_cstr (&file));
140 return ds_cstr (&file);
145 verbose_msg (2, _("...not found"));
151 /* Returns the directory part of FILE_NAME, as a malloc()'d
154 fn_dir_name (const char *file_name)
160 len = strlen (file_name);
161 if (len == 1 && file_name[0] == '/')
163 else if (len && file_name[len - 1] == '/')
164 p = buf_find_reverse (file_name, len - 1, file_name + len - 1, 1);
166 p = strrchr (file_name, '/');
170 s = xmalloc (p - file_name + 1);
171 memcpy (s, file_name, p - file_name);
172 s[p - file_name] = 0;
177 /* Returns the extension part of FILE_NAME as a malloc()'d string.
178 If FILE_NAME does not have an extension, returns an empty
181 fn_extension (const char *file_name)
183 const char *extension = strrchr (file_name, '.');
184 if (extension == NULL)
186 return xstrdup (extension);
189 /* Find out information about files. */
191 /* Returns true iff NAME specifies an absolute file name. */
193 fn_is_absolute (const char *name)
195 return name[0] == '/';
198 /* Returns true if FILE_NAME is a virtual file that doesn't
199 really exist on disk, false if it's a real file name. */
201 fn_is_special (const char *file_name)
203 if (!strcmp (file_name, "-") || !strcmp (file_name, "stdin")
204 || !strcmp (file_name, "stdout") || !strcmp (file_name, "stderr")
206 || file_name[0] == '|'
207 || (*file_name && file_name[strlen (file_name) - 1] == '|')
215 /* Returns true if file with name NAME exists. */
217 fn_exists (const char *name)
220 return stat (name, &temp) == 0;
223 /* Environment variables. */
225 /* Simulates $VER and $ARCH environment variables. */
227 fn_getenv (const char *s)
229 if (!strcmp (s, "VER"))
230 return fn_getenv_default ("STAT_VER", bare_version);
231 else if (!strcmp (s, "ARCH"))
232 return fn_getenv_default ("STAT_ARCH", host_system);
237 /* Returns getenv(KEY) if that's non-NULL; else returns DEF. */
239 fn_getenv_default (const char *key, const char *def)
241 const char *value = getenv (key);
242 return value ? value : def;
245 /* Basic file handling. */
248 /* Used for giving an error message on a set_safer security
251 safety_violation (const char *fn)
253 msg (SE, _("Not opening pipe file `%s' because SAFER option set."), fn);
259 /* As a general comment on the following routines, a `sensible value'
260 for errno includes 0 if there is no associated system error. The
261 routines will only set errno to 0 if there is an error in a
262 callback that sets errno to 0; they themselves won't. */
264 /* File open routine that understands `-' as stdin/stdout and `|cmd'
265 as a pipe to command `cmd'. Returns resultant FILE on success,
266 NULL on failure. If NULL is returned then errno is set to a
269 fn_open (const char *fn, const char *mode)
271 assert (mode[0] == 'r' || mode[0] == 'w');
273 if (mode[0] == 'r' && (!strcmp (fn, "stdin") || !strcmp (fn, "-")))
275 else if (mode[0] == 'w' && (!strcmp (fn, "stdout") || !strcmp (fn, "-")))
277 else if (mode[0] == 'w' && !strcmp (fn, "stderr"))
283 if (get_safer_mode ())
284 return safety_violation (fn);
286 return popen (&fn[1], mode);
288 else if (*fn && fn[strlen (fn) - 1] == '|')
293 if (get_safer_mode ())
294 return safety_violation (fn);
296 s = local_alloc (strlen (fn));
297 memcpy (s, fn, strlen (fn) - 1);
298 s[strlen (fn) - 1] = 0;
309 FILE *f = fopen (fn, mode);
311 if (f && mode[0] == 'w')
312 setvbuf (f, NULL, _IOLBF, 0);
318 /* Counterpart to fn_open that closes file F with name FN; returns 0
319 on success, EOF on failure. If EOF is returned, errno is set to a
322 fn_close (const char *fn, FILE *f)
324 if (!strcmp (fn, "-"))
327 else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|'))
338 /* A file's identity. */
341 dev_t device; /* Device number. */
342 ino_t inode; /* Inode number. */
345 /* Returns a pointer to a dynamically allocated structure whose
346 value can be used to tell whether two files are actually the
347 same file. Returns a null pointer if no information about the
348 file is available, perhaps because it does not exist. The
349 caller is responsible for freeing the structure with
350 fn_free_identity() when finished. */
351 struct file_identity *
352 fn_get_identity (const char *file_name)
356 if (stat (file_name, &s) == 0)
358 struct file_identity *identity = xmalloc (sizeof *identity);
359 identity->device = s.st_dev;
360 identity->inode = s.st_ino;
367 /* Frees IDENTITY obtained from fn_get_identity(). */
369 fn_free_identity (struct file_identity *identity)
374 /* Compares A and B, returning a strcmp()-type result. */
376 fn_compare_file_identities (const struct file_identity *a,
377 const struct file_identity *b)
381 if (a->device != b->device)
382 return a->device < b->device ? -1 : 1;
384 return a->inode < b->inode ? -1 : a->inode > b->inode;
387 /* A file's identity. */
390 char *normalized_file_name; /* File's normalized name. */
393 /* Returns a pointer to a dynamically allocated structure whose
394 value can be used to tell whether two files are actually the
395 same file. Returns a null pointer if no information about the
396 file is available, perhaps because it does not exist. The
397 caller is responsible for freeing the structure with
398 fn_free_identity() when finished. */
399 struct file_identity *
400 fn_get_identity (const char *file_name)
402 struct file_identity *identity = xmalloc (sizeof *identity);
403 char *cname = canonicalize_filename_mode (file_name, CAN_MISSING);
405 cname = xstrdup (file_name);
406 identity->normalized_file_name = cname;
410 /* Frees IDENTITY obtained from fn_get_identity(). */
412 fn_free_identity (struct file_identity *identity)
414 if (identity != NULL)
416 free (identity->normalized_file_name);
421 /* Compares A and B, returning a strcmp()-type result. */
423 fn_compare_file_identities (const struct file_identity *a,
424 const struct file_identity *b)
426 return strcmp (a->normalized_file_name, b->normalized_file_name);
428 #endif /* not unix */