-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
70 lines (58 loc) · 1.82 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
int main(int argc, char *argv[]) {
void *handle;
void *handle2;
void (*formula_printf)(const char*);
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 = dlopen("./chlorine.so", RTLD_NOW);
if (argc == 3 && strcmp(argv[2], "HOBr") == 0) {
handle2 = dlopen("./bromine.so", RTLD_NOW);
}
} else if (strcmp(argv[1], "HOBr") == 0) {
handle = dlopen("./bromine.so", RTLD_NOW);
if (argc == 3 && strcmp(argv[2], "HOCl") == 0) {
handle2 = dlopen("./chlorine.so", RTLD_NOW);
}
} 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", dlerror());
return EXIT_FAILURE;
}
*(void**)(&formula_printf) = dlsym(handle, "chemical_name");
if (!formula_printf) {
/* no such symbol */
fprintf(stderr, "Error: %s\n", dlerror());
dlclose(handle);
return EXIT_FAILURE;
}
formula_printf(argv[1]);
dlclose(handle);
if (argc == 3) {
if (!handle2) {
/* fail to load the library */
fprintf(stderr, "Error: %s\n", dlerror());
return EXIT_FAILURE;
}
*(void**)(&formula_printf) = dlsym(handle2, "chemical_name");
if (!formula_printf) {
/* no such symbol */
fprintf(stderr, "Error: %s\n", dlerror());
dlclose(handle2);
return EXIT_FAILURE;
}
formula_printf(argv[2]);
dlclose(handle2);
}
return EXIT_SUCCESS;
}