[hackers] [sbase[PATCH] numfmt: very minimal implementation

From: ilmich <ardutu_AT_gmail.com>
Date: Tue, 20 Nov 2018 22:53:56 +0100

---
 Makefile |  1 +
 numfmt.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 numfmt.c
diff --git a/Makefile b/Makefile
index 0e421e7..ae3d640 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -130,6 +130,7 @@ BIN =\
 	nice\
 	nl\
 	nohup\
+	numfmt\
 	od\
 	paste\
 	pathchk\
diff --git a/numfmt.c b/numfmt.c
new file mode 100644
index 0000000..b44e344
--- /dev/null
+++ b/numfmt.c
_AT_@ -0,0 +1,48 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+
+static void
+usage(void)
+{
+	eprintf("usage: %s [--to=iec] number \n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+	int ret = 0;
+	char *mode;
+
+	ARGBEGIN {
+	case '-': /* long options */
+		if ( strncmp(argv[0], "-to=", 4) == 0 )
+		{
+		    mode = &argv[0][4];
+		    brk_=1; /* shift to next arg*/
+		    break;
+		}
+	default:
+		usage();
+	} ARGEND
+
+	if (!argc) {
+		usage();
+    } else {
+        for (; argc ; argv++, argc--) {
+			if (mode) {
+				if (strcmp(mode,"iec") == 0) {
+					printf("%s\n", humansize(strtol(*argv,NULL,10)));
+					continue;
+				}
+				eprintf("for now only iec output format is supported\n");
+			} else {
+			/* print  numbers as is */
+				printf("%s\n", *argv);
+			}		
+		}
+	}
+	return ret;
+}
-- 
2.14.5
Received on Tue Nov 20 2018 - 22:53:56 CET

This archive was generated by hypermail 2.3.0 : Tue Nov 20 2018 - 23:00:24 CET