static int fireperf_tui_update_status(struct fireperf_tui* tui) {
const struct fireperf_stats* stats = NULL;
+ char buffer[32];
int r;
int max_x = 0;
if (r < 0)
goto ERROR;
+ // Fetch peak bandwidth
size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x);
+ // Format peak bandwidth
+ r = format_size(buffer, peak_bps, FIREPERF_FORMAT_BITS);
+ if (r < 0)
+ goto ERROR;
+
// Print Peak Bandwidth
- r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l,
- format_size(peak_bps, FIREPERF_FORMAT_BITS));
+ r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l, buffer);
if (r < 0)
goto ERROR;
*/
static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
const struct fireperf_stats* stats = NULL;
- char* scale = NULL;
+ char scale[32];
+ int r;
int max_x = 0;
int max_y = 0;
// Draw the grid
for (double line = 0; line <= 1; line += 0.25) {
- scale = format_size(line * peak_bps, FIREPERF_FORMAT_BITS);
- if (!scale)
+ r = format_size(scale, line * peak_bps, FIREPERF_FORMAT_BITS);
+ if (r < 0)
continue;
size_t l = strlen(scale);
// Write the scale
mvwaddnstr(tui->graph, max_y - (max_y * line), max_x - l, scale, l);
- free(scale);
}
wattroff(tui->graph, COLOR_PAIR(5));
# #
#############################################################################*/
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
static const char* suffixes_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL };
-char* format_size(ssize_t size, int unit) {
- const char** suffixes;
+int __format_size(char* buffer, const size_t length, ssize_t size, const int unit) {
+ const char** suffixes = NULL;
unsigned int divisor;
+ int r;
switch (unit) {
case FIREPERF_FORMAT_BYTES:
// Invalid input
default:
- return NULL;
+ return -EINVAL;
}
- const char** suffix;
- char* retval = NULL;
+ const char** suffix = NULL;
// Convert into double
double s = size;
}
if (!*suffix)
- return NULL;
+ return -EINVAL;
// Format the output string
- int r = asprintf(&retval, "%.02f %s", s, *suffix);
+ r = snprintf(buffer, length, "%.02f %s", s, *suffix);
if (r < 0)
- return NULL;
+ return r;
- return retval;
+ return 0;
}
-static char __timespec[20];
-
const char* format_timespec(const struct timespec* t) {
+ static char __thread __timespec[20];
+
// Convert to local time
struct tm* tm = localtime(&t->tv_sec);
FIREPERF_FORMAT_BITS,
};
-char* format_size(ssize_t size, int unit);
+#define format_size(buffer, size, unit) \
+ __format_size(buffer, sizeof(buffer), size, unit)
+
+int __format_size(char* buffer, const size_t length, ssize_t size, const int unit);
+
const char* format_timespec(const struct timespec* t);
double timespec_delta(struct timespec* t1, struct timespec* t2);