12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#import "OFPlugin.h"
#import "OFExceptions.h"
@implementation OFPlugin
+ pluginFromFile: (OFString*)path
{
char *file;
size_t pathlen, suffixlen;
void *handle;
OFPlugin *(*init_plugin)();
OFPlugin *plugin;
pathlen = [path cStringLength];
suffixlen = strlen(PLUGIN_SUFFIX);
if ((file = malloc(pathlen + suffixlen + 1)) == NULL) {
@throw [OFOutOfMemoryException newWithClass: self
size: pathlen +
suffixlen + 1];
}
memcpy(file, [path cString], pathlen);
memcpy(file + pathlen, PLUGIN_SUFFIX, suffixlen);
file[pathlen + suffixlen] = 0;
if ((handle = dlopen(file, RTLD_LAZY)) == NULL) {
free(file);
@throw [OFInitializationFailedException newWithClass: self];
}
free(file);
if ((init_plugin = dlsym(handle, "init_plugin")) == NULL ||
(plugin = init_plugin()) == nil) {
dlclose(handle);
@throw [OFInitializationFailedException newWithClass: self];
}
|
>
>
|
<
<
<
|
<
<
|
<
<
<
|
<
|
<
|
|
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#import "OFPlugin.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
@implementation OFPlugin
+ pluginFromFile: (OFString*)path
{
OFAutoreleasePool *pool;
OFString *file;
void *handle;
OFPlugin *(*init_plugin)();
OFPlugin *plugin;
pool = [[OFAutoreleasePool alloc] init];
file = [OFMutableString stringWithString: path];
[file appendCString: PLUGIN_SUFFIX];
if ((handle = dlopen([file cString], RTLD_LAZY)) == NULL)
@throw [OFInitializationFailedException newWithClass: self];
[pool release];
if ((init_plugin = dlsym(handle, "init_plugin")) == NULL ||
(plugin = init_plugin()) == nil) {
dlclose(handle);
@throw [OFInitializationFailedException newWithClass: self];
}
|