relocatable.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Provide relocatable packages.
  2. Copyright (C) 2003, 2005, 2008-2020 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _RELOCATABLE_H
  15. #define _RELOCATABLE_H
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* This can be enabled through the configure --enable-relocatable option. */
  20. #if ENABLE_RELOCATABLE
  21. /* When building a DLL, we must export some functions. Note that because
  22. this is a private .h file, we don't need to use __declspec(dllimport)
  23. in any case. */
  24. #if HAVE_VISIBILITY && BUILDING_DLL
  25. # define RELOCATABLE_DLL_EXPORTED __attribute__((__visibility__("default")))
  26. #elif defined _MSC_VER && BUILDING_DLL
  27. # define RELOCATABLE_DLL_EXPORTED __declspec(dllexport)
  28. #else
  29. # define RELOCATABLE_DLL_EXPORTED
  30. #endif
  31. /* Sets the original and the current installation prefix of the package.
  32. Relocation simply replaces a pathname starting with the original prefix
  33. by the corresponding pathname with the current prefix instead. Both
  34. prefixes should be directory names without trailing slash (i.e. use ""
  35. instead of "/"). */
  36. extern RELOCATABLE_DLL_EXPORTED void
  37. set_relocation_prefix (const char *orig_prefix,
  38. const char *curr_prefix);
  39. /* Returns the pathname, relocated according to the current installation
  40. directory.
  41. The returned string is either PATHNAME unmodified or a freshly allocated
  42. string that you can free with free() after casting it to 'char *'. */
  43. extern const char * relocate (const char *pathname);
  44. /* Returns the pathname, relocated according to the current installation
  45. directory.
  46. This function sets *ALLOCATEDP to the allocated memory, or to NULL if
  47. no memory allocation occurs. So that, after you're done with the return
  48. value, to reclaim allocated memory, you can do: free (*ALLOCATEDP). */
  49. extern const char * relocate2 (const char *pathname, char **allocatedp);
  50. /* Memory management: relocate() potentially allocates memory, because it has
  51. to construct a fresh pathname. If this is a problem because your program
  52. calls relocate() frequently or because you want to fix all potential memory
  53. leaks anyway, you have three options:
  54. 1) Use this idiom:
  55. const char *pathname = ...;
  56. const char *rel_pathname = relocate (pathname);
  57. ...
  58. if (rel_pathname != pathname)
  59. free ((char *) rel_pathname);
  60. 2) Use this idiom:
  61. char *allocated;
  62. const char *rel_pathname = relocate2 (..., &allocated);
  63. ...
  64. free (allocated);
  65. 3) Think about caching the result. */
  66. /* Convenience function:
  67. Computes the current installation prefix, based on the original
  68. installation prefix, the original installation directory of a particular
  69. file, and the current pathname of this file.
  70. Returns it, freshly allocated. Returns NULL upon failure. */
  71. extern char * compute_curr_prefix (const char *orig_installprefix,
  72. const char *orig_installdir,
  73. const char *curr_pathname);
  74. #else
  75. /* By default, we use the hardwired pathnames. */
  76. #define relocate(pathname) (pathname)
  77. #define relocate2(pathname,allocatedp) (*(allocatedp) = NULL, (pathname))
  78. #endif
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif /* _RELOCATABLE_H */