-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiMan.c
More file actions
111 lines (108 loc) · 4.07 KB
/
iMan.c
File metadata and controls
111 lines (108 loc) · 4.07 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
#include "headers.h"
void iMan(char **command, int bg, int cmd_count, int *pipe_fd, int *pipe_fd2, int active_pipe)
{
struct hostent *web = gethostbyname("man.he.net");
if (web != NULL)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock != -1)
{
struct sockaddr_in web_address;
struct in_addr **addr_list = (struct in_addr **)web->h_addr_list;
web_address.sin_addr = *addr_list[0];
web_address.sin_family = AF_INET;
web_address.sin_port = htons(80);
int connection = connect(sock, (struct sockaddr *)&web_address, sizeof(web_address));
if (connection != -1)
{
char requested_content[20480];
snprintf(requested_content, sizeof(requested_content), "GET /?topic=%s§ion=all HTTP/1.1\r\nHost: man.he.net\r\n\r\n", command[1]);
int request = send(sock, requested_content, strlen(requested_content), 0);
if (request != -1)
{
char manual[40960];
int result = recv(sock, manual, sizeof(manual) - 1, 0);
if (result > 0)
{
manual[result] = '\0';
char *newline = strchr(manual, '\n');
(newline)++;
int i;
for (i = 0; newline != NULL && i <= 30; i++)
{
if (!strncmp(newline, "NAME", 4))
{
break;
}
newline = strchr(newline, '\n');
if (newline != NULL)
{
(newline)++;
}
else
{
printf("Error:\n\tNo Such Command %s\n", command[1]);
return;
}
}
if (i == 31 || newline == NULL)
{
printf("Error:\n\t\tNo Such Command %s\n", command[1]);
return;
}
char *lastlines = strchr(newline, '\n');
(lastlines)++;
while (lastlines != NULL)
{
if (!strncmp(lastlines, "SEE ALSO", strlen("SEE ALSO")))
{
break;
}
lastlines = strchr(lastlines, '\n');
if (lastlines != NULL)
{
(lastlines)++;
}
else
{
break;
}
}
if (lastlines != NULL)
{
lastlines = strchr(lastlines, '\n');
(lastlines)++;
lastlines = strchr(lastlines, '\n');
(lastlines)++;
lastlines = strchr(lastlines, '\n');
(lastlines)++;
(*lastlines) = '\0';
}
printf("%s\n", newline);
printf("\n\n");
}
else
{
perror("failed getting manual");
}
}
else
{
perror("Request not send");
}
}
else
{
perror("Connection failed");
}
}
else
{
perror("failed to create a socket");
}
}
else
{
perror("DNS failed");
}
}