mirror of
https://github.com/troglobit/finit.git
synced 2026-10-30 12:50:34 +00:00
initctl: recactor, share fgetval() with rest of Finit
Note: this highly useful function should probably migrate to libite. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
d3a0631390
commit
006dd12b09
2 files changed
+41
-26
No files matched your search
@@ -25,7 +25,9 @@
|
|||||||
#ifndef FINIT_HELPERS_H_
|
#ifndef FINIT_HELPERS_H_
|
||||||
#define FINIT_HELPERS_H_
|
#define FINIT_HELPERS_H_
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/ttydefaults.h> /* Not included by default in musl libc */
|
#include <sys/ttydefaults.h> /* Not included by default in musl libc */
|
||||||
@@ -114,6 +116,45 @@ static inline int dprint(int fd, const char *s, size_t len)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline char *fgetval(char *line, const char *key, char *sep)
|
||||||
|
{
|
||||||
|
char *ptr, *str, *copy;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
str = copy = strdup(line);
|
||||||
|
if (!copy)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
ptr = strsep(&str, sep);
|
||||||
|
if (ptr && strcmp(ptr, key)) {
|
||||||
|
fail:
|
||||||
|
free(copy);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = strsep(&str, sep);
|
||||||
|
if (!ptr)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
if (*ptr == '"' || *ptr == '\'') {
|
||||||
|
char *end = &ptr[strlen(ptr) - 1];
|
||||||
|
char q = *ptr;
|
||||||
|
|
||||||
|
while (end > ptr && isspace(*end))
|
||||||
|
*end-- = 0;
|
||||||
|
|
||||||
|
if (*end == q) {
|
||||||
|
*end = 0;
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
len = strlen(ptr) + 1;
|
||||||
|
memmove(copy, ptr, len);
|
||||||
|
|
||||||
|
return realloc(copy, len);
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* FINIT_HELPERS_H_ */
|
#endif /* FINIT_HELPERS_H_ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1363,32 +1363,6 @@ static int cmd_parse(int argc, char *argv[], struct cmd *command)
|
|||||||
return command[0].cb(NULL); /* default cmd */
|
return command[0].cb(NULL); /* default cmd */
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *fgetval(char *line, const char *key, char *sep)
|
|
||||||
{
|
|
||||||
char *ptr, *str, *copy;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
str = copy = strdup(line);
|
|
||||||
if (!copy)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
ptr = strsep(&str, sep);
|
|
||||||
if (ptr && strcmp(ptr, key)) {
|
|
||||||
fail:
|
|
||||||
free(copy);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr = strsep(&str, sep);
|
|
||||||
if (!ptr)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
len = strlen(ptr) + 1;
|
|
||||||
memmove(copy, ptr, len);
|
|
||||||
|
|
||||||
return realloc(copy, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void cleanup(void)
|
static void cleanup(void)
|
||||||
{
|
{
|
||||||
if (finit_conf)
|
if (finit_conf)
|
||||||
|
|||||||
Reference in new issue
Block a user