diff --git a/Makefile b/Makefile index 9d969d3..50db65a 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ LIBUTFSRC =\ libutf/utf.c\ libutf/chartorunearr.c\ libutf/fgetrune.c\ - libutf/writerune.c\ + libutf/fputrune.c\ libutf/isalnumrune.c\ libutf/isalpharune.c\ libutf/isblankrune.c\ diff --git a/expand.c b/expand.c index 8adaa24..c643df7 100644 --- a/expand.c +++ b/expand.c @@ -72,7 +72,7 @@ expand(const char *file, FILE *fp) col++; if (r != ' ') bol = 0; - writerune("", stdout, &r); + efputrune(&r, stdout, ""); break; } } diff --git a/libutf/fputrune.c b/libutf/fputrune.c new file mode 100644 index 0000000..6a393b5 --- /dev/null +++ b/libutf/fputrune.c @@ -0,0 +1,27 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include + +#include "../utf.h" + +int +fputrune(const Rune *r, FILE *fp) +{ + char buf[UTFmax]; + + return fwrite(buf, runetochar(buf, r), 1, fp); +} + +int +efputrune(const Rune *r, FILE *fp, const char *file) +{ + int ret; + + if ((ret = fputrune(r, fp)) < 0) { + fprintf(stderr, "fputrune %s: %s\n", file, strerror(errno)); + exit(1); + } + return ret; +} diff --git a/libutf/writerune.c b/libutf/writerune.c deleted file mode 100644 index eb9cfac..0000000 --- a/libutf/writerune.c +++ /dev/null @@ -1,23 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include -#include -#include -#include - -#include "../utf.h" - -void -writerune(const char *file, FILE *fp, Rune *r) -{ - char buf[UTFmax]; - int n; - - if ((n = runetochar(buf, r)) > 0) { - fwrite(buf, n, 1, fp); - if (ferror(fp)) { - fprintf(stderr, "%s: write error: %s\n", - file, strerror(errno)); - exit(1); - } - } -} diff --git a/paste.c b/paste.c index 32832d7..cd7eb3e 100644 --- a/paste.c +++ b/paste.c @@ -26,17 +26,17 @@ sequential(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen) while (efgetrune(&c, dsc[i].fp, dsc[i].name)) { if (last == '\n') { if (delim[d] != '\0') - writerune("", stdout, &delim[d]); + efputrune(&delim[d], stdout, ""); d = (d + 1) % delimlen; } if (c != '\n') - writerune("", stdout, &c); + efputrune(&c, stdout, ""); last = c; } if (last == '\n') - writerune("", stdout, &last); + efputrune(&last, stdout, ""); } } @@ -56,22 +56,22 @@ nextline: for (; efgetrune(&c, dsc[i].fp, dsc[i].name) ;) { for (m = last + 1; m < i; m++) - writerune("", stdout, &(delim[m % delimlen])); + efputrune(&(delim[m % delimlen]), stdout, ""); last = i; if (c == '\n') { if (i != fdescrlen - 1) c = d; - writerune("", stdout, &c); + efputrune(&c, stdout, ""); break; } - writerune("", stdout, &c); + efputrune(&c, stdout, ""); } if (c == 0 && last != -1) { if (i == fdescrlen - 1) putchar('\n'); else - writerune("", stdout, &d); + efputrune(&d, stdout, ""); last++; } } diff --git a/tail.c b/tail.c index 8b8dc38..9f7eca3 100644 --- a/tail.c +++ b/tail.c @@ -63,7 +63,7 @@ taketail(FILE *fp, const char *str) fputs(ring[j], stdout); free(ring[j]); } else if (r) { - writerune("", stdout, &r[j]); + efputrune(&r[j], stdout, ""); } } while ((j = (j + 1) % num) != i); diff --git a/tr.c b/tr.c index 2d6d37b..fa3d412 100644 --- a/tr.c +++ b/tr.c @@ -271,6 +271,6 @@ read: goto read; write: lastrune = r; - writerune("", stdout, &r); + efputrune(&r, stdout, ""); goto read; } diff --git a/unexpand.c b/unexpand.c index 0b16f74..17852b6 100644 --- a/unexpand.c +++ b/unexpand.c @@ -44,10 +44,10 @@ unexpandspan(size_t last, size_t col) r = '\t'; for (; last + tablist[i] <= col; last += tablist[i]) - writerune("", stdout, &r); + efputrune(&r, stdout, ""); r = ' '; for (; last < col; last++) - writerune("", stdout, &r); + efputrune(&r, stdout, ""); } else { for (i = 0; i < tablistlen; i++) if (col < tablist[i]) @@ -57,12 +57,12 @@ unexpandspan(size_t last, size_t col) break; r = '\t'; for (; j < i; j++) { - writerune("", stdout, &r); + efputrune(&r, stdout, ""); last = tablist[j]; } r = ' '; for (; last < col; last++) - writerune("", stdout, &r); + efputrune(&r, stdout, ""); } } @@ -115,7 +115,7 @@ unexpand(const char *file, FILE *fp) break; } if ((r != ' ' && r != '\t') || (!aflag && !bol)) - writerune("", stdout, &r); + efputrune(&r, stdout, ""); } if (last < col && (bol || aflag)) unexpandspan(last, col); diff --git a/utf.h b/utf.h index bd58f0a..203849a 100644 --- a/utf.h +++ b/utf.h @@ -61,5 +61,7 @@ Rune toupperrune(Rune); int fgetrune(Rune *, FILE *); int efgetrune(Rune *, FILE *, const char *); -void writerune(const char *, FILE *, Rune *); +int fputrune(const Rune *, FILE *); +int efputrune(const Rune *, FILE *, const char *); + int chartorunearr(const char*, Rune **);