src/glpenv08.c
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/glpenv08.c	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* glpenv08.c (shared library support) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#ifdef HAVE_CONFIG_H
    1.29 +#include <config.h>
    1.30 +#endif
    1.31 +
    1.32 +#include "glpenv.h"
    1.33 +
    1.34 +/* GNU version ********************************************************/
    1.35 +
    1.36 +#if defined(HAVE_LTDL)
    1.37 +
    1.38 +#include <ltdl.h>
    1.39 +
    1.40 +void *xdlopen(const char *module)
    1.41 +{     void *h = NULL;
    1.42 +      if (lt_dlinit() != 0)
    1.43 +      {  lib_err_msg(lt_dlerror());
    1.44 +         goto done;
    1.45 +      }
    1.46 +      h = lt_dlopen(module);
    1.47 +      if (h == NULL)
    1.48 +      {  lib_err_msg(lt_dlerror());
    1.49 +         if (lt_dlexit() != 0)
    1.50 +            xerror("xdlopen: %s\n", lt_dlerror());
    1.51 +      }
    1.52 +done: return h;
    1.53 +}
    1.54 +
    1.55 +void *xdlsym(void *h, const char *symbol)
    1.56 +{     void *ptr;
    1.57 +      xassert(h != NULL);
    1.58 +      ptr = lt_dlsym(h, symbol);
    1.59 +      if (ptr == NULL)
    1.60 +         xerror("xdlsym: %s: %s\n", symbol, lt_dlerror());
    1.61 +      return ptr;
    1.62 +}
    1.63 +
    1.64 +void xdlclose(void *h)
    1.65 +{     xassert(h != NULL);
    1.66 +      if (lt_dlclose(h) != 0)
    1.67 +         xerror("xdlclose: %s\n", lt_dlerror());
    1.68 +      if (lt_dlexit() != 0)
    1.69 +         xerror("xdlclose: %s\n", lt_dlerror());
    1.70 +      return;
    1.71 +}
    1.72 +
    1.73 +/* POSIX version ******************************************************/
    1.74 +
    1.75 +#elif defined(HAVE_DLFCN)
    1.76 +
    1.77 +#include <dlfcn.h>
    1.78 +
    1.79 +void *xdlopen(const char *module)
    1.80 +{     void *h;
    1.81 +      h = dlopen(module, RTLD_NOW);
    1.82 +      if (h == NULL)
    1.83 +         lib_err_msg(dlerror());
    1.84 +      return h;
    1.85 +}
    1.86 +
    1.87 +void *xdlsym(void *h, const char *symbol)
    1.88 +{     void *ptr;
    1.89 +      xassert(h != NULL);
    1.90 +      ptr = dlsym(h, symbol);
    1.91 +      if (ptr == NULL)
    1.92 +         xerror("xdlsym: %s: %s\n", symbol, dlerror());
    1.93 +      return ptr;
    1.94 +}
    1.95 +
    1.96 +void xdlclose(void *h)
    1.97 +{     xassert(h != NULL);
    1.98 +      if (dlclose(h) != 0)
    1.99 +         xerror("xdlclose: %s\n", dlerror());
   1.100 +      return;
   1.101 +}
   1.102 +
   1.103 +/* Windows version ****************************************************/
   1.104 +
   1.105 +#elif defined(__WOE__)
   1.106 +
   1.107 +#include <windows.h>
   1.108 +
   1.109 +void *xdlopen(const char *module)
   1.110 +{     void *h;
   1.111 +      h = LoadLibrary(module);
   1.112 +      if (h == NULL)
   1.113 +      {  char msg[20];
   1.114 +         sprintf(msg, "Error %d", GetLastError());
   1.115 +         lib_err_msg(msg);
   1.116 +      }
   1.117 +      return h;
   1.118 +}
   1.119 +
   1.120 +void *xdlsym(void *h, const char *symbol)
   1.121 +{     void *ptr;
   1.122 +      xassert(h != NULL);
   1.123 +      ptr = GetProcAddress(h, symbol);
   1.124 +      if (ptr == NULL)
   1.125 +         xerror("xdlsym: %s: Error %d\n", symbol, GetLastError());
   1.126 +      return ptr;
   1.127 +}
   1.128 +
   1.129 +void xdlclose(void *h)
   1.130 +{     xassert(h != NULL);
   1.131 +      if (!FreeLibrary(h))
   1.132 +         xerror("xdlclose: Error %d\n", GetLastError());
   1.133 +      return;
   1.134 +}
   1.135 +
   1.136 +/* NULL version *******************************************************/
   1.137 +
   1.138 +#else
   1.139 +
   1.140 +void *xdlopen(const char *module)
   1.141 +{     xassert(module == module);
   1.142 +      lib_err_msg("Shared libraries not supported");
   1.143 +      return NULL;
   1.144 +}
   1.145 +
   1.146 +void *xdlsym(void *h, const char *symbol)
   1.147 +{     xassert(h != h);
   1.148 +      xassert(symbol != symbol);
   1.149 +      return NULL;
   1.150 +}
   1.151 +
   1.152 +void xdlclose(void *h)
   1.153 +{     xassert(h != h);
   1.154 +      return;
   1.155 +}
   1.156 +
   1.157 +#endif
   1.158 +
   1.159 +/* eof */