Repository navigation
Expand file tree
/
Copy pathiman.c
More file actions
138 lines (120 loc) · 3.49 KB
/
Copy pathiman.c
File metadata and controls
138 lines (120 loc) · 3.49 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
#include "headers.h"
int iman(char *command)
{
struct hostent *he;
he = gethostbyname("man.he.net");
if (he == NULL)
{
perror("gethostbyname");
exit(EXIT_FAILURE);
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80); // HTTP port
server_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(server_addr.sin_zero), '\0', 8);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect");
close(sockfd);
exit(EXIT_FAILURE);
}
char request[1024];
snprintf(request, sizeof(request), "GET /?topic=%s§ion=all HTTP/1.1\r\nHost: man.he.net\r\n\r\n", command);
if (send(sockfd, request, strlen(request), 0) == -1)
{
perror("send");
close(sockfd);
exit(EXIT_FAILURE);
}
char buffer[1024];
int bytes_read;
char *response_buffer = NULL;
size_t buffer_size = 0;
size_t total_bytes_received = 0;
while ((bytes_read = recv(sockfd, buffer, sizeof(buffer), 0)) > 0)
{
response_buffer = realloc(response_buffer, total_bytes_received + bytes_read);
if (response_buffer == NULL)
{
perror("realloc");
exit(EXIT_FAILURE);
}
memcpy(response_buffer + total_bytes_received, buffer, bytes_read);
total_bytes_received += bytes_read;
}
// printf("%s",response_buffer);
// return 1;
char *tag = "<h2";
char *check = strstr(response_buffer, tag);
// printf("%s",search);
if(check!=NULL)
{
printf("Invalid Command! \n");
return 1;
}
char replaced[1024];
strcpy(replaced, "<STRONG>");
char *findtag = "<STRONG>";
char *endtag = "</STRONG>";
char *search = strstr(response_buffer, findtag);
if (findtag)
{
search += strlen(findtag);
char *end = strstr(search, endtag);
if (end)
{
size_t length = end - search;
char title[length + 1];
if (title)
{
strncpy(title, search, length);
title[length] = '\0';
strcat(replaced, title);
}
}
}
strcat(replaced, "</STRONG>");
char *pos = strstr(response_buffer, replaced);
while ((pos = strstr(pos, replaced)) != NULL)
{
size_t find_len = strlen(replaced);
size_t replace_len = strlen("");
size_t new_len = strlen(response_buffer) - find_len + replace_len;
char temp[new_len + 1];
memset(temp, 0, sizeof(temp));
strncpy(temp, response_buffer, pos - response_buffer);
strcat(temp, "");
strcat(temp, pos + find_len);
strcpy(response_buffer, temp);
pos += replace_len;
}
char *start_tag = "<PRE>";
char *end_tag = "</PRE>";
char *start = strstr(response_buffer, start_tag);
if (start)
{
start += strlen(start_tag);
char *end = strstr(start, end_tag);
if (end)
{
size_t length = end - start;
char title[length+1];
if (title)
{
strncpy(title, start, length);
title[length] = '\0';
printf("%s", title);
}
}
}
printf("\n");
free(response_buffer);
close(sockfd);
}