home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pytho152.zip / emx / src / Python / PC / os2emx / dlfcn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-29  |  5.6 KB  |  222 lines

  1. /* -*- C -*- ***********************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /*
  33.     This library implements dlopen() - functions for OS/2 using
  34.     DosLoadModule() and company.
  35. */
  36.  
  37. #define INCL_DOS
  38. #define INCL_DOSERRORS
  39. #define INCL_DOSSESMGR
  40. #define INCL_WINPROGRAMLIST
  41. #define INCL_WINFRAMEMGR
  42. #include <os2.h>
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <malloc.h>
  48.  
  49. /*-------------------------------------- Unix-like dynamic linking emulation -*/
  50.  
  51. typedef struct _track_rec {
  52.     char *name;
  53.     HMODULE handle;
  54.     void *id;
  55.     struct _track_rec *next;
  56. } tDLLchain, *DLLchain;
  57.  
  58. static DLLchain dlload = NULL;        /* A simple chained list of DLL names */
  59. static char dlerr [256];                /* last error text string */
  60. static void *last_id;
  61.  
  62. static DLLchain find_id(void *id)
  63. {
  64.     DLLchain tmp;
  65.  
  66.     for (tmp = dlload; tmp; tmp = tmp->next)
  67.         if (id == tmp->id)
  68.             return (tmp);
  69.  
  70.     return (NULL);
  71. }
  72.  
  73. /* load a dynamic-link library and return handle */
  74. void *dlopen (char *filename, int flags)
  75. {
  76.     HMODULE hm;
  77.     DLLchain tmp;
  78.     char err[256];
  79.     char *errtxt;
  80.     int rc = 0, set_chain = 0;
  81.  
  82.     for (tmp = dlload; tmp; tmp = tmp->next)
  83.         if (strnicmp(tmp->name, filename, 999) == 0)
  84.             break;
  85.  
  86.     if (!tmp)
  87.     {
  88.         tmp = (DLLchain)malloc (sizeof (tDLLchain));
  89.         if (!tmp)
  90.             goto nomem;
  91.         tmp->name = strdup (filename);
  92.         tmp->next = dlload;
  93.         set_chain = 1;
  94.     }
  95.  
  96.     switch (rc = DosLoadModule((PSZ)&err, sizeof(err), filename, &hm))
  97.     {
  98.         case NO_ERROR:
  99.             tmp->handle = hm;
  100.             if (set_chain) {
  101.                 do {
  102.                     last_id++;
  103.                 } while ((last_id == 0) || (find_id(last_id)));
  104.                 tmp->id = last_id;
  105.                 dlload = tmp;
  106.             }
  107.             return (tmp->id);
  108.         case ERROR_FILE_NOT_FOUND:
  109.         case ERROR_PATH_NOT_FOUND:
  110.             errtxt = "module `%s' not found";
  111.             break;
  112.         case ERROR_TOO_MANY_OPEN_FILES:
  113.         case ERROR_NOT_ENOUGH_MEMORY:
  114.         case ERROR_SHARING_BUFFER_EXCEEDED:
  115. nomem:
  116.             errtxt = "out of system resources";
  117.             break;
  118.         case ERROR_ACCESS_DENIED:
  119.             errtxt = "access denied";
  120.             break;
  121.         case ERROR_BAD_FORMAT:
  122.         case ERROR_INVALID_SEGMENT_NUMBER:
  123.         case ERROR_INVALID_ORDINAL:
  124.         case ERROR_INVALID_MODULETYPE:
  125.         case ERROR_INVALID_EXE_SIGNATURE:
  126.         case ERROR_EXE_MARKED_INVALID:
  127.         case ERROR_ITERATED_DATA_EXCEEDS_64K:
  128.         case ERROR_INVALID_MINALLOCSIZE:
  129.         case ERROR_INVALID_SEGDPL:
  130.         case ERROR_AUTODATASEG_EXCEEDS_64K:
  131.         case ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT:
  132.             errtxt = "invalid module format";
  133.             break;
  134.         case ERROR_SHARING_VIOLATION:
  135.         case ERROR_LOCK_VIOLATION:
  136.             errtxt = "sharing violation";
  137.             break;
  138.         case ERROR_INIT_ROUTINE_FAILED:
  139.             errtxt = "module initialization failed";
  140.             break;
  141.         default:
  142.             errtxt = "cause `%s', error code = %d";
  143.             break;
  144.     }
  145.     snprintf (dlerr, sizeof (dlerr), errtxt, &err, rc);
  146.     if (tmp) {
  147.         if (tmp->name)
  148.             free(tmp->name);
  149.         free (tmp);
  150.     }
  151.     return (0);
  152. }
  153.  
  154. /* return a pointer to the `symbol' in DLL */
  155. void *dlsym (void *handle, char *symbol)
  156. {
  157.     int rc = 0;
  158.     PFN addr;
  159.     char *errtxt;
  160.     int symord = 0;
  161.     DLLchain tmp = find_id (handle);
  162.  
  163.     if (!tmp)
  164.         goto inv_handle;
  165.  
  166.     if (*symbol == '#')
  167.         symord = atoi (symbol + 1);
  168.  
  169.     switch (rc = DosQueryProcAddr(tmp->handle, symord, symbol, &addr))
  170.     {
  171.         case NO_ERROR:
  172.             return ((void *)addr);
  173.         case ERROR_INVALID_HANDLE:
  174. inv_handle:
  175.             errtxt = "invalid module handle";
  176.             break;
  177.         case ERROR_PROC_NOT_FOUND:
  178.         case ERROR_INVALID_NAME:
  179.             errtxt = "no symbol `%s' in module";
  180.             break;
  181.         default:
  182.             errtxt = "symbol `%s', error code = %d";
  183.             break;
  184.     }
  185.     snprintf (dlerr, sizeof (dlerr), errtxt, symbol, rc);
  186.     return (NULL);
  187. }
  188.  
  189. /* free dynamicaly-linked library */
  190. int dlclose (void *handle)
  191. {
  192.     int rc;
  193.     DLLchain tmp = find_id (handle);
  194.  
  195.     if (!tmp)
  196.         goto inv_handle;
  197.  
  198.     switch (rc = DosFreeModule (tmp->handle))
  199.     {
  200.         case NO_ERROR:
  201.             free (tmp->name);
  202.             dlload = tmp->next;
  203.             free (tmp);
  204.             return (0);
  205.         case ERROR_INVALID_HANDLE:
  206. inv_handle:
  207.             strcpy(dlerr, "invalid module handle");
  208.             return (-1);
  209.         case ERROR_INVALID_ACCESS:
  210.             strcpy (dlerr, "access denied");
  211.             return (-1);
  212.         default:
  213.             return (-1);
  214.     }
  215. }
  216.  
  217. /* return a string describing last occured dl error */
  218. char *dlerror()
  219. {
  220.     return (dlerr);
  221. }
  222.