-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipconfig.c
executable file
·93 lines (79 loc) · 2.95 KB
/
ipconfig.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <errno.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <fileXio_rpc.h>
#include "ipconfig.h"
static char *GetNextToken(char *line, char delimiter)
{
char *field_end, *result;
static char *current_line = NULL;
result = NULL;
if (line != NULL) {
current_line = line;
}
while (*current_line == delimiter)
current_line++;
if (current_line[0] != '\0') {
if ((field_end = strchr(current_line, delimiter)) == NULL) {
field_end = ¤t_line[strlen(current_line)];
}
*field_end = '\0';
if (current_line[1] != '\0') { // Test to see if there is another token after this one.
result = current_line;
current_line = field_end + 1;
} else
current_line = NULL;
} else
current_line = NULL;
return result;
}
int ParseConfig(const char *path, char *ip_address, char *subnet_mask, char *gateway)
{
int fd, result, size;
char *FileBuffer, *line, *field;
unsigned int i, DataLineNum;
if ((fd = fileXioOpen(path, O_RDONLY)) >= 0) {
size = fileXioLseek(fd, 0, SEEK_END);
fileXioLseek(fd, 0, SEEK_SET);
if ((FileBuffer = malloc(size)) != NULL) {
if (fileXioRead(fd, FileBuffer, size) == size) {
if ((line = strtok(FileBuffer, "\r\n")) != NULL) {
result = EINVAL;
DataLineNum = 0;
do {
i = 0;
while (line[i] == ' ')
i++;
if (line[i] != '#' && line[i] != '\0') {
if (DataLineNum == 0) {
if ((field = GetNextToken(line, ' ')) != NULL) {
strncpy(ip_address, field, 15);
ip_address[15] = '\0';
if ((field = GetNextToken(NULL, ' ')) != NULL) {
strncpy(subnet_mask, field, 15);
subnet_mask[15] = '\0';
if ((field = GetNextToken(NULL, ' ')) != NULL) {
strncpy(gateway, field, 15);
gateway[15] = '\0';
result = 0;
DataLineNum++;
}
}
}
}
}
} while ((line = strtok(NULL, "\r\n")) != NULL);
} else
result = EINVAL;
} else
result = EIO;
free(FileBuffer);
} else
result = ENOMEM;
fileXioClose(fd);
} else
result = fd;
return result;
}