questd: tools.c with helpful functions

This commit is contained in:
Sukru Senli 2015-04-16 11:48:21 +02:00 committed by Martin Schröder
parent 03a481bfaf
commit 501bd3f487
4 changed files with 85 additions and 11 deletions

View file

@ -2,8 +2,8 @@ CC = gcc
CFLAGS = -g -Wall CFLAGS = -g -Wall
LOCLIBS = LOCLIBS =
LIBS = -luci -lubus -lubox -lpthread LIBS = -luci -lubus -lubox -lpthread
OBJS = questd.o dumper.o port.o arping.o usb.o ndisc.o dslstats.o 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 SRCS = questd.c dumper.c port.c arping.c usb.c ndisc.c dslstats.c tools.c
LIBSRCS = LIBSRCS =
ISRCS = questd.h ISRCS = questd.h

View file

@ -61,15 +61,6 @@ get_db_hw_value(char *opt, char **val)
*val = ptr.o->v.string; *val = ptr.o->v.string;
} }
void
remove_newline(char *buf)
{
int len;
len = strlen(buf) - 1;
if (buf[len] == '\n')
buf[len] = 0;
}
void void
get_jif_val(jiffy_counts_t *p_jif) get_jif_val(jiffy_counts_t *p_jif)
{ {

View file

@ -175,6 +175,9 @@ void recalc_sleep_time(bool calc, int toms);
void init_db_hw_config(void); void init_db_hw_config(void);
bool arping(char *target, char *device, int toms); bool arping(char *target, char *device, int toms);
void remove_newline(char *buf); 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 get_jif_val(jiffy_counts_t *p_jif);
void dump_keys(Key *keys); void dump_keys(Key *keys);
void dump_specs(Spec *spec); void dump_specs(Spec *spec);

80
questd/src/tools.c Normal file
View file

@ -0,0 +1,80 @@
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#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;
}