Fixing copyright issues and code contributors
[alexxy/gromacs.git] / src / gmxlib / vmddlopen.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * This file is part of Gromacs        Copyright (c) 1991-2008
5  * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
6  * Copyright (c) 2012,2013, by the GROMACS development team, led by
7  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
8  * others, as listed in the AUTHORS file in the top-level source
9  * directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41
42 /***************************************************************************
43  *cr
44  *cr            (C) Copyright 1995-2009 The Board of Trustees of the
45  *cr                        University of Illinois
46  *cr                         All Rights Reserved
47  *cr
48 Developed by:           Theoretical and Computational Biophysics Group
49                         University of Illinois at Urbana-Champaign
50                         http://www.ks.uiuc.edu/
51
52 Permission is hereby granted, free of charge, to any person obtaining a copy of
53 this software and associated documentation files (the Software), to deal with
54 the Software without restriction, including without limitation the rights to
55 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
56 of the Software, and to permit persons to whom the Software is furnished to
57 do so, subject to the following conditions:
58
59 Redistributions of source code must retain the above copyright notice,
60 this list of conditions and the following disclaimers.
61
62 Redistributions in binary form must reproduce the above copyright notice,
63 this list of conditions and the following disclaimers in the documentation
64 and/or other materials provided with the distribution.
65
66 Neither the names of Theoretical and Computational Biophysics Group,
67 University of Illinois at Urbana-Champaign, nor the names of its contributors
68 may be used to endorse or promote products derived from this Software without
69 specific prior written permission.
70
71 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
74 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
75 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
76 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
77 OTHER DEALINGS WITH THE SOFTWARE.
78  ***************************************************************************/
79
80 /***************************************************************************
81  * RCS INFORMATION:
82  *
83  *      $RCSfile: vmddlopen.c,v $
84  *      $Author: johns $        $Locker:  $             $State: Exp $
85  *      $Revision: 1.18 $      $Date: 2009/07/07 02:40:05 $
86  *
87  ***************************************************************************
88  * DESCRIPTION:
89  *   Routines for loading dynamic link libraries and shared object files
90  *   on various platforms, abstracting from machine dependent APIs.
91  *
92  ***************************************************************************/
93
94 #include <stdio.h> 
95 #include <stdlib.h>
96 #include <string.h>
97 #include "vmddlopen.h"
98
99 #ifdef HAVE_CONFIG_H
100 #include <config.h>
101 #endif
102
103
104 #ifdef GMX_USE_PLUGINS
105 #if defined(__hpux)
106
107 #include <dl.h>
108 #include <errno.h>
109 #include <string.h>
110
111 void *vmddlopen( const char *path) {
112     void *ret;
113     ret = shl_load( path, BIND_IMMEDIATE | BIND_FIRST | BIND_VERBOSE, 0);
114     return ret;
115 }
116
117 int vmddlclose( void *handle ) {
118     return shl_unload( (shl_t) handle );
119 }
120
121 void *vmddlsym( void *handle, const char *sym ) {
122     void *value=0;
123
124     if ( shl_findsym( (shl_t*)&handle, sym, TYPE_UNDEFINED, &value ) != 0 ) 
125         return 0;
126     return value;
127 }
128
129 const char *vmddlerror( void  ) {
130     return strerror( errno );
131 }
132
133 #elif 0 && defined(__APPLE__)
134 /*
135  * This is only needed for MacOS X version 10.3 or older
136  */
137 #include <mach-o/dyld.h>
138
139 void *vmddlopen( const char *path) {
140   NSObjectFileImage image;
141   NSObjectFileImageReturnCode retval;
142   NSModule module;
143
144   retval = NSCreateObjectFileImageFromFile(path, &image);
145   if (retval != NSObjectFileImageSuccess)
146     return NULL;
147
148   module = NSLinkModule(image, path,
149             NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE
150             | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
151   return module;  /* module will be NULL on error */
152 }
153
154 int vmddlclose( void *handle ) {
155   NSModule module = (NSModule *)handle;
156   NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE);
157   return 0;
158 }
159
160 void *vmddlsym( void *handle, const char *symname ) {
161   char *realsymname;
162   NSModule module;
163   NSSymbol sym;
164   /* Hack around the leading underscore in the symbol name */
165   realsymname = (char *)malloc(strlen(symname)+2);
166   strcpy(realsymname, "_");
167   strcat(realsymname, symname);
168   module = (NSModule)handle;
169   sym = NSLookupSymbolInModule(module, realsymname);
170   free(realsymname);
171   if (sym) 
172     return (void *)(NSAddressOfSymbol(sym));
173   return NULL;
174 }
175
176 const char *vmddlerror( void  ) {
177   NSLinkEditErrors c;
178   int errorNumber;
179   const char *fileName;
180   const char *errorString = NULL;
181   NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
182   return errorString;
183 }
184
185 #elif defined(_MSC_VER)
186
187 #include <windows.h>
188
189 void *vmddlopen(const char *fname) {
190   return (void *)LoadLibrary(fname);
191 }
192
193 const char *vmddlerror(void) {
194   static CHAR szBuf[80]; 
195   DWORD dw = GetLastError(); 
196  
197   sprintf(szBuf, "vmddlopen failed: GetLastError returned %u\n", dw); 
198   return szBuf;
199 }
200
201 void *vmddlsym(void *h, const char *sym) {
202   return (void *)GetProcAddress((HINSTANCE)h, sym);
203 }
204
205 int vmddlclose(void *h) {
206   /* FreeLibrary returns nonzero on success */
207   return !FreeLibrary((HINSTANCE)h);
208 }
209
210 #else
211
212 /* All remaining platforms (not Windows, HP-UX, or MacOS X <= 10.3) */
213 #include <dlfcn.h>
214
215 void *vmddlopen(const char *fname) {
216   return dlopen(fname, RTLD_NOW);
217 }
218 const char *vmddlerror(void) {
219   return dlerror();
220 }
221 void *vmddlsym(void *h, const char *sym) {
222   return dlsym(h, sym);
223 }
224 int vmddlclose(void *h) {
225   return dlclose(h);
226 }
227 #endif 
228 #else
229 void *vmddlopen(const char *fname) {
230   return NULL;
231 }
232 const char *vmddlerror(void) {
233   return NULL;
234 }
235 void *vmddlsym(void *h, const char *sym) {
236   return NULL;
237 }
238 int vmddlclose(void *h) {
239   return 0;
240 }
241 #endif