Repository navigation
Expand file tree
/
Copy pathiMan.c
More file actions
137 lines (114 loc) · 3.84 KB
/
Copy pathiMan.c
File metadata and controls
137 lines (114 loc) · 3.84 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
#include "headers.h"
char *httpGet(const char *url) {
char *response = NULL;
int response_size = 1;
char host[256];
char path[256];
if (sscanf(url, "http://%255[^/]/%255[^\n]", host, path) != 2) {
printError("Invalid URL format\n");
return NULL;
}
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, "http", &hints, &res) != 0) {
printError("Failed to resolve host\n");
return NULL;
}
int sockfd = socket(res->ai_family, res->ai_socktype, 0);
if (sockfd == -1) {
printError("socket");
freeaddrinfo(res);
return NULL;
}
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) {
printError("connect");
close(sockfd);
freeaddrinfo(res);
return NULL;
}
char request[512];
sprintf(request, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
if (send(sockfd, request, strlen(request), 0) == -1) {
printError("send");
close(sockfd);
freeaddrinfo(res);
return NULL;
}
char buffer[1024];
response = malloc(response_size);
response[0] = '\0';
while (1) {
int bytes_received = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
if (bytes_received <= 0) break;
buffer[bytes_received] = '\0';
// Calculate the new size
int new_size = response_size + bytes_received;
// Reallocate memory
response = realloc(response, new_size);
if (response == NULL) {
printError("realloc");
free(response);
close(sockfd);
freeaddrinfo(res);
return NULL;
}
strcat(response, buffer);
response_size = new_size;
}
// Clean up and close the socket
close(sockfd);
freeaddrinfo(res);
return response;
}
int noCommandFound(const char *text) {
const char *pattern = "Man Pages Copyright Respective Owners. Site Copyright (C) 1994 - 2023\nHurricane Electric.\nAll Rights Reserved.";
int len = strlen(text);
while (len > 0 && (isspace(text[len - 1]) || text[len - 1] == '\n')) {
len--;
}
if (strncmp(text, pattern, len) == 0 && pattern[len] == '\0') {
return 1; // Match found
} else {
return 0; // No match
}
}
void iMan(Command *cmd) {
if (cmd->argc != 2) {
printError("ERROR\n\tUsage: iMan <command>\n");
return;
}
char *command = cmd->argv[1];
char url[512];
sprintf(url, "http://man.he.net/?topic=%s§ion=all", command);
char *manPageHTML = httpGet(url);
if (manPageHTML) {
// Locate "NAME" section
char *nameStart = strstr(manPageHTML, "NAME\n");
if (nameStart) {
// Print from "NAME" to "AUTHOR" if "AUTHOR" is found
char *authorStart = strstr(manPageHTML, "AUTHOR");
if (authorStart && authorStart > nameStart) {
size_t section_length = authorStart - nameStart;
printf("%.*s\n", (int) section_length, nameStart);
}
// Print from "NAME" to "SEE ALSO" if "AUTHOR" is not found
else {
char *seealsoStart = strstr(manPageHTML, "SEE ALSO");
if (seealsoStart && seealsoStart > nameStart) {
size_t section_length = seealsoStart - nameStart;
printf("%.*s\n", (int) section_length, nameStart);
} else {
// "SEE ALSO" not found, print the whole "NAME" section
printf("%s\n", nameStart);
}
}
} else {
printError("ERROR\n\tSection not found\n");
}
free(manPageHTML);
} else {
printError("Failed to retrieve the man page HTML\n");
}
}