Skip to content

Commit 18bdc5e

Browse files
committed
Revert "Merge pull request zcutlip#1 from edmurphy16/filefake"
need to put this in a seperate library, I think. This reverts commit 03fd387, reversing changes made to bc6ee8f.
1 parent fb41088 commit 18bdc5e

File tree

4 files changed

+29
-221
lines changed

4 files changed

+29
-221
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $(INI_OBJ):
2424
$(CC) $(INCLUDES) $(CFLAGS) -fPIC -c -o $@ $<
2525

2626
$(LIB): $(OBJS) $(INI_OBJ)
27-
$(CC) -shared -o $@ $^ -Wl,-nostdlib -ldl
27+
$(CC) -shared -o $@ $^ -Wl,-nostdlib
2828

2929
clean:
3030
-rm *.o

contrib/inih/ini.c

+4-28
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,20 @@ home page for more info:
2020
#define MAX_SECTION 50
2121
#define MAX_NAME 50
2222

23-
/* writing my own implementation of isspace */
24-
int myisspace(int c){
25-
if(c == ' ')
26-
return 1;
27-
else if(c == '\t')
28-
return 1;
29-
else if(c == '\v')
30-
return 1;
31-
else if(c == '\r')
32-
return 1;
33-
else if(c == '\n')
34-
return 1;
35-
else
36-
return 0;
37-
}
38-
3923
/* Strip whitespace chars off end of given string, in place. Return s. */
4024
static char* rstrip(char* s)
4125
{
42-
int i;
43-
char* p;
44-
45-
p=s+strlen(s);
46-
while (p > s && myisspace((int)(*(--p)))){
26+
char* p = s + strlen(s);
27+
while (p > s && isspace((unsigned char)(*--p)))
4728
*p = '\0';
48-
}
49-
5029
return s;
5130
}
5231

5332
/* Return pointer to first non-whitespace char in given string. */
5433
static char* lskip(const char* s)
5534
{
56-
while (*s && myisspace((unsigned char)(*s))){
35+
while (*s && isspace((unsigned char)(*s)))
5736
s++;
58-
}
5937
return (char*)s;
6038
}
6139

@@ -66,7 +44,7 @@ static char* find_char_or_comment(const char* s, char c)
6644
{
6745
int was_whitespace = 0;
6846
while (*s && *s != c && !(was_whitespace && *s == ';')) {
69-
was_whitespace = myisspace((unsigned char)(*s));
47+
was_whitespace = isspace((unsigned char)(*s));
7048
s++;
7149
}
7250
return (char*)s;
@@ -114,15 +92,13 @@ int ini_parse_file(FILE* file,
11492
lineno++;
11593

11694
start = line;
117-
11895
#if INI_ALLOW_BOM
11996
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
12097
(unsigned char)start[1] == 0xBB &&
12198
(unsigned char)start[2] == 0xBF) {
12299
start += 3;
123100
}
124101
#endif
125-
126102
start = lskip(rstrip(start));
127103

128104
if (*start == ';' || *start == '#') {

nvram-faker.c

+24-174
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,92 @@
1-
#define _GNU_SOURCE
21
#include <stdlib.h>
32
#include <stdio.h>
4-
#include <stdint.h>
53
#include <string.h>
6-
#include <dlfcn.h>
74
#include "nvram-faker.h"
85
#include "ini.h"
96

107
#define RED_ON "\033[22;31m"
118
#define RED_OFF "\033[22;00m"
12-
#define DEFAULT_BUFFER_LEN 1024
9+
#define DEFAULT_KV_PAIR_LEN 1024
1310

1411
#ifndef INI_FILE_PATH
1512
#define INI_FILE_PATH "/nvram.ini"
1613
#endif
1714

18-
#ifndef LOG_FILE_PATH
19-
#define LOG_FILE_PATH "/tmp/faker.log"
20-
#endif
21-
2215
static int kv_count=0;
23-
static int fn_count=0;
24-
static int key_value_pair_len=DEFAULT_BUFFER_LEN;
25-
static int fake_filenames_len=DEFAULT_BUFFER_LEN;
26-
ini_info_t *ii=NULL;
16+
static int key_value_pair_len=DEFAULT_KV_PAIR_LEN;
2717
static char **key_value_pairs=NULL;
28-
static char **fake_filenames=NULL;
29-
FILE* flog;
3018

3119
void initialize_ini(void) __attribute__((constructor));
3220
void end(void) __attribute__((destructor));
3321

34-
int fname_check(const char *filename);
35-
3622
static int ini_handler(void *user, const char *section, const char *name,const char *value)
3723
{
3824

3925
int old_kv_len;
40-
ini_info_t *new_ii;
4126
char **kv;
42-
char **fn;
4327
char **new_kv;
4428
int i;
45-
29+
4630
if(NULL == user || NULL == section || NULL == name || NULL == value)
4731
{
48-
fprintf(flog,"bad parameter to ini_handler\n");
32+
printf("bad parameter to ini_handler\n");
4933
return 0;
5034
}
51-
52-
new_ii = *((ini_info_t **) user);
53-
54-
if(NULL == new_ii)
35+
kv = *((char ***)user);
36+
if(NULL == kv)
5537
{
56-
fprintf(flog,"ini_info struct is NULL\n");
38+
printf("kv is NULL\n");
5739
return 0;
5840
}
59-
60-
kv = new_ii->kv_pairs;
61-
fn = new_ii->fake_fns;
62-
41+
6342
if(kv_count >= key_value_pair_len)
6443
{
6544
old_kv_len=key_value_pair_len;
6645
key_value_pair_len=(key_value_pair_len * 2);
6746
new_kv=(char **)malloc(key_value_pair_len);
68-
if(NULL == new_kv)
47+
if(NULL == kv)
6948
{
70-
fprintf(flog,"Failed to reallocate key value array.\n");
49+
printf("Failed to reallocate key value array.\n");
7150
return 0;
7251
}
7352
for(i=0;i<old_kv_len;i++)
7453
{
7554
new_kv[i]=kv[i];
7655
}
77-
78-
free(new_ii->kv_pairs);
56+
free(*(char ***)user);
7957
kv=new_kv;
80-
new_ii->kv_pairs=kv;
81-
}
82-
83-
if(fn_count >= fake_filenames_len)
84-
{
85-
fprintf(flog,"Sorry you are limited to 1024 filenames to fake\n");
86-
fprintf(flog,"Seriously it will probably make things painfully slow\n");
87-
fprintf(flog,"if you had more.\n");
88-
return 0;
89-
}
90-
91-
fprintf(flog,"Got %s:%s\n",name,value);
92-
if(strstr(name,"fake_filename")){
93-
fn[fn_count++]=strdup(value);
94-
}else{
95-
kv[kv_count++]=strdup(name);
96-
kv[kv_count++]=strdup(value);
58+
*(char ***)user=kv;
9759
}
60+
printf("Got %s:%s\n",name,value);
61+
kv[kv_count++]=strdup(name);
62+
kv[kv_count++]=strdup(value);
9863

9964
return 1;
10065
}
10166

10267
void initialize_ini(void)
10368
{
10469
int ret;
105-
char *flog_path=NULL;
106-
flog_path = getenv("FAKER_LOG");
107-
108-
109-
if(NULL == flog_path || '\0' == flog_path[0]){
110-
//if no log provided log to stderr
111-
flog = stderr;
112-
fprintf(flog,RED_ON"**************************************************\n");
113-
fprintf(flog,"**\t\tTO LOG TO A FILE\t\t**\n");
114-
fprintf(flog,"**\t SET \"FAKER_LOG\" ENV VARIABLE\t\t**\n");
115-
fprintf(flog,"**************************************************\n"RED_OFF);
116-
}else{
117-
flog = fopen(flog_path,"w+");
118-
if( NULL == flog){
119-
fprintf(stderr,"Failed to open log file \"%s\"\n",flog_path);
120-
exit(1);
121-
}
122-
}
123-
124-
setvbuf(flog,NULL,_IONBF,0);
125-
fprintf(flog,"Initializing.\n");
126-
if(NULL == ii){
127-
ii = malloc(sizeof(ini_info_t));
128-
if(NULL == ii){
129-
fprintf(flog,"Failed to allocate memory for update info struct. Terminating.\n");
130-
exit(1);
131-
}
132-
133-
}
134-
70+
printf("Initializing.\n");
13571
if (NULL == key_value_pairs)
13672
{
13773
key_value_pairs=malloc(key_value_pair_len);
13874
}
13975
if(NULL == key_value_pairs)
14076
{
141-
fprintf(flog,"Failed to allocate memory for key value array. Terminating.\n");
77+
printf("Failed to allocate memory for key value array. Terminating.\n");
14278
exit(1);
14379
}
144-
145-
if(NULL == fake_filenames)
146-
{
147-
fake_filenames = malloc(fake_filenames_len);
148-
}
149-
if(NULL == fake_filenames)
150-
{
151-
fprintf(flog,"Failed to allocate memory for fake filenames array. Terminating.\n");
152-
exit(1);
153-
}
154-
155-
ii->kv_pairs = key_value_pairs;
156-
ii->fake_fns = fake_filenames;
157-
158-
ret = ini_parse(INI_FILE_PATH,ini_handler,(void *)&ii);
159-
fprintf(flog,"ret from ini_parse was: %d\n",ret);
80+
81+
ret = ini_parse(INI_FILE_PATH,ini_handler,(void *)&key_value_pairs);
82+
printf("ret from ini_parse was: %d\n",ret);
16083
if (0 != ret)
16184
{
162-
fprintf(flog,"INI parse failed. Terminating\n");
85+
printf("INI parse failed. Terminating\n");
16386
free(key_value_pairs);
16487
key_value_pairs=NULL;
16588
exit(1);
16689
}
167-
168-
16990

17091
return;
17192

@@ -176,23 +97,10 @@ void end(void)
17697
int i;
17798
for (i=0;i<kv_count;i++)
17899
{
179-
if(NULL != key_value_pairs[i])
180-
free(key_value_pairs[i]);
181-
}
182-
183-
for (i=0;i<fn_count;i++)
184-
{
185-
if(NULL != fake_filenames[i])
186-
free(fake_filenames[i]);
100+
free(key_value_pairs[i]);
187101
}
188102
free(key_value_pairs);
189-
free(fake_filenames);
190103
key_value_pairs=NULL;
191-
fake_filenames=NULL;
192-
free(ii);
193-
ii=NULL;
194-
195-
fclose(flog);
196104

197105
return;
198106
}
@@ -207,7 +115,7 @@ char *nvram_get(const char *key)
207115
{
208116
if(strcmp(key,key_value_pairs[i]) == 0)
209117
{
210-
fprintf(flog,"%s=%s\n",key,key_value_pairs[i+1]);
118+
printf("%s=%s\n",key,key_value_pairs[i+1]);
211119
found = 1;
212120
value=key_value_pairs[i+1];
213121
break;
@@ -217,10 +125,7 @@ char *nvram_get(const char *key)
217125
ret = NULL;
218126
if(!found)
219127
{
220-
if(flog == stderr)
221-
fprintf(flog, RED_ON"%s=Unknown\n"RED_OFF,key);
222-
else
223-
fprintf(flog, "%s=Unknown (FAILED LOOKUP)\n",key);
128+
printf( RED_ON"%s=Unknown\n"RED_OFF,key);
224129
}else
225130
{
226131

@@ -229,59 +134,4 @@ char *nvram_get(const char *key)
229134
return ret;
230135
}
231136

232-
FILE* fopen(const char *filename, const char *mode){
233-
static FILE* (*my_fopen) (const char *filename, const char *mode) = NULL;
234-
char *base;
235-
char fakename[64];
236-
237-
if(!my_fopen)
238-
my_fopen = dlsym(RTLD_NEXT,"fopen");
239-
FILE* p = my_fopen(filename,mode);
240-
if(p==0){
241-
if( fname_check(filename) ){
242-
base = basename(filename);
243-
if(strlen(base) > 54)
244-
base[53]='\0';
245-
snprintf(fakename,64,"/tmp/%s.fake",base);
246-
247-
p = my_fopen(fakename,"a+");
248-
}
249-
}
250-
return p;
251-
}
252-
253-
int fname_check(const char *filename){
254-
int i=0;
255-
256-
for(i=0; i<fn_count; i++){
257-
if(strstr(filename,fake_filenames[i])){
258-
fprintf(flog,"FILE %s not found providing false positive\n",filename);
259-
return 1;
260-
}
261-
}
262-
263-
return 0;
264-
}
265-
266-
int open(const char *fn, int flags){
267-
static int (*my_open) (const char *fn, int flags) = NULL;
268-
char *base;
269-
char fakename[64];
270-
271-
272-
if(!my_open)
273-
my_open = dlsym(RTLD_NEXT, "open");
274-
275-
int p = my_open(fn,flags);
276-
if(p==-1){
277-
if( fname_check(fn) ){
278-
base = basename(fn);
279-
if(strlen(base)>54)
280-
base[53]='\0';
281-
snprintf(fakename,64,"/tmp/%s.fake",base);
282-
p = my_open(fakename,O_RDWR|O_CREAT|O_APPEND);
283-
}
284-
}
285137

286-
return p;
287-
}

0 commit comments

Comments
 (0)