GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: cfgparse.c Lines: 75 79 94.9 %
Date: 2026-02-24 22:34:48 Branches: 43 62 69.4 %

Line Branch Exec Source
1
/*
2
* cfgparse.c
3
*
4
*  Created on: Aug 20, 2016
5
*
6
*  Modified on: Feb 09, 2018
7
*
8
*      Author: lightftp
9
*/
10
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <unistd.h>
15
#include <sys/types.h>
16
#include <sys/stat.h>
17
#include <fcntl.h>
18
#include "x_malloc.h"
19
20
143383
char * skip_comments_and_blanks(char *p)
21
{
22
371659
	while (*p != 0) {
23
24
886049
		while (
25
886049
				(*p == ' ') ||
26
				(*p == '\n')
27
				)
28
514390
			++p;
29
30
371659
		if (*p == '#')
31
		{
32
228276
			++p;
33
34
228276
			while (
35
9169785
					(*p != 0) &&
36
					(*p != '\n')
37
					)
38
8941509
				++p;
39
40
228276
			continue;
41
		}
42
		else
43
			break;
44
45
	}
46
47
143383
	return p;
48
}
49
50
/* TODO: Refactor this function */
51
52
10521
int ParseConfig(
53
    const char      *pcfg,
54
    const char      *section_name,
55
    const char      *key_name,
56
    char            *value,
57
    unsigned long   value_size_max)
58
{
59
	unsigned long	sp;
60
	char			vname[256], *p = (char *)pcfg;
61
62
10521
	if (value_size_max == 0)
63
		return 0;
64
10521
	--value_size_max;
65
66
20111
	while (*p != 0)
67
	{
68
		/*
69
		 *  skip all characters before first '['
70
		 */
71
19628
		p = skip_comments_and_blanks(p);
72
73
		/*
74
		 *  if we got EOF so quit
75
		 */
76
19628
		if (*p == 0)
77
			break;
78
79
19628
		if (*p != '[')
80
		{
81
			++p;
82
			continue;
83
		}
84
85
		/*
86
		 *  skip '[' that we found
87
		 */
88
19628
		++p;
89
90
		/*
91
		 * copy section name to vname
92
		 */
93
		sp = 0;
94
19628
		while (
95
184287
				(*p != ']') &&
96
164659
				(*p != 0) &&
97
164659
				(*p != '\n') &&
98
				(sp < 255)
99
				)
100
		{
101
164659
			vname[sp] = *p;
102
164659
			++sp;
103
164659
			++p;
104
		}
105
19628
		vname[sp] = 0;
106
107
19628
		if (*p == 0)
108
			break;
109
110
		/*
111
		 * newline - start over again
112
		 */
113
19628
		if (*p == '\n')
114
			continue;
115
116
		/*
117
		 * skip ']' that we found
118
		 */
119
19628
		++p;
120
121
19628
		if (strcmp(vname, section_name) == 0)
122
		{
123
			do {
124
55749
				p = skip_comments_and_blanks(p);
125
55749
				if ((*p == 0) || (*p == '['))
126
					break;
127
128
				sp = 0;
129
517236
				while (
130
517236
						(*p != '=') &&
131
461487
						(*p != ' ') &&
132
461487
						(*p != 0) &&
133
461487
						(*p != '\n') &&
134
						(sp < 255)
135
						)
136
				{
137
461487
					vname[sp] = *p;
138
461487
					++sp;
139
461487
					++p;
140
				}
141
55749
				vname[sp] = 0;
142
143
55749
				if (*p == 0)
144
					break;
145
146
55749
				while (*p == ' ')
147
					++p;
148
149
55749
				if (*p != '=')
150
					break;
151
152
55749
				p++;
153
154
55749
				if (strcmp(vname, key_name) == 0)
155
				{
156
					sp = 0;
157
158
143606
					while (
159
143606
							(*p != '\n') &&
160
							(*p != 0)
161
							)
162
					{
163
133568
						if (sp < value_size_max)
164
133568
							value[sp] = *p;
165
						else
166
							return 0;
167
133568
						++sp;
168
133568
						++p;
169
					}
170
10038
					value[sp] = 0;
171
10038
					return 1;
172
				}
173
				else
174
				{
175
458758
					while (
176
458758
							(*p != '\n') &&
177
							(*p != 0)
178
							)
179
413047
						++p;
180
				}
181
182
45711
			} while (*p != 0);
183
		}
184
		else
185
		{
186
			do {
187
68006
				p = skip_comments_and_blanks(p);
188
189
68006
				if ((*p == 0) || (*p == '['))
190
					break;
191
192
1319307
				while (
193
1319307
						(*p != '\n') &&
194
						(*p != 0)
195
						)
196
1260891
					++p;
197
198
			} while (1);
199
		}
200
	}
201
202
	return 0;
203
}
204
205
657
char *InitConfig(char *cfg_filename)
206
{
207
	int		f_config;
208
	char	*buffer = NULL;
209
	off_t	fsz;
210
211
	f_config = open(cfg_filename, O_RDONLY);
212
657
	while (f_config != -1)
213
	{
214
657
		fsz = lseek(f_config, 0L, SEEK_END) + 1;
215
657
		lseek(f_config, 0L, SEEK_SET);
216
217
657
		buffer = x_malloc(fsz);
218
219
		fsz = read(f_config, buffer, fsz);
220
657
		buffer[fsz] = 0;
221
657
		break;
222
	}
223
224
657
	if (f_config != -1)
225
657
		close(f_config);
226
227
657
	return buffer;
228
}