-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain
More file actions
147 lines (134 loc) · 3.56 KB
/
Main
File metadata and controls
147 lines (134 loc) · 3.56 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
//#define path "test.txt"
#define MAC_STD 17
#define MAC_DELM_COL ':'
#define MAC_DELM_DAS '-'
static unsigned long deleteEntry( char*, size_t, const char* );
int isMacAddress(char *s)
{
int i;
int ret = 0;
if(strlen(s) > MAC_STD)
return ret;
//2 5 8 11 14
if( (s[2] == MAC_DELM_COL && s[5] == MAC_DELM_COL && s[8] == MAC_DELM_COL &&
s[11] == MAC_DELM_COL && s[14] == MAC_DELM_COL ) ||
(s[2] == MAC_DELM_DAS && s[5] == MAC_DELM_DAS && s[8] == MAC_DELM_DAS &&
s[11] == MAC_DELM_DAS && s[14] == MAC_DELM_DAS ) )
{
for(i = 0; i < MAC_STD; i++)
{
if(s[i] == '\0')
return ret;
if(i % 3 != 2 && !isxdigit(s[i]))
return ret;
}
return !ret;
}
return ret;
}
int xmain(char *path,char *word_2_search)
{
struct stat st;
int ret = -1;
if(!isMacAddress(word_2_search))
{
printf("\n Not in formate");
return ret;
}
if ( stat( path, &st ) != -1 )
{
FILE* fp = fopen( path, "rb" );
if ( fp != NULL )
{
char* buffer = malloc( st.st_size );
long tmp = st.st_size;
//if ( fread(buffer, 1, st.st_size, fp) == tmp)//((long)st.st_size))
if ( fread(buffer, 1, st.st_size, fp) == (unsigned long)st.st_size)
{
fclose(fp);
unsigned long newSize = deleteEntry( buffer, st.st_size, word_2_search ); //X
if(newSize == 0)
{
printf("\n No Found ...");
}
else
{
fp = fopen( path, "wb" );
if ( fp != NULL )
{
fwrite(buffer, 1, newSize, fp);
fclose(fp);
ret = 0;
}
else
{
return ret;
}
}
}
free(buffer);
}
else
{
return ret;
}
}
else
{
printf( "did not find %s", path );
}
return ret;
}
static unsigned long deleteEntry( char* buffer, size_t size, const char* macaddress_to_search )
{
char* p = buffer;
bool done = false;
size_t len = strlen(macaddress_to_search);
unsigned long newSize = 0;//X
do
{
char* q = strchr( p, *macaddress_to_search );
if ( q != NULL )
{
if ( strncmp( q, macaddress_to_search, len ) == 0 )
{
unsigned long lineSize = 1; //X
char *line = q;
for ( ; *line != '\n'; ++line)
{
++lineSize;
}
// obtain left over length and adjust length
size_t restSize = (size_t)((buffer + size) - (q + lineSize));
memmove( q, q + lineSize, restSize );
newSize = size - lineSize;
done = true;
}
else
{
p = q + 1;
}
}
else
{
done = true;
}
}
while (!done);
return newSize;
}
int main()
{
char word_2_search[100]={0};
printf("\n Enter Word to be removed");
scanf("%s",word_2_search);
if(xmain("test.txt",word_2_search) < 0)
printf("\n Error occured");
else
printf("\n All fine");
}