login

<     >

2020-04-02 18:19:36 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

first commit - basic percent counter

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..eb5918b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+CFLAGS=-std=c99 -Wall -Wextra
+
+all: mpb
+
+.PHONE: clean
+clean:
+	$(RM) mpb

diff --git a/mpb.c b/mpb.c
new file mode 100644
index 0000000..8ea8b92
--- /dev/null
+++ b/mpb.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <poll.h>
+
+void
+hide_cursor()
+{
+    printf("\x1B[?25l");
+    fflush(stdout);
+}
+
+void
+show_cursor()
+{
+    printf("\x1B[?25h");
+    fflush(stdout);
+}
+
+void
+print_bar(unsigned long percent)
+{
+    printf("\r%3lu%%", percent);
+    fflush(stdout);
+}
+
+int
+main(int argc, char *argv[])
+{
+    int ret = 0;
+    unsigned long count, total, percent;
+    if (argc != 2) {
+        fprintf(stderr, "usage:\n  %s total\n", argv[0]);
+        ret = 1;
+        goto quit;
+    }
+    total = (unsigned) atol(argv[1]);
+    if (total == 0) {
+        fprintf(stderr, "total must be nonzero\n");
+        ret = 1;
+        goto quit;
+    }
+    hide_cursor();
+    print_bar(0);
+    for (count = 0; count < total; count++) {
+        poll(NULL, 0, 25);
+        percent = count * 100 / total;
+        print_bar(percent);
+    }
+    print_bar(100);
+quit:
+    show_cursor();
+    puts("");
+    return ret;
+}