2 /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "searchpath.h"
29 search_path::search_path(const char *envvar
, const char *standard
)
31 char *e
= envvar
? getenv(envvar
) : 0;
33 dirs
= new char[strlen(e
) + strlen(standard
) + 2];
36 strcat(dirs
, standard
);
39 dirs
= strsave(e
? e
: standard
);
40 init_len
= dirs
? strlen(dirs
) : 0;
43 search_path::~search_path()
49 void search_path::command_line_dir(const char *s
)
55 unsigned old_len
= strlen(old
);
56 unsigned slen
= strlen(s
);
57 dirs
= new char[old_len
+ 1 + slen
+ 1];
58 memcpy(dirs
, old
, old_len
- init_len
);
60 p
+= old_len
- init_len
;
67 memcpy(p
, old
+ old_len
- init_len
, init_len
);
75 FILE *search_path::open_file(const char *name
, char **pathp
)
78 if (*name
== '/' || dirs
== 0 || *dirs
== '\0') {
79 FILE *fp
= fopen(name
, "r");
82 *pathp
= strsave(name
);
88 unsigned namelen
= strlen(name
);
92 char *end
= strchr(p
, ';');
94 char *end
= strchr(p
, ':');
97 end
= strchr(p
, '\0');
98 int need_slash
= end
> p
&& end
[-1] != '/';
99 char *path
= new char[(end
- p
) + need_slash
+ namelen
+ 1];
100 memcpy(path
, p
, end
- p
);
103 strcpy(path
+ (end
- p
) + need_slash
, name
);
105 fprintf(stderr
, "trying `%s'\n", path
);
107 FILE *fp
= fopen(path
, "r");