From: Teemu Toivola Date: Wed, 6 Mar 2019 21:14:41 +0000 (+0200) Subject: refactoring X-Git-Tag: v2.2~23 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1bac4ff0f0a313e775bc6667a6b148d91e7bfc45;p=vnstat refactoring --- diff --git a/src/common.c b/src/common.c index 7728f92..128a447 100644 --- a/src/common.c +++ b/src/common.c @@ -12,7 +12,7 @@ int pidfile; int disableprints; -int printe(PrintType type) +int printe(const PrintType type) { int result = 1; @@ -66,7 +66,7 @@ int printe(PrintType type) return result; } -int logprint(PrintType type) +int logprint(const PrintType type) { /* buffer needs some extra space for timestamp + infor compared to errorstring */ char timestamp[22], buffer[1060]; @@ -157,7 +157,7 @@ int verifylogaccess(void) return 1; } -int dmonth(int month) +int dmonth(const int month) { static int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int year; @@ -177,7 +177,7 @@ int dmonth(int month) } } -int isleapyear(int year) +int isleapyear(const int year) { if (year % 4 != 0) { return 0; diff --git a/src/common.h b/src/common.h index 7ee5726..27bec18 100644 --- a/src/common.h +++ b/src/common.h @@ -309,11 +309,11 @@ typedef enum PrintType { } PrintType; /* common functions */ -int printe(PrintType type); -int logprint(PrintType type); +int printe(const PrintType type); +int logprint(const PrintType type); int verifylogaccess(void); -int dmonth(int month); -int isleapyear(int year); +int dmonth(const int month); +int isleapyear(const int year); time_t mosecs(time_t month, time_t updated); uint64_t countercalc(const uint64_t *a, const uint64_t *b, const short is64bit); char *strncpy_nt(char *dest, const char *src, size_t n); diff --git a/src/dbshow.c b/src/dbshow.c index e5acc3f..008e6e9 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -106,11 +106,11 @@ void showsummary(const interfaceinfo *interface, const int shortmode) printf(" since %s\n\n", datebuff); indent(10); - printf("rx: %s", getvalue(interface->rxtotal, 1, 1)); + printf("rx: %s", getvalue(interface->rxtotal, 1, RT_Normal)); indent(3); - printf(" tx: %s", getvalue(interface->txtotal, 1, 1)); + printf(" tx: %s", getvalue(interface->txtotal, 1, RT_Normal)); indent(3); - printf(" total: %s\n\n", getvalue(interface->rxtotal+interface->txtotal, 1, 1)); + printf(" total: %s\n\n", getvalue(interface->rxtotal+interface->txtotal, 1, RT_Normal)); indent(3); printf("monthly\n"); @@ -138,12 +138,12 @@ void showsummary(const interfaceinfo *interface, const int shortmode) indent(5); d = localtime(&datalist_i->timestamp); if (strftime(datebuff, DATEBUFFLEN, cfg.mformat, d)<=8) { - printf("%*s %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1)); + printf("%*s %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, RT_Normal)); } else { - printf("%-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1)); + printf("%-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, RT_Normal)); } - printf("%s%s", fieldseparator, getvalue(datalist_i->tx, 11, 1)); - printf("%s%s", fieldseparator, getvalue(datalist_i->rx+datalist_i->tx, 11, 1)); + printf("%s%s", fieldseparator, getvalue(datalist_i->tx, 11, RT_Normal)); + printf("%s%s", fieldseparator, getvalue(datalist_i->rx+datalist_i->tx, 11, RT_Normal)); if (cfg.ostyle >= 2) { if (datalist_i->next == NULL) { if ( datalist_i->rx == 0 || datalist_i->tx == 0 || (interface->updated-datalist_i->timestamp) == 0 ) { @@ -153,7 +153,7 @@ void showsummary(const interfaceinfo *interface, const int shortmode) e_tx = (uint64_t)(datalist_i->tx/(float)(mosecs(datalist_i->timestamp, interface->updated)))*(uint64_t)(dmonth(d->tm_mon)*86400); } if (shortmode && cfg.ostyle != 0) { - printf("%s%s", fieldseparator, getvalue(e_rx+e_tx, 11, 2)); + printf("%s%s", fieldseparator, getvalue(e_rx+e_tx, 11, RT_Estimate)); } else if (!shortmode) { printf("%s%s", fieldseparator, gettrafficrate(datalist_i->rx+datalist_i->tx, mosecs(datalist_i->timestamp, interface->updated), 14)); } @@ -173,9 +173,9 @@ void showsummary(const interfaceinfo *interface, const int shortmode) printf("------------------------+-------------+------------\n"); } indent(5); - printf("estimated %s", getvalue(e_rx, 11, 2)); - printf(" | %s", getvalue(e_tx, 11, 2)); - printf(" | %s", getvalue(e_rx+e_tx, 11, 2)); + printf("estimated %s", getvalue(e_rx, 11, RT_Estimate)); + printf(" | %s", getvalue(e_tx, 11, RT_Estimate)); + printf(" | %s", getvalue(e_rx+e_tx, 11, RT_Estimate)); if (cfg.ostyle >= 2) { printf(" |\n\n"); } else { @@ -225,12 +225,12 @@ void showsummary(const interfaceinfo *interface, const int shortmode) snprintf(datebuff, DATEBUFFLEN, "yesterday"); } if (strlen(datebuff) <= 8) { - printf("%*s %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1)); + printf("%*s %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, RT_Normal)); } else { - printf("%-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1)); + printf("%-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, RT_Normal)); } - printf("%s%s", fieldseparator, getvalue(datalist_i->tx, 11, 1)); - printf("%s%s", fieldseparator, getvalue(datalist_i->rx+datalist_i->tx, 11, 1)); + printf("%s%s", fieldseparator, getvalue(datalist_i->tx, 11, RT_Normal)); + printf("%s%s", fieldseparator, getvalue(datalist_i->rx+datalist_i->tx, 11, RT_Normal)); if (cfg.ostyle >= 2) { if (datalist_i->next == NULL) { d = localtime(&interface->updated); @@ -241,7 +241,7 @@ void showsummary(const interfaceinfo *interface, const int shortmode) e_tx = (uint64_t)((datalist_i->tx)/(float)(d->tm_hour*60+d->tm_min))*1440; } if (shortmode && cfg.ostyle != 0) { - printf("%s%s", fieldseparator, getvalue(e_rx+e_tx, 11, 2)); + printf("%s%s", fieldseparator, getvalue(e_rx+e_tx, 11, RT_Estimate)); } else if (!shortmode) { printf("%s%s", fieldseparator, gettrafficrate(datalist_i->rx+datalist_i->tx, d->tm_sec+(d->tm_min*60)+(d->tm_hour*3600), 14)); } @@ -261,9 +261,9 @@ void showsummary(const interfaceinfo *interface, const int shortmode) printf("------------------------+-------------+------------\n"); } indent(5); - printf("estimated %s", getvalue(e_rx, 11, 2)); - printf(" | %s", getvalue(e_tx, 11, 2)); - printf(" | %s", getvalue(e_rx+e_tx, 11, 2)); + printf("estimated %s", getvalue(e_rx, 11, RT_Estimate)); + printf(" | %s", getvalue(e_tx, 11, RT_Estimate)); + printf(" | %s", getvalue(e_rx+e_tx, 11, RT_Estimate)); if (cfg.ostyle >= 2) { printf(" |\n"); } else { @@ -437,12 +437,12 @@ void showlist(const interfaceinfo *interface, const char *listname, const char * } if (strlen(datebuff)<=9 && listtype != 4) { - printf(" %*s %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1)); + printf(" %*s %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, RT_Normal)); } else { - printf(" %-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1)); + printf(" %-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, RT_Normal)); } - printf(" | %s", getvalue(datalist_i->tx, 11, 1)); - printf(" | %s", getvalue(datalist_i->rx+datalist_i->tx, 11, 1)); + printf(" | %s", getvalue(datalist_i->tx, 11, RT_Normal)); + printf(" | %s", getvalue(datalist_i->rx+datalist_i->tx, 11, RT_Normal)); if (cfg.ostyle == 3) { if (datalist_i->next == NULL) { d = localtime(&interface->updated); @@ -539,18 +539,18 @@ void showlist(const interfaceinfo *interface, const char *listname, const char * printf(" "); } if (strlen(dataend) == 0) { - printf(" estimated %s", getvalue(e_rx, 11, 2)); - printf(" | %s", getvalue(e_tx, 11, 2)); - printf(" | %s", getvalue(e_rx + e_tx, 11, 2)); + printf(" estimated %s", getvalue(e_rx, 11, RT_Estimate)); + printf(" | %s", getvalue(e_tx, 11, RT_Estimate)); + printf(" | %s", getvalue(e_rx + e_tx, 11, RT_Estimate)); } else { if (datainfo.count < 100) { snprintf(datebuff, DATEBUFFLEN, "sum of %"PRIu32"", datainfo.count); } else { snprintf(datebuff, DATEBUFFLEN, "sum"); } - printf(" %9s %s", datebuff, getvalue(datainfo.sumrx, 11, 1)); - printf(" | %s", getvalue(datainfo.sumtx, 11, 1)); - printf(" | %s", getvalue(datainfo.sumrx + datainfo.sumtx, 11, 1)); + printf(" %9s %s", datebuff, getvalue(datainfo.sumrx, 11, RT_Normal)); + printf(" | %s", getvalue(datainfo.sumtx, 11, RT_Normal)); + printf(" | %s", getvalue(datainfo.sumrx + datainfo.sumtx, 11, RT_Normal)); } if (cfg.ostyle == 3) { printf(" |"); @@ -608,9 +608,9 @@ void showoneline(const interfaceinfo *interface) } printf("%"PRIu64";", (datalist->rx+datalist->tx)/div); } else { - printf("%s;", getvalue(datalist->rx, 1, 1)); - printf("%s;", getvalue(datalist->tx, 1, 1)); - printf("%s;", getvalue(datalist->rx+datalist->tx, 1, 1)); + printf("%s;", getvalue(datalist->rx, 1, RT_Normal)); + printf("%s;", getvalue(datalist->tx, 1, RT_Normal)); + printf("%s;", getvalue(datalist->rx+datalist->tx, 1, RT_Normal)); printf("%s;", gettrafficrate(datalist->rx+datalist->tx, (time_t)(d->tm_sec+(d->tm_min*60)+(d->tm_hour*3600)), 1)); } } else { @@ -639,9 +639,9 @@ void showoneline(const interfaceinfo *interface) } printf("%"PRIu64";", (datalist->rx+datalist->tx)/div); } else { - printf("%s;", getvalue(datalist->rx, 1, 1)); - printf("%s;", getvalue(datalist->tx, 1, 1)); - printf("%s;", getvalue(datalist->rx+datalist->tx, 1, 1)); + printf("%s;", getvalue(datalist->rx, 1, RT_Normal)); + printf("%s;", getvalue(datalist->tx, 1, RT_Normal)); + printf("%s;", getvalue(datalist->rx+datalist->tx, 1, RT_Normal)); printf("%s;", gettrafficrate(datalist->rx+datalist->tx, mosecs(datalist->timestamp, interface->updated), 1)); } } else { @@ -655,9 +655,9 @@ void showoneline(const interfaceinfo *interface) printf("%"PRIu64";", interface->txtotal); printf("%"PRIu64"\n", interface->rxtotal+interface->txtotal); } else { - printf("%s;", getvalue(interface->rxtotal, 1, 1)); - printf("%s;", getvalue(interface->txtotal, 1, 1)); - printf("%s\n", getvalue(interface->rxtotal+interface->txtotal, 1, 1)); + printf("%s;", getvalue(interface->rxtotal, 1, RT_Normal)); + printf("%s;", getvalue(interface->txtotal, 1, RT_Normal)); + printf("%s\n", getvalue(interface->rxtotal+interface->txtotal, 1, RT_Normal)); } timeused(__func__, 0); } diff --git a/src/image.c b/src/image.c index 15dbb01..402c28c 100644 --- a/src/image.c +++ b/src/image.c @@ -672,11 +672,11 @@ void drawlist(IMAGECONTENT *ic, const char *listname) snprintf(buffer, 32, " %-*s ", getpadding(11, datebuff), datebuff); } } - strncat(buffer, getvalue(datalist_i->rx, 10, 1), 32); + strncat(buffer, getvalue(datalist_i->rx, 10, RT_Normal), 32); strcat(buffer, " "); - strncat(buffer, getvalue(datalist_i->tx, 10, 1), 32); + strncat(buffer, getvalue(datalist_i->tx, 10, RT_Normal), 32); strcat(buffer, " "); - strncat(buffer, getvalue(datalist_i->rx+datalist_i->tx, 10, 1), 32); + strncat(buffer, getvalue(datalist_i->rx+datalist_i->tx, 10, RT_Normal), 32); if (cfg.ostyle>2) { strcat(buffer, " "); if (datalist_i->next == NULL) { @@ -773,11 +773,11 @@ void drawlist(IMAGECONTENT *ic, const char *listname) } if (strlen(ic->dataend) == 0) { snprintf(buffer, 32, " estimated "); - strncat(buffer, getvalue(e_rx, 10, 2), 32); + strncat(buffer, getvalue(e_rx, 10, RT_Estimate), 32); strcat(buffer, " "); - strncat(buffer, getvalue(e_tx, 10, 2), 32); + strncat(buffer, getvalue(e_tx, 10, RT_Estimate), 32); strcat(buffer, " "); - strncat(buffer, getvalue(e_rx+e_tx, 10, 2), 32); + strncat(buffer, getvalue(e_rx+e_tx, 10, RT_Estimate), 32); } else { if (datainfo.count < 100) { snprintf(datebuff, 16, "sum of %"PRIu32"", datainfo.count); @@ -785,11 +785,11 @@ void drawlist(IMAGECONTENT *ic, const char *listname) snprintf(datebuff, 16, "sum"); } snprintf(buffer, 32, " %9s ", datebuff); - strncat(buffer, getvalue(datainfo.sumrx, 10, 2), 32); + strncat(buffer, getvalue(datainfo.sumrx, 10, RT_Normal), 32); strcat(buffer, " "); - strncat(buffer, getvalue(datainfo.sumtx, 10, 2), 32); + strncat(buffer, getvalue(datainfo.sumtx, 10, RT_Normal), 32); strcat(buffer, " "); - strncat(buffer, getvalue(datainfo.sumrx + datainfo.sumtx, 10, 2), 32); + strncat(buffer, getvalue(datainfo.sumrx + datainfo.sumtx, 10, RT_Normal), 32); } gdImageString(ic->im, gdFontGetSmall(), textx, texty+8, (unsigned char*)buffer, ic->ctext); @@ -912,13 +912,13 @@ void drawsummary(IMAGECONTENT *ic, int type, int rate) } snprintf(buffer, 4, "rx "); - strncat(buffer, getvalue(data_current->rx, 12, 1), 32); + strncat(buffer, getvalue(data_current->rx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+18, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, "tx "); - strncat(buffer, getvalue(data_current->tx, 12, 1), 32); + strncat(buffer, getvalue(data_current->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+30, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, " = "); - strncat(buffer, getvalue(data_current->rx+data_current->tx, 12, 1), 32); + strncat(buffer, getvalue(data_current->rx+data_current->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+44, (unsigned char*)buffer, ic->ctext); /* yesterday */ @@ -966,13 +966,13 @@ void drawsummary(IMAGECONTENT *ic, int type, int rate) } snprintf(buffer, 4, "rx "); - strncat(buffer, getvalue(data_previous->rx, 12, 1), 32); + strncat(buffer, getvalue(data_previous->rx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+18, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, "tx "); - strncat(buffer, getvalue(data_previous->tx, 12, 1), 32); + strncat(buffer, getvalue(data_previous->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+30, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, " = "); - strncat(buffer, getvalue(data_previous->rx+data_previous->tx, 12, 1), 32); + strncat(buffer, getvalue(data_previous->rx+data_previous->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+44, (unsigned char*)buffer, ic->ctext); } @@ -1033,13 +1033,13 @@ void drawsummary(IMAGECONTENT *ic, int type, int rate) } snprintf(buffer, 4, "rx "); - strncat(buffer, getvalue(data_current->rx, 12, 1), 32); + strncat(buffer, getvalue(data_current->rx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+18, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, "tx "); - strncat(buffer, getvalue(data_current->tx, 12, 1), 32); + strncat(buffer, getvalue(data_current->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+30, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, " = "); - strncat(buffer, getvalue(data_current->rx+data_current->tx, 12, 1), 32); + strncat(buffer, getvalue(data_current->rx+data_current->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+44, (unsigned char*)buffer, ic->ctext); /* previous month */ @@ -1077,13 +1077,13 @@ void drawsummary(IMAGECONTENT *ic, int type, int rate) } snprintf(buffer, 4, "rx "); - strncat(buffer, getvalue(data_previous->rx, 12, 1), 32); + strncat(buffer, getvalue(data_previous->rx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+18, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, "tx "); - strncat(buffer, getvalue(data_previous->tx, 12, 1), 32); + strncat(buffer, getvalue(data_previous->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+30, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, " = "); - strncat(buffer, getvalue(data_previous->rx+data_previous->tx, 12, 1), 32); + strncat(buffer, getvalue(data_previous->rx+data_previous->tx, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx-74, texty+44, (unsigned char*)buffer, ic->ctext); } @@ -1097,13 +1097,13 @@ void drawsummary(IMAGECONTENT *ic, int type, int rate) gdImageString(ic->im, gdFontGetLarge(), textx+12, texty, (unsigned char*)"all time", ic->ctext); snprintf(buffer, 4, "rx "); - strncat(buffer, getvalue(ic->interface.rxtotal, 12, 1), 32); + strncat(buffer, getvalue(ic->interface.rxtotal, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx, texty+24, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, "tx "); - strncat(buffer, getvalue(ic->interface.txtotal, 12, 1), 32); + strncat(buffer, getvalue(ic->interface.txtotal, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx, texty+36, (unsigned char*)buffer, ic->ctext); snprintf(buffer, 4, " = "); - strncat(buffer, getvalue(ic->interface.rxtotal+ic->interface.txtotal, 12, 1), 32); + strncat(buffer, getvalue(ic->interface.rxtotal+ic->interface.txtotal, 12, RT_Normal), 32); gdImageString(ic->im, gdFontGetSmall(), textx, texty+50, (unsigned char*)buffer, ic->ctext); d = localtime(&ic->interface.created); strftime(datebuff, 16, cfg.tformat, d); diff --git a/src/misc.c b/src/misc.c index aaf5dbd..f5348cc 100644 --- a/src/misc.c +++ b/src/misc.c @@ -145,14 +145,13 @@ uint64_t getbtime(void) return result; } -char *getvalue(const uint64_t bytes, const int len, const int type) +char *getvalue(const uint64_t bytes, const int len, const RequestType type) { static char buffer[64]; int i, declen = cfg.defaultdecimals, p = 1024; uint64_t limit; - /* request types: 1) normal 2) estimate 3) image scale */ - if (type == 3) { + if (type == RT_ImageScale) { declen = 0; } @@ -160,7 +159,7 @@ char *getvalue(const uint64_t bytes, const int len, const int type) p = 1000; } - if ( (type == 2) && (bytes == 0) ){ + if ( (type == RT_Estimate) && (bytes == 0) ) { declen = len-(int)strlen(getunitprefix(2))-2; if (declen < 2) { declen = 2; @@ -173,7 +172,7 @@ char *getvalue(const uint64_t bytes, const int len, const int type) if (i>1) { snprintf(buffer, 64, "%"DECCONV"*.*f %s", getunitspacing(len, 5), declen, bytes/(double)(getunitdivisor(cfg.unitmode, i+1)), getunitprefix(i+1)); } else { - if (type == 2) { + if (type == RT_Estimate) { declen = 0; } snprintf(buffer, 64, "%"DECCONV"*.*f %s", getunitspacing(len, 2), declen, bytes/(double)(getunitdivisor(cfg.unitmode, i+1)), getunitprefix(i+1)); diff --git a/src/misc.h b/src/misc.h index cbf1a8d..cf388d0 100644 --- a/src/misc.h +++ b/src/misc.h @@ -3,10 +3,16 @@ #define UNITPREFIXCOUNT 7 +typedef enum RequestType { + RT_Normal = 1, + RT_Estimate, + RT_ImageScale +} RequestType; + int spacecheck(const char *path); void sighandler(int sig); uint64_t getbtime(void); -char *getvalue(const uint64_t bytes, const int len, const int type); +char *getvalue(const uint64_t bytes, const int len, const RequestType type); int getunitspacing(const int len, const int index); char *gettrafficrate(const uint64_t bytes, const time_t interval, const int len); const char *getunitprefix(const int index); diff --git a/src/traffic.c b/src/traffic.c index be80f6e..d5a125b 100644 --- a/src/traffic.c +++ b/src/traffic.c @@ -221,8 +221,8 @@ void livetrafficmeter(char iface[32], int mode) snprintf(buffer2, 128, " %*s tx: %s %*"PRIu64" p/s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), ppswidth, (uint64_t)txp/LIVETIME); } else { /* total transfer amount visible */ - snprintf(buffer, 128, " rx: %s %s", gettrafficrate(rx, LIVETIME, ratewidth), getvalue(rxtotal, 1, 1)); - snprintf(buffer2, 128, " %*s tx: %s %s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), getvalue(txtotal, 1, 1)); + snprintf(buffer, 128, " rx: %s %s", gettrafficrate(rx, LIVETIME, ratewidth), getvalue(rxtotal, 1, RT_Normal)); + snprintf(buffer2, 128, " %*s tx: %s %s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), getvalue(txtotal, 1, RT_Normal)); } strcat(buffer, buffer2); @@ -275,8 +275,8 @@ void livetrafficmeter(char iface[32], int mode) printf(" rx | tx\n"); printf("--------------------------------------+------------------\n"); - printf(" bytes %s", getvalue(rxtotal, 15, 1)); - printf(" | %s", getvalue(txtotal, 15, 1)); + printf(" bytes %s", getvalue(rxtotal, 15, RT_Normal)); + printf(" | %s", getvalue(txtotal, 15, RT_Normal)); printf("\n"); printf("--------------------------------------+------------------\n"); printf(" max %s", gettrafficrate(rxmax, LIVETIME, 15)); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 8f02462..eb3c802 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -84,29 +84,29 @@ START_TEST(getvalue_normal) { cfg.defaultdecimals = 2; cfg.unitmode = 0; - ck_assert_str_eq(getvalue(100, 0, 1), "100 B"); - ck_assert_str_eq(getvalue(1024, 0, 1), "1.00 KiB"); - ck_assert_str_eq(getvalue(1048576, 0, 1), "1.00 MiB"); - ck_assert_str_eq(getvalue(1073741824, 0, 1), "1.00 GiB"); - ck_assert_str_eq(getvalue(1099511627776ULL, 0, 1), "1.00 TiB"); - ck_assert_str_eq(getvalue(1125899906842624ULL, 0, 1), "1.00 PiB"); - ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, 1), "1.00 EiB"); + ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B"); + ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.00 KiB"); + ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.00 MiB"); + ck_assert_str_eq(getvalue(1073741824, 0, RT_Normal), "1.00 GiB"); + ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Normal), "1.00 TiB"); + ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Normal), "1.00 PiB"); + ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Normal), "1.00 EiB"); cfg.unitmode = 1; - ck_assert_str_eq(getvalue(100, 0, 1), "100 B"); - ck_assert_str_eq(getvalue(1024, 0, 1), "1.00 KB"); - ck_assert_str_eq(getvalue(1048576, 0, 1), "1.00 MB"); - ck_assert_str_eq(getvalue(1073741824, 0, 1), "1.00 GB"); - ck_assert_str_eq(getvalue(1099511627776ULL, 0, 1), "1.00 TB"); - ck_assert_str_eq(getvalue(1125899906842624ULL, 0, 1), "1.00 PB"); - ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, 1), "1.00 EB"); + ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B"); + ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.00 KB"); + ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.00 MB"); + ck_assert_str_eq(getvalue(1073741824, 0, RT_Normal), "1.00 GB"); + ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Normal), "1.00 TB"); + ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Normal), "1.00 PB"); + ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Normal), "1.00 EB"); cfg.unitmode = 2; - ck_assert_str_eq(getvalue(100, 0, 1), "100 B"); - ck_assert_str_eq(getvalue(1000, 0, 1), "1.00 kB"); - ck_assert_str_eq(getvalue(1000000, 0, 1), "1.00 MB"); - ck_assert_str_eq(getvalue(1000000000, 0, 1), "1.00 GB"); - ck_assert_str_eq(getvalue(1000000000000ULL, 0, 1), "1.00 TB"); - ck_assert_str_eq(getvalue(1000000000000000ULL, 0, 1), "1.00 PB"); - ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, 1), "1.00 EB"); + ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B"); + ck_assert_str_eq(getvalue(1000, 0, RT_Normal), "1.00 kB"); + ck_assert_str_eq(getvalue(1000000, 0, RT_Normal), "1.00 MB"); + ck_assert_str_eq(getvalue(1000000000, 0, RT_Normal), "1.00 GB"); + ck_assert_str_eq(getvalue(1000000000000ULL, 0, RT_Normal), "1.00 TB"); + ck_assert_str_eq(getvalue(1000000000000000ULL, 0, RT_Normal), "1.00 PB"); + ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, RT_Normal), "1.00 EB"); } END_TEST @@ -114,29 +114,29 @@ START_TEST(getvalue_estimate) { cfg.defaultdecimals = 2; cfg.unitmode = 0; - ck_assert_str_eq(getvalue(100, 0, 2), "100 B"); - ck_assert_str_eq(getvalue(1024, 0, 2), "1 KiB"); - ck_assert_str_eq(getvalue(1048576, 0, 2), "1.00 MiB"); - ck_assert_str_eq(getvalue(1073741824, 0, 2), "1.00 GiB"); - ck_assert_str_eq(getvalue(1099511627776ULL, 0, 2), "1.00 TiB"); - ck_assert_str_eq(getvalue(1125899906842624ULL, 0, 2), "1.00 PiB"); - ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, 2), "1.00 EiB"); + ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B"); + ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KiB"); + ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.00 MiB"); + ck_assert_str_eq(getvalue(1073741824, 0, RT_Estimate), "1.00 GiB"); + ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Estimate), "1.00 TiB"); + ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Estimate), "1.00 PiB"); + ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Estimate), "1.00 EiB"); cfg.unitmode = 1; - ck_assert_str_eq(getvalue(100, 0, 2), "100 B"); - ck_assert_str_eq(getvalue(1024, 0, 2), "1 KB"); - ck_assert_str_eq(getvalue(1048576, 0, 2), "1.00 MB"); - ck_assert_str_eq(getvalue(1073741824, 0, 2), "1.00 GB"); - ck_assert_str_eq(getvalue(1099511627776ULL, 0, 2), "1.00 TB"); - ck_assert_str_eq(getvalue(1125899906842624ULL, 0, 2), "1.00 PB"); - ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, 2), "1.00 EB"); + ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B"); + ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KB"); + ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.00 MB"); + ck_assert_str_eq(getvalue(1073741824, 0, RT_Estimate), "1.00 GB"); + ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Estimate), "1.00 TB"); + ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Estimate), "1.00 PB"); + ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Estimate), "1.00 EB"); cfg.unitmode = 2; - ck_assert_str_eq(getvalue(100, 0, 2), "100 B"); - ck_assert_str_eq(getvalue(1000, 0, 2), "1 kB"); - ck_assert_str_eq(getvalue(1000000, 0, 2), "1.00 MB"); - ck_assert_str_eq(getvalue(1000000000, 0, 2), "1.00 GB"); - ck_assert_str_eq(getvalue(1000000000000ULL, 0, 2), "1.00 TB"); - ck_assert_str_eq(getvalue(1000000000000000ULL, 0, 2), "1.00 PB"); - ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, 2), "1.00 EB"); + ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B"); + ck_assert_str_eq(getvalue(1000, 0, RT_Estimate), "1 kB"); + ck_assert_str_eq(getvalue(1000000, 0, RT_Estimate), "1.00 MB"); + ck_assert_str_eq(getvalue(1000000000, 0, RT_Estimate), "1.00 GB"); + ck_assert_str_eq(getvalue(1000000000000ULL, 0, RT_Estimate), "1.00 TB"); + ck_assert_str_eq(getvalue(1000000000000000ULL, 0, RT_Estimate), "1.00 PB"); + ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, RT_Estimate), "1.00 EB"); } END_TEST @@ -144,29 +144,29 @@ START_TEST(getvalue_imagescale) { cfg.defaultdecimals = 2; cfg.unitmode = 0; - ck_assert_str_eq(getvalue(100, 0, 3), "100 B"); - ck_assert_str_eq(getvalue(1024, 0, 3), "1 KiB"); - ck_assert_str_eq(getvalue(1048576, 0, 3), "1 MiB"); - ck_assert_str_eq(getvalue(1073741824, 0, 3), "1 GiB"); - ck_assert_str_eq(getvalue(1099511627776ULL, 0,3), "1 TiB"); - ck_assert_str_eq(getvalue(1125899906842624ULL, 0, 3), "1 PiB"); - ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, 3), "1 EiB"); + ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B"); + ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KiB"); + ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MiB"); + ck_assert_str_eq(getvalue(1073741824, 0, RT_ImageScale), "1 GiB"); + ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_ImageScale), "1 TiB"); + ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_ImageScale), "1 PiB"); + ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_ImageScale), "1 EiB"); cfg.unitmode = 1; - ck_assert_str_eq(getvalue(100, 0, 3), "100 B"); - ck_assert_str_eq(getvalue(1024, 0, 3), "1 KB"); - ck_assert_str_eq(getvalue(1048576, 0, 3), "1 MB"); - ck_assert_str_eq(getvalue(1073741824, 0, 3), "1 GB"); - ck_assert_str_eq(getvalue(1099511627776ULL, 0, 3), "1 TB"); - ck_assert_str_eq(getvalue(1125899906842624ULL, 0, 3), "1 PB"); - ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, 3), "1 EB"); + ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B"); + ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KB"); + ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MB"); + ck_assert_str_eq(getvalue(1073741824, 0, RT_ImageScale), "1 GB"); + ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_ImageScale), "1 TB"); + ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_ImageScale), "1 PB"); + ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_ImageScale), "1 EB"); cfg.unitmode = 2; - ck_assert_str_eq(getvalue(100, 0, 3), "100 B"); - ck_assert_str_eq(getvalue(1000, 0, 3), "1 kB"); - ck_assert_str_eq(getvalue(1000000, 0, 3), "1 MB"); - ck_assert_str_eq(getvalue(1000000000, 0, 3), "1 GB"); - ck_assert_str_eq(getvalue(1000000000000ULL, 0, 3), "1 TB"); - ck_assert_str_eq(getvalue(1000000000000000ULL, 0, 3), "1 PB"); - ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, 3), "1 EB"); + ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B"); + ck_assert_str_eq(getvalue(1000, 0, RT_ImageScale), "1 kB"); + ck_assert_str_eq(getvalue(1000000, 0, RT_ImageScale), "1 MB"); + ck_assert_str_eq(getvalue(1000000000, 0, RT_ImageScale), "1 GB"); + ck_assert_str_eq(getvalue(1000000000000ULL, 0, RT_ImageScale), "1 TB"); + ck_assert_str_eq(getvalue(1000000000000000ULL, 0, RT_ImageScale), "1 PB"); + ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, RT_ImageScale), "1 EB"); } END_TEST @@ -174,34 +174,34 @@ START_TEST(getvalue_padding) { cfg.defaultdecimals = 2; cfg.unitmode = 0; - ck_assert_str_eq(getvalue(1024, 10, 1), " 1.00 KiB"); + ck_assert_str_eq(getvalue(1024, 10, RT_Normal), " 1.00 KiB"); cfg.unitmode = 1; - ck_assert_str_eq(getvalue(1024, 10, 1), " 1.00 KB"); + ck_assert_str_eq(getvalue(1024, 10, RT_Normal), " 1.00 KB"); cfg.unitmode = 2; - ck_assert_str_eq(getvalue(1000, 10, 1), " 1.00 kB"); + ck_assert_str_eq(getvalue(1000, 10, RT_Normal), " 1.00 kB"); } END_TEST START_TEST(getvalue_zero_values) { cfg.unitmode = 0; - ck_assert_str_eq(getvalue(0, 0, 1), "0 B"); - ck_assert_str_eq(getvalue(0, 10, 2), " -- "); - ck_assert_int_eq((int)strlen(getvalue(0, 10, 2)), 10); - ck_assert_int_eq((int)strlen(getvalue(0, 20, 2)), 20); - ck_assert_str_eq(getvalue(0, 0, 3), "0 B"); + ck_assert_str_eq(getvalue(0, 0, RT_Normal), "0 B"); + ck_assert_str_eq(getvalue(0, 10, RT_Estimate), " -- "); + ck_assert_int_eq((int)strlen(getvalue(0, 10, RT_Estimate)), 10); + ck_assert_int_eq((int)strlen(getvalue(0, 20, RT_Estimate)), 20); + ck_assert_str_eq(getvalue(0, 0, RT_ImageScale), "0 B"); cfg.unitmode = 1; - ck_assert_str_eq(getvalue(0, 0, 1), "0 B"); - ck_assert_str_eq(getvalue(0, 10, 2), " -- "); - ck_assert_int_eq((int)strlen(getvalue(0, 10, 2)), 10); - ck_assert_int_eq((int)strlen(getvalue(0, 20, 2)), 20); - ck_assert_str_eq(getvalue(0, 0, 3), "0 B"); + ck_assert_str_eq(getvalue(0, 0, RT_Normal), "0 B"); + ck_assert_str_eq(getvalue(0, 10, RT_Estimate), " -- "); + ck_assert_int_eq((int)strlen(getvalue(0, 10, RT_Estimate)), 10); + ck_assert_int_eq((int)strlen(getvalue(0, 20, RT_Estimate)), 20); + ck_assert_str_eq(getvalue(0, 0, RT_ImageScale), "0 B"); cfg.unitmode = 2; - ck_assert_str_eq(getvalue(0, 0, 1), "0 B"); - ck_assert_str_eq(getvalue(0, 10, 2), " -- "); - ck_assert_int_eq((int)strlen(getvalue(0, 10, 2)), 10); - ck_assert_int_eq((int)strlen(getvalue(0, 20, 2)), 20); - ck_assert_str_eq(getvalue(0, 0, 3), "0 B"); + ck_assert_str_eq(getvalue(0, 0, RT_Normal), "0 B"); + ck_assert_str_eq(getvalue(0, 10, RT_Estimate), " -- "); + ck_assert_int_eq((int)strlen(getvalue(0, 10, RT_Estimate)), 10); + ck_assert_int_eq((int)strlen(getvalue(0, 20, RT_Estimate)), 20); + ck_assert_str_eq(getvalue(0, 0, RT_ImageScale), "0 B"); } END_TEST