GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: cfgparse.c Lines: 75 79 94.9 %
Date: 2026-02-24 22:34:36 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
141921
char * skip_comments_and_blanks(char *p)
21
{
22
369128
	while (*p != 0) {
23
24
880598
		while (
25
880598
				(*p == ' ') ||
26
				(*p == '\n')
27
				)
28
511470
			++p;
29
30
369128
		if (*p == '#')
31
		{
32
227207
			++p;
33
34
227207
			while (
35
9127650
					(*p != 0) &&
36
					(*p != '\n')
37
					)
38
8900443
				++p;
39
40
227207
			continue;
41
		}
42
		else
43
			break;
44
45
	}
46
47
141921
	return p;
48
}
49
50
/* TODO: Refactor this function */
51
52
10527
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
10527
	if (value_size_max == 0)
63
		return 0;
64
10527
	--value_size_max;
65
66
19893
	while (*p != 0)
67
	{
68
		/*
69
		 *  skip all characters before first '['
70
		 */
71
19284
		p = skip_comments_and_blanks(p);
72
73
		/*
74
		 *  if we got EOF so quit
75
		 */
76
19284
		if (*p == 0)
77
			break;
78
79
19284
		if (*p != '[')
80
		{
81
			++p;
82
			continue;
83
		}
84
85
		/*
86
		 *  skip '[' that we found
87
		 */
88
19284
		++p;
89
90
		/*
91
		 * copy section name to vname
92
		 */
93
		sp = 0;
94
19284
		while (
95
181580
				(*p != ']') &&
96
162296
				(*p != 0) &&
97
162296
				(*p != '\n') &&
98
				(sp < 255)
99
				)
100
		{
101
162296
			vname[sp] = *p;
102
162296
			++sp;
103
162296
			++p;
104
		}
105
19284
		vname[sp] = 0;
106
107
19284
		if (*p == 0)
108
			break;
109
110
		/*
111
		 * newline - start over again
112
		 */
113
19284
		if (*p == '\n')
114
			continue;
115
116
		/*
117
		 * skip ']' that we found
118
		 */
119
19284
		++p;
120
121
19284
		if (strcmp(vname, section_name) == 0)
122
		{
123
			do {
124
56166
				p = skip_comments_and_blanks(p);
125
56166
				if ((*p == 0) || (*p == '['))
126
					break;
127
128
				sp = 0;
129
521862
				while (
130
521862
						(*p != '=') &&
131
465696
						(*p != ' ') &&
132
465696
						(*p != 0) &&
133
465696
						(*p != '\n') &&
134
						(sp < 255)
135
						)
136
				{
137
465696
					vname[sp] = *p;
138
465696
					++sp;
139
465696
					++p;
140
				}
141
56166
				vname[sp] = 0;
142
143
56166
				if (*p == 0)
144
					break;
145
146
56166
				while (*p == ' ')
147
					++p;
148
149
56166
				if (*p != '=')
150
					break;
151
152
56166
				p++;
153
154
56166
				if (strcmp(vname, key_name) == 0)
155
				{
156
					sp = 0;
157
158
143639
					while (
159
143639
							(*p != '\n') &&
160
							(*p != 0)
161
							)
162
					{
163
133721
						if (sp < value_size_max)
164
133721
							value[sp] = *p;
165
						else
166
							return 0;
167
133721
						++sp;
168
133721
						++p;
169
					}
170
9918
					value[sp] = 0;
171
9918
					return 1;
172
				}
173
				else
174
				{
175
463581
					while (
176
463581
							(*p != '\n') &&
177
							(*p != 0)
178
							)
179
417333
						++p;
180
				}
181
182
46248
			} while (*p != 0);
183
		}
184
		else
185
		{
186
			do {
187
66471
				p = skip_comments_and_blanks(p);
188
189
66471
				if ((*p == 0) || (*p == '['))
190
					break;
191
192
1290333
				while (
193
1290333
						(*p != '\n') &&
194
						(*p != 0)
195
						)
196
1233228
					++p;
197
198
			} while (1);
199
		}
200
	}
201
202
	return 0;
203
}
204
205
664
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
664
	while (f_config != -1)
213
	{
214
664
		fsz = lseek(f_config, 0L, SEEK_END) + 1;
215
664
		lseek(f_config, 0L, SEEK_SET);
216
217
664
		buffer = x_malloc(fsz);
218
219
		fsz = read(f_config, buffer, fsz);
220
664
		buffer[fsz] = 0;
221
664
		break;
222
	}
223
224
664
	if (f_config != -1)
225
664
		close(f_config);
226
227
664
	return buffer;
228
}