[BACK]Return to os2.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / pari-2.2 / src / systems

Annotation of OpenXM_contrib/pari-2.2/src/systems/os2.c, Revision 1.1

1.1     ! noro        1: #include "dlfcn.h"
        !             2:
        !             3: #define INCL_BASE
        !             4: #include <os2.h>
        !             5: #include <float.h>
        !             6:
        !             7: static ULONG retcode;
        !             8: static char fail[300];
        !             9:
        !            10: static ULONG dllHandle;
        !            11: static char dllname[80];
        !            12: static int handle_found;
        !            13: static int handle_loaded;
        !            14:
        !            15: #ifdef DLOPEN_INITTERM
        !            16: unsigned long _DLL_InitTerm(unsigned long modHandle, unsigned long flag)
        !            17: {
        !            18:     switch (flag) {
        !            19:     case 0:     /* INIT */
        !            20:         /* Save handle */
        !            21:         dllHandle = modHandle;
        !            22:        handle_found = 1;
        !            23:         return TRUE;
        !            24:
        !            25:     case 1:     /* TERM */
        !            26:        handle_found = 0;
        !            27:         dllHandle = (unsigned long)NULLHANDLE;
        !            28:         return TRUE;
        !            29:     }
        !            30:
        !            31:     return FALSE;
        !            32: }
        !            33:
        !            34: #endif
        !            35:
        !            36: void *
        !            37: dlopen(char *path, int mode)
        !            38: {
        !            39:        HMODULE handle;
        !            40:        char tmp[260], *beg, *dot;
        !            41:        ULONG rc;
        !            42:        unsigned fpflag = _control87(0,0);
        !            43:
        !            44:        fail[0] = 0;
        !            45:        if (!path) {                    /* Our own handle. */
        !            46:            if (handle_found) {
        !            47:                if (handle_loaded)
        !            48:                    return (void*)dllHandle;
        !            49:                rc = DosQueryModuleName(dllHandle, sizeof(dllname), dllname);
        !            50:                if (rc) {
        !            51:                    strcpy(fail, "can't find my DLL name by the handle");
        !            52:                    return 0;
        !            53:                }
        !            54:                rc = DosLoadModule(fail, sizeof fail, dllname, &handle);
        !            55:                if (rc) {
        !            56:                    strcpy(fail, "can't load my own DLL");
        !            57:                    return 0;
        !            58:                }
        !            59:                handle_loaded = 1;
        !            60:                goto ret;
        !            61:            }
        !            62:             strcpy(fail, "can't load from myself: compiled without -DDLOPEN_INITTERM");
        !            63:            return 0;
        !            64:        }
        !            65:        if ((rc = DosLoadModule(fail, sizeof fail, path, &handle)) == 0)
        !            66:                goto ret;
        !            67:
        !            68:        retcode = rc;
        !            69:
        !            70:        /* Not found. Check for non-FAT name and try truncated name. */
        !            71:        /* Don't know if this helps though... */
        !            72:        for (beg = dot = path + strlen(path);
        !            73:             beg > path && !strchr(":/\\", *(beg-1));
        !            74:             beg--)
        !            75:                if (*beg == '.')
        !            76:                        dot = beg;
        !            77:        if (dot - beg > 8) {
        !            78:                int n = beg+8-path;
        !            79:                memmove(tmp, path, n);
        !            80:                memmove(tmp+n, dot, strlen(dot)+1);
        !            81:                if (DosLoadModule(fail, sizeof fail, tmp, &handle) == 0)
        !            82:                        goto ret;
        !            83:        }
        !            84:        handle = 0;
        !            85:
        !            86:       ret:
        !            87:        _control87(fpflag, MCW_EM); /* Some modules reset FP flags on load */
        !            88:        return (void *)handle;
        !            89: }
        !            90:
        !            91: void *
        !            92: dlsym(void *handle, char *symbol)
        !            93: {
        !            94:        ULONG rc, type;
        !            95:        PFN addr;
        !            96:
        !            97:        fail[0] = 0;
        !            98:        rc = DosQueryProcAddr((HMODULE)handle, 0, symbol, &addr);
        !            99:        if (rc == 0) {
        !           100:                rc = DosQueryProcType((HMODULE)handle, 0, symbol, &type);
        !           101:                if (rc == 0 && type == PT_32BIT)
        !           102:                        return (void *)addr;
        !           103:                rc = ERROR_CALL_NOT_IMPLEMENTED;
        !           104:        }
        !           105:        retcode = rc;
        !           106:        return NULL;
        !           107: }
        !           108:
        !           109: char *
        !           110: dlerror(void)
        !           111: {
        !           112:        static char buf[700];
        !           113:        ULONG len;
        !           114:
        !           115:        if (retcode == 0)
        !           116:                return NULL;
        !           117:        if (DosGetMessage(NULL, 0, buf, sizeof buf - 1, retcode,
        !           118:                          "OSO001.MSG", &len)) {
        !           119:                if (fail[0])
        !           120:                  sprintf(buf,
        !           121: "OS/2 system error code %d, possible problematic module: '%s'",
        !           122:                          (int)retcode, fail);
        !           123:                else
        !           124:                  sprintf(buf, "OS/2 system error code %d", (int)retcode);
        !           125:        } else {
        !           126:                buf[len] = '\0';
        !           127:                if (len && buf[len - 1] == '\n')
        !           128:                        buf[--len] = 0;
        !           129:                if (len && buf[len - 1] == '\r')
        !           130:                        buf[--len] = 0;
        !           131:                if (len && buf[len - 1] == '.')
        !           132:                        buf[--len] = 0;
        !           133:                if (fail[0] && len < 300)
        !           134:                  sprintf(buf + len, ", possible problematic module: '%s'",
        !           135:                          fail);
        !           136:        }
        !           137:        retcode = 0;
        !           138:        return buf;
        !           139: }
        !           140:
        !           141: int
        !           142: dlclose(void *handle)
        !           143: {
        !           144:        ULONG rc;
        !           145:
        !           146:        if ((rc = DosFreeModule((HMODULE)handle)) == 0) return 0;
        !           147:
        !           148:        retcode = rc;
        !           149:        return 2;
        !           150: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>