-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltdl_main.c
73 lines (60 loc) · 1.86 KB
/
ltdl_main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ltdl.h>
int main(int argc, char *argv[]) {
lt_dlhandle handle;
lt_dlhandle handle2;
void (*formula_printf)(const char*);
lt_dlinit();
if (argc < 2 || argc > 3) {
fprintf(stderr, "Improper number of arguments : %s .\n", argv[0]);
return EXIT_FAILURE;
}
if (strcmp(argv[1], "HOCl") == 0) {
handle = lt_dlopen("./chlorine.so");
if (argc == 3 && strcmp(argv[2], "HOBr") == 0) {
handle2 = lt_dlopen("./bromine.so");
}
} else if (strcmp(argv[1], "HOBr") == 0) {
handle = lt_dlopen("./bromine.so");
if (argc == 3 && strcmp(argv[2], "HOCl") == 0) {
handle2 = lt_dlopen("./chlorine.so");
}
} else {
fprintf(stderr, "Error: unknown formula: %s\n", argv[1]);
return EXIT_FAILURE;
}
if (!handle) {
// fail to load the library
fprintf(stderr, "Error: %s\n", lt_dlerror());
return EXIT_FAILURE;
}
*(void**)(&formula_printf) = lt_dlsym(handle, "chemical_name");
if (!formula_printf) {
// no such symbol
fprintf(stderr, "Error: %s\n", lt_dlerror());
lt_dlclose(handle);
return EXIT_FAILURE;
}
formula_printf(argv[1]);
lt_dlclose(handle);
if (argc == 3) {
if (!handle2) {
// fail to load the library
fprintf(stderr, "Error: %s\n", lt_dlerror());
return EXIT_FAILURE;
}
*(void**)(&formula_printf) = lt_dlsym(handle2, "chemical_name");
if (!formula_printf) {
// no such symbol
fprintf(stderr, "Error: %s\n", lt_dlerror());
lt_dlclose(handle2);
return EXIT_FAILURE;
}
formula_printf(argv[2]);
lt_dlclose(handle2);
}
lt_dlexit();
return EXIT_SUCCESS;
}