diff options
author | Elliott Hughes <enh@google.com> | 2019-07-19 09:48:04 -0700 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-07-22 16:42:07 -0500 |
commit | b30674681b9d72430a029ba7bd3e16a3139244f0 (patch) | |
tree | f4b9b66e5c1537ea2a73d606f6b8aa3fe9e102c2 /toys/other | |
parent | 43d398ad5d7b14fb344fc2e5338177761b9a199a (diff) | |
download | toybox-b30674681b9d72430a029ba7bd3e16a3139244f0.tar.gz |
Start replacing get_line() with getline().
I started this last night, but thought I'd aim to send multiple small
patches rather than work through all the callers and send one big patch.
I've deliberately chosen the ugly name `allocated_length` because we've
had historical bugs where folks think this a line length in the sense of
the return value. I do wonder whether we should actually have some kind
of getline() wrapper that hides the `char *`/`size_t` pair in lib/,
which makes the function easier to use in most cases but does add the
less common gotcha that you wouldn't be able to getline() through
multiple files at once (which does happen in at least one toy).
But maybe the real fix is to look harder for places where we can just
use loopfiles_lines? Speaking of which, should we actually add two more
arguments to that? Specifically: switch it to getdelim() rather than
getline() behind the scenes, and also add a way to have the trailing
'\n' automatically removed, since that seems to be what most callers
want?
Anyway, that seemed like enough questions that it was time to send this
initial patch out before doing too much more...
Diffstat (limited to 'toys/other')
-rw-r--r-- | toys/other/rev.c | 26 |
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); } |