summaryrefslogtreecommitdiffstats
path: root/doc/sources/c/main.c
blob: 96d60ad463a0ca411f875e48126f39e9c560f381 (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
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>

#define PATH_LENGTH 256
      
int main(int argc, char * argv[])
{
    char path[PATH_LENGTH], *msg = NULL;
    const char* (*fmt)();
    void *module;
      
    getcwd(path, PATH_LENGTH);
    strcat(path, "/");
    strcat(path, "module.so");

    /* Load module */
    module = dlopen(path, RTLD_NOW);
      
    /* Error ! */
    if(!module)
    {
        msg = dlerror();

	if(msg != NULL)
        {
              dlclose(module);
              exit(1);
        }
    }

    /* Try to resolve function "fmt_info()" */
    fmt = dlsym(module, "fmt_info");

    msg = dlerror();

    if(msg != NULL)
    {
        perror(msg);
        dlclose(module);
        exit(1);
    }
      
    /* call fmt_info() through a pointer*/
    printf("%s\n", fmt());

    /* close module */
    if(dlclose(module))
    {
        perror("error");
        exit(1);
    }
      
    return 0;
}