aboutsummaryrefslogtreecommitdiff
path: root/toys/other/rev.c
blob: a95d66156c3a57115c06e2d1cfb97bc38d55d416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* 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 lines reversed, when no files are given stdin is used.
*/

#include "toys.h"

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

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

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