relocatable.h 3.9 KB

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