aboutsummaryrefslogtreecommitdiff
path: root/toys/other/rev.c
diff options
context:
space:
mode:
Diffstat (limited to 'toys/other/rev.c')
-rw-r--r--toys/other/rev.c26
1 files changed, 12 insertions, 14 deletions
diff --git a/toys/other/rev.c b/toys/other/rev.c
index 15066310..adfc90da 100644
--- a/toys/other/rev.c
+++ b/toys/other/rev.c
@@ -15,27 +15,25 @@ config REV
#include "toys.h"
-static void do_rev(int fd, char *name)
+static void rev_line(char **pline, long len)
{
- char *c;
+ char *line;
+ long i;
- for (;;) {
- unsigned len, i;
+ if (!pline) return;
+ line = *pline;
+ if (len && line[len-1]=='\n') line[--len] = 0;
- if (!(c = get_line(fd))) break;
- len = strlen(c);
- if (len--) for (i = 0; i <= len/2; i++) {
- char tmp = c[i];
+ if (len--) for (i = 0; i <= len/2; i++) {
+ char tmp = line[i];
- c[i] = c[len-i];
- c[len-i] = tmp;
- }
- xputs(c);
- free(c);
+ line[i] = line[len-i];
+ line[len-i] = tmp;
}
+ xputs(line);
}
void rev_main(void)
{
- loopfiles(toys.optargs, do_rev);
+ loopfiles_lines(toys.optargs, rev_line);
}