Skip to content

Commit

Permalink
reduce config file handling code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
vergoh committed Jan 7, 2015
1 parent 99e0f29 commit 2f5ac72
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.h ibw.h
common.o: common.c common.h
misc.o: misc.c misc.h common.h
cfg.o: cfg.c cfg.h common.h
ibw.o: ibw.c ibw.h common.h
ibw.o: ibw.c ibw.h cfg.h ifinfo.h common.h
daemon.o: daemon.c daemon.h common.h ifinfo.h dbaccess.h dbcache.h misc.h cfg.h ibw.h
image.o: image.c image.h vnstati.h common.h misc.h

Expand Down
105 changes: 58 additions & 47 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ void printcfgfile(void)
int loadcfg(const char *cfgfile)
{
FILE *fd;
char buffer[512];
int i, j, k, linelen, cfglen, tryhome;
int i, j, k, linelen, cfglen;

char value[512], cfgline[512];

Expand Down Expand Up @@ -223,54 +222,12 @@ int loadcfg(const char *cfgfile)
{ 0, 0, 0, 0, 0 }
};

/* clear buffer */
for (i=0; i<512; i++) {
buffer[i] = '\0';
}

/* load default config */
defaultcfg();

/* possible config files: 1) --config 2) $HOME/.vnstatrc 3) /etc/vnstat.conf 4) none */

if (cfgfile[0]!='\0') {

/* try to open given file */
if ((fd=fopen(cfgfile, "r"))!=NULL) {
if (debug)
printf("Config file: --config\n");
} else {
snprintf(errorstring, 512, "Unable to open given config file \"%s\": %s\n", cfgfile, strerror(errno));
printe(PT_Error);
return 0;
}

} else {

if (getenv("HOME")) {
strncpy_nt(buffer, getenv("HOME"), 500);
strcat(buffer, "/.vnstatrc");
tryhome = 1;
} else {
tryhome = 0;
}

/* try to open first available config file */
if (tryhome && (fd=fopen(buffer, "r"))!=NULL) {
if (debug)
printf("Config file: $HOME/.vnstatrc\n");
} else if ((fd=fopen("/etc/vnstat.conf", "r"))!=NULL) {
if (debug)
printf("Config file: /etc/vnstat.conf\n");
} else if ((fd=fopen("/usr/local/etc/vnstat.conf", "r"))!=NULL) {
if (debug)
printf("Config file: /usr/local/etc/vnstat.conf\n");
} else {
if (debug)
printf("Config file: none\n");
return 1;
}
}
i = opencfgfile(cfgfile, &fd);
if (i != 2)
return i;

rewind(fd);

Expand Down Expand Up @@ -588,3 +545,57 @@ void defaultcfg(void)
strncpy_nt(cfg.ctx, CTX, 8);
strncpy_nt(cfg.ctxd, CTXD, 8);
}

int opencfgfile(const char *cfgfile, FILE **fd)
{
char buffer[512];
int i, tryhome;

/* clear buffer */
for (i=0; i<512; i++) {
buffer[i] = '\0';
}

/* possible config files: 1) --config 2) $HOME/.vnstatrc 3) /etc/vnstat.conf 4) none */

if (cfgfile[0]!='\0') {

/* try to open given file */
if ((*fd=fopen(cfgfile, "r"))!=NULL) {
if (debug)
printf("Config file: --config\n");
} else {
snprintf(errorstring, 512, "Unable to open given config file \"%s\": %s\n", cfgfile, strerror(errno));
printe(PT_Error);
return 0;
}

} else {

if (getenv("HOME")) {
strncpy_nt(buffer, getenv("HOME"), 500);
strcat(buffer, "/.vnstatrc");
tryhome = 1;
} else {
tryhome = 0;
}

/* try to open first available config file */
if (tryhome && (*fd=fopen(buffer, "r"))!=NULL) {
if (debug)
printf("Config file: $HOME/.vnstatrc\n");
} else if ((*fd=fopen("/etc/vnstat.conf", "r"))!=NULL) {
if (debug)
printf("Config file: /etc/vnstat.conf\n");
} else if ((*fd=fopen("/usr/local/etc/vnstat.conf", "r"))!=NULL) {
if (debug)
printf("Config file: /usr/local/etc/vnstat.conf\n");
} else {
if (debug)
printf("Config file: none\n");
return 1;
}
}

return 2;
}
1 change: 1 addition & 0 deletions src/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ void printcfgfile(void);
int loadcfg(const char *cfgfile);
void validatecfg(void);
void defaultcfg(void);
int opencfgfile(const char *cfgfile, FILE **fd);

struct cfgsetting {
const char *name;
Expand Down
53 changes: 6 additions & 47 deletions src/ibw.c
Original file line number Diff line number Diff line change
@@ -1,59 +1,18 @@
#include "common.h"
#include "cfg.h"
#include "ifinfo.h"
#include "ibw.h"

int ibwloadcfg(const char *cfgfile)
{
FILE *fd;
char buffer[512];
int i, tryhome;
int ret;

ifacebw = NULL;

/* clear buffer */
for (i=0; i<512; i++) {
buffer[i] = '\0';
}

/* possible config files: 1) --config 2) $HOME/.vnstatrc 3) /etc/vnstat.conf 4) none */

if (cfgfile[0]!='\0') {

/* try to open given file */
if ((fd=fopen(cfgfile, "r"))!=NULL) {
if (debug)
printf("IBW Config file: --config\n");
} else {
snprintf(errorstring, 512, "Unable to open given ibw config file \"%s\": %s\n", cfgfile, strerror(errno));
printe(PT_Error);
return 0;
}

} else {

if (getenv("HOME")) {
strncpy_nt(buffer, getenv("HOME"), 500);
strcat(buffer, "/.vnstatrc");
tryhome = 1;
} else {
tryhome = 0;
}

/* try to open first available config file */
if (tryhome && (fd=fopen(buffer, "r"))!=NULL) {
if (debug)
printf("IBW Config file: $HOME/.vnstatrc\n");
} else if ((fd=fopen("/etc/vnstat.conf", "r"))!=NULL) {
if (debug)
printf("IBW Config file: /etc/vnstat.conf\n");
} else if ((fd=fopen("/usr/local/etc/vnstat.conf", "r"))!=NULL) {
if (debug)
printf("IBW Config file: /usr/local/etc/vnstat.conf\n");
} else {
if (debug)
printf("IBW Config file: none\n");
return 1;
}
}
ret = opencfgfile(cfgfile, &fd);
if (ret != 2)
return ret;

rewind(fd);
ibwcfgread(fd);
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dbaccess.o: dbaccess.c dbaccess.h common.h
dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.h ibw.h
dbshow.o: dbshow.c dbshow.h misc.h common.h
cfg.o: cfg.c cfg.h common.h
ibw.o: ibw.c ibw.h common.h
ibw.o: ibw.c ibw.h cfg.h ifinfo.h common.h
misc.o: misc.c misc.h common.h
daemon.o: daemon.c daemon.h common.h ifinfo.h dbaccess.h dbcache.h misc.h cfg.h ibw.h

Expand Down

0 comments on commit 2f5ac72

Please sign in to comment.