2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "searchpath.h"
30 search_path::search_path(const char *envvar, const char *standard,
31 int add_home, int add_current)
35 home = getenv("HOME");
39 dirs = new char[((e && *e) ? strlen(e) + 1 : 0)
40 + (add_current ? 1 + 1 : 0)
41 + ((home && *home) ? strlen(home) + 1 : 0)
42 + ((standard && *standard) ? strlen(standard) : 0)
47 strcat(dirs, PATH_SEP);
51 strcat(dirs, PATH_SEP);
55 strcat(dirs, PATH_SEP);
57 if (standard && *standard)
58 strcat(dirs, standard);
59 init_len = strlen(dirs);
62 search_path::~search_path()
64 // dirs is always allocated
68 void search_path::command_line_dir(const char *s)
71 unsigned old_len = strlen(old);
72 unsigned slen = strlen(s);
73 dirs = new char[old_len + 1 + slen + 1];
74 memcpy(dirs, old, old_len - init_len);
76 p += old_len - init_len;
83 memcpy(p, old + old_len - init_len, init_len);
90 FILE *search_path::open_file(const char *name, char **pathp)
93 if (IS_ABSOLUTE(name) || *dirs == '\0') {
94 FILE *fp = fopen(name, "r");
97 *pathp = strsave(name);
103 unsigned namelen = strlen(name);
106 char *end = strchr(p, PATH_SEP[0]);
108 end = strchr(p, '\0');
109 int need_slash = end > p && strchr(DIR_SEPS, end[-1]) == 0;
110 char *path = new char[(end - p) + need_slash + namelen + 1];
111 memcpy(path, p, end - p);
114 strcpy(path + (end - p) + need_slash, name);
116 fprintf(stderr, "trying `%s'\n", path);
118 FILE *fp = fopen(path, "r");