glpenv08.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* glpenv08.c (shared library support) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include "glpenv.h"
  27. /* GNU version ********************************************************/
  28. #if defined(HAVE_LTDL)
  29. #include <ltdl.h>
  30. void *xdlopen(const char *module)
  31. { void *h = NULL;
  32. if (lt_dlinit() != 0)
  33. { lib_err_msg(lt_dlerror());
  34. goto done;
  35. }
  36. h = lt_dlopen(module);
  37. if (h == NULL)
  38. { lib_err_msg(lt_dlerror());
  39. if (lt_dlexit() != 0)
  40. xerror("xdlopen: %s\n", lt_dlerror());
  41. }
  42. done: return h;
  43. }
  44. void *xdlsym(void *h, const char *symbol)
  45. { void *ptr;
  46. xassert(h != NULL);
  47. ptr = lt_dlsym(h, symbol);
  48. if (ptr == NULL)
  49. xerror("xdlsym: %s: %s\n", symbol, lt_dlerror());
  50. return ptr;
  51. }
  52. void xdlclose(void *h)
  53. { xassert(h != NULL);
  54. if (lt_dlclose(h) != 0)
  55. xerror("xdlclose: %s\n", lt_dlerror());
  56. if (lt_dlexit() != 0)
  57. xerror("xdlclose: %s\n", lt_dlerror());
  58. return;
  59. }
  60. /* POSIX version ******************************************************/
  61. #elif defined(HAVE_DLFCN)
  62. #include <dlfcn.h>
  63. void *xdlopen(const char *module)
  64. { void *h;
  65. h = dlopen(module, RTLD_NOW);
  66. if (h == NULL)
  67. lib_err_msg(dlerror());
  68. return h;
  69. }
  70. void *xdlsym(void *h, const char *symbol)
  71. { void *ptr;
  72. xassert(h != NULL);
  73. ptr = dlsym(h, symbol);
  74. if (ptr == NULL)
  75. xerror("xdlsym: %s: %s\n", symbol, dlerror());
  76. return ptr;
  77. }
  78. void xdlclose(void *h)
  79. { xassert(h != NULL);
  80. if (dlclose(h) != 0)
  81. xerror("xdlclose: %s\n", dlerror());
  82. return;
  83. }
  84. /* Windows version ****************************************************/
  85. #elif defined(__WOE__)
  86. #include <windows.h>
  87. void *xdlopen(const char *module)
  88. { void *h;
  89. h = LoadLibrary(module);
  90. if (h == NULL)
  91. { char msg[20];
  92. sprintf(msg, "Error %d", GetLastError());
  93. lib_err_msg(msg);
  94. }
  95. return h;
  96. }
  97. void *xdlsym(void *h, const char *symbol)
  98. { void *ptr;
  99. xassert(h != NULL);
  100. ptr = GetProcAddress(h, symbol);
  101. if (ptr == NULL)
  102. xerror("xdlsym: %s: Error %d\n", symbol, GetLastError());
  103. return ptr;
  104. }
  105. void xdlclose(void *h)
  106. { xassert(h != NULL);
  107. if (!FreeLibrary(h))
  108. xerror("xdlclose: Error %d\n", GetLastError());
  109. return;
  110. }
  111. /* NULL version *******************************************************/
  112. #else
  113. void *xdlopen(const char *module)
  114. { xassert(module == module);
  115. lib_err_msg("Shared libraries not supported");
  116. return NULL;
  117. }
  118. void *xdlsym(void *h, const char *symbol)
  119. { xassert(h != h);
  120. xassert(symbol != symbol);
  121. return NULL;
  122. }
  123. void xdlclose(void *h)
  124. { xassert(h != h);
  125. return;
  126. }
  127. #endif
  128. /* eof */