Fix MingW build
[alexxy/gromacs.git] / src / external / vmd_molfile / vmddlopen.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
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  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * To help us fund GROMACS development, we humbly ask that you cite
13  * the research papers on the package. Check out http://www.gromacs.org
14  * 
15  * And Hey:
16  * Gnomes, ROck Monsters And Chili Sauce
17  */
18
19 /***************************************************************************
20  *cr
21  *cr            (C) Copyright 1995-2009 The Board of Trustees of the
22  *cr                        University of Illinois
23  *cr                         All Rights Reserved
24  *cr
25 Developed by:           Theoretical and Computational Biophysics Group
26                         University of Illinois at Urbana-Champaign
27                         http://www.ks.uiuc.edu/
28
29 Permission is hereby granted, free of charge, to any person obtaining a copy of
30 this software and associated documentation files (the Software), to deal with
31 the Software without restriction, including without limitation the rights to
32 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
33 of the Software, and to permit persons to whom the Software is furnished to
34 do so, subject to the following conditions:
35
36 Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimers.
38
39 Redistributions in binary form must reproduce the above copyright notice,
40 this list of conditions and the following disclaimers in the documentation
41 and/or other materials provided with the distribution.
42
43 Neither the names of Theoretical and Computational Biophysics Group,
44 University of Illinois at Urbana-Champaign, nor the names of its contributors
45 may be used to endorse or promote products derived from this Software without
46 specific prior written permission.
47
48 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
51 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
52 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
53 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
54 OTHER DEALINGS WITH THE SOFTWARE.
55  ***************************************************************************/
56
57 /***************************************************************************
58  * RCS INFORMATION:
59  *
60  *      $RCSfile: vmddlopen.c,v $
61  *      $Author: johns $        $Locker:  $             $State: Exp $
62  *      $Revision: 1.18 $      $Date: 2009/07/07 02:40:05 $
63  *
64  ***************************************************************************
65  * DESCRIPTION:
66  *   Routines for loading dynamic link libraries and shared object files
67  *   on various platforms, abstracting from machine dependent APIs.
68  *
69  ***************************************************************************/
70
71 #include <stdio.h> 
72 #include <stdlib.h>
73 #include <string.h>
74 #include "vmddlopen.h"
75
76 #if defined(__hpux)
77
78 #include <dl.h>
79 #include <errno.h>
80 #include <string.h>
81
82 void *vmddlopen( const char *path) {
83     void *ret;
84     ret = shl_load( path, BIND_IMMEDIATE | BIND_FIRST | BIND_VERBOSE, 0);
85     return ret;
86 }
87
88 int vmddlclose( void *handle ) {
89     return shl_unload( (shl_t) handle );
90 }
91
92 void *vmddlsym( void *handle, const char *sym ) {
93     void *value=0;
94
95     if ( shl_findsym( (shl_t*)&handle, sym, TYPE_UNDEFINED, &value ) != 0 ) 
96         return 0;
97     return value;
98 }
99
100 const char *vmddlerror( void  ) {
101     return strerror( errno );
102 }
103
104 #elif 0 && defined(__APPLE__)
105 /*
106  * This is only needed for MacOS X version 10.3 or older
107  */
108 #include <mach-o/dyld.h>
109
110 void *vmddlopen( const char *path) {
111   NSObjectFileImage image;
112   NSObjectFileImageReturnCode retval;
113   NSModule module;
114
115   retval = NSCreateObjectFileImageFromFile(path, &image);
116   if (retval != NSObjectFileImageSuccess)
117     return NULL;
118
119   module = NSLinkModule(image, path,
120             NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE
121             | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
122   return module;  /* module will be NULL on error */
123 }
124
125 int vmddlclose( void *handle ) {
126   NSModule module = (NSModule *)handle;
127   NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE);
128   return 0;
129 }
130
131 void *vmddlsym( void *handle, const char *symname ) {
132   char *realsymname;
133   NSModule module;
134   NSSymbol sym;
135   /* Hack around the leading underscore in the symbol name */
136   realsymname = (char *)malloc(strlen(symname)+2);
137   strcpy(realsymname, "_");
138   strcat(realsymname, symname);
139   module = (NSModule)handle;
140   sym = NSLookupSymbolInModule(module, realsymname);
141   free(realsymname);
142   if (sym) 
143     return (void *)(NSAddressOfSymbol(sym));
144   return NULL;
145 }
146
147 const char *vmddlerror( void  ) {
148   NSLinkEditErrors c;
149   int errorNumber;
150   const char *fileName;
151   const char *errorString = NULL;
152   NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
153   return errorString;
154 }
155
156 #elif defined( _WIN32 ) || defined( _WIN64 )
157
158 #include <windows.h>
159
160 void *vmddlopen(const char *fname) {
161   return (void *)LoadLibrary(fname);
162 }
163
164 const char *vmddlerror(void) {
165   static CHAR szBuf[80]; 
166   DWORD dw = GetLastError(); 
167  
168   sprintf(szBuf, "vmddlopen failed: GetLastError returned %lu\n", dw);
169   return szBuf;
170 }
171
172 void *vmddlsym(void *h, const char *sym) {
173   return (void *)GetProcAddress((HINSTANCE)h, sym);
174 }
175
176 int vmddlclose(void *h) {
177   /* FreeLibrary returns nonzero on success */
178   return !FreeLibrary((HINSTANCE)h);
179 }
180
181 #else
182
183 /* All remaining platforms (not Windows, HP-UX, or MacOS X <= 10.3) */
184 #include <dlfcn.h>
185
186 void *vmddlopen(const char *fname) {
187   return dlopen(fname, RTLD_NOW);
188 }
189 const char *vmddlerror(void) {
190   return dlerror();
191 }
192 void *vmddlsym(void *h, const char *sym) {
193   return dlsym(h, sym);
194 }
195 int vmddlclose(void *h) {
196   return dlclose(h);
197 }
198 #endif