-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.c
282 lines (273 loc) · 8.68 KB
/
crawler.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
//#include <html.h>
#define BASE_URL "www.chitkara.edu.in"
#define urlLength 1000
struct LinkList {
char* url;
int depth;
struct LinkList *next;
}*listHead;
char* returnSubUrl(char *url) {
int i = 0;
char *subUrl = (char*)malloc(sizeof(char) * urlLength);
subUrl = url;
while(url[i] != '/') {
if(url[i] == '\0')
break;
i++;
}
subUrl[i] = '\0';
return subUrl;
}
int checkArgueCount(int argc) {
if(argc == 4)
return 1;
printf("Arguement are not passed as specified");
exit(0);
}
int checkDepth(char *depth) {
int depthInInt;
sscanf(depth, "%d", &depthInInt);
if(depthInInt < 0) {
printf("Depth is not allowed in negative");
exit(0);
} else if(depthInInt > 4) {
printf("That much of deep depth is not allowed");
}
return 1;
}
int checkUrl(char *url) {
char cmnd[urlLength] = "wget --spider ";
strcat(cmnd, url);
if((!strcmp(returnSubUrl(url), BASE_URL)) && !system(cmnd))
printf("Valid Url");
else {
printf("Invalid Url");
exit(0);
}
}
void checkDir(char *dir) {
struct stat dirInfo;
if(stat(dir, &dirInfo) == -1) {
// this function will return -1 if the dir get not found and also this fnc will store all the dir information in the dirInfo structure
printf("-----------------\n");
printf("Invalid Directroy\n");
printf("-----------------\n");
exit(1);
}
if(!S_ISDIR(dirInfo.st_mode)) {
// this function will check that the inputed string is a directory or not
printf("----------------------------\n");
printf("You input is not a directory\n");
printf("----------------------------\n");
exit(1);
}
if((dirInfo.st_mode & S_IWUSR) != S_IWUSR) {
//This function will check that wheather the dir is in writeable mode or not
printf("-------------------------------------------\n");
printf("Your inputed dir is not having write access\n");
printf("-------------------------------------------\n");
exit(1);
}
}
void check(int argc, char *argv[]) {
if(checkArgueCount(argc)) {
if(checkDepth(argv[2])) {
if(checkUrl(argv[1])) {
checkDir(argv[3]);
printf("\nAll things are correct\n");
}
}
}
}
void getPage(char *url, char *dir) {
char urlBuffer[urlLength + 300] = {0};
strcat(urlBuffer, "wget -O ");
strcat(dir, "htmlIntxt.txt ");
strcat(urlBuffer, dir);
strcat(urlBuffer, url);
system(urlBuffer);
}
void transferFile() {
static int file_no = 1;
char fileName[10], ch;
sprintf(fileName, "%d", file_no);
strcat(fileName, ".txt");
FILE *oldFile = fopen("htmlIntxt.txt", "r");
FILE *newFile = fopen(fileName, "w");
ch = getc(oldFile);
while(ch != EOF) {
putc(ch, newFile);
ch = getc(oldFile);
}
file_no++;
printf("New file created\n");
fclose(oldFile);
fclose(newFile);
}
void removeWhiteSpace(char* html) {
int i;
char *buffer = malloc(strlen(html)+1), *p=malloc (sizeof(char)+1);
memset(buffer,0,strlen(html)+1);
for (i=0;html[i];i++)
{
if(html[i]>32)
{
sprintf(p,"%c",html[i]);
strcat(buffer,p);
}
}
strcpy(html,buffer);
free(buffer); free(p);
}
int GetNextURL(char* html, char* urlofthispage, char* result, int pos) {
char c;
int len, i, j;
char* linkStart; //!< pointer pointed to the start of a new-founded URL.
char* linkEnd; //!< pointer pointed to the end of a new-founded URL.
// Clean up \n chars
if(pos == 0) {
removeWhiteSpace(html);
}
// Find the <a> <A> HTML tag.
while (0 != (c = html[pos])) {
if ((c=='<') && ((html[pos+1] == 'a') || (html[pos+1] == 'A'))) {
break;
}
pos++;
}
//! Find the URL it the HTML tag. They usually look like <a href="www.abc.com">
//! We try to find the quote mark in order to find the URL inside the quote mark.
if (c) {
// check for equals first... some HTML tags don't have quotes...or use single quotes instead
//Here we are finding = because the link will start after it
linkStart = strchr(&(html[pos+1]), '=');
if ((!linkStart) || (*(linkStart-1) == 'e') || ((linkStart - html - pos) > 10))
{
// keep going...
return GetNextURL(html,urlofthispage,result,pos+1);
}
//this condition is checking double or single quotes
if (*(linkStart+1) == '\"' || *(linkStart+1) == '\'')
linkStart++;
//so the link will be after quotes that's why we increamented by one
linkStart++;
//strpbrk will find the occurence of character from second string in first string
linkEnd = strpbrk(linkStart, "\'\">");
//with the help of this we will get the end of the link
if(!linkEnd) {
// keep going... if we didn't find the end of the link
return GetNextURL(html,urlofthispage,result,pos+1);
}
if (*linkStart == '#') {
// Why bother returning anything here....recursively keep going...
return GetNextURL(html,urlofthispage,result,pos+1);
}
if (!strncmp(linkStart, "mailto:",7))
return GetNextURL(html, urlofthispage, result, pos+1);
if (!strncmp(linkStart, "http", 4) || !strncmp(linkStart, "HTTP", 4)) {
//! Nice! The URL we found is in absolute path.
strncpy(result, linkStart, (linkEnd-linkStart));
return (int)(linkEnd - html + 1);
} else {
//! We find a URL. HTML is a terrible standard. So there are many ways to present a URL.
if (linkStart[0] == '.') {
//! Some URLs are like <a href="../../../a.txt"> I cannot handle this.
// again...probably good to recursively keep going..
return GetNextURL(html,urlofthispage,result,pos+1);
}
if (linkStart[0] == '/') {
//! this means the URL is the relative path
for (i = 7; i < strlen(urlofthispage); i++)
if (urlofthispage[i] == '/')
break;
strcpy(result, urlofthispage);
result[i] = 0;
strncat(result, linkStart, (linkEnd - linkStart));
return (int)(linkEnd - html + 1);
} else {
//! the URL is a absolute path.
len = strlen(urlofthispage);
for (i = (len - 1); i >= 0; i--)
if (urlofthispage[i] == '/')
break;
for (j = (len - 1); j >= 0; j--)
if (urlofthispage[j] == '.')
break;
if (i == (len -1)) {
//! urlofthis page is like http://www.abc.com/
strcpy(result, urlofthispage);
result[i + 1] = 0;
strncat(result, linkStart, linkEnd - linkStart);
return (int)(linkEnd - html + 1);
}
if ((i <= 6) || (i > j)) {
//! urlofthis page is like http://www.abc.com/~xyz
//! or http://www.abc.com
strcpy(result, urlofthispage);
result[len] = '/';
strncat(result, linkStart, linkEnd - linkStart);
return (int)(linkEnd - html + 1);
}
strcpy(result, urlofthispage);
result[i + 1] = 0;
strncat(result, linkStart, linkEnd - linkStart);
return (int)(linkEnd - html + 1);
}
}
}
return -1;
}
char* convertDataInStr(char *fileName) {
struct stat st;
stat(fileName, &st);
int fileSize = st.st_size, i = 0;
char *fileContent = (char*)malloc(sizeof(char) * fileSize), ch;
FILE *file = fopen(fileName, "r");
ch = getc(file);
while(ch != EOF) {
fileContent[i] = ch;
ch = getc(file);
i++;
}
fileContent[i] = '\0';
// fclose(file);
return fileContent;
}
void putInList(char **links) {
struct LinkList *obj, *listHeadPtr;
listHead = (struct LinkList*)malloc(sizeof(struct LinkList));
listHeadPtr = listHead;
listHeadPtr->url = links[0];
listHeadPtr->next = 0;
for(int i = 1;i<100;i++) {
obj = (struct LinkList*)malloc(sizeof(struct LinkList));
//obj->url = (char*)malloc(sizeof(char) * 1000);
obj->url = links[i];
obj->next = 0;
listHeadPtr->next = obj;
listHeadPtr = listHeadPtr->next;
}
}
int main(int argc, char* argv[]) {
check(argc, argv);
getPage(argv[1], argv[3]);
//transferFile();
char *fileContent = convertDataInStr("htmlIntxt.txt");
int pos = 0;
char **links = (char**) malloc(sizeof(char*) * 100);
for(int i = 0;i<100;i++) {
links[i] = (char*)malloc(sizeof(char)*100);
pos = GetNextURL(fileContent, argv[1], links[i], pos);
}
putInList(links);
while(listHead->next != 0) {
printf("%s\n", listHead->url);
listHead = listHead->next;
}
return 0;
}