This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathhosts-set.c
174 lines (139 loc) · 3.94 KB
/
hosts-set.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @file
*
* @brief Contains the set direction of the hosts plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "hosts.h"
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <kdbextension.h>
static int keyCmpOrderWrapper (const void * a, const void * b)
{
return elektraKeyCmpOrder (*((const Key **) a), *((const Key **) b));
}
static void writeComment (const char * spaces, const char * start, const char * comment, FILE * fp)
{
if (spaces)
{
char * endptr;
long spaceValue = strtol (spaces, &endptr, 10);
if (*endptr == '\0')
{
for (int i = 0; i < spaceValue; i++)
{
fprintf (fp, " ");
}
}
}
if (start)
{
fprintf (fp, "%s", start);
}
if (comment)
{
fprintf (fp, "%s", comment);
}
}
static const char * getMetaValue (Key * key, const char * metaName)
{
const Key * metaKey = keyGetMeta (key, metaName);
if (metaKey) return keyString (metaKey);
return 0;
}
static void writeLineComments (Key * key, FILE * fp)
{
KeySet * metaKeys = keyMeta (key);
Key * commentParent = keyNew ("meta:/comment", KEY_END);
KeySet * comments = elektraArrayGet (commentParent, metaKeys);
keyDel (commentParent);
Key * current;
for (elektraCursor it = 0; it < ksGetSize (comments); ++it)
{
current = ksAtCursor (comments, it);
if (strcmp (keyName (current), "meta:/comment/#0") != 0)
{
Key * spaceKey = keyDup (current, KEY_CP_ALL);
keyAddBaseName (spaceKey, "space");
Key * startKey = keyDup (current, KEY_CP_ALL);
keyAddBaseName (startKey, "start");
const char * spaces = getMetaValue (key, keyName (spaceKey));
const char * start = getMetaValue (key, keyName (startKey));
const char * comment = getMetaValue (key, keyName (current));
keyDel (spaceKey);
keyDel (startKey);
writeComment (spaces, start, comment, fp);
fprintf (fp, "\n");
}
}
ksDel (comments);
}
static void writeInlineComment (Key * key, FILE * fp)
{
const char * spaces = getMetaValue (key, "comment/#0/space");
const char * start = getMetaValue (key, "comment/#0/start");
const char * comment = getMetaValue (key, "comment/#0");
writeComment (spaces, start, comment, fp);
}
static void writeHostsEntry (Key * key, KeySet * returned, FILE * fp)
{
fprintf (fp, "%s\t%s", (char *) keyValue (key), (char *) keyBaseName (key));
/* set the iterator to the current key and
* iterate over its subkeys
*/
for (elektraCursor it = ksSearch (returned, key) + 1; it < ksGetSize (returned); ++it)
{
Key * alias = ksAtCursor (returned, it);
if (keyIsBelow (key, alias) != 1) break;
fprintf (fp, " %s", (char *) keyBaseName (alias));
}
}
int elektraHostsSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
int errnosave = errno;
FILE * fp;
fp = fopen (keyString (parentKey), "w");
if (fp == 0)
{
ELEKTRA_SET_ERROR_SET (parentKey);
errno = errnosave;
return -1;
}
/* build an array of entries and sort them according to their order metadata */
Key ** keyArray;
size_t arraySize = ksGetSize (returned);
keyArray = calloc (arraySize, sizeof (Key *));
int ret = elektraKsToMemArray (returned, keyArray);
if (ret < 0)
{
ELEKTRA_SET_RESOURCE_ERROR (parentKey, strerror (errno));
fclose (fp);
return -1;
}
qsort (keyArray, arraySize, sizeof (Key *), keyCmpOrderWrapper);
Key * ipv4Base = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (ipv4Base, "ipv4");
Key * ipv6Base = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (ipv6Base, "ipv6");
/* now write the hosts file */
for (size_t i = 0; i < arraySize; ++i)
{
Key * key = keyArray[i];
/* only process canonical name keys */
if (!keyIsDirectlyBelow (ipv4Base, key) && !keyIsDirectlyBelow (ipv6Base, key)) continue;
writeLineComments (key, fp);
writeHostsEntry (key, returned, fp);
writeInlineComment (key, fp);
fprintf (fp, "\n");
}
writeLineComments (parentKey, fp);
keyDel (ipv4Base);
keyDel (ipv6Base);
fclose (fp);
errno = errnosave;
elektraFree (keyArray);
return 1;
}