view toys/other/rev.c @ 1189:95ae2805622f draft

Add Szabolcs Nagy's deflate/inflate code from git://git.suckless.org/flate Confirmed with him on IRC it's ok to use under toybox license, glued the files together and hammered square peg into round hole, no other changes yet.
author Rob Landley <rob@landley.net>
date Fri, 31 Jan 2014 06:01:30 -0600
parents c42f64ea297d
children
line wrap: on
line source

/* rev.c - reverse lines of a set of given input files
 *
 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>

USE_REV(NEWTOY(rev, NULL, TOYFLAG_USR|TOYFLAG_BIN))

config REV
  bool "rev"
  default y
  help
    usage: rev [FILE...]

    Output each line reversed, when no files are given stdin is used.
*/

#include "toys.h"

void do_rev(int fd, char *name)
{
  char *c;

  for (;;) {
    int len, i;

    if (!(c = get_line(fd))) break;
    len = strlen(c) - 1;
    for (i = 0; i <= len/2; i++) {
      char tmp = c[i];

      c[i] = c[len-i];
      c[len-i] = tmp;
    }
    xputs(c);
    free(c);
  }
}

void rev_main(void)
{
  loopfiles(toys.optargs, do_rev);
}