END_TEST START_TEST(ibwlist_empty) { suppress_output(); ibwlist(); }
END_TEST START_TEST(ibwlist_filled) { cfg.maxbw = 0; ck_assert_int_eq(ibwadd("name1", 1), 1); ck_assert_int_eq(ibwadd("name2", 2), 1); suppress_output(); ibwlist(); }
int loadcfg(const char *cfgfile) { FILE *fd; char buffer[512]; int i, j, k, linelen, cfglen, tryhome; char value[512], cfgline[512]; struct cfgsetting cset[] = { /* cfg string, char var name, int var name, char len, fill status */ { "Interface", cfg.iface, 0, 32, 0 }, { "DatabaseDir", cfg.dbdir, 0, 512, 0 }, { "Locale", cfg.locale, 0, 32, 0 }, { "MonthRotate", 0, &cfg.monthrotate, 0, 0 }, { "DayFormat", cfg.dformat, 0, 64, 0 }, { "MonthFormat", cfg.mformat, 0, 64, 0 }, { "TopFormat", cfg.tformat, 0, 64, 0 }, { "RXCharacter", cfg.rxchar, 0, 2, 0 }, { "TXCharacter", cfg.txchar, 0, 2, 0 }, { "RXHourCharacter", cfg.rxhourchar, 0, 2, 0 }, { "TXHourCharacter", cfg.txhourchar, 0, 2, 0 }, { "UnitMode", 0, &cfg.unit, 0, 0 }, { "OutputStyle", 0, &cfg.ostyle, 0, 0 }, { "RateUnit", 0, &cfg.rateunit, 0, 0 }, { "MaxBandwidth", 0, &cfg.maxbw, 0, 0 }, { "Sampletime", 0, &cfg.sampletime, 0, 0 }, { "QueryMode", 0, &cfg.qmode, 0, 0 }, { "CheckDiskSpace", 0, &cfg.spacecheck, 0, 0 }, { "UseFileLocking", 0, &cfg.flock, 0, 0 }, { "BootVariation", 0, &cfg.bvar, 0, 0 }, { "TrafficlessDays", 0, &cfg.traflessday, 0, 0 }, { "DaemonUser", cfg.daemonuser, 0, 33, 0 }, { "DaemonGroup", cfg.daemongroup, 0, 33, 0 }, { "UpdateInterval", 0, &cfg.updateinterval, 0, 0 }, { "PollInterval", 0, &cfg.pollinterval, 0, 0 }, { "SaveInterval", 0, &cfg.saveinterval, 0, 0 }, { "OfflineSaveInterval", 0, &cfg.offsaveinterval, 0, 0 }, { "SaveOnStatusChange", 0, &cfg.savestatus, 0, 0 }, { "UseLogging", 0, &cfg.uselogging, 0, 0 }, { "CreateDirs", 0, &cfg.createdirs, 0, 0 }, { "UpdateFileOwner", 0, &cfg.updatefileowner, 0, 0 }, { "LogFile", cfg.logfile, 0, 512, 0 }, { "PidFile", cfg.pidfile, 0, 512, 0 }, { "HeaderFormat", cfg.hformat, 0, 64, 0 }, { "HourlyRate", 0, &cfg.hourlyrate, 0, 0 }, { "SummaryRate", 0, &cfg.summaryrate, 0, 0 }, { "SummaryLayout", 0, &cfg.slayout, 0, 0 }, { "TransparentBg", 0, &cfg.transbg, 0, 0 }, { "CBackground", cfg.cbg, 0, 8, 0 }, { "CEdge", cfg.cedge, 0, 8, 0 }, { "CHeader", cfg.cheader, 0, 8, 0 }, { "CHeaderTitle", cfg.cheadertitle, 0, 8, 0 }, { "CHeaderDate", cfg.cheaderdate, 0, 8, 0 }, { "CText", cfg.ctext, 0, 8, 0 }, { "CLine", cfg.cline, 0, 8, 0 }, { "CLineL", cfg.clinel, 0, 8, 0 }, { "CRx", cfg.crx, 0, 8, 0 }, { "CRxD", cfg.crxd, 0, 8, 0 }, { "CTx", cfg.ctx, 0, 8, 0 }, { "CTxD", cfg.ctxd, 0, 8, 0 }, { 0, 0, 0, 0, 0 } }; ifacebw = NULL; /* 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 (debug) printf("Config file: none\n"); return 1; } } rewind(fd); /* parse every config file line */ while (!feof(fd)) { cfgline[0] = '\0'; /* get current line */ if (fgets(cfgline, 512, fd)==NULL) { break; } linelen = (int)strlen(cfgline); if (linelen>2 && cfgline[0]!='#') { for (i=0; cset[i].name!=0; i++) { if (cset[i].found) { continue; } cfglen = (int)strlen(cset[i].name); if ( (linelen>=(cfglen+2)) && (strncasecmp(cfgline, cset[i].name, cfglen)==0) ) { /* clear value buffer */ for (j=0; j<512; j++) { value[j]='\0'; } /* search value */ j = 0; for (k=cfglen; k<linelen; k++) { if (cfgline[k]=='\n' || cfgline[k]=='\r') { break; } else if (cfgline[k]=='\"') { if (j==0) { continue; } else { break; } } else { if (j==0 && (cfgline[k]==' ' || cfgline[k]=='=' || cfgline[k]=='\t')) { continue; } else { value[j] = cfgline[k]; j++; } } } /* set value and get new line if valid value was found */ if (strlen(value)!=0) { if (cset[i].namelen>0) { strncpy_nt(cset[i].locc, value, cset[i].namelen); cset[i].locc[cset[i].namelen-1]='\0'; if (debug) printf(" c: %s -> \"%s\": \"%s\"\n", cfgline, cset[i].name, cset[i].locc); } else if (isdigit(value[0])) { *cset[i].loci = atoi(value); if (debug) printf(" i: %s -> \"%s\": %d\n", cfgline, cset[i].name, *cset[i].loci); } else { continue; } cset[i].found = 1; break; } else if (strlen(value)==0) { if (debug) printf(" c: %s -> \"%s\" with no value, keeping default.\n", cfgline, cset[i].name); cset[i].found = 1; break; } } /* if */ } /* for */ if ((debug) && (!cset[i].found) && (strncasecmp(cfgline, "MaxBW", 5)!=0)) printf("Unknown configuration line: %s", cfgline); } /* if */ } /* while */ /* search for interface specific limits */ ibwcfgread(fd); fclose(fd); if (debug) ibwlist(); /* validate config */ validatecfg(); return 1; }