forked from russdill/ncsvc-socks-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fopen.c
68 lines (60 loc) · 1.57 KB
/
fopen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dbg.h"
#include "preload.h"
#include "list.h"
#include "fopen.h"
static LIST_HEAD(intercepts);
static LIST_HEAD(finfos);
void fopen_add_intercept(const struct fopen_intercept *fi)
{
struct fopen_intercept *ni;
ni = malloc(sizeof(*ni));
memcpy(ni, fi, sizeof(*ni));
list_add(&ni->node, &intercepts);
}
FILE *fopen(const char *filename, const char *modes)
{
static FILE *(*orig_fopen)(const char *filename, const char *modes);
struct fopen_intercept *fi;
ASSIGN(fopen);
dbg("%s(filename=%s, modes=%s)\n", __func__, filename, modes);
list_for_each_entry(fi, &intercepts, node)
if (fi->intercept(filename, modes)) {
struct fopen_info *info;
FILE *fp;
info = malloc(sizeof(*info));
memset(info, 0, sizeof(*info));
info->orig_fopen = orig_fopen;
info->intercept = fi;
info->fp = fp = fi->fopen(info, filename, modes);
if (fp)
list_add(&info->node, &finfos);
else
free(info);
return fp;
}
return orig_fopen(filename, modes);
}
FILE *fopen64(const char *filename, const char *modes)
{
static FILE *(*orig_fopen64)(const char *filename, const char *modes);
ASSIGN(fopen64);
dbg("%s(filename=%s, modes=%s)\n", __func__, filename, modes);
return orig_fopen64(filename, modes);
}
int fclose(FILE *fp)
{
static int (*orig_fclose)(FILE *fp);
struct fopen_info *info;
ASSIGN(fclose);
dbg("%s\n", __func__);
list_for_each_entry(info, &finfos, node)
if (info->fp == fp) {
if (info->intercept->fclose)
info->intercept->fclose(info);
break;
}
return orig_fclose(fp);
}