2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2003
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. */
28 #include "searchpath.h"
31 search_path::search_path(const char *envvar
, const char *standard
,
32 int add_home
, int add_current
)
36 home
= getenv("HOME");
40 dirs
= new char[((e
&& *e
) ? strlen(e
) + 1 : 0)
41 + (add_current
? 1 + 1 : 0)
42 + ((home
&& *home
) ? strlen(home
) + 1 : 0)
43 + ((standard
&& *standard
) ? strlen(standard
) : 0)
48 strcat(dirs
, PATH_SEP
);
52 strcat(dirs
, PATH_SEP
);
56 strcat(dirs
, PATH_SEP
);
58 if (standard
&& *standard
)
59 strcat(dirs
, standard
);
60 init_len
= strlen(dirs
);
63 search_path::~search_path()
65 // dirs is always allocated
69 void search_path::command_line_dir(const char *s
)
72 unsigned old_len
= strlen(old
);
73 unsigned slen
= strlen(s
);
74 dirs
= new char[old_len
+ 1 + slen
+ 1];
75 memcpy(dirs
, old
, old_len
- init_len
);
77 p
+= old_len
- init_len
;
84 memcpy(p
, old
+ old_len
- init_len
, init_len
);
91 FILE *search_path::open_file(const char *name
, char **pathp
)
94 if (IS_ABSOLUTE(name
) || *dirs
== '\0') {
95 FILE *fp
= fopen(name
, "r");
98 *pathp
= strsave(name
);
104 unsigned namelen
= strlen(name
);
107 char *end
= strchr(p
, PATH_SEP
[0]);
109 end
= strchr(p
, '\0');
110 int need_slash
= end
> p
&& strchr(DIR_SEPS
, end
[-1]) == 0;
111 char *path
= new char[(end
- p
) + need_slash
+ namelen
+ 1];
112 memcpy(path
, p
, end
- p
);
115 strcpy(path
+ (end
- p
) + need_slash
, name
);
117 fprintf(stderr
, "trying `%s'\n", path
);
119 FILE *fp
= fopen(path
, "r");
135 FILE *search_path::open_file_cautious(const char *name
, char **pathp
,
140 bool reading
= (strchr(mode
, 'r') != 0);
141 if (name
== 0 || strcmp(name
, "-") == 0) {
143 *pathp
= strsave(reading
? "stdin" : "stdout");
144 return (reading
? stdin
: stdout
);
146 if (!reading
|| IS_ABSOLUTE(name
) || *dirs
== '\0') {
147 FILE *fp
= fopen(name
, mode
);
150 *pathp
= strsave(name
);
156 unsigned namelen
= strlen(name
);
159 char *end
= strchr(p
, PATH_SEP
[0]);
161 end
= strchr(p
, '\0');
162 int need_slash
= end
> p
&& strchr(DIR_SEPS
, end
[-1]) == 0;
163 char *path
= new char[(end
- p
) + need_slash
+ namelen
+ 1];
164 memcpy(path
, p
, end
- p
);
167 strcpy(path
+ (end
- p
) + need_slash
, name
);
169 fprintf(stderr
, "trying `%s'\n", path
);
171 FILE *fp
= fopen(path
, mode
);