diff options
author | Sami Kerola | 2013-07-28 23:11:21 +0200 |
---|---|---|
committer | Karel Zak | 2013-08-01 13:35:39 +0200 |
commit | 4b4eb34004378fe70259acd8f2f859e7b5cc3726 (patch) | |
tree | 26d869b4a842c7078e0137997e0f02254f50e120 /text-utils/rev.c | |
parent | build-sys: use backticks rather than $() for commands in configure (diff) | |
download | kernel-qcow2-util-linux-4b4eb34004378fe70259acd8f2f859e7b5cc3726.tar.gz kernel-qcow2-util-linux-4b4eb34004378fe70259acd8f2f859e7b5cc3726.tar.xz kernel-qcow2-util-linux-4b4eb34004378fe70259acd8f2f859e7b5cc3726.zip |
rev: use string printing rather than character output
Fliping a string in memory, and printing it with multibyte output
function makes the command about 1/3 quicker.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'text-utils/rev.c')
-rw-r--r-- | text-utils/rev.c | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/text-utils/rev.c b/text-utils/rev.c index 1c440366c..85e16306b 100644 --- a/text-utils/rev.c +++ b/text-utils/rev.c @@ -86,10 +86,20 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } +static void reverse_str(wchar_t *str, size_t n) +{ + size_t i; + + for (i = 0; i < n / 2; ++i) { + wchar_t tmp = str[i]; + str[i] = str[n - 1 - i]; + str[n - 1 - i] = tmp; + } +} + int main(int argc, char *argv[]) { char *filename = "stdin"; - wchar_t *t; size_t len, bufsiz = BUFSIZ; FILE *fp = stdin; int ch, rval = EXIT_SUCCESS; @@ -153,13 +163,8 @@ int main(int argc, char *argv[]) len = wcslen(buf); } - - if (*(t = buf + len - 1) == '\n') - --t; - for ( ; buf <= t; --t) - putwchar(*t); - if (!feof(fp)) - putwchar('\n'); + reverse_str(buf, len - 1); + fputws(buf, stdout); } if (ferror(fp)) { warn("%s", filename); |