conf: new function conf_parse_env()

Reduce code duplication.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2025-07-02 14:28:27 +02:00
1 parent 6f0ba22a0f
commit 33f6697cd5
3 files changed
+35 -60

No files matched your search

+28 -8
View File
@@ -490,14 +490,16 @@ void conf_save_service(int type, char *cfg, char *file)
fclose(fp);
}
/*
* Sets, and makes a note of, all KEY=VALUE lines in a given .conf line
* from finit.conf, or other .conf file. Note, PATH is always reset in
* the conf_reset_env() function.
/**
* conf_parse_env - Parse a key=value line
* @line: Line buffer without newline
* @value: Whitespace trimmed value
*
* Returns:
* %NULL on error, otherwise a whitespace trimmed key.
*/
static void parse_env(char *line)
char *conf_parse_env(char *line, char **value)
{
struct env_entry *node;
char *key, *val, *end;
/* skip any leading whitespace */
@@ -519,7 +521,7 @@ static void parse_env(char *line)
val = strchr(key, '=');
if (!val)
return;
return NULL;
*val++ = 0;
/* strip leading whitespace from value */
@@ -528,6 +530,7 @@ static void parse_env(char *line)
/* unquote value, if quoted */
unquote(&val, end);
*value = val;
/* find end of key */
end = key;
@@ -554,9 +557,26 @@ static void parse_env(char *line)
end++;
if (*end != 0) {
warnx("'%s=%s': not a valid identifier", key, val);
return; /* invalid key */
return NULL; /* invalid key */
}
return key;
}
/*
* Sets, and makes a note of, all KEY=VALUE lines in a given .conf line
* from finit.conf, or other .conf file. Note, PATH is always reset in
* the conf_reset_env() function.
*/
static void parse_env(char *line)
{
struct env_entry *node;
char *key, *val;
key = conf_parse_env(line, &val);
if (!key)
return;
dbg("Global env '%s'='%s'", key, val);
setenv(key, val, 1);