toybox/toys/other/rev.c
<<
>>
Prefs
   1/* rev.c - reverse lines of a set of given input files
   2 *
   3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
   4
   5USE_REV(NEWTOY(rev, NULL, TOYFLAG_USR|TOYFLAG_BIN))
   6
   7config REV
   8  bool "rev"
   9  default y
  10  help
  11    usage: rev [FILE...]
  12
  13    Output each line reversed, when no files are given stdin is used.
  14*/
  15
  16#include "toys.h"
  17
  18static void do_rev(int fd, char *name)
  19{
  20  char *c;
  21
  22  for (;;) {
  23    unsigned len, i;
  24
  25    if (!(c = get_line(fd))) break;
  26    len = strlen(c);
  27    if (len--) for (i = 0; i <= len/2; i++) {
  28      char tmp = c[i];
  29
  30      c[i] = c[len-i];
  31      c[len-i] = tmp;
  32    }
  33    xputs(c);
  34    free(c);
  35  }
  36}
  37
  38void rev_main(void)
  39{
  40  loopfiles(toys.optargs, do_rev);
  41}
  42