-
Notifications
You must be signed in to change notification settings - Fork 0
/
iMan.c
71 lines (55 loc) · 1.83 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
#include "header.h"
#define PORT 80
#define BUFFER_SIZE 4096
void print_without_html_tags(const char *str){
int inside_tag = 0;
for(int i = 0; str[i] != '\0'; i++){
if (str[i] == '<') inside_tag = 1;
else if (str[i] == '>') inside_tag = 0;
else if (!inside_tag) putchar(str[i]);
}
}
void get_man_page(const char *command_name) {
int sock;
struct sockaddr_in server_addr;
struct hostent *server;
char request[1024], response[BUFFER_SIZE];
int bytes_read;
char *response_body;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
perror("Socket creation failed");
return;
}
server = gethostbyname("man.he.net");
if(server == NULL){
fprintf(stderr, "No such host\n");
return;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
if(connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
return;
}
snprintf(request, sizeof(request), "GET /?topic=%s§ion=all\r\nHost: man.he.net\r\nConnection: close\r\n\r\n", command_name);
if(send(sock, request, strlen(request), 0) < 0){
perror("Send failed");
return;
}
int no_match_found = 0;
while((bytes_read = recv(sock, response, BUFFER_SIZE - 1, 0)) > 0){
response[bytes_read] = '\0';
if(strstr(response,"No matches for")) no_match_found = 1;
print_without_html_tags(response);
}
if(no_match_found) printf(RED "No Match Found !\n" RESET);
close(sock);
}
void iMan(char* command){
if(strlen(command) == 4) return;
char* term = strtok(command+4," ");
get_man_page(term);
}