-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicLoading.c
73 lines (54 loc) · 1.75 KB
/
dynamicLoading.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
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
void checkForError() {
char *error = NULL;
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
}
int main()
{
void *handle = NULL;
// set up function pointers
int (*numChars) (char *, char) = NULL;
int (*removeNonAlpha) (char *) = NULL;
int (*lengthStr) (char *) = NULL;
int (*strConcat) (char *, char *) = NULL;
int (*strCopy) (char *, char *) = NULL;
int (*substring) (char *, int, int, char *) = NULL;
char temp[] = "mehmet &&''''ozgen";
char temp2[] = "apple";
char temp3[100];
handle = dlopen("/home/moezgen/CLionProjects/c-playground/dynamic-lib-ex/libStrigFunctions.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
dlerror();
numChars = dlsym(handle, "numberOfCharactersInString");
checkForError();
printf("Number of 'p's in apples is %d\n", (*numChars)(temp2, 'p'));
removeNonAlpha = dlsym(handle, "removeNonAlphaCharacters");
checkForError();
(*removeNonAlpha) (temp);
printf("String temp with alpha characters removed is: %s\n", temp);
lengthStr = dlsym(handle, "lengthOfString");
checkForError();
printf("String temp length is: %d\n", (*lengthStr) (temp));
strConcat = dlsym(handle, "strConcat");
checkForError();
(*strConcat) (temp, temp2);
printf("String concatenated with string2 is: %s\n", temp);
strCopy = dlsym(handle, "strCopy");
checkForError();
(*strCopy) (temp2, temp3);
printf("String copied is: %s\n", temp3);
substring = dlsym(handle, "substring");
checkForError();
(*substring) (temp, 3, 8, temp3);
printf("Substring is: %s\n", temp3);
dlclose(handle);
return 0;
}