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