const char * relocate (const char *pathname) { #if defined PIC && defined INSTALLDIR static int initialized; if (!initialized) { const char *orig_installprefix = INSTALLPREFIX; const char *orig_installdir = INSTALLDIR; const char *curr_prefix_better; curr_prefix_better = compute_curr_prefix (orig_installprefix, orig_installdir, get_shared_library_fullname ()); if (curr_prefix_better == NULL) curr_prefix_better = curr_prefix; set_relocation_prefix (orig_installprefix, curr_prefix_better); initialized = 1; } #endif if (orig_prefix != NULL && curr_prefix != NULL && strncmp (pathname, orig_prefix, orig_prefix_len) == 0) { if (pathname[orig_prefix_len] == '\0') return curr_prefix; if (ISSLASH (pathname[orig_prefix_len])) { const char *pathname_tail = &pathname[orig_prefix_len]; char *result = (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { memcpy (result, curr_prefix, curr_prefix_len); strcpy (result + curr_prefix_len, pathname_tail); return result; } } } return pathname; }
static void prepare_relocate (const char *orig_installprefix, const char *orig_installdir, const char *argv0) { const char *curr_prefix; /* Determine the full pathname of the current executable. */ executable_fullname = find_executable (argv0); /* Determine the current installation prefix from it. */ curr_prefix = compute_curr_prefix (orig_installprefix, orig_installdir, executable_fullname); if (curr_prefix != NULL) /* Now pass this prefix to all copies of the relocate.c source file. */ set_relocation_prefix (orig_installprefix, curr_prefix); }
/* Returns the pathname, relocated according to the current installation directory. */ const char * relocate (const char *pathname) { #if defined PIC && defined INSTALLDIR static int initialized; /* Initialization code for a shared library. */ if (!initialized) { /* At this point, orig_prefix and curr_prefix likely have already been set through the main program's set_program_name_and_installdir function. This is sufficient in the case that the library has initially been installed in the same orig_prefix. But we can do better, to also cover the cases that 1. it has been installed in a different prefix before being moved to orig_prefix and (later) to curr_prefix, 2. unlike the program, it has not moved away from orig_prefix. */ const char *orig_installprefix = INSTALLPREFIX; const char *orig_installdir = INSTALLDIR; const char *curr_prefix_better; curr_prefix_better = compute_curr_prefix (orig_installprefix, orig_installdir, get_shared_library_fullname ()); if (curr_prefix_better == NULL) curr_prefix_better = curr_prefix; set_relocation_prefix (orig_installprefix, curr_prefix_better); initialized = 1; } #endif /* Note: It is not necessary to perform case insensitive comparison here, even for DOS-like filesystems, because the pathname argument was typically created from the same Makefile variable as orig_prefix came from. */ if (orig_prefix != NULL && curr_prefix != NULL && strncmp (pathname, orig_prefix, orig_prefix_len) == 0) { if (pathname[orig_prefix_len] == '\0') /* pathname equals orig_prefix. */ return curr_prefix; if (ISSLASH (pathname[orig_prefix_len])) { /* pathname starts with orig_prefix. */ const char *pathname_tail = &pathname[orig_prefix_len]; char *result = (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { memcpy (result, curr_prefix, curr_prefix_len); strcpy (result + curr_prefix_len, pathname_tail); return result; } } } /* Nothing to relocate. */ return pathname; }
/* Returns the pathname, relocated according to the current installation directory. The returned string is either PATHNAME unmodified or a freshly allocated string that you can free with free() after casting it to 'char *'. */ const char * relocate (const char *pathname) { #if defined PIC && defined INSTALLDIR && ENABLE_COSTLY_RELOCATABLE static int initialized; /* Initialization code for a shared library. */ if (!initialized) { /* At this point, orig_prefix and curr_prefix likely have already been set through the main program's set_program_name_and_installdir function. This is sufficient in the case that the library has initially been installed in the same orig_prefix. But we can do better, to also cover the cases that 1. it has been installed in a different prefix before being moved to orig_prefix and (later) to curr_prefix, 2. unlike the program, it has not moved away from orig_prefix. */ const char *orig_installprefix = INSTALLPREFIX; const char *orig_installdir = INSTALLDIR; char *curr_prefix_better; curr_prefix_better = compute_curr_prefix (orig_installprefix, orig_installdir, get_shared_library_fullname ()); set_relocation_prefix (orig_installprefix, curr_prefix_better != NULL ? curr_prefix_better : curr_prefix); if (curr_prefix_better != NULL) free (curr_prefix_better); initialized = 1; } #endif /* Note: It is not necessary to perform case insensitive comparison here, even for DOS-like file systems, because the pathname argument was typically created from the same Makefile variable as orig_prefix came from. */ if (orig_prefix != NULL && curr_prefix != NULL && strncmp (pathname, orig_prefix, orig_prefix_len) == 0) { if (pathname[orig_prefix_len] == '\0') { /* pathname equals orig_prefix. */ char *result = (char *) xmalloc (strlen (curr_prefix) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { strcpy (result, curr_prefix); return result; } } else if (ISSLASH (pathname[orig_prefix_len])) { /* pathname starts with orig_prefix. */ const char *pathname_tail = &pathname[orig_prefix_len]; char *result = (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { memcpy (result, curr_prefix, curr_prefix_len); strcpy (result + curr_prefix_len, pathname_tail); return result; } } } #ifdef __EMX__ # ifdef __KLIBC__ # undef strncmp if (pathname && strncmp (pathname, "/@unixroot", 10) == 0 && (pathname[10] == '\0' || pathname[10] == '/' || pathname[10] == '\\')) { /* kLIBC itself processes /@unixroot prefix */ return pathname; } else # endif if (pathname && ISSLASH (pathname[0])) { const char *unixroot = getenv ("UNIXROOT"); if (unixroot && HAS_DEVICE (unixroot) && !unixroot[2]) { char *result = (char *) xmalloc (2 + strlen (pathname) + 1); #ifdef NO_XMALLOC if (result != NULL) #endif { strcpy (result, unixroot); strcpy (result + 2, pathname); return result; } } } #endif /* Nothing to relocate. */ return pathname; }