xref: /openbsd-src/gnu/usr.bin/texinfo/intl/relocatable.c (revision a1acfa9b69ad64eb720639240c8438f11107dc85)
1*a1acfa9bSespie /* Provide relocatable packages.
2*a1acfa9bSespie    Copyright (C) 2003 Free Software Foundation, Inc.
3*a1acfa9bSespie    Written by Bruno Haible <bruno@clisp.org>, 2003.
4*a1acfa9bSespie 
5*a1acfa9bSespie    This program is free software; you can redistribute it and/or modify it
6*a1acfa9bSespie    under the terms of the GNU Library General Public License as published
7*a1acfa9bSespie    by the Free Software Foundation; either version 2, or (at your option)
8*a1acfa9bSespie    any later version.
9*a1acfa9bSespie 
10*a1acfa9bSespie    This program is distributed in the hope that it will be useful,
11*a1acfa9bSespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*a1acfa9bSespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*a1acfa9bSespie    Library General Public License for more details.
14*a1acfa9bSespie 
15*a1acfa9bSespie    You should have received a copy of the GNU Library General Public
16*a1acfa9bSespie    License along with this program; if not, write to the Free Software
17*a1acfa9bSespie    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18*a1acfa9bSespie    USA.  */
19*a1acfa9bSespie 
20*a1acfa9bSespie 
21*a1acfa9bSespie /* Tell glibc's <stdio.h> to provide a prototype for getline().
22*a1acfa9bSespie    This must come before <config.h> because <config.h> may include
23*a1acfa9bSespie    <features.h>, and once <features.h> has been included, it's too late.  */
24*a1acfa9bSespie #ifndef _GNU_SOURCE
25*a1acfa9bSespie # define _GNU_SOURCE	1
26*a1acfa9bSespie #endif
27*a1acfa9bSespie 
28*a1acfa9bSespie #ifdef HAVE_CONFIG_H
29*a1acfa9bSespie # include "config.h"
30*a1acfa9bSespie #endif
31*a1acfa9bSespie 
32*a1acfa9bSespie /* Specification.  */
33*a1acfa9bSespie #include "relocatable.h"
34*a1acfa9bSespie 
35*a1acfa9bSespie #if ENABLE_RELOCATABLE
36*a1acfa9bSespie 
37*a1acfa9bSespie #include <stddef.h>
38*a1acfa9bSespie #include <stdio.h>
39*a1acfa9bSespie #include <stdlib.h>
40*a1acfa9bSespie #include <string.h>
41*a1acfa9bSespie 
42*a1acfa9bSespie #ifdef NO_XMALLOC
43*a1acfa9bSespie # define xmalloc malloc
44*a1acfa9bSespie #else
45*a1acfa9bSespie # include "xalloc.h"
46*a1acfa9bSespie #endif
47*a1acfa9bSespie 
48*a1acfa9bSespie #if defined _WIN32 || defined __WIN32__
49*a1acfa9bSespie # define WIN32_LEAN_AND_MEAN
50*a1acfa9bSespie # include <windows.h>
51*a1acfa9bSespie #endif
52*a1acfa9bSespie 
53*a1acfa9bSespie #if DEPENDS_ON_LIBCHARSET
54*a1acfa9bSespie # include <libcharset.h>
55*a1acfa9bSespie #endif
56*a1acfa9bSespie #if DEPENDS_ON_LIBICONV && HAVE_ICONV
57*a1acfa9bSespie # include <iconv.h>
58*a1acfa9bSespie #endif
59*a1acfa9bSespie #if DEPENDS_ON_LIBINTL && ENABLE_NLS
60*a1acfa9bSespie # include <libintl.h>
61*a1acfa9bSespie #endif
62*a1acfa9bSespie 
63*a1acfa9bSespie /* Faked cheap 'bool'.  */
64*a1acfa9bSespie #undef bool
65*a1acfa9bSespie #undef false
66*a1acfa9bSespie #undef true
67*a1acfa9bSespie #define bool int
68*a1acfa9bSespie #define false 0
69*a1acfa9bSespie #define true 1
70*a1acfa9bSespie 
71*a1acfa9bSespie /* Pathname support.
72*a1acfa9bSespie    ISSLASH(C)           tests whether C is a directory separator character.
73*a1acfa9bSespie    IS_PATH_WITH_DIR(P)  tests whether P contains a directory specification.
74*a1acfa9bSespie  */
75*a1acfa9bSespie #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
76*a1acfa9bSespie   /* Win32, OS/2, DOS */
77*a1acfa9bSespie # define ISSLASH(C) ((C) == '/' || (C) == '\\')
78*a1acfa9bSespie # define HAS_DEVICE(P) \
79*a1acfa9bSespie     ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
80*a1acfa9bSespie      && (P)[1] == ':')
81*a1acfa9bSespie # define IS_PATH_WITH_DIR(P) \
82*a1acfa9bSespie     (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
83*a1acfa9bSespie # define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
84*a1acfa9bSespie #else
85*a1acfa9bSespie   /* Unix */
86*a1acfa9bSespie # define ISSLASH(C) ((C) == '/')
87*a1acfa9bSespie # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
88*a1acfa9bSespie # define FILESYSTEM_PREFIX_LEN(P) 0
89*a1acfa9bSespie #endif
90*a1acfa9bSespie 
91*a1acfa9bSespie /* Original installation prefix.  */
92*a1acfa9bSespie static char *orig_prefix;
93*a1acfa9bSespie static size_t orig_prefix_len;
94*a1acfa9bSespie /* Current installation prefix.  */
95*a1acfa9bSespie static char *curr_prefix;
96*a1acfa9bSespie static size_t curr_prefix_len;
97*a1acfa9bSespie /* These prefixes do not end in a slash.  Anything that will be concatenated
98*a1acfa9bSespie    to them must start with a slash.  */
99*a1acfa9bSespie 
100*a1acfa9bSespie /* Sets the original and the current installation prefix of this module.
101*a1acfa9bSespie    Relocation simply replaces a pathname starting with the original prefix
102*a1acfa9bSespie    by the corresponding pathname with the current prefix instead.  Both
103*a1acfa9bSespie    prefixes should be directory names without trailing slash (i.e. use ""
104*a1acfa9bSespie    instead of "/").  */
105*a1acfa9bSespie static void
set_this_relocation_prefix(const char * orig_prefix_arg,const char * curr_prefix_arg)106*a1acfa9bSespie set_this_relocation_prefix (const char *orig_prefix_arg,
107*a1acfa9bSespie 			    const char *curr_prefix_arg)
108*a1acfa9bSespie {
109*a1acfa9bSespie   if (orig_prefix_arg != NULL && curr_prefix_arg != NULL
110*a1acfa9bSespie       /* Optimization: if orig_prefix and curr_prefix are equal, the
111*a1acfa9bSespie 	 relocation is a nop.  */
112*a1acfa9bSespie       && strcmp (orig_prefix_arg, curr_prefix_arg) != 0)
113*a1acfa9bSespie     {
114*a1acfa9bSespie       /* Duplicate the argument strings.  */
115*a1acfa9bSespie       char *memory;
116*a1acfa9bSespie 
117*a1acfa9bSespie       orig_prefix_len = strlen (orig_prefix_arg);
118*a1acfa9bSespie       curr_prefix_len = strlen (curr_prefix_arg);
119*a1acfa9bSespie       memory = (char *) xmalloc (orig_prefix_len + 1 + curr_prefix_len + 1);
120*a1acfa9bSespie #ifdef NO_XMALLOC
121*a1acfa9bSespie       if (memory != NULL)
122*a1acfa9bSespie #endif
123*a1acfa9bSespie 	{
124*a1acfa9bSespie 	  memcpy (memory, orig_prefix_arg, orig_prefix_len + 1);
125*a1acfa9bSespie 	  orig_prefix = memory;
126*a1acfa9bSespie 	  memory += orig_prefix_len + 1;
127*a1acfa9bSespie 	  memcpy (memory, curr_prefix_arg, curr_prefix_len + 1);
128*a1acfa9bSespie 	  curr_prefix = memory;
129*a1acfa9bSespie 	  return;
130*a1acfa9bSespie 	}
131*a1acfa9bSespie     }
132*a1acfa9bSespie   orig_prefix = NULL;
133*a1acfa9bSespie   curr_prefix = NULL;
134*a1acfa9bSespie   /* Don't worry about wasted memory here - this function is usually only
135*a1acfa9bSespie      called once.  */
136*a1acfa9bSespie }
137*a1acfa9bSespie 
138*a1acfa9bSespie /* Sets the original and the current installation prefix of the package.
139*a1acfa9bSespie    Relocation simply replaces a pathname starting with the original prefix
140*a1acfa9bSespie    by the corresponding pathname with the current prefix instead.  Both
141*a1acfa9bSespie    prefixes should be directory names without trailing slash (i.e. use ""
142*a1acfa9bSespie    instead of "/").  */
143*a1acfa9bSespie void
set_relocation_prefix(const char * orig_prefix_arg,const char * curr_prefix_arg)144*a1acfa9bSespie set_relocation_prefix (const char *orig_prefix_arg, const char *curr_prefix_arg)
145*a1acfa9bSespie {
146*a1acfa9bSespie   set_this_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
147*a1acfa9bSespie 
148*a1acfa9bSespie   /* Now notify all dependent libraries.  */
149*a1acfa9bSespie #if DEPENDS_ON_LIBCHARSET
150*a1acfa9bSespie   libcharset_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
151*a1acfa9bSespie #endif
152*a1acfa9bSespie #if DEPENDS_ON_LIBICONV && HAVE_ICONV && _LIBICONV_VERSION >= 0x0109
153*a1acfa9bSespie   libiconv_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
154*a1acfa9bSespie #endif
155*a1acfa9bSespie #if DEPENDS_ON_LIBINTL && ENABLE_NLS && defined libintl_set_relocation_prefix
156*a1acfa9bSespie   libintl_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
157*a1acfa9bSespie #endif
158*a1acfa9bSespie }
159*a1acfa9bSespie 
160*a1acfa9bSespie #if !defined IN_LIBRARY || (defined PIC && defined INSTALLDIR)
161*a1acfa9bSespie 
162*a1acfa9bSespie /* Convenience function:
163*a1acfa9bSespie    Computes the current installation prefix, based on the original
164*a1acfa9bSespie    installation prefix, the original installation directory of a particular
165*a1acfa9bSespie    file, and the current pathname of this file.  Returns NULL upon failure.  */
166*a1acfa9bSespie #ifdef IN_LIBRARY
167*a1acfa9bSespie #define compute_curr_prefix local_compute_curr_prefix
168*a1acfa9bSespie static
169*a1acfa9bSespie #endif
170*a1acfa9bSespie const char *
compute_curr_prefix(const char * orig_installprefix,const char * orig_installdir,const char * curr_pathname)171*a1acfa9bSespie compute_curr_prefix (const char *orig_installprefix,
172*a1acfa9bSespie 		     const char *orig_installdir,
173*a1acfa9bSespie 		     const char *curr_pathname)
174*a1acfa9bSespie {
175*a1acfa9bSespie   const char *curr_installdir;
176*a1acfa9bSespie   const char *rel_installdir;
177*a1acfa9bSespie 
178*a1acfa9bSespie   if (curr_pathname == NULL)
179*a1acfa9bSespie     return NULL;
180*a1acfa9bSespie 
181*a1acfa9bSespie   /* Determine the relative installation directory, relative to the prefix.
182*a1acfa9bSespie      This is simply the difference between orig_installprefix and
183*a1acfa9bSespie      orig_installdir.  */
184*a1acfa9bSespie   if (strncmp (orig_installprefix, orig_installdir, strlen (orig_installprefix))
185*a1acfa9bSespie       != 0)
186*a1acfa9bSespie     /* Shouldn't happen - nothing should be installed outside $(prefix).  */
187*a1acfa9bSespie     return NULL;
188*a1acfa9bSespie   rel_installdir = orig_installdir + strlen (orig_installprefix);
189*a1acfa9bSespie 
190*a1acfa9bSespie   /* Determine the current installation directory.  */
191*a1acfa9bSespie   {
192*a1acfa9bSespie     const char *p_base = curr_pathname + FILESYSTEM_PREFIX_LEN (curr_pathname);
193*a1acfa9bSespie     const char *p = curr_pathname + strlen (curr_pathname);
194*a1acfa9bSespie     char *q;
195*a1acfa9bSespie 
196*a1acfa9bSespie     while (p > p_base)
197*a1acfa9bSespie       {
198*a1acfa9bSespie 	p--;
199*a1acfa9bSespie 	if (ISSLASH (*p))
200*a1acfa9bSespie 	  break;
201*a1acfa9bSespie       }
202*a1acfa9bSespie 
203*a1acfa9bSespie     q = (char *) xmalloc (p - curr_pathname + 1);
204*a1acfa9bSespie #ifdef NO_XMALLOC
205*a1acfa9bSespie     if (q == NULL)
206*a1acfa9bSespie       return NULL;
207*a1acfa9bSespie #endif
208*a1acfa9bSespie     memcpy (q, curr_pathname, p - curr_pathname);
209*a1acfa9bSespie     q[p - curr_pathname] = '\0';
210*a1acfa9bSespie     curr_installdir = q;
211*a1acfa9bSespie   }
212*a1acfa9bSespie 
213*a1acfa9bSespie   /* Compute the current installation prefix by removing the trailing
214*a1acfa9bSespie      rel_installdir from it.  */
215*a1acfa9bSespie   {
216*a1acfa9bSespie     const char *rp = rel_installdir + strlen (rel_installdir);
217*a1acfa9bSespie     const char *cp = curr_installdir + strlen (curr_installdir);
218*a1acfa9bSespie     const char *cp_base =
219*a1acfa9bSespie       curr_installdir + FILESYSTEM_PREFIX_LEN (curr_installdir);
220*a1acfa9bSespie 
221*a1acfa9bSespie     while (rp > rel_installdir && cp > cp_base)
222*a1acfa9bSespie       {
223*a1acfa9bSespie 	bool same = false;
224*a1acfa9bSespie 	const char *rpi = rp;
225*a1acfa9bSespie 	const char *cpi = cp;
226*a1acfa9bSespie 
227*a1acfa9bSespie 	while (rpi > rel_installdir && cpi > cp_base)
228*a1acfa9bSespie 	  {
229*a1acfa9bSespie 	    rpi--;
230*a1acfa9bSespie 	    cpi--;
231*a1acfa9bSespie 	    if (ISSLASH (*rpi) || ISSLASH (*cpi))
232*a1acfa9bSespie 	      {
233*a1acfa9bSespie 		if (ISSLASH (*rpi) && ISSLASH (*cpi))
234*a1acfa9bSespie 		  same = true;
235*a1acfa9bSespie 		break;
236*a1acfa9bSespie 	      }
237*a1acfa9bSespie #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
238*a1acfa9bSespie 	    /* Win32, OS/2, DOS - case insignificant filesystem */
239*a1acfa9bSespie 	    if ((*rpi >= 'a' && *rpi <= 'z' ? *rpi - 'a' + 'A' : *rpi)
240*a1acfa9bSespie 		!= (*cpi >= 'a' && *cpi <= 'z' ? *cpi - 'a' + 'A' : *cpi))
241*a1acfa9bSespie 	      break;
242*a1acfa9bSespie #else
243*a1acfa9bSespie 	    if (*rpi != *cpi)
244*a1acfa9bSespie 	      break;
245*a1acfa9bSespie #endif
246*a1acfa9bSespie 	  }
247*a1acfa9bSespie 	if (!same)
248*a1acfa9bSespie 	  break;
249*a1acfa9bSespie 	/* The last pathname component was the same.  opi and cpi now point
250*a1acfa9bSespie 	   to the slash before it.  */
251*a1acfa9bSespie 	rp = rpi;
252*a1acfa9bSespie 	cp = cpi;
253*a1acfa9bSespie       }
254*a1acfa9bSespie 
255*a1acfa9bSespie     if (rp > rel_installdir)
256*a1acfa9bSespie       /* Unexpected: The curr_installdir does not end with rel_installdir.  */
257*a1acfa9bSespie       return NULL;
258*a1acfa9bSespie 
259*a1acfa9bSespie     {
260*a1acfa9bSespie       size_t curr_prefix_len = cp - curr_installdir;
261*a1acfa9bSespie       char *curr_prefix;
262*a1acfa9bSespie 
263*a1acfa9bSespie       curr_prefix = (char *) xmalloc (curr_prefix_len + 1);
264*a1acfa9bSespie #ifdef NO_XMALLOC
265*a1acfa9bSespie       if (curr_prefix == NULL)
266*a1acfa9bSespie 	return NULL;
267*a1acfa9bSespie #endif
268*a1acfa9bSespie       memcpy (curr_prefix, curr_installdir, curr_prefix_len);
269*a1acfa9bSespie       curr_prefix[curr_prefix_len] = '\0';
270*a1acfa9bSespie 
271*a1acfa9bSespie       return curr_prefix;
272*a1acfa9bSespie     }
273*a1acfa9bSespie   }
274*a1acfa9bSespie }
275*a1acfa9bSespie 
276*a1acfa9bSespie #endif /* !IN_LIBRARY || PIC */
277*a1acfa9bSespie 
278*a1acfa9bSespie #if defined PIC && defined INSTALLDIR
279*a1acfa9bSespie 
280*a1acfa9bSespie /* Full pathname of shared library, or NULL.  */
281*a1acfa9bSespie static char *shared_library_fullname;
282*a1acfa9bSespie 
283*a1acfa9bSespie #if defined _WIN32 || defined __WIN32__
284*a1acfa9bSespie 
285*a1acfa9bSespie /* Determine the full pathname of the shared library when it is loaded.  */
286*a1acfa9bSespie 
287*a1acfa9bSespie BOOL WINAPI
DllMain(HINSTANCE module_handle,DWORD event,LPVOID reserved)288*a1acfa9bSespie DllMain (HINSTANCE module_handle, DWORD event, LPVOID reserved)
289*a1acfa9bSespie {
290*a1acfa9bSespie   (void) reserved;
291*a1acfa9bSespie 
292*a1acfa9bSespie   if (event == DLL_PROCESS_ATTACH)
293*a1acfa9bSespie     {
294*a1acfa9bSespie       /* The DLL is being loaded into an application's address range.  */
295*a1acfa9bSespie       static char location[MAX_PATH];
296*a1acfa9bSespie 
297*a1acfa9bSespie       if (!GetModuleFileName (module_handle, location, sizeof (location)))
298*a1acfa9bSespie 	/* Shouldn't happen.  */
299*a1acfa9bSespie 	return FALSE;
300*a1acfa9bSespie 
301*a1acfa9bSespie       if (!IS_PATH_WITH_DIR (location))
302*a1acfa9bSespie 	/* Shouldn't happen.  */
303*a1acfa9bSespie 	return FALSE;
304*a1acfa9bSespie 
305*a1acfa9bSespie       shared_library_fullname = strdup (location);
306*a1acfa9bSespie     }
307*a1acfa9bSespie 
308*a1acfa9bSespie   return TRUE;
309*a1acfa9bSespie }
310*a1acfa9bSespie 
311*a1acfa9bSespie #else /* Unix */
312*a1acfa9bSespie 
313*a1acfa9bSespie static void
find_shared_library_fullname()314*a1acfa9bSespie find_shared_library_fullname ()
315*a1acfa9bSespie {
316*a1acfa9bSespie #if defined __linux__ && __GLIBC__ >= 2
317*a1acfa9bSespie   /* Linux has /proc/self/maps. glibc 2 has the getline() function.  */
318*a1acfa9bSespie   FILE *fp;
319*a1acfa9bSespie 
320*a1acfa9bSespie   /* Open the current process' maps file.  It describes one VMA per line.  */
321*a1acfa9bSespie   fp = fopen ("/proc/self/maps", "r");
322*a1acfa9bSespie   if (fp)
323*a1acfa9bSespie     {
324*a1acfa9bSespie       unsigned long address = (unsigned long) &find_shared_library_fullname;
325*a1acfa9bSespie       for (;;)
326*a1acfa9bSespie 	{
327*a1acfa9bSespie 	  unsigned long start, end;
328*a1acfa9bSespie 	  int c;
329*a1acfa9bSespie 
330*a1acfa9bSespie 	  if (fscanf (fp, "%lx-%lx", &start, &end) != 2)
331*a1acfa9bSespie 	    break;
332*a1acfa9bSespie 	  if (address >= start && address <= end - 1)
333*a1acfa9bSespie 	    {
334*a1acfa9bSespie 	      /* Found it.  Now see if this line contains a filename.  */
335*a1acfa9bSespie 	      while (c = getc (fp), c != EOF && c != '\n' && c != '/')
336*a1acfa9bSespie 		continue;
337*a1acfa9bSespie 	      if (c == '/')
338*a1acfa9bSespie 		{
339*a1acfa9bSespie 		  size_t size;
340*a1acfa9bSespie 		  int len;
341*a1acfa9bSespie 
342*a1acfa9bSespie 		  ungetc (c, fp);
343*a1acfa9bSespie 		  shared_library_fullname = NULL; size = 0;
344*a1acfa9bSespie 		  len = getline (&shared_library_fullname, &size, fp);
345*a1acfa9bSespie 		  if (len >= 0)
346*a1acfa9bSespie 		    {
347*a1acfa9bSespie 		      /* Success: filled shared_library_fullname.  */
348*a1acfa9bSespie 		      if (len > 0 && shared_library_fullname[len - 1] == '\n')
349*a1acfa9bSespie 			shared_library_fullname[len - 1] = '\0';
350*a1acfa9bSespie 		    }
351*a1acfa9bSespie 		}
352*a1acfa9bSespie 	      break;
353*a1acfa9bSespie 	    }
354*a1acfa9bSespie 	  while (c = getc (fp), c != EOF && c != '\n')
355*a1acfa9bSespie 	    continue;
356*a1acfa9bSespie 	}
357*a1acfa9bSespie       fclose (fp);
358*a1acfa9bSespie     }
359*a1acfa9bSespie #endif
360*a1acfa9bSespie }
361*a1acfa9bSespie 
362*a1acfa9bSespie #endif /* WIN32 / Unix */
363*a1acfa9bSespie 
364*a1acfa9bSespie /* Return the full pathname of the current shared library.
365*a1acfa9bSespie    Return NULL if unknown.
366*a1acfa9bSespie    Guaranteed to work only on Linux and Woe32.  */
367*a1acfa9bSespie static char *
get_shared_library_fullname()368*a1acfa9bSespie get_shared_library_fullname ()
369*a1acfa9bSespie {
370*a1acfa9bSespie #if !(defined _WIN32 || defined __WIN32__)
371*a1acfa9bSespie   static bool tried_find_shared_library_fullname;
372*a1acfa9bSespie   if (!tried_find_shared_library_fullname)
373*a1acfa9bSespie     {
374*a1acfa9bSespie       find_shared_library_fullname ();
375*a1acfa9bSespie       tried_find_shared_library_fullname = true;
376*a1acfa9bSespie     }
377*a1acfa9bSespie #endif
378*a1acfa9bSespie   return shared_library_fullname;
379*a1acfa9bSespie }
380*a1acfa9bSespie 
381*a1acfa9bSespie #endif /* PIC */
382*a1acfa9bSespie 
383*a1acfa9bSespie /* Returns the pathname, relocated according to the current installation
384*a1acfa9bSespie    directory.  */
385*a1acfa9bSespie const char *
relocate(const char * pathname)386*a1acfa9bSespie relocate (const char *pathname)
387*a1acfa9bSespie {
388*a1acfa9bSespie #if defined PIC && defined INSTALLDIR
389*a1acfa9bSespie   static int initialized;
390*a1acfa9bSespie 
391*a1acfa9bSespie   /* Initialization code for a shared library.  */
392*a1acfa9bSespie   if (!initialized)
393*a1acfa9bSespie     {
394*a1acfa9bSespie       /* At this point, orig_prefix and curr_prefix likely have already been
395*a1acfa9bSespie 	 set through the main program's set_program_name_and_installdir
396*a1acfa9bSespie 	 function.  This is sufficient in the case that the library has
397*a1acfa9bSespie 	 initially been installed in the same orig_prefix.  But we can do
398*a1acfa9bSespie 	 better, to also cover the cases that 1. it has been installed
399*a1acfa9bSespie 	 in a different prefix before being moved to orig_prefix and (later)
400*a1acfa9bSespie 	 to curr_prefix, 2. unlike the program, it has not moved away from
401*a1acfa9bSespie 	 orig_prefix.  */
402*a1acfa9bSespie       const char *orig_installprefix = INSTALLPREFIX;
403*a1acfa9bSespie       const char *orig_installdir = INSTALLDIR;
404*a1acfa9bSespie       const char *curr_prefix_better;
405*a1acfa9bSespie 
406*a1acfa9bSespie       curr_prefix_better =
407*a1acfa9bSespie 	compute_curr_prefix (orig_installprefix, orig_installdir,
408*a1acfa9bSespie 			     get_shared_library_fullname ());
409*a1acfa9bSespie       if (curr_prefix_better == NULL)
410*a1acfa9bSespie 	curr_prefix_better = curr_prefix;
411*a1acfa9bSespie 
412*a1acfa9bSespie       set_relocation_prefix (orig_installprefix, curr_prefix_better);
413*a1acfa9bSespie 
414*a1acfa9bSespie       initialized = 1;
415*a1acfa9bSespie     }
416*a1acfa9bSespie #endif
417*a1acfa9bSespie 
418*a1acfa9bSespie   /* Note: It is not necessary to perform case insensitive comparison here,
419*a1acfa9bSespie      even for DOS-like filesystems, because the pathname argument was
420*a1acfa9bSespie      typically created from the same Makefile variable as orig_prefix came
421*a1acfa9bSespie      from.  */
422*a1acfa9bSespie   if (orig_prefix != NULL && curr_prefix != NULL
423*a1acfa9bSespie       && strncmp (pathname, orig_prefix, orig_prefix_len) == 0)
424*a1acfa9bSespie     {
425*a1acfa9bSespie       if (pathname[orig_prefix_len] == '\0')
426*a1acfa9bSespie 	/* pathname equals orig_prefix.  */
427*a1acfa9bSespie 	return curr_prefix;
428*a1acfa9bSespie       if (ISSLASH (pathname[orig_prefix_len]))
429*a1acfa9bSespie 	{
430*a1acfa9bSespie 	  /* pathname starts with orig_prefix.  */
431*a1acfa9bSespie 	  const char *pathname_tail = &pathname[orig_prefix_len];
432*a1acfa9bSespie 	  char *result =
433*a1acfa9bSespie 	    (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1);
434*a1acfa9bSespie 
435*a1acfa9bSespie #ifdef NO_XMALLOC
436*a1acfa9bSespie 	  if (result != NULL)
437*a1acfa9bSespie #endif
438*a1acfa9bSespie 	    {
439*a1acfa9bSespie 	      memcpy (result, curr_prefix, curr_prefix_len);
440*a1acfa9bSespie 	      strcpy (result + curr_prefix_len, pathname_tail);
441*a1acfa9bSespie 	      return result;
442*a1acfa9bSespie 	    }
443*a1acfa9bSespie 	}
444*a1acfa9bSespie     }
445*a1acfa9bSespie   /* Nothing to relocate.  */
446*a1acfa9bSespie   return pathname;
447*a1acfa9bSespie }
448*a1acfa9bSespie 
449*a1acfa9bSespie #endif
450