diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2017-04-19 11:29:39 +0200 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2017-04-19 11:29:39 +0200 |
commit | 41a79081b0037c6fef2325478255df5397ad0a5a (patch) | |
tree | 510a3652e8fa9818ffb72b01fa40327f69608645 | |
download | sayhello-41a79081b0037c6fef2325478255df5397ad0a5a.tar.bz2 sayhello-41a79081b0037c6fef2325478255df5397ad0a5a.tar.xz |
initial commit
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | sayhello-spanish.c | 5 | ||||
-rw-r--r-- | sayhello-stderr.c | 6 | ||||
-rw-r--r-- | sayhello-swedish.c | 5 | ||||
-rw-r--r-- | sayhello.c | 29 |
6 files changed, 58 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8d7262a --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ + +all: sayhello sayhello-spanish.so sayhello-swedish.so sayhello-stderr.so + +sayhello: sayhello.c + $(CC) -o $@ $(CFLAGS) $< + +sayhello-%.so: sayhello-%.c + $(CC) -o $@ $(CFLAGS) -shared $< + + +clean: + rm -f sayhello sayhello-*.so @@ -0,0 +1 @@ +Simple dlopen example diff --git a/sayhello-spanish.c b/sayhello-spanish.c new file mode 100644 index 0000000..0c6fa0e --- /dev/null +++ b/sayhello-spanish.c @@ -0,0 +1,5 @@ +#include <stdio.h> + +void sayhello(void) { + printf("hola\n"); +} diff --git a/sayhello-stderr.c b/sayhello-stderr.c new file mode 100644 index 0000000..b993cf4 --- /dev/null +++ b/sayhello-stderr.c @@ -0,0 +1,6 @@ +#include <unistd.h> + +void sayhello(void) { + const char *buf="hello\n"; + write(2, buf, 6); +} diff --git a/sayhello-swedish.c b/sayhello-swedish.c new file mode 100644 index 0000000..36dc318 --- /dev/null +++ b/sayhello-swedish.c @@ -0,0 +1,5 @@ +#include <stdio.h> + +void sayhello(void) { + printf("hej\n"); +} diff --git a/sayhello.c b/sayhello.c new file mode 100644 index 0000000..1f132df --- /dev/null +++ b/sayhello.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <err.h> +#include <dlfcn.h> + +int main(int argc, const char *argv[]) { + int i; + if (argc < 2) + errx(1, "usage: sayhello PLUGIN..."); + + for (i=1; i<argc; i++) { + const char *plugin = argv[i]; + void (*sayhello)(void); + void *handle = dlopen(plugin, RTLD_NOW | RTLD_LOCAL); + + if (handle == NULL) { + warn("%s", plugin); + continue; + } + dlerror(); /* clear existing error */ + + /* get the "sayhello" function from plugin */ + sayhello = (void (*)(void)) dlsym(handle, "sayhello"); + sayhello(); + + dlclose(handle); + } + + return 0; +} |