-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_io.c
71 lines (66 loc) · 1.67 KB
/
file_io.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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "hosts_file.h"
static FILE *openFile(const char *path, const char *mode) {
FILE *file = fopen(path, mode);
if (file == NULL) {
char *errstr;
switch (errno) {
case EACCES:
errstr = "Permission denied";
break;
case ENOENT:
errstr = "File not found";
break;
case EISDIR:
errstr = "Is a directory";
break;
default:
errstr = "Unknown error";
}
fprintf(stderr, "Error opening %s: %s\n", path, errstr);
exit(EXIT_FAILURE);
}
return file;
}
static void processLine(char *line, HostsFile *hostsFile) {
char *ip = strtok(line, " \t\n");
char *hostname;
if (ip != NULL) {
while ((hostname = strtok(NULL, " \t\n")) != NULL) {
addHostsFileEntry(hostsFile, ip, hostname);
}
}
}
static void readHostsFileEntries(FILE *file, HostsFile *hostsFile) {
char line[256];
while (!feof(file)) {
if (fgets(line, 256, file) == NULL) {
break;
}
if (strlen(line) > 255) {
fputs("Error: Line too long (max. 255 characters)", stderr);
exit(EXIT_FAILURE);
}
if (line[0] == '#') {
continue;
}
processLine(line, hostsFile);
}
}
HostsFile *readHostsFile(const char *path) {
FILE *file = openFile(path, "r");
HostsFile *hostsFile = createHostsFile(path);
readHostsFileEntries(file, hostsFile);
fclose(file);
return hostsFile;
}
void writeHostsFile(HostsFile *hostsFile) {
FILE *file = openFile(hostsFile->path, "w");
for (int i = 0; i < hostsFile->numEntries; ++i) {
fprintf(file, "%s\t\t%s\n", hostsFile->entries[i].ip, hostsFile->entries[i].hostname);
}
fclose(file);
}