11/* Provide relocatable packages.
2- Copyright (C) 2003-2006 Free Software Foundation, Inc.
2+ Copyright (C) 2003-2006, 2008 Free Software Foundation, Inc.
33 Written by Bruno Haible <bruno@clisp.org>, 2003.
44
55 This program is free software; you can redistribute it and/or modify it
@@ -160,17 +160,18 @@ set_relocation_prefix (const char *orig_prefix_arg, const char *curr_prefix_arg)
160160/* Convenience function:
161161 Computes the current installation prefix, based on the original
162162 installation prefix, the original installation directory of a particular
163- file, and the current pathname of this file. Returns NULL upon failure. */
163+ file, and the current pathname of this file.
164+ Returns it, freshly allocated. Returns NULL upon failure. */
164165#ifdef IN_LIBRARY
165166#define compute_curr_prefix local_compute_curr_prefix
166167static
167168#endif
168- const char *
169+ char *
169170compute_curr_prefix (const char * orig_installprefix ,
170171 const char * orig_installdir ,
171172 const char * curr_pathname )
172173{
173- const char * curr_installdir ;
174+ char * curr_installdir ;
174175 const char * rel_installdir ;
175176
176177 if (curr_pathname == NULL )
@@ -254,8 +255,11 @@ compute_curr_prefix (const char *orig_installprefix,
254255 }
255256
256257 if (rp > rel_installdir )
257- /* Unexpected: The curr_installdir does not end with rel_installdir. */
258- return NULL ;
258+ {
259+ /* Unexpected: The curr_installdir does not end with rel_installdir. */
260+ free (curr_installdir );
261+ return NULL ;
262+ }
259263
260264 {
261265 size_t curr_prefix_len = cp - curr_installdir ;
@@ -264,11 +268,16 @@ compute_curr_prefix (const char *orig_installprefix,
264268 curr_prefix = (char * ) xmalloc (curr_prefix_len + 1 );
265269#ifdef NO_XMALLOC
266270 if (curr_prefix == NULL )
267- return NULL ;
271+ {
272+ free (curr_installdir );
273+ return NULL ;
274+ }
268275#endif
269276 memcpy (curr_prefix , curr_installdir , curr_prefix_len );
270277 curr_prefix [curr_prefix_len ] = '\0' ;
271278
279+ free (curr_installdir );
280+
272281 return curr_prefix ;
273282 }
274283 }
@@ -400,7 +409,9 @@ get_shared_library_fullname ()
400409#endif /* PIC */
401410
402411/* Returns the pathname, relocated according to the current installation
403- directory. */
412+ directory.
413+ The returned string is either PATHNAME unmodified or a freshly allocated
414+ string that you can free with free() after casting it to 'char *'. */
404415const char *
405416relocate (const char * pathname )
406417{
@@ -420,15 +431,19 @@ relocate (const char *pathname)
420431 orig_prefix. */
421432 const char * orig_installprefix = INSTALLPREFIX ;
422433 const char * orig_installdir = INSTALLDIR ;
423- const char * curr_prefix_better ;
434+ char * curr_prefix_better ;
424435
425436 curr_prefix_better =
426437 compute_curr_prefix (orig_installprefix , orig_installdir ,
427438 get_shared_library_fullname ());
428- if (curr_prefix_better == NULL )
429- curr_prefix_better = curr_prefix ;
430439
431- set_relocation_prefix (orig_installprefix , curr_prefix_better );
440+ set_relocation_prefix (orig_installprefix ,
441+ curr_prefix_better != NULL
442+ ? curr_prefix_better
443+ : curr_prefix );
444+
445+ if (curr_prefix_better != NULL )
446+ free (curr_prefix_better );
432447
433448 initialized = 1 ;
434449 }
@@ -442,9 +457,19 @@ relocate (const char *pathname)
442457 && strncmp (pathname , orig_prefix , orig_prefix_len ) == 0 )
443458 {
444459 if (pathname [orig_prefix_len ] == '\0' )
445- /* pathname equals orig_prefix. */
446- return curr_prefix ;
447- if (ISSLASH (pathname [orig_prefix_len ]))
460+ {
461+ /* pathname equals orig_prefix. */
462+ char * result = (char * ) xmalloc (strlen (curr_prefix ) + 1 );
463+
464+ #ifdef NO_XMALLOC
465+ if (result != NULL )
466+ #endif
467+ {
468+ strcpy (result , curr_prefix );
469+ return result ;
470+ }
471+ }
472+ else if (ISSLASH (pathname [orig_prefix_len ]))
448473 {
449474 /* pathname starts with orig_prefix. */
450475 const char * pathname_tail = & pathname [orig_prefix_len ];
0 commit comments