logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: c21e7d0aaeb3fa47cf7ed0bef5c61f14efc9957a
parent 36500620827964b007ed258c3b4afb3d443b1aed
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 13 May 2021 23:08:21 +0200

bin/humanize: fixes + manpage

Diffstat:

Abin/humanize.127+++++++++++++++++++++++++++
Mbin/humanize.c33+++++++++++++++++++++------------
2 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/bin/humanize.1 b/bin/humanize.1 @@ -0,0 +1,27 @@ +.Dd 2021-05-13 +.Dt HUMANIZE 1 +.Os +.Sh NAME +.Nm humanize +.Nd format numbers into human readable form +.Sh SYNOPSIS +.Nm +.Op Fl db +.Ar number ... +.Sh DESCRIPTION +Takes each +.Ar number +and format it in a human readable form. +It supports the following options: +.Bl -tag -width Ds +.It Fl b +Divide by 1024, typically used for bytes. +.It Fl d +Divide by 1000, this is the default but made explicit in case of changes without breaking scripts. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr humanize_number 3 +.Sh AUTHORS +.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me diff --git a/bin/humanize.c b/bin/humanize.c @@ -2,12 +2,13 @@ // strtonum #define _OPENBSD_SOURCE // On non-BSDs use libbsd -#include <err.h> // errx, +#include <err.h> // errx #include <errno.h> // EINVAL, ERANGE +#include <limits.h> // LLONG_MIN, LLONG_MAX #include <stdio.h> // fprintf #include <stdlib.h> // humanize_number, strtonum +#include <string.h> // strerror #include <unistd.h> // opt*, getopt -#include <limits.h> // LLONG_MIN, LLONG_MAX void usage() @@ -19,14 +20,15 @@ int main(int argc, char *argv[]) { int c; - int flags = HN_DIVISOR_1000|HN_NOSPACE; + int flags = HN_NOSPACE | HN_DECIMAL; + int divisor = HN_DIVISOR_1000; - while((c = getopt(argc, argv, "d")) != -1) + while((c = getopt(argc, argv, "bd")) != -1) { switch(c) { - case 'd': flags &= HN_DIVISOR_1000; break; - case 'b': flags &= ~HN_DIVISOR_1000; break; + case 'b': divisor = ~HN_DIVISOR_1000; break; + case 'd': divisor = HN_DIVISOR_1000; break; case '?': fprintf(stderr, "Error: Unrecognised option: '-%c'\n", optopt); usage(); @@ -36,8 +38,8 @@ main(int argc, char *argv[]) for(; optind < argc; optind++) { - const char *errstr = NULL; - char buffer[6] = "-"; + const char *errstr = NULL; + char buffer[6] = "-"; // 1024^(6+1) goes higher than long long; don't ask for the moon long long n = strtonum(argv[optind], LLONG_MIN, LLONG_MAX, &errstr); @@ -46,14 +48,21 @@ main(int argc, char *argv[]) errx(1, "strtonum: %s is %s", argv[optind], errstr); } - int err = humanize_number(buffer, sizeof(buffer), n, "", HN_AUTOSCALE, flags); + int err = humanize_number(buffer, sizeof(buffer), n, "", HN_AUTOSCALE, flags | divisor); if(err < 0) { switch(errno) { - case EINVAL: fprintf(stderr, "Number carried an unknown suffix.\n"); return 1; - case ERANGE: fprintf(stderr, "Represents a number that does not fit in result.\n"); return 1; - default: errx(1, "humanize_number: %s is %s", argv[optind], errstr); + case EINVAL: + fprintf(stderr, "humanize_number: Number carried an unknown suffix.\n"); + return 1; + case ERANGE: + fprintf(stderr, "humanize_number: Represents a number that does not fit in result.\n"); + return 1; + case 0: fprintf(stderr, "humanize_number: Unknown error.\n"); return 1; + default: + fprintf(stderr, "humanize_number: %s is %s\n", argv[optind], strerror(errno)); + return 1; } }