2 /* Provide relocation for macro and font files.
3 Copyright (C) 2005 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301,
20 // Made after relocation code in kpathsea and gettext.
33 # define WIN32_LEAN_AND_MEAN
37 #define INSTALLPATHLEN (sizeof(INSTALLPATH) - 1)
42 extern "C" const char *program_name;
44 // The prefix (parent directory) corresponding to the binary.
45 char *curr_prefix = 0;
46 size_t curr_prefix_len = 0;
48 // Return the directory part of a filename, or `.' if no path separators.
49 char *xdirname(char *s)
51 static const char dot[] = ".";
54 // DIR_SEPS[] are possible directory separator characters, see nonposix.h.
55 // We want the rightmost separator of all possible ones.
56 // Example: d:/foo\\bar.
57 char *p = strrchr(s, DIR_SEPS[0]);
58 const char *sep = &DIR_SEPS[1];
60 char *p1 = strrchr(s, *sep);
61 if (p1 && (!p || p1 > p))
72 // Return the full path of NAME along the path PATHP.
73 // Adapted from search_path::open_file in searchpath.cpp.
74 char *searchpath(const char *name, const char *pathp)
80 fprintf(stderr, "searchpath: pathp: `%s'\n", pathp);
81 fprintf(stderr, "searchpath: trying `%s'\n", name);
83 // Try first NAME as such; success if NAME is an absolute filename,
84 // or if NAME is found in the current directory.
85 if (!access (name, F_OK)) {
86 path = new char[path_name_max()];
88 path = _fullpath(path, name, path_name_max());
90 path = realpath(name, path);
93 fprintf(stderr, "searchpath: found `%s'\n", path);
97 // Secondly, try the current directory.
98 // Now search along PATHP.
99 size_t namelen = strlen(name);
100 char *p = (char *)pathp;
102 char *end = strchr(p, PATH_SEP_CHAR);
104 end = strchr(p, '\0');
105 int need_slash = end > p && strchr(DIR_SEPS, end[-1]) == 0;
106 path = new char[end - p + need_slash + namelen + 1];
107 memcpy(path, p, end - p);
110 strcpy(path + (end - p) + need_slash, name);
112 fprintf(stderr, "searchpath: trying `%s'\n", path);
114 if (!access(path, F_OK)) {
116 fprintf(stderr, "searchpath: found `%s'\n", name);
128 // Search NAME along PATHP with the elements of PATHEXT in turn added.
129 char *searchpathext(const char *name, const char *pathext, const char *pathp)
132 char *tmpathext = strsave(pathext); // strtok modifies this string,
134 char *ext = strtok(tmpathext, PATH_SEP);
136 char *namex = new char[strlen(name) + strlen(ext) + 1];
139 found = searchpath(namex, pathp);
143 ext = strtok(0, PATH_SEP);
149 // Convert an MS path to a POSIX path.
150 char *msw2posixpath(char *path)
161 // Compute the current prefix.
162 void set_current_prefix()
165 curr_prefix = new char[path_name_max()];
166 // Obtain the full path of the current binary;
167 // using GetModuleFileName on MS-Windows,
168 // and searching along PATH on other systems.
170 int len = GetModuleFileName(0, curr_prefix, path_name_max());
172 len = GetShortPathName(curr_prefix, curr_prefix, path_name_max());
174 fprintf(stderr, "curr_prefix: %s\n", curr_prefix);
177 curr_prefix = searchpath(program_name, getenv("PATH"));
178 if (!curr_prefix && !strchr(program_name, '.')) { // try with extensions
179 pathextstr = strsave(getenv("PATHEXT"));
181 pathextstr = strsave(PATH_EXT);
182 curr_prefix = searchpathext(program_name, pathextstr, getenv("PATH"));
188 msw2posixpath(curr_prefix);
190 fprintf(stderr, "curr_prefix: %s\n", curr_prefix);
192 curr_prefix = xdirname(curr_prefix); // directory of executable
193 curr_prefix = xdirname(curr_prefix); // parent directory of executable
194 curr_prefix_len = strlen(curr_prefix);
196 fprintf(stderr, "curr_prefix: %s\n", curr_prefix);
197 fprintf(stderr, "curr_prefix_len: %d\n", curr_prefix_len);
201 // Strip the installation prefix and replace it
202 // with the current installation prefix; return the relocated path.
203 char *relocatep(const char *path)
206 fprintf(stderr, "relocatep: path = %s\n", path);
207 fprintf(stderr, "relocatep: INSTALLPATH = %s\n", INSTALLPATH);
208 fprintf(stderr, "relocatep: INSTALLPATHLEN = %d\n", INSTALLPATHLEN);
211 set_current_prefix();
212 if (strncmp(INSTALLPATH, path, INSTALLPATHLEN))
213 return strsave(path);
214 char *relative_path = (char *)path + INSTALLPATHLEN;
215 size_t relative_path_len = strlen(relative_path);
216 char *relocated_path = new char[curr_prefix_len + relative_path_len + 1];
217 strcpy(relocated_path, curr_prefix);
218 strcat(relocated_path, relative_path);
220 fprintf(stderr, "relocated_path: %s\n", relocated_path);
222 return relocated_path;
225 // Return the original pathname if it exists;
226 // otherwise return the relocated path.
227 char *relocate(const char *path)
230 if (access(path, F_OK))
235 fprintf (stderr, "relocate: %s\n", p);