From c4d75bef5c6504790058652416c47477c60c37f5 Mon Sep 17 00:00:00 2001 From: Francois Tigeot Date: Sun, 27 Nov 2011 11:41:08 +0100 Subject: [PATCH] vquota(8): add a -h flag * humanize numbers where it makes sense --- sbin/vquota/Makefile | 2 +- sbin/vquota/vquota.8 | 13 +++++++++ sbin/vquota/vquota.c | 67 +++++++++++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/sbin/vquota/Makefile b/sbin/vquota/Makefile index 62e1124404..550bedef51 100644 --- a/sbin/vquota/Makefile +++ b/sbin/vquota/Makefile @@ -1,6 +1,6 @@ PROG= vquota SRCS= vquota.c -LDADD= -lprop +LDADD= -lprop -lutil MAN= vquota.8 .include diff --git a/sbin/vquota/vquota.8 b/sbin/vquota/vquota.8 index 69e0d1e283..e199315457 100644 --- a/sbin/vquota/vquota.8 +++ b/sbin/vquota/vquota.8 @@ -34,6 +34,9 @@ . .Sh SYNOPSIS .Nm +.Oo +.Fl D | h +.Oc .Ar command .Ar argument .Sh DESCRIPTION @@ -42,6 +45,16 @@ This manual page documents the utility which provides functions for managing virtual file system accounting and quotas. .Pp +The following options are available: +.Bl -tag -width Ds +.It Fl D +Debug flag, show raw messages sent to and received from kernel +.It Fl h +"Human-readable" output. Use unit suffixes: Byte, Kilobyte, Megabyte, +Gigabyte, Terabyte and Petabyte in order to reduce the number of +digits to four or fewer. +.El +.Pp The commands are as follows: .Bl -tag -width indent .\" ==== check ==== diff --git a/sbin/vquota/vquota.c b/sbin/vquota/vquota.c index 5e13b02097..fa5c7561f2 100644 --- a/sbin/vquota/vquota.c +++ b/sbin/vquota/vquota.c @@ -44,8 +44,11 @@ #include #include #include +#include +#include static bool flag_debug = 0; +static bool flag_humanize = 0; static void usage(int); static int get_dirsize(char *); @@ -54,9 +57,9 @@ static int get_fslist(void); static void usage(int retcode) { - fprintf(stderr, "usage: vquota [-D] check directory\n"); - fprintf(stderr, " vquota [-D] lsfs\n"); - fprintf(stderr, " vquota [-D] show mount_point\n"); + fprintf(stderr, "usage: vquota [-Dh] check directory\n"); + fprintf(stderr, " vquota [-Dh] lsfs\n"); + fprintf(stderr, " vquota [-Dh] show mount_point\n"); exit(retcode); } @@ -257,6 +260,8 @@ get_dirsize(char* dirname) struct ac_unode *unp, ufind; struct ac_gnode *gnp, gfind; int i; + char hbuf[5]; + uint32_t uid, gid; /* TODO: check directory name sanity */ fts_args[0] = dirname; @@ -304,25 +309,40 @@ get_dirsize(char* dirname) } fts_close(fts); - printf("total: %"PRIu64"\n", global_size); + if (flag_humanize) { + humanize_number(hbuf, sizeof(hbuf), global_size, "", + HN_AUTOSCALE, HN_NOSPACE); + printf("total: %s\n", hbuf); + } else { + printf("total: %"PRIu64"\n", global_size); + } RB_FOREACH(unp, ac_utree, &ac_uroot) { - for (i=0; iuid_chunk[i] != 0) { - printf("uid %"PRIu32": %"PRIu64"\n", - (unp->left_bits << ACCT_CHUNK_BITS) + i, - unp->uid_chunk[i]); - } - + for (i=0; iuid_chunk[i] != 0) { + uid = (unp->left_bits << ACCT_CHUNK_BITS) + i; + if (flag_humanize) { + humanize_number(hbuf, sizeof(hbuf), + unp->uid_chunk[i], "", HN_AUTOSCALE, HN_NOSPACE); + printf("uid %"PRIu32": %s\n", uid, hbuf); + } else { + printf("uid %"PRIu32": %"PRIu64"\n", uid, unp->uid_chunk[i]); + } } + } } RB_FOREACH(gnp, ac_gtree, &ac_groot) { - for (i=0; igid_chunk[i] != 0) { - printf("gid %"PRIu32": %"PRIu64"\n", - (gnp->left_bits << ACCT_CHUNK_BITS) + i, - gnp->gid_chunk[i]); - } + for (i=0; igid_chunk[i] != 0) { + gid = (gnp->left_bits << ACCT_CHUNK_BITS) + i; + if (flag_humanize) { + humanize_number(hbuf, sizeof(hbuf), + gnp->gid_chunk[i], "", HN_AUTOSCALE, HN_NOSPACE); + printf("gid %"PRIu32": %s\n", gid, hbuf); + } else { + printf("gid %"PRIu32": %"PRIu64"\n", gid, gnp->gid_chunk[i]); + } } + } } return retval; @@ -422,6 +442,7 @@ show_mp(char *path) prop_dictionary_t item; uint32_t id; uint64_t space; + char hbuf[5]; args = prop_dictionary_create(); res = prop_dictionary_create(); @@ -456,7 +477,12 @@ show_mp(char *path) printf("gid %u:", id); else printf("total space used"); - printf(" %"PRIu64"\n", space); + if (flag_humanize) { + humanize_number(hbuf, sizeof(hbuf), space, "", HN_AUTOSCALE, HN_NOSPACE); + printf(" %s\n", hbuf); + } else { + printf(" %" PRIu64 "\n", space); + } } prop_object_iterator_release(iter); @@ -471,11 +497,14 @@ main(int argc, char **argv) { int ch; - while ((ch = getopt(argc, argv, "D")) != -1) { + while ((ch = getopt(argc, argv, "Dh")) != -1) { switch(ch) { case 'D': flag_debug = 1; break; + case 'h': + flag_humanize = 1; + break; } } argc -= optind; -- 2.41.0