Saturday 15 January 2011

c - How to call a function with the same name but inside another shared library without dlsym? -



c - How to call a function with the same name but inside another shared library without dlsym? -

i want phone call fopen() defined in libc.so within own implementation of fopen. possible without relying on dlsym, dlopen (and ld_library)?

i don't know whether that's idea, phone call own fopen else, e.g. do_fopen , write macro fopen redirects function. when calling fopen in wrapper, either #undef fopen or place fopen in parentheses avoid macro expansion:

#include <stdlib.h> #include <stdio.h> #define fopen(fn, mode) do_fopen(fn, mode) file *do_fopen(const char *fn, const char *mode) { file *f = (fopen)(fn, mode); fprintf(stderr, "fopen(\"%s\", \"%s\") == %p\n", fn, mode, f); homecoming f; } int main(int argc, char *argv[]) { file *f; f = fopen(argv[1], "r"); if (f) { /* stuff */ } fclose(f); homecoming 0; }

that trick applies fopen calls own code, not libraries.

c hook

No comments:

Post a Comment