commit d9594406a4792febd9307caf04fc3009ab57863e
parent 87b8daac8c038171cf9fc160f15da8511ea91ff7
Author: parazyd <parazyd@dyne.org>
Date: Sat, 3 Mar 2018 01:42:13 +0100
Move humansize to util.c
Diffstat:
4 files changed, 35 insertions(+), 25 deletions(-)
diff --git a/bin/rpsend b/bin/rpsend
@@ -83,7 +83,7 @@ _dest="$(printf "$email" | rpheaders To)"
einfo "sending using msmtp"
einfo "subject: $_subj"
einfo "to: $_dest"
-einfo "sending $(humansize $(printf "$email\n" | wc -c)) over the network"
+einfo "sending $(rputil humansize $(printf "$email\n" | wc -c)) over the network"
printf "$email\n" | \
diff --git a/rplib/common.sh b/rplib/common.sh
@@ -29,23 +29,3 @@ edie() {
printf "${boldred} * ${reset}%s\n" "$*" >&2
exit 1
}
-
-humansize() {
- [ "$1" -gt 1073741824 ] && {
- printf "$(( $1 >> 30 )) GiB"
- return 0
- }
-
- [ "$1" -gt 1048576 ] && {
- printf "$(( $1 >> 20 )) MiB"
- return 0
- }
-
- [ "$1" -gt 1024 ] && {
- printf "$(( $1 >> 10 )) KiB"
- return 0
- }
-
- printf "$1 Bytes"
- return 0
-}
diff --git a/util.c b/util.c
@@ -4,6 +4,7 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <time.h>
@@ -15,10 +16,12 @@ char be[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
void utilusage(void) {
- die("usage: %s [cmd]\n"
+ die("usage: %s [cmd] [args]\n"
" available commands:\n"
- "\t * genmsgid: generates a Message-ID header\n",
- "\t * genmimeb: generates a semi-random MIME boundary\n", "rputil");
+ "\t * genmsgid: generates a Message-ID header\n"
+ "\t * genmimeb: generates a semi-random MIME boundary\n"
+ "\t * humansize: calculates byte size into human readable "
+ "and prints it\n", "rputil");
}
int randint(int max) {
@@ -60,8 +63,28 @@ int genmimeb(void) {
return 0;
}
-int utilmain(int argc, char *argv[]) {
+int humansize(long long amnt) {
+ if (amnt > 1073741824) {
+ printf("%lld GiB", amnt >> 30);
+ return 0;
+ }
+
+ if (amnt > 1048576) {
+ printf("%lld MiB", amnt >> 20);
+ return 0;
+ }
+
+ if (amnt > 1024) {
+ printf("%lld KiB", amnt >> 10);
+ return 0;
+ }
+
+ printf("%lld Bytes", amnt);
+ return 0;
+}
+int utilmain(int argc, char *argv[]) {
+ /* TODO: do these checks in a better way */
if (argc < 2)
utilusage();
@@ -71,6 +94,12 @@ int utilmain(int argc, char *argv[]) {
if (!strncmp("genmimeb", argv[1], 8))
return genmimeb();
+ if (argc < 2)
+ utilusage();
+
+ if (!strncmp("humansize", argv[1], 9))
+ return humansize(atoll(argv[2]));
+
utilusage();
return 1;
}
diff --git a/util.h b/util.h
@@ -11,5 +11,6 @@ int randint(int max);
char *randstring(char *str, size_t size);
int genmsgid(void);
int genmimeb(void);
+int humansize(long long amnt);
#endif