Mercurial > hg > toybox
comparison toys/other/rev.c @ 753:7d258f3c13dc
Adding implementation of rev including tests
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sun, 16 Dec 2012 12:31:15 +0100 |
parents | |
children | 06c32155cb95 |
comparison
equal
deleted
inserted
replaced
752:d020ee45c227 | 753:7d258f3c13dc |
---|---|
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 lines reversed, when no files are given stdin is used. | |
14 */ | |
15 | |
16 #include "toys.h" | |
17 | |
18 void do_rev(int fd, char *name) | |
19 { | |
20 char *c; | |
21 | |
22 for (;;) { | |
23 int len; | |
24 int i; | |
25 if (!(c = get_line(fd))) break; | |
26 len = strlen(c) - 1; | |
27 i = 0; | |
28 while ( i <= len/2) | |
29 { | |
30 char tmp; | |
31 tmp = c[i]; | |
32 c[i] = c[len-i]; | |
33 c[len-i] = tmp; | |
34 i++; | |
35 } | |
36 xputs(c); | |
37 free(c); | |
38 } | |
39 } | |
40 | |
41 void rev_main(void) | |
42 { | |
43 loopfiles(toys.optargs, do_rev); | |
44 } |