From bb184f1a4e8a27d3b5608bcedbb62e7ca4a200fd Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 23 Dec 2018 16:22:16 -0600 Subject: Add grep --color --- lib/lib.h | 2 ++ lib/xwrap.c | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/lib.h b/lib/lib.h index 5d806508..10308ff6 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -125,6 +125,8 @@ char *xstrdup(char *s); void *xmemdup(void *s, long len); char *xmprintf(char *format, ...) printf_format; void xprintf(char *format, ...) printf_format; +void xputsl(char *s, int len); +void xputsn(char *s); void xputs(char *s); void xputc(char c); void xflush(void); diff --git a/lib/xwrap.c b/lib/xwrap.c index b2416a4e..7e602d5f 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -141,6 +141,11 @@ char *xmprintf(char *format, ...) return ret; } +void xflush(void) +{ + if (fflush(0) || ferror(stdout)) perror_exit("write"); +} + void xprintf(char *format, ...) { va_list va; @@ -148,23 +153,39 @@ void xprintf(char *format, ...) vprintf(format, va); va_end(va); - if (fflush(stdout) || ferror(stdout)) perror_exit("write"); + xflush(); } -void xputs(char *s) +// Put string with length (does not append newline) +void xputsl(char *s, int len) +{ + int out; + + while (len != (out = fwrite(s, 1, len, stdout))) { + if (out<1) perror_exit("write"); + len -= out; + s += out; + } + xflush(); +} + +// xputs with no newline +void xputsn(char *s) { - if (EOF == puts(s) || fflush(stdout) || ferror(stdout)) perror_exit("write"); + xputsl(s, strlen(s)); } -void xputc(char c) +// Write string to stdout with newline, flushing and checking for errors +void xputs(char *s) { - if (EOF == fputc(c, stdout) || fflush(stdout) || ferror(stdout)) - perror_exit("write"); + puts(s); + xflush(); } -void xflush(void) +void xputc(char c) { - if (fflush(stdout) || ferror(stdout)) perror_exit("write");; + if (EOF == fputc(c, stdout)) perror_exit("write"); + xflush(); } // This is called through the XVFORK macro because parent/child of vfork -- cgit v1.2.3