sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 7c578bf5b0ea8b79bea201f0eaf9bbc958b9efa3
parent a5ae899a48709d1afaa297470322364e4c5b1b0e
Author: FRIGN <dev@frign.de>
Date:   Wed, 11 Feb 2015 20:58:00 +0100

Scrap writerune(), introducing fputrune()

Interface and function as proposed by cls.
Code is also shorter, everything else analogous to fgetrune().

Diffstat:
MMakefile | 2+-
Mexpand.c | 2+-
Alibutf/fputrune.c | 27+++++++++++++++++++++++++++
Dlibutf/writerune.c | 23-----------------------
Mpaste.c | 14+++++++-------
Mtail.c | 2+-
Mtr.c | 2+-
Munexpand.c | 10+++++-----
Mutf.h | 4+++-
9 files changed, 46 insertions(+), 40 deletions(-)

diff --git 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 @@ -72,7 +72,7 @@ expand(const char *file, FILE *fp) col++; if (r != ' ') bol = 0; - writerune("<stdout>", stdout, &r); + efputrune(&r, stdout, "<stdout>"); break; } } diff --git a/libutf/fputrune.c b/libutf/fputrune.c @@ -0,0 +1,27 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#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 @@ -1,23 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <errno.h> -#include <stdio.h> -#include <string.h> -#include <stdlib.h> - -#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 @@ -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>", stdout, &delim[d]); + efputrune(&delim[d], stdout, "<stdout>"); d = (d + 1) % delimlen; } if (c != '\n') - writerune("<stdout>", stdout, &c); + efputrune(&c, stdout, "<stdout>"); last = c; } if (last == '\n') - writerune("<stdout>", stdout, &last); + efputrune(&last, stdout, "<stdout>"); } } @@ -56,22 +56,22 @@ nextline: for (; efgetrune(&c, dsc[i].fp, dsc[i].name) ;) { for (m = last + 1; m < i; m++) - writerune("<stdout>", stdout, &(delim[m % delimlen])); + efputrune(&(delim[m % delimlen]), stdout, "<stdout>"); last = i; if (c == '\n') { if (i != fdescrlen - 1) c = d; - writerune("<stdout>", stdout, &c); + efputrune(&c, stdout, "<stdout>"); break; } - writerune("<stdout>", stdout, &c); + efputrune(&c, stdout, "<stdout>"); } if (c == 0 && last != -1) { if (i == fdescrlen - 1) putchar('\n'); else - writerune("<stdout>", stdout, &d); + efputrune(&d, stdout, "<stdout>"); last++; } } diff --git 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>", stdout, &r[j]); + efputrune(&r[j], stdout, "<stdout>"); } } while ((j = (j + 1) % num) != i); diff --git a/tr.c b/tr.c @@ -271,6 +271,6 @@ read: goto read; write: lastrune = r; - writerune("<stdout>", stdout, &r); + efputrune(&r, stdout, "<stdout>"); goto read; } diff --git 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>", stdout, &r); + efputrune(&r, stdout, "<stdout>"); r = ' '; for (; last < col; last++) - writerune("<stdout>", stdout, &r); + efputrune(&r, stdout, "<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>", stdout, &r); + efputrune(&r, stdout, "<stdout>"); last = tablist[j]; } r = ' '; for (; last < col; last++) - writerune("<stdout>", stdout, &r); + efputrune(&r, stdout, "<stdout>"); } } @@ -115,7 +115,7 @@ unexpand(const char *file, FILE *fp) break; } if ((r != ' ' && r != '\t') || (!aflag && !bol)) - writerune("<stdout>", stdout, &r); + efputrune(&r, stdout, "<stdout>"); } if (last < col && (bol || aflag)) unexpandspan(last, col); diff --git 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 **);