dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnet/support dynlib.c,1.8,1.9


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnet/support dynlib.c,1.8,1.9
Date: Mon, 16 Jun 2003 19:56:49 -0400

Update of /cvsroot/dotgnu-pnet/pnet/support
In directory subversions:/tmp/cvs-serv8986/support

Modified Files:
        dynlib.c 
Log Message:


Put the MacOS X version of dynamic loading before the generic
"dlopen" version, as some MacOS X systems have "dlopen", but it
doesn't do the right thing.


Index: dynlib.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/support/dynlib.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** dynlib.c    15 Jun 2003 03:53:36 -0000      1.8
--- dynlib.c    16 Jun 2003 23:56:47 -0000      1.9
***************
*** 32,151 ****
  #endif
  
! #if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
! 
! void *ILDynLibraryOpen(const char *name)
! {
!       void *handle;
!       const char *error;
!       handle = dlopen(name, RTLD_LAZY | RTLD_GLOBAL);
!       if(!handle)
!       {
!               /* If the name does not start with "lib" and does not
!                  contain a path, then prepend "lib" and try again */
!               if(strncmp(name, "lib", 3) != 0)
!               {
!                       error = name;
!                       while(*error != '\0' && *error != '/' && *error != '\\')
!                       {
!                               ++error;
!                       }
!                       if(*error == '\0')
!                       {
!                               /* Try adding "lib" to the start */
!                               char *temp = (char *)ILMalloc(strlen(name) + 4);
!                               if(temp)
!                               {
!                                       strcpy(temp, "lib");
!                                       strcat(temp, name);
!                                       handle = dlopen(temp, RTLD_LAZY | 
RTLD_GLOBAL);
!                                       ILFree(temp);
!                                       if(handle)
!                                       {
!                                               return handle;
!                                       }
!                               }
! 
!                               /* Reload the original error state */
!                               handle = dlopen(name, RTLD_LAZY | RTLD_GLOBAL);
!                       }
!               }
! 
!               /* Report the error */
!               error = dlerror();
!               fprintf(stderr, "%s: %s\n", name,
!                               (error ? error : "could not load dynamic 
library"));
!               return 0;
!       }
!       else
!       {
!               return handle;
!       }
! }
! 
! void  ILDynLibraryClose(void *handle)
! {
!       dlclose(handle);
! }
! 
! void *ILDynLibraryGetSymbol(void *handle, const char *symbol)
! {
!       void *value = dlsym(handle, (char *)symbol);
!       const char *error = dlerror();
!       char *newName;
!       if(error == 0)
!       {
!               return value;
!       }
!       newName = (char *)ILMalloc(strlen(symbol) + 2);
!       if(newName)
!       {
!               /* Try again with '_' prepended to the name in case
!                  we are running on a system with a busted "dlsym" */
!               newName[0] = '_';
!               strcpy(newName + 1, symbol);
!               value = dlsym(handle, newName);
!               error = dlerror();
!               if(error == 0)
!               {
!                       ILFree(newName);
!                       return value;
!               }
!               ILFree(newName);
!       }
!       fprintf(stderr, "%s: %s\n", symbol, error);
!       return 0;
! }
! 
! #elif defined(IL_WIN32_NATIVE)        /* Native Win32 */
! 
! void *ILDynLibraryOpen(const char *name)
! {
!       void *libHandle = (void *)LoadLibrary(name);
!       if(libHandle == 0)
!       {
!               fprintf(stderr, "%s: could not load dynamic library\n", name);
!               return 0;
!       }
!       return libHandle;
! }
! 
! void ILDynLibraryClose(void *handle)
! {
!       FreeLibrary((HINSTANCE)handle);
! }
! 
! void *ILDynLibraryGetSymbol(void *handle, const char *symbol)
! {
!       void *procAddr;
!       procAddr = (void *)GetProcAddress((HINSTANCE)handle, symbol);
!       if(procAddr == 0)
!       {
!               fprintf(stderr, "%s: could not resolve symbol", symbol);
!               return 0;
!       }
!       return procAddr;
! }
! 
! #elif defined(__APPLE__) && defined(__MACH__) /* MacOS X */
  
  #include <mach-o/dyld.h>
--- 32,36 ----
  #endif
  
! #if defined(__APPLE__) && defined(__MACH__)   /* MacOS X */
  
  #include <mach-o/dyld.h>
***************
*** 268,271 ****
--- 153,271 ----
        fprintf(stderr, "%s: could not find the specified symbol\n", symbol);
        return 0;
+ }
+ 
+ #elif defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
+ 
+ void *ILDynLibraryOpen(const char *name)
+ {
+       void *handle;
+       const char *error;
+       handle = dlopen(name, RTLD_LAZY | RTLD_GLOBAL);
+       if(!handle)
+       {
+               /* If the name does not start with "lib" and does not
+                  contain a path, then prepend "lib" and try again */
+               if(strncmp(name, "lib", 3) != 0)
+               {
+                       error = name;
+                       while(*error != '\0' && *error != '/' && *error != '\\')
+                       {
+                               ++error;
+                       }
+                       if(*error == '\0')
+                       {
+                               /* Try adding "lib" to the start */
+                               char *temp = (char *)ILMalloc(strlen(name) + 4);
+                               if(temp)
+                               {
+                                       strcpy(temp, "lib");
+                                       strcat(temp, name);
+                                       handle = dlopen(temp, RTLD_LAZY | 
RTLD_GLOBAL);
+                                       ILFree(temp);
+                                       if(handle)
+                                       {
+                                               return handle;
+                                       }
+                               }
+ 
+                               /* Reload the original error state */
+                               handle = dlopen(name, RTLD_LAZY | RTLD_GLOBAL);
+                       }
+               }
+ 
+               /* Report the error */
+               error = dlerror();
+               fprintf(stderr, "%s: %s\n", name,
+                               (error ? error : "could not load dynamic 
library"));
+               return 0;
+       }
+       else
+       {
+               return handle;
+       }
+ }
+ 
+ void  ILDynLibraryClose(void *handle)
+ {
+       dlclose(handle);
+ }
+ 
+ void *ILDynLibraryGetSymbol(void *handle, const char *symbol)
+ {
+       void *value = dlsym(handle, (char *)symbol);
+       const char *error = dlerror();
+       char *newName;
+       if(error == 0)
+       {
+               return value;
+       }
+       newName = (char *)ILMalloc(strlen(symbol) + 2);
+       if(newName)
+       {
+               /* Try again with '_' prepended to the name in case
+                  we are running on a system with a busted "dlsym" */
+               newName[0] = '_';
+               strcpy(newName + 1, symbol);
+               value = dlsym(handle, newName);
+               error = dlerror();
+               if(error == 0)
+               {
+                       ILFree(newName);
+                       return value;
+               }
+               ILFree(newName);
+       }
+       fprintf(stderr, "%s: %s\n", symbol, error);
+       return 0;
+ }
+ 
+ #elif defined(IL_WIN32_NATIVE)        /* Native Win32 */
+ 
+ void *ILDynLibraryOpen(const char *name)
+ {
+       void *libHandle = (void *)LoadLibrary(name);
+       if(libHandle == 0)
+       {
+               fprintf(stderr, "%s: could not load dynamic library\n", name);
+               return 0;
+       }
+       return libHandle;
+ }
+ 
+ void ILDynLibraryClose(void *handle)
+ {
+       FreeLibrary((HINSTANCE)handle);
+ }
+ 
+ void *ILDynLibraryGetSymbol(void *handle, const char *symbol)
+ {
+       void *procAddr;
+       procAddr = (void *)GetProcAddress((HINSTANCE)handle, symbol);
+       if(procAddr == 0)
+       {
+               fprintf(stderr, "%s: could not resolve symbol", symbol);
+               return 0;
+       }
+       return procAddr;
  }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]