more instructive output
authorHeiko Schlittermann <hs@schlittermann.de>
Thu, 23 Dec 2010 01:06:04 +0100
changeset 1 9ccc1574a367
parent 0 7d3c07f8acfb
child 2 3701dbcb7d1b
more instructive output - changed the selector (skipper) criterion for readdir - using the libc.so.6 as "default" library if no soname is passed to lib_function
README
dl.c
dl.h
puts.c
readdir.c
--- a/README	Thu Dec 23 00:46:07 2010 +0100
+++ b/README	Thu Dec 23 01:06:04 2010 +0100
@@ -10,12 +10,12 @@
 the "hallo" should use the modified versions of the above
 funtions too.
 
-The modification is visibable, as puts(3) prints an
-extra string before the real output, and readdir(3) skips
-any directory entry starting with 'M'.
+The modification is visable, as puts(3) prints an XML-like marker around
+the real output, and readdir(3) skips any directory entry containing a
+dot.
 
-No considerations done about race conditions and other nice things.
-It is just an nice example…
+No considerations are done about race conditions and other nice things.
+It is just an example…
 
 -- 
 Heiko Schlittermann
--- a/dl.c	Thu Dec 23 00:46:07 2010 +0100
+++ b/dl.c	Thu Dec 23 01:06:04 2010 +0100
@@ -3,18 +3,22 @@
 #include <stdlib.h>
 #include "dl.h"
 
-void *get_original_function(const char *soname, const char *name)
+#define DEFAULT_LIB "libc.so.6"
+
+void *lib_function(const char *name, const char *soname)
 {
+    void *rc;
     const char* error;
-    void *handle = dlopen(soname, RTLD_LAZY);
-    void *rc;
+    void *handle;
+
 
-    if (!handle) {
+
+    if (!(handle = dlopen(soname ? soname : DEFAULT_LIB, RTLD_LAZY))) {
 	fprintf(stderr, "dlopen on %s: %s\n", soname, dlerror());
 	exit(1);
     }
 
-    dlerror();
+    dlerror();			    /* clear the error flag */
     rc = dlsym(handle, name);
     if ((error = dlerror())) {
 	fprintf(stderr, "dlsym for %s: %s\n", name, error);
--- a/dl.h	Thu Dec 23 00:46:07 2010 +0100
+++ b/dl.h	Thu Dec 23 01:06:04 2010 +0100
@@ -1,4 +1,4 @@
 #ifndef _DL_H_
 #define _DL_H_
-void *get_original_function(const char *soname, const char *name);
+void *lib_function(const char* name, const char *soname);
 #endif
--- a/puts.c	Thu Dec 23 00:46:07 2010 +0100
+++ b/puts.c	Thu Dec 23 01:06:04 2010 +0100
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include "dl.h"
 
 /* shameless stolen from
@@ -12,7 +13,7 @@
     int rc;
 
     if (!orig)
-	orig = get_original_function("libc.so.6", "puts");
+	orig = lib_function("puts", NULL);
 
     (*orig)("<puts>");
     rc = (*orig)(s);
--- a/readdir.c	Thu Dec 23 00:46:07 2010 +0100
+++ b/readdir.c	Thu Dec 23 01:06:04 2010 +0100
@@ -1,3 +1,5 @@
+#include <string.h>
+#include <stdlib.h>
 #include <dirent.h>
 #include "dl.h"
 
@@ -14,13 +16,14 @@
     struct dirent *dent;
 
     if (!orig) 
-	orig = get_original_function("libc.so.6", "readdir64");
+	orig = lib_function("readdir64", NULL);
 
     dent = (*orig)(dirp);
 
     /* skip selected entries */
-    if (dent && dent->d_name[0] == 'M') 
+    while (dent && strchr(dent->d_name, '.')) {
 	dent = (*orig)(dirp);
+    }
 
     return dent;
 }