• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* rev.c - reverse lines of a set of given input files
2  *
3  * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
4 
5 USE_REV(NEWTOY(rev, NULL, TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config 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 
rev_line(char ** pline,long len)18 static void rev_line(char **pline, long len)
19 {
20   char *line;
21   long i;
22 
23   if (!pline) return;
24   line = *pline;
25   if (len && line[len-1]=='\n') line[--len] = 0;
26 
27   if (len--) for (i = 0; i <= len/2; i++) {
28     char tmp = line[i];
29 
30     line[i] = line[len-i];
31     line[len-i] = tmp;
32   }
33   xputs(line);
34 }
35 
rev_main(void)36 void rev_main(void)
37 {
38   loopfiles_lines(toys.optargs, rev_line);
39 }
40