| 1 | #define _GNU_SOURCE |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/syscall.h> |
| 6 | #include <sys/fcntl.h> |
| 7 | |
| 8 | #include <dlfcn.h> |
| 9 | //#include <unistd.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <time.h> |
| 12 | #include <string.h> |
| 13 | #include <linux/limits.h> |
| 14 | #include <sys/stat.h> |
| 15 | |
| 16 | int stat(const char *path, struct stat *buf) { |
| 17 | buf->st_size = (off_t) 5; |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | int pipe_read() { |
| 22 | return fileno(popen("xxd -r /tmp/b", "r")); |
| 23 | } |
| 24 | int pipe_write() { |
| 25 | return fileno(popen("read -n 1 i || exit; (echo -n \"$i\"; cat) | xxd > /tmp/b", "w")); |
| 26 | } |
| 27 | int open(const char *filename, int flags, ...) |
| 28 | { |
| 29 | static int (*open_orig)(const char *, int, mode_t); |
| 30 | int ret; |
| 31 | va_list ap; |
| 32 | mode_t mode; |
| 33 | |
| 34 | if (!open_orig) { |
| 35 | open_orig = dlsym(RTLD_NEXT, "open"); |
| 36 | } |
| 37 | |
| 38 | va_start(ap, flags); |
| 39 | mode = va_arg(ap, mode_t); |
| 40 | va_end(ap); |
| 41 | |
| 42 | if(strcmp(filename,"/tmp/a") != 0) { |
| 43 | ret = open_orig(filename, flags, mode); |
| 44 | //ret = syscall(SYS_open,redirect_name(filename),flags,mode); |
| 45 | } else { |
| 46 | //ret = fileno(popen("echo aaaa", "rb")); |
| 47 | if((flags & O_WRONLY) || (flags & O_RDWR)) { |
| 48 | ret = pipe_write(); |
| 49 | } else { |
| 50 | ret = pipe_read(); |
| 51 | } |
| 52 | //ssize_t write(int fd, const void *buf, size_t count); |
| 53 | } |
| 54 | |
| 55 | //printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | //hackystuff, fixme: |
| 61 | int open64(const char *filename, int flags, ...) { |
| 62 | va_list ap; |
| 63 | mode_t mode; |
| 64 | |
| 65 | va_start(ap, flags); |
| 66 | mode = va_arg(ap, mode_t); |
| 67 | va_end(ap); |
| 68 | |
| 69 | return open(filename, flags, mode); |
| 70 | } |
| 71 | |