summaryrefslogtreecommitdiffstats
path: root/eglib/gmodule.c
blob: 7224a9f6ef9ec00ae69b68f2dbf0880be6a4c93f (plain)
1
2
3
4
5
6
7
8
9
10
11
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>

#include <gmain.h>
#include <gmodule.h>

struct _GModule {
	void *handle;
	gchar *file_name;
};

static const char *dl_error_string = NULL;

GModule *g_module_open(const gchar *file_name, GModuleFlags flags)
{
	GModule *module;

	module = g_try_new0(GModule, 1);
	if (module == NULL) {
		dl_error_string = strerror(ENOMEM);
		return NULL;
	}

	module->handle = dlopen(file_name, flags);

	if (module->handle == NULL) {
		dl_error_string = dlerror();
		g_free(module);
		return NULL;
	}

	module->file_name = g_strdup(file_name);

	return module;
}

gboolean g_module_symbol(GModule *module, const gchar *symbol_name,
				gpointer *symbol)
{
	void *sym;

	dlerror();
	sym = dlsym(module->handle, symbol_name);
	dl_error_string = dlerror();

	if (dl_error_string != NULL)
		return FALSE;

	*symbol = sym;

	return TRUE;
}

gboolean g_module_close(GModule *module)
{
	if (dlclose(module->handle) != 0) {
		dl_error_string = dlerror();
		return FALSE;
	}

	g_free(module->file_name);
	g_free(module);

	return TRUE;
}

const gchar *g_module_error(void)
{
	const char *str;

	str = dl_error_string;
	dl_error_string = NULL;

	return str;
}

const gchar *g_module_name(GModule *module)
{
	if (module == NULL)
		return NULL;

	return module->file_name;
}