* src/libs/libgroff/searchpath.cc (open_file): Adapting to WinNT.
[s-roff.git] / src / libs / libgroff / searchpath.cc
blob78b1fcd9ea556457267b16cdfd15e24057bc5050
1 // -*- C++ -*-
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
10 version.
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
15 for more details.
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. */
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <assert.h>
26 #include "lib.h"
27 #include "searchpath.h"
29 search_path::search_path(const char *envvar, const char *standard)
31 char *e = envvar ? getenv(envvar) : 0;
32 if (e && standard) {
33 dirs = new char[strlen(e) + strlen(standard) + 2];
34 strcpy(dirs, e);
35 strcat(dirs, ":");
36 strcat(dirs, standard);
38 else
39 dirs = strsave(e ? e : standard);
40 init_len = dirs ? strlen(dirs) : 0;
43 search_path::~search_path()
45 if (dirs)
46 a_delete dirs;
49 void search_path::command_line_dir(const char *s)
51 if (!dirs)
52 dirs = strsave(s);
53 else {
54 char *old = dirs;
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);
59 char *p = dirs;
60 p += old_len - init_len;
61 if (init_len == 0)
62 *p++ = ':';
63 memcpy(p, s, slen);
64 p += slen;
65 if (init_len > 0) {
66 *p++ = ':';
67 memcpy(p, old + old_len - init_len, init_len);
68 p += init_len;
70 *p++ = '\0';
71 a_delete old;
75 FILE *search_path::open_file(const char *name, char **pathp)
77 assert(name != 0);
78 if (*name == '/' || dirs == 0 || *dirs == '\0') {
79 FILE *fp = fopen(name, "r");
80 if (fp) {
81 if (pathp)
82 *pathp = strsave(name);
83 return fp;
85 else
86 return 0;
88 unsigned namelen = strlen(name);
89 char *p = dirs;
90 for (;;) {
91 #ifdef _MSC_VER
92 char *end = strchr(p, ';');
93 #else
94 char *end = strchr(p, ':');
95 #endif
96 if (!end)
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);
101 if (need_slash)
102 path[end - p] = '/';
103 strcpy(path + (end - p) + need_slash, name);
104 #if 0
105 fprintf(stderr, "trying `%s'\n", path);
106 #endif
107 FILE *fp = fopen(path, "r");
108 if (fp) {
109 if (pathp)
110 *pathp = path;
111 else
112 a_delete path;
113 return fp;
115 a_delete path;
116 if (*end == '\0')
117 break;
118 p = end + 1;
120 return 0;