-
Notifications
You must be signed in to change notification settings - Fork 0
/
iman.c
172 lines (162 loc) · 4.69 KB
/
iman.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
#include "iman.h"
int min(int x, int y)
{
if (x < y)
return x;
else
return y;
}
void remove_html_tags(char *str)
{
int in_tag = 0; // Flag to track if we are inside an HTML tag
char *dst = str; // Destination pointer to overwrite the input string
for (char *src = str; *src != '\0'; src++)
{
if (*src == '<')
{
in_tag = 1;
continue;
}
if (*src == '>')
{
in_tag = 0;
continue;
}
if (!in_tag)
{
*dst++ = *src;
}
}
*dst = '\0'; // Null-terminate the result string
}
void fetchManPage(char *url)
{
char *hostname = "man.he.net\0"; // Replace with the hostname of the website
char *path = (char *)malloc(sizeof(char) * 1024);
// strcat(path, "/");
// char *path = "/"; // Replace with the path you want to request
// strcat(path, "?topic=");
// strcat(path, url);
// strcat(path, "§ion=all");
// path[strlen(path)]='\0';
// Create a socket
snprintf(path,1024,"?topic=%s§ion=all",url);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
perror("socket");
exit(1);
}
// Resolve the hostname to an IP address
struct hostent *host = gethostbyname(hostname);
if (host == NULL)
{
perror("gethostbyname");
close(sockfd);
exit(1);
}
// Create a sockaddr_in structure to store server information
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80); // HTTP port
memcpy(&server_addr.sin_addr.s_addr, host->h_addr, host->h_length);
// Connect to the server
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
perror("connect");
close(sockfd);
exit(1);
}
// Prepare the HTTP GET request
char request[1000000];
snprintf(request, sizeof(request), "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, hostname);
// printf("\n\nthhis%sthiss\n\n", request);
// Send the request
if (write(sockfd, request, strlen(request)) == -1)
{
perror("write");
close(sockfd);
exit(1);
}
// Receive and print the response
char response[1000000];
int bytes_received;
int f = 0;
int ct = 0;
bytes_received = read(sockfd, response, sizeof(response));
int br = 0;
while (bytes_received > 0)
{
ct++;
// Remove HTML tags from the response
remove_html_tags(response);
if (f == 0)
{
for (int i = 0; i < bytes_received; i++)
{
if (f == 0)
{
char *x = "NAME\n";
char *y1 = "AUTHO";
char *y = (char *)malloc(sizeof(char) * 10);
int ct = 0;
for (int j = i; j < min(i + 5, bytes_received); j++)
{
y[ct++] = response[j];
}
y[ct] = '\0';
if (strcmp(x, y) == 0)
{
i--;
f = 1;
// break;
}
else if (strcmp(y1, y) == 0)
{
br = 1;
break;
}
free(y);
}
else if (f == 1)
{
char *x = "NAME\n";
char *y1 = "AUTHO";
char *y = (char *)malloc(sizeof(char) * 10);
int ct = 0;
for (int j = i; j < min(i + 5, bytes_received); j++)
{
y[ct++] = response[j];
}
y[ct] = '\0';
// if (strcmp(x, y) == 0)
// {
// i--;
// f = 1;
// // break;
// }
if (strcmp(y1, y) == 0)
{
br = 1;
break;
}
printf("%c", response[i]);
free(y);
}
}
}
else
printf("%s\n", response);
if (br)
break;
printf("%s", response);
// Print the response without HTML tags
// fwrite(response, 1, bytes_received, stdout);
bytes_received = read(sockfd, response, sizeof(response));
}
// printf("%d\n", ct);
printf("\n");
// Close the socket
close(sockfd);
return;
}