diff options
author | Elie De Brauwer <eliedebrauwer@gmail.com> | 2012-12-16 12:31:15 +0100 |
---|---|---|
committer | Elie De Brauwer <eliedebrauwer@gmail.com> | 2012-12-16 12:31:15 +0100 |
commit | fadbd6078081afe62cf120f9346b1d69aa79d486 (patch) | |
tree | dc9a439143bdf8128dc7cb1892244ece6497abbe /toys/other | |
parent | e9e10b58c2ea156e59c78642d5a88756e3649d56 (diff) | |
download | toybox-fadbd6078081afe62cf120f9346b1d69aa79d486.tar.gz |
Adding implementation of rev including tests
Diffstat (limited to 'toys/other')
-rw-r--r-- | toys/other/rev.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/toys/other/rev.c b/toys/other/rev.c new file mode 100644 index 00000000..a95d6615 --- /dev/null +++ b/toys/other/rev.c @@ -0,0 +1,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); +} |