commit 7c5179b792a8504564131d08692a838c5a019749
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 14 Feb 2023 01:04:51 +0100
Initial version
Diffstat:
A | .gitignore | | | 1 | + |
A | Makefile | | | 19 | +++++++++++++++++++ |
A | revb.c | | | 75 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 95 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+revb
diff --git a/Makefile b/Makefile
@@ -0,0 +1,19 @@
+PREFIX ?= /usr/local
+BINDIR ?= /bin
+
+CFLAGS = -g
+
+all: revb
+
+clean:
+ rm -f revb
+
+revb: revb.c
+
+install:
+ install -m 755 revb -t "$(DESTDIR)$(PREFIX)$(BINDIR)"
+
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)$(BINDIR)/revb"
+
+.PHONY: all clean install uninstall
diff --git a/revb.c b/revb.c
@@ -0,0 +1,75 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define CHUNKSIZE 4096
+
+uint8_t *
+load(FILE *file, uint8_t *data, ssize_t *datalen)
+{
+ ssize_t cap, len;
+ ssize_t nread;
+ int ret;
+
+ len = *datalen;
+ cap = len + CHUNKSIZE;
+ data = realloc(data, cap);
+ if (!data) err(1, "realloc");
+
+ while (1) {
+ if (len + CHUNKSIZE > cap) {
+ cap *= 2;
+ data = realloc(data, cap);
+ if (!data) err(1, "realloc");
+ }
+
+ nread = fread(data + len, 1, CHUNKSIZE, file);
+ if (nread <= 0) break;
+
+ len += nread;
+ }
+
+ *datalen = len;
+
+ return data;
+}
+
+int
+main(int argc, const char **argv)
+{
+ uint8_t *data, tmp;
+ ssize_t a, b, len;
+ FILE *file;
+ int i;
+
+ len = 0;
+ data = NULL;
+ if (argc > 1) {
+ for (i = 1; i < argc; i++) {
+ file = fopen(argv[i], "r");
+ if (!file) err(1, "fopen");
+ data = load(file, data, &len);
+ fclose(file);
+ }
+ } else {
+ data = load(stdin, data, &len);
+ }
+ if (!len) return 0;
+
+ a = 0;
+ b = len - 1;
+ while (a < b) {
+ tmp = data[a];
+ data[a] = data[b];
+ data[b] = tmp;
+ a++; b--;
+ }
+
+ if (!fwrite(data, len, 1, stdout))
+ err(1, "fwrite");
+
+ free(data);
+}