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