diff --git a/questd/src/Makefile b/questd/src/Makefile index e2f7cac0c..054b4a2b6 100644 --- a/questd/src/Makefile +++ b/questd/src/Makefile @@ -2,8 +2,8 @@ CC = gcc CFLAGS = -g -Wall LOCLIBS = LIBS = -luci -lubus -lubox -lpthread -OBJS = questd.o dumper.o port.o arping.o usb.o ndisc.o dslstats.o -SRCS = questd.c dumper.c port.c arping.c usb.c ndisc.c dslstats.c +OBJS = questd.o dumper.o port.o arping.o usb.o ndisc.o dslstats.o tools.o +SRCS = questd.c dumper.c port.c arping.c usb.c ndisc.c dslstats.c tools.c LIBSRCS = ISRCS = questd.h diff --git a/questd/src/dumper.c b/questd/src/dumper.c index 905f23b2d..3741d47cb 100644 --- a/questd/src/dumper.c +++ b/questd/src/dumper.c @@ -61,15 +61,6 @@ get_db_hw_value(char *opt, char **val) *val = ptr.o->v.string; } -void -remove_newline(char *buf) -{ - int len; - len = strlen(buf) - 1; - if (buf[len] == '\n') - buf[len] = 0; -} - void get_jif_val(jiffy_counts_t *p_jif) { diff --git a/questd/src/questd.h b/questd/src/questd.h index 17b463f24..95d8b704e 100644 --- a/questd/src/questd.h +++ b/questd/src/questd.h @@ -175,6 +175,9 @@ void recalc_sleep_time(bool calc, int toms); void init_db_hw_config(void); bool arping(char *target, char *device, int toms); void remove_newline(char *buf); +void replace_char(char *buf, char a, char b); +void runCmd(const char *pFmt, ...); +const char *chrCmd(const char *pFmt, ...); void get_jif_val(jiffy_counts_t *p_jif); void dump_keys(Key *keys); void dump_specs(Spec *spec); diff --git a/questd/src/tools.c b/questd/src/tools.c new file mode 100644 index 000000000..328e62fd5 --- /dev/null +++ b/questd/src/tools.c @@ -0,0 +1,80 @@ +#include +#include +#include + +#include "questd.h" + +void +remove_newline(char *buf) +{ + int len; + len = strlen(buf) - 1; + if (buf[len] == '\n') + buf[len] = 0; +} + +void +replace_char(char *buf, char a, char b) +{ + int i = 0; + + while (buf[i]) { + if (buf[i] == a) + buf[i] = b; + i++; + } + buf[i] = '\0'; +} + +void +runCmd(const char *pFmt, ...) +{ + va_list ap; + char cmd[256] = {0}; + int len=0, maxLen; + + maxLen = sizeof(cmd); + + va_start(ap, pFmt); + + if (len < maxLen) + { + maxLen -= len; + vsnprintf(&cmd[len], maxLen, pFmt, ap); + } + + system(cmd); + + va_end(ap); +} + +const char* +chrCmd(const char *pFmt, ...) +{ + va_list ap; + char cmd[256] = {0}; + int len=0, maxLen; + + maxLen = sizeof(cmd); + + va_start(ap, pFmt); + + if (len < maxLen) + { + maxLen -= len; + vsnprintf(&cmd[len], maxLen, pFmt, ap); + } + + va_end(ap); + + FILE *pipe; + char buffer[128]; + if ((pipe = popen(cmd, "r"))) + fgets(buffer, sizeof(buffer), pipe); + pclose(pipe); + + if (strlen(buffer)) + return strdup(buffer); + else + return NULL; +}