wpa_supplicant: Move wpa_config_get_line() into utils

The function wpa_config_get_line() is used by the wpa_supplicant config
file parser to retrieve the next non-comment non-blank line. We'll need
the same kind of functionality to implement the file-based external
password backend, so as a preparatory step this commit extracts the
function into its own standalone file in the utils package.

No functional changes are expected from this commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
This commit is contained in:
Patrick Steinhardt 2021-02-14 12:16:28 +01:00 committed by Jouni Malinen
parent fd78ab8270
commit e9f449ba59
10 changed files with 143 additions and 100 deletions

View file

@ -6,6 +6,7 @@ LIB_OBJS= \
base64.o \
bitfield.o \
common.o \
config.o \
crc32.o \
ip_addr.o \
json.o \

97
src/utils/config.c Normal file
View file

@ -0,0 +1,97 @@
/*
* Configuration parsing
* Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "includes.h"
#include "utils/config.h"
#include "common.h"
static int newline_terminated(const char *buf, size_t buflen)
{
size_t len = os_strlen(buf);
if (len == 0)
return 0;
if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
buf[len - 1] != '\n')
return 0;
return 1;
}
static void skip_line_end(FILE *stream)
{
char buf[100];
while (fgets(buf, sizeof(buf), stream)) {
buf[sizeof(buf) - 1] = '\0';
if (newline_terminated(buf, sizeof(buf)))
return;
}
}
char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
char **_pos)
{
char *pos, *end, *sstart;
while (fgets(s, size, stream)) {
(*line)++;
s[size - 1] = '\0';
if (!newline_terminated(s, size)) {
/*
* The line was truncated - skip rest of it to avoid
* confusing error messages.
*/
wpa_printf(MSG_INFO, "Long line in configuration file "
"truncated");
skip_line_end(stream);
}
pos = s;
/* Skip white space from the beginning of line. */
while (*pos == ' ' || *pos == '\t' || *pos == '\r')
pos++;
/* Skip comment lines and empty lines */
if (*pos == '#' || *pos == '\n' || *pos == '\0')
continue;
/*
* Remove # comments unless they are within a double quoted
* string.
*/
sstart = os_strchr(pos, '"');
if (sstart)
sstart = os_strrchr(sstart + 1, '"');
if (!sstart)
sstart = pos;
end = os_strchr(sstart, '#');
if (end)
*end-- = '\0';
else
end = pos + os_strlen(pos) - 1;
/* Remove trailing white space. */
while (end > pos &&
(*end == '\n' || *end == ' ' || *end == '\t' ||
*end == '\r'))
*end-- = '\0';
if (*pos == '\0')
continue;
if (_pos)
*_pos = pos;
return pos;
}
if (_pos)
*_pos = NULL;
return NULL;
}

29
src/utils/config.h Normal file
View file

@ -0,0 +1,29 @@
/*
* Configuration parsing
* Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#ifndef UTILS_CONFIG_H
#define UTILS_CONFIG_H
/**
* wpa_config_get_line - Read the next configuration file line
* @s: Buffer for the line
* @size: The buffer length
* @stream: File stream to read from
* @line: Pointer to a variable storing the file line number
* @_pos: Buffer for the pointer to the beginning of data on the text line or
* %NULL if not needed (returned value used instead)
* Returns: Pointer to the beginning of data on the text line or %NULL if no
* more text lines are available.
*
* This function reads the next non-empty line from the configuration file and
* removes comments. The returned string is guaranteed to be null-terminated.
*/
char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
char **_pos);
#endif /* UTILS_CONFIG_H */