diff options
Diffstat (limited to 'preload.c')
-rw-r--r-- | preload.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/preload.c b/preload.c new file mode 100644 index 0000000..1665515 --- /dev/null +++ b/preload.c | |||
@@ -0,0 +1,42 @@ | |||
1 | #define _GNU_SOURCE | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <dlfcn.h> | ||
6 | #include <sys/types.h> | ||
7 | #include <sys/stat.h> | ||
8 | #include <unistd.h> | ||
9 | |||
10 | static const char *munge(const char *n) { | ||
11 | static const char *e = NULL; | ||
12 | static size_t elen; | ||
13 | if (!e) { | ||
14 | e = getenv("WIX"); | ||
15 | if (!e) | ||
16 | return n; | ||
17 | elen = strlen(e); | ||
18 | } | ||
19 | |||
20 | const char *last_slash = strrchr(n, '/'); | ||
21 | if (!last_slash) | ||
22 | return n; | ||
23 | if (last_slash - n > elen && !strncmp(last_slash - elen, e, elen)) | ||
24 | return last_slash - elen; | ||
25 | return n; | ||
26 | } | ||
27 | |||
28 | #define dofunc(rettype,sym,proto,proto2) \ | ||
29 | rettype sym proto { \ | ||
30 | rettype (*real) proto; \ | ||
31 | real = dlsym(RTLD_NEXT, #sym); \ | ||
32 | fname = munge(fname); \ | ||
33 | return real proto2; \ | ||
34 | } | ||
35 | |||
36 | dofunc(int,open,(const char *fname,int flags,mode_t mode),(fname,flags,mode)) | ||
37 | dofunc(int,open64,(const char *fname,int flags,mode_t mode),(fname,flags,mode)) | ||
38 | dofunc(int,__open,(const char *fname,int flags,mode_t mode),(fname,flags,mode)) | ||
39 | dofunc(int,__open64,(const char *fname,int flags,mode_t mode),(fname,flags,mode)) | ||
40 | dofunc(int,stat,(const char *fname,struct stat *sbuf),(fname,sbuf)) | ||
41 | dofunc(int,lstat,(const char *fname,struct stat *sbuf),(fname,sbuf)) | ||
42 | dofunc(int,access,(const char *fname,int mode),(fname,mode)) | ||