This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH 5.6.1] $^E on OS/2
[perl5.git] / os2 / dl_os2.c
CommitLineData
c692d670 1#include "dlfcn.h"
2d766320
IZ
2#include "string.h"
3#include "stdio.h"
c692d670
PP
4
5#define INCL_BASE
6#include <os2.h>
7
8static ULONG retcode;
ed344e4f 9static char fail[300];
c692d670 10
9fed8b87
IZ
11char *os2error(int rc);
12
c692d670
PP
13void *
14dlopen(char *path, int mode)
15{
16 HMODULE handle;
17 char tmp[260], *beg, *dot;
c692d670
PP
18 ULONG rc;
19
ed344e4f 20 fail[0] = 0;
c692d670
PP
21 if ((rc = DosLoadModule(fail, sizeof fail, path, &handle)) == 0)
22 return (void *)handle;
23
24 retcode = rc;
25
26 /* Not found. Check for non-FAT name and try truncated name. */
27 /* Don't know if this helps though... */
28 for (beg = dot = path + strlen(path);
29 beg > path && !strchr(":/\\", *(beg-1));
30 beg--)
31 if (*beg == '.')
32 dot = beg;
33 if (dot - beg > 8) {
34 int n = beg+8-path;
35 memmove(tmp, path, n);
36 memmove(tmp+n, dot, strlen(dot)+1);
37 if (DosLoadModule(fail, sizeof fail, tmp, &handle) == 0)
38 return (void *)handle;
39 }
40
41 return NULL;
42}
43
44void *
45dlsym(void *handle, char *symbol)
46{
47 ULONG rc, type;
48 PFN addr;
49
ed344e4f 50 fail[0] = 0;
c692d670
PP
51 rc = DosQueryProcAddr((HMODULE)handle, 0, symbol, &addr);
52 if (rc == 0) {
53 rc = DosQueryProcType((HMODULE)handle, 0, symbol, &type);
54 if (rc == 0 && type == PT_32BIT)
55 return (void *)addr;
56 rc = ERROR_CALL_NOT_IMPLEMENTED;
57 }
58 retcode = rc;
59 return NULL;
60}
61
62char *
63dlerror(void)
64{
ed344e4f 65 static char buf[700];
c692d670 66 ULONG len;
9fed8b87 67 char *err;
c692d670
PP
68
69 if (retcode == 0)
70 return NULL;
9fed8b87
IZ
71 err = os2error(retcode);
72 len = strlen(err);
73 if (len > sizeof(buf) - 1)
74 len = sizeof(buf) - 1;
75 strncpy(buf, err, len+1);
76 if (fail[0] && len < 300)
77 sprintf(buf + len, ", possible problematic module: '%s'", fail);
c692d670
PP
78 retcode = 0;
79 return buf;
80}
81
403d6f8e
GS
82int
83dlclose(void *handle)
84{
85 ULONG rc;
86
87 if ((rc = DosFreeModule((HMODULE)handle)) == 0) return 0;
88
89 retcode = rc;
90 return 2;
91}