Table of Content

The rev tool is missing in git/msys, sometimes it is very useful for parse string.

use case

Here is use case: I want to get last field of string since cut command only support nth field.

rev source code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int buffer[2048], ch, lines=0, i=0;
    do
    {
        ch = fgetc(stdin);
        if ((ch == '\n') || 
            (ch == EOF && i > 0) || 
            (i == (sizeof(buffer)/sizeof(*buffer)-1)))
        {
            ++lines;
            while (i != 0)
                fputc(buffer[--i], stdout);
            fputc('\n', stdout);
        }
        else
        {
            buffer[i++] = ch;
        }
    } while (ch != EOF);

    return 0;
}

compile

$ gcc -o rev rev.c 
$ mv rev /usr/bin

example

$ pwd | rev | cut -d/ -f1 | rev