-
Notifications
You must be signed in to change notification settings - Fork 1
/
hosts_file.c
66 lines (58 loc) · 2.22 KB
/
hosts_file.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hosts_file.h"
HostsFile *createHostsFile(const char *path) {
HostsFile *hostsFile = (HostsFile*)malloc(sizeof(HostsFile));
if (hostsFile == NULL) {
fputs("Memory problem encountered", stderr);
exit(EXIT_FAILURE);
}
hostsFile->path = path;
hostsFile->entries = (HostsFileEntry*)malloc(10*sizeof(HostsFileEntry));
if (hostsFile->entries == NULL) {
fputs("Memory problem encountered", stderr);
exit(EXIT_FAILURE);
}
hostsFile->numEntries = 0;
return hostsFile;
}
void addHostsFileEntry(HostsFile *hostsFile, const char *ip, const char *hostname) {
int maxEntries = sizeof(hostsFile->entries) / sizeof(hostsFile->entries[0]);
if (maxEntries == hostsFile->numEntries) {
hostsFile->entries = (HostsFileEntry*)realloc(hostsFile->entries, (maxEntries+10)*sizeof(HostsFileEntry));
if (hostsFile->entries == NULL) {
fputs("Memory problem encountered", stderr);
exit(EXIT_FAILURE);
}
}
int ipLength = strlen(ip);
hostsFile->entries[hostsFile->numEntries].ip = (char*)malloc(ipLength + 1);
if (hostsFile->entries[hostsFile->numEntries].ip == NULL) {
fputs("Memory problem encountered", stderr);
exit(EXIT_FAILURE);
}
strncpy(hostsFile->entries[hostsFile->numEntries].ip, ip, ipLength);
hostsFile->entries[hostsFile->numEntries].ip[ipLength] = 0;
int hostnameLength = strlen(hostname);
hostsFile->entries[hostsFile->numEntries].hostname = (char*)malloc(hostnameLength + 1);
if (hostsFile->entries[hostsFile->numEntries].hostname == NULL) {
fputs("Memory problem encountered", stderr);
exit(EXIT_FAILURE);
}
strncpy(hostsFile->entries[hostsFile->numEntries].hostname, hostname, hostnameLength);
hostsFile->entries[hostsFile->numEntries].hostname[hostnameLength] = 0;
hostsFile->numEntries = hostsFile->numEntries + 1;
}
int removeHostsFileEntry(HostsFile *hostsFile, const char *hostname) {
for (int i = 0; i < hostsFile->numEntries; ++i) {
if (strcmp(hostsFile->entries[i].hostname, hostname) == 0) {
for (int j = i; j < hostsFile->numEntries - 1; ++j) {
hostsFile->entries[j] = hostsFile->entries[j+1];
}
--hostsFile->numEntries;
return 0;
}
}
return -1;
}