57 lines
1.4 KiB
C
57 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <dlfcn.h>
|
|
#include <unistd.h>
|
|
|
|
static FILE *(*real_fopen)(const char *path, const char *mode) = NULL;
|
|
|
|
static void init_real_fopen(void) {
|
|
if (!real_fopen) {
|
|
real_fopen = dlsym(RTLD_NEXT, "fopen");
|
|
if (!real_fopen) {
|
|
fprintf(stderr, "proc_redirect: dlsym(fopen) failed: %s\n", dlerror());
|
|
abort();
|
|
}
|
|
}
|
|
}
|
|
|
|
FILE *fopen(const char *path, const char *mode) {
|
|
init_real_fopen();
|
|
|
|
if (path && strncmp(path, "/proc/", 6) == 0) {
|
|
const char *rel_suffix = path + 1;
|
|
size_t len = strlen(rel_suffix) + 4;
|
|
char *new_path = malloc(len);
|
|
if (!new_path) {
|
|
fprintf(stderr, "proc_redirect: malloc failed\n");
|
|
abort();
|
|
}
|
|
new_path[0] = '.';
|
|
new_path[1] = '/';
|
|
new_path[2] = '.';
|
|
strcpy(new_path + 3, rel_suffix);
|
|
|
|
FILE *f = real_fopen(new_path, mode);
|
|
free(new_path);
|
|
return f;
|
|
}
|
|
|
|
return real_fopen(path, mode);
|
|
}
|
|
|
|
FILE *tmpfile(void) {
|
|
char path[] = "./tmpfile_XXXXXX";
|
|
int fd = mkstemp(path);
|
|
if (fd == -1) {
|
|
fprintf(stderr, "proc_redirect: mkstemp failed\n");
|
|
abort();
|
|
}
|
|
fprintf(stderr, "proc_redirect: tmpfile created at %s\n", path);
|
|
FILE *f = fdopen(fd, "w+");
|
|
if (!f) {
|
|
close(fd);
|
|
abort();
|
|
}
|
|
return f;
|
|
} |