aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-12-23 16:22:16 -0600
committerRob Landley <rob@landley.net>2018-12-23 16:22:16 -0600
commitbb184f1a4e8a27d3b5608bcedbb62e7ca4a200fd (patch)
treecd3e2f29cd1eb3e81eab3d1423d9aa2ed903459a /lib
parent40e162e58dff93bb941340d832bca8e6ce368600 (diff)
downloadtoybox-bb184f1a4e8a27d3b5608bcedbb62e7ca4a200fd.tar.gz
Add grep --color
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.h2
-rw-r--r--lib/xwrap.c37
2 files changed, 31 insertions, 8 deletions
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