commit 28129a87c4db31cd43f0e4289e68587f2a01127e
Author:     Mattias Andrée <maand...@kth.se>
AuthorDate: Sat Mar 26 17:23:03 2016 +0100
Commit:     Laslo Hunhold <d...@frign.de>
CommitDate: Tue Dec 27 11:35:27 2016 +0100

    Add rev(1)
    
    Signed-off-by: Mattias Andrée <maand...@kth.se>

diff --git a/Makefile b/Makefile
index 25bab70..58a92a9 100644
--- a/Makefile
+++ b/Makefile
@@ -137,6 +137,7 @@ BIN =\
        pwd\
        readlink\
        renice\
+       rev\
        rm\
        rmdir\
        sed\
diff --git a/README b/README
index d60d8fc..da2e500 100644
--- a/README
+++ b/README
@@ -66,6 +66,7 @@ The following tools are implemented:
 0=*|o pwd             .
 0=*|x readlink        .
 0=*|o renice          .
+0#* x rev             .
 0=*|o rm              (-i)
 0=*|o rmdir           .
  #    sed             .
diff --git a/rev.1 b/rev.1
new file mode 100644
index 0000000..d30c850
--- /dev/null
+++ b/rev.1
@@ -0,0 +1,22 @@
+.Dd 2016-03-26
+.Dt REV 1
+.Os sbase
+.Sh NAME
+.Nm rev
+.Nd reverse each line
+.Sh SYNOPSIS
+.Nm
+.Op Ar file ...
+.Sh DESCRIPTION
+.Nm
+reads each
+.Ar file
+in sequence and writes it to stdout, but
+with all characters in each line in reverse
+order. If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.Sh SEE ALSO
+.Xr tac 1
diff --git a/rev.c b/rev.c
new file mode 100644
index 0000000..2d89df1
--- /dev/null
+++ b/rev.c
@@ -0,0 +1,74 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "text.h"
+#include "util.h"
+
+static void
+usage(void)
+{
+       eprintf("usage: %s [file ...]\n", argv0);
+}
+
+static void
+rev(FILE *fp)
+{
+       static char *line = NULL;
+       static size_t size = 0;
+       size_t i;
+       ssize_t n;
+       int lf;
+
+       while ((n = getline(&line, &size, fp)) > 0) {
+               lf = n && line[n - 1] == '\n';
+               i = n -= lf;
+               for (n = 0; i--;) {
+                       if ((line[i] & 0xC0) == 0x80) {
+                               n++;
+                       } else {
+                               fwrite(line + i, 1, n + 1, stdout);
+                               n = 0;
+                       }
+               }
+               if (n)
+                       fwrite(line, 1, n, stdout);
+               if (lf)
+                       fputc('\n', stdout);
+       }
+}
+
+int
+main(int argc, char *argv[])
+{
+       FILE *fp;
+       int ret = 0;
+
+       ARGBEGIN {
+       default:
+               usage();
+       } ARGEND
+
+       if (!argc) {
+               rev(stdin);
+       } else {
+               for (; *argv; argc--, argv++) {
+                       if (!strcmp(*argv, "-")) {
+                               *argv = "<stdin>";
+                               fp = stdin;
+                       } else if (!(fp = fopen(*argv, "r"))) {
+                               weprintf("fopen %s:", *argv);
+                               ret = 1;
+                               continue;
+                       }
+                       rev(fp);
+                       if (fp != stdin && fshut(fp, *argv))
+                               ret = 1;
+               }
+       }
+
+       ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
+
+       return ret;
+}

Reply via email to