Skip to content

Commit

Permalink
Simplify btos
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ortmann committed Oct 16, 2023
1 parent 322bddb commit f0c9dc2
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions src/cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -3269,33 +3269,20 @@ static void cmd_traffic(struct userrec *u, int idx, char *par)
putlog(LOG_CMDS, "*", "#%s# traffic", dcc[idx].nick);
}

static char traffictxt[20];
static char *btos(unsigned long bytes)
{
const char *unit;
float xbytes;
static char traffictxt[20];

xbytes = bytes;
if (xbytes > 1024.0) {
unit = "KBytes";
xbytes = xbytes / 1024.0;
}
if (xbytes > 1024.0) {
unit = "MBytes";
xbytes = xbytes / 1024.0;
}
if (xbytes > 1024.0) {
unit = "GBytes";
xbytes = xbytes / 1024.0;
}
if (xbytes > 1024.0) {
unit = "TBytes";
xbytes = xbytes / 1024.0;
}
if (bytes > 1024)
sprintf(traffictxt, "%.2f %s", xbytes, unit);
else
if (bytes <= 1024)
sprintf(traffictxt, "%lu Bytes", bytes);
else if (bytes <= (1024 * 1024))
sprintf(traffictxt, "%.2f KBytes", (float) bytes / 1024);
else if (bytes <= (1024 * 1024 * 1024))
sprintf(traffictxt, "%.2f MBytes", (float) bytes / 1024 / 1024);
else if (bytes <= (1024u * 1024 * 1024 * 1024))
sprintf(traffictxt, "%.2f GBytes", (float) bytes / 1024 / 1024 / 1024);
else if (bytes <= (1024u * 1024 * 1024 * 1024 * 1024))
sprintf(traffictxt, "%.2f TBytes", (float) bytes / 1024 / 1024 / 1024 / 1024);
return traffictxt;
}

Expand Down

0 comments on commit f0c9dc2

Please sign in to comment.