1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
20 /* AIX requires this to be the first thing in the file. */
23 #define alloca __builtin_alloca
31 #ifndef alloca /* predefined by HP cc +Olibcalls */
49 #include "debug-print.h"
51 /* PORTME: Everything in this file is system dependent. */
65 #include <win32/windows.h>
74 const char *config_path;
79 config_path = fn_getenv_default ("STAT_CONFIG_PATH", default_config_path);
82 /* Functions for performing operations on filenames. */
84 /* Substitutes $variables as defined by GETENV into INPUT and returns
85 a copy of the resultant string. Supports $var and ${var} syntaxes;
86 $$ substitutes as $. */
88 fn_interp_vars (const char *input, const char *(*getenv) (const char *))
92 if (NULL == strchr (input, '$'))
93 return xstrdup (input);
95 ds_init (NULL, &output, strlen (input));
101 return ds_value (&output);
108 ds_putchar (&output, '$');
117 start = ds_length (&output);
124 else if (*input == '{')
132 while (*input && *input != stop
133 && (stop || isalpha ((unsigned char) *input)))
134 ds_putchar (&output, *input++);
136 value = getenv (ds_value (&output) + start);
137 ds_truncate (&output, start);
138 ds_concat (&output, value);
140 if (stop && *input == stop)
145 ds_putchar (&output, *input++);
150 /* Expands csh tilde notation from the path INPUT into a malloc()'d
153 fn_tilde_expand (const char *input)
156 struct string output;
158 if (NULL == strchr (input, '~'))
159 return xstrdup (input);
160 ds_init (NULL, &output, strlen (input));
164 for (ip = input; *ip; )
165 if (*ip != '~' || (ip != input && ip[-1] != PATH_DELIMITER))
166 ds_putchar (&output, *ip++);
169 static const char stop_set[3] = {DIR_SEPARATOR, PATH_DELIMITER, 0};
174 cp = ip + strcspn (ip, stop_set);
181 strncpy (username, ip, cp - ip + 1);
183 pwd = getpwnam (username);
185 if (!pwd || !pwd->pw_dir)
186 ds_putchar (&output, *ip++);
188 ds_concat (&output, pwd->pw_dir);
192 const char *home = fn_getenv ("HOME");
194 ds_putchar (&output, *ip++);
196 ds_concat (&output, home);
202 return ds_value (&output);
206 fn_tilde_expand (char *input)
208 return xstrdup (input);
212 /* Searches for a configuration file with name NAME in the path given
213 by PATH, which is tilde- and environment-interpolated. Directories
214 in PATH are delimited by PATH_DELIMITER, defined in <pref.h>.
215 Returns the malloc'd full name of the first file found, or NULL if
218 If PREPEND is non-NULL, then it is prepended to each filename;
219 i.e., it looks like PREPEND/PATH_COMPONENT/NAME. This is not done
220 with absolute directories in the path. */
221 #if unix || __MSDOS__ || __WIN32__
223 fn_search_path (const char *basename, const char *path, const char *prepend)
226 struct string filename;
229 if (fn_absolute_p (basename))
230 return fn_tilde_expand (basename);
233 char *temp = fn_interp_vars (path, fn_getenv);
234 bp = subst_path = fn_tilde_expand (temp);
238 msg (VM (4), _("Searching for `%s'..."), basename);
239 ds_init (NULL, &filename, 64);
246 msg (VM (4), _("Search unsuccessful!"));
247 ds_destroy (&filename);
252 for (ep = bp; *ep && *ep != PATH_DELIMITER; ep++)
255 /* Paste together PREPEND/PATH/BASENAME. */
256 ds_clear (&filename);
257 if (prepend && !fn_absolute_p (bp))
259 ds_concat (&filename, prepend);
260 ds_putchar (&filename, DIR_SEPARATOR);
262 ds_concat_buffer (&filename, bp, ep - bp);
264 && ds_value (&filename)[ds_length (&filename) - 1] != DIR_SEPARATOR)
265 ds_putchar (&filename, DIR_SEPARATOR);
266 ds_concat (&filename, basename);
268 msg (VM (5), " - %s", ds_value (&filename));
269 if (fn_exists_p (ds_value (&filename)))
271 msg (VM (4), _("Found `%s'."), ds_value (&filename));
273 return ds_value (&filename);
278 msg (VM (4), _("Search unsuccessful!"));
280 ds_destroy (&filename);
286 #else /* not unix, msdog, lose32 */
288 fn_search_path (const char *basename, const char *path, const char *prepend)
290 size_t size = strlen (path) + 1 + strlen (basename) + 1;
295 size += strlen (prepend) + 1;
296 string = xmalloc (size);
301 cp = stpcpy (cp, prepend);
302 *cp++ = DIR_SEPARATOR;
304 cp = stpcpy (cp, path);
305 *cp++ = DIR_SEPARATOR;
306 strcpy (cp, basename);
310 #endif /* not unix, msdog, lose32 */
312 /* Prepends directory DIR to filename FILE and returns a malloc()'d
315 fn_prepend_dir (const char *file, const char *dir)
320 if (fn_absolute_p (file))
321 return xstrdup (file);
323 temp = xmalloc (strlen (file) + 1 + strlen (dir) + 1);
324 cp = stpcpy (temp, dir);
325 if (cp != temp && cp[-1] != DIR_SEPARATOR)
326 *cp++ = DIR_SEPARATOR;
327 cp = stpcpy (cp, file);
332 /* fn_normalize(): This very OS-dependent routine canonicalizes
333 filename FN1. The filename should not need to be the name of an
334 existing file. Returns a malloc()'d copy of the canonical name.
335 This function must always succeed; if it needs to bail out then it
336 should return xstrdup(FN1). */
339 fn_normalize (const char *filename)
342 char *fn1, *fn2, *dest;
345 if (fn_special_p (filename))
346 return xstrdup (filename);
348 fn1 = fn_tilde_expand (filename);
350 /* Follow symbolic links. */
354 fn1 = fn_readlink (fn1);
363 maxlen = strlen (fn1) * 2;
366 dest = fn2 = xmalloc (maxlen + 1);
369 if (*src == DIR_SEPARATOR)
374 while (getcwd (dest, maxlen - (dest - fn2)) == NULL && errno == ERANGE)
377 dest = fn2 = xrealloc (fn2, maxlen + 1);
386 dest = strchr (fn2, '\0');
387 if (dest - fn2 >= maxlen)
389 int ofs = dest - fn2;
391 fn2 = xrealloc (fn2, maxlen + 1);
394 if (dest[-1] != DIR_SEPARATOR)
395 *dest++ = DIR_SEPARATOR;
405 if (c == DIR_SEPARATOR || c == 0)
407 /* remove `./', `../' from directory */
408 if (dest[-1] == '.' && dest[-2] == DIR_SEPARATOR)
410 else if (dest[-1] == '.' && dest[-2] == '.' && dest[-3] == DIR_SEPARATOR)
415 while (dest[-1] != DIR_SEPARATOR)
418 else if (dest[-1] != DIR_SEPARATOR) /* remove extra slashes */
423 if (dest[-1] == DIR_SEPARATOR && dest > fn2 + 1)
428 return xrealloc (fn2, strlen (fn2) + 1);
436 if (dest - fn2 >= maxlen)
438 int ofs = dest - fn2;
440 fn2 = xrealloc (fn2, maxlen + 1);
449 fn_normalize (const char *fn1)
455 /* Don't change special filenames. */
456 if (is_special_filename (filename))
457 return xstrdup (filename);
459 /* First find the required buffer length. */
460 len = GetFullPathName (fn1, 0, NULL, NULL);
467 /* Then make a buffer that big. */
469 success = GetFullPathName (fn1, len, fn2, NULL);
470 if (success >= len || success == 0)
480 fn_normalize (const char *fn1)
482 char *fn2 = _fullpath (NULL, fn1, 0);
486 for (cp = fn2; *cp; cp++)
487 *cp = toupper ((unsigned char) (*cp));
490 return xstrdup (fn1);
494 fn_normalize (const char *fn1)
496 char *fn2 = xmalloc (1024);
498 fn2 = xrealloc (fn2, strlen (fn2) + 1);
501 #else /* not Lose32, Unix, or DJGPP */
503 fn_normalize (const char *fn)
507 #endif /* not Lose32, Unix, or DJGPP */
509 /* Returns the directory part of FILENAME, as a malloc()'d
512 fn_dirname (const char *filename)
518 len = strlen (filename);
519 if (len == 1 && filename[0] == '/')
521 else if (len && filename[len - 1] == DIR_SEPARATOR)
522 p = mm_find_reverse (filename, len - 1, filename + len - 1, 1);
524 p = strrchr (filename, DIR_SEPARATOR);
528 s = xmalloc (p - filename + 1);
529 memcpy (s, filename, p - filename);
535 /* Returns the basename part of FILENAME as a malloc()'d string. */
538 fn_basename (const char *filename)
540 /* Not used, not implemented. */
545 /* Returns the current working directory, as a malloc()'d string.
551 char *buffer = xmalloc (size);
555 char *value = getcwd (buffer, size);
561 buffer = xmalloc (size);
565 /* Find out information about files. */
567 /* Returns nonzero iff NAME specifies an absolute filename. */
569 fn_absolute_p (const char *name)
573 || !strncmp (name, "./", 2)
574 || !strncmp (name, "../", 3)
579 || !strncmp (name, ".\\", 2)
580 || !strncmp (name, "..\\", 3)
581 || (name[0] && name[1] == ':'))
588 /* Returns 1 if the filename specified is a virtual file that doesn't
589 really exist on disk, 0 if it's a real filename. */
591 fn_special_p (const char *filename)
593 if (!strcmp (filename, "-") || !strcmp (filename, "stdin")
594 || !strcmp (filename, "stdout") || !strcmp (filename, "stderr")
596 || filename[0] == '|'
597 || (*filename && filename[strlen (filename) - 1] == '|')
605 /* Returns nonzero if file with name NAME exists. */
607 fn_exists_p (const char *name)
612 return stat (name, &temp) == 0;
614 FILE *f = fopen (name, "r");
623 /* Stolen from libc.info but heavily modified, this is a wrapper
624 around readlink() that allows for arbitrary filename length. */
626 fn_readlink (const char *filename)
632 char *buffer = xmalloc (size);
633 int nchars = readlink (filename, buffer, size);
640 if (nchars < size - 1)
649 #else /* Not UNIX. */
651 fn_readlink (const char *filename)
655 #endif /* Not UNIX. */
657 /* Environment variables. */
659 /* Simulates $VER and $ARCH environment variables. */
661 fn_getenv (const char *s)
663 if (!strcmp (s, "VER"))
664 return fn_getenv_default ("STAT_VER", bare_version);
665 else if (!strcmp (s, "ARCH"))
666 return fn_getenv_default ("STAT_ARCH", host_system);
671 /* Returns getenv(KEY) if that's non-NULL; else returns DEF. */
673 fn_getenv_default (const char *key, const char *def)
675 const char *value = getenv (key);
676 return value ? value : def;
679 /* Basic file handling. */
681 /* Used for giving an error message on a set_safer security
684 safety_violation (const char *fn)
686 msg (SE, _("Not opening pipe file `%s' because SAFER option set."), fn);
691 /* As a general comment on the following routines, a `sensible value'
692 for errno includes 0 if there is no associated system error. The
693 routines will only set errno to 0 if there is an error in a
694 callback that sets errno to 0; they themselves won't. */
696 /* File open routine that understands `-' as stdin/stdout and `|cmd'
697 as a pipe to command `cmd'. Returns resultant FILE on success,
698 NULL on failure. If NULL is returned then errno is set to a
701 fn_open (const char *fn, const char *mode)
703 assert (mode[0] == 'r' || mode[0] == 'w');
705 if (mode[0] == 'r' && (!strcmp (fn, "stdin") || !strcmp (fn, "-")))
707 else if (mode[0] == 'w' && (!strcmp (fn, "stdout") || !strcmp (fn, "-")))
709 else if (mode[0] == 'w' && !strcmp (fn, "stderr"))
716 return safety_violation (fn);
718 return popen (&fn[1], mode);
720 else if (*fn && fn[strlen (fn) - 1] == '|')
726 return safety_violation (fn);
728 s = local_alloc (strlen (fn));
729 memcpy (s, fn, strlen (fn) - 1);
730 s[strlen (fn) - 1] = 0;
741 FILE *f = fopen (fn, mode);
743 if (f && mode[0] == 'w')
744 setvbuf (f, NULL, _IOLBF, 0);
750 /* Counterpart to fn_open that closes file F with name FN; returns 0
751 on success, EOF on failure. If EOF is returned, errno is set to a
754 fn_close (const char *fn, FILE *f)
756 if (!strcmp (fn, "-"))
759 else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|'))
769 /* More extensive file handling. */
771 /* File open routine that extends fn_open(). Opens or reopens a
772 file according to the contents of file_ext F. Returns nonzero on
773 success. If 0 is returned, errno is set to a sensible value. */
775 fn_open_ext (struct file_ext *f)
779 p = strstr (f->filename, "%d");
782 char *s = local_alloc (strlen (f->filename) + INT_DIGITS - 1);
785 memcpy (s, f->filename, p - f->filename);
786 cp = spprintf (&s[p - f->filename], "%d", *f->sequence_no);
794 if (f->preclose (f) == 0)
797 if (EOF == fn_close (f->filename, f->file) || error)
811 f->file = fn_open (s, f->mode);
814 if (f->file && f->postopen)
815 if (f->postopen (f) == 0)
818 fn_close (f->filename, f->file);
824 return (f->file != NULL);
830 f->file = fn_open (f->filename, f->mode);
832 if (f->file && f->postopen)
833 if (f->postopen (f) == 0)
836 fn_close (f->filename, f->file);
842 return (f->file != NULL);
846 /* Properly closes the file associated with file_ext F, if any.
847 Return nonzero on success. If zero is returned, errno is set to a
850 fn_close_ext (struct file_ext *f)
857 if (f->preclose (f) == 0)
860 if (EOF == fn_close (f->filename, f->file) || error)