aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-04-16 16:53:27 -0500
committerRob Landley <rob@landley.net>2019-04-16 16:53:27 -0500
commit2a1f89e5d941a77e8c93ad0a5fe78229a4207d61 (patch)
treef54c6364e70eafb604bff6e078728e6faf66a57d
parent63a0e7afff271ac1b1df3309bbf35f52e4771419 (diff)
downloadtoybox-2a1f89e5d941a77e8c93ad0a5fe78229a4207d61.tar.gz
Add argument to xflush() so it can test for stdout err without flushing.
-rw-r--r--lib/lib.h2
-rw-r--r--lib/xwrap.c16
-rw-r--r--toys/other/hexedit.c2
-rw-r--r--toys/pending/arping.c1
-rw-r--r--toys/pending/vi.c4
-rw-r--r--toys/posix/echo.c4
-rw-r--r--toys/posix/head.c1
7 files changed, 14 insertions, 16 deletions
diff --git a/lib/lib.h b/lib/lib.h
index 0f15484e..6ec2c696 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -129,7 +129,7 @@ void xputsl(char *s, int len);
void xputsn(char *s);
void xputs(char *s);
void xputc(char c);
-void xflush(void);
+void xflush(int flush);
void xexec(char **argv);
pid_t xpopen_both(char **argv, int *pipes);
int xwaitpid(pid_t pid);
diff --git a/lib/xwrap.c b/lib/xwrap.c
index 12170ada..a242cd53 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -54,7 +54,7 @@ void xexit(void)
free(al);
}
- if (fflush(0) || ferror(stdout)) if (!toys.exitval) perror_msg("write");
+ xflush(1);
_xexit();
}
@@ -139,9 +139,11 @@ char *xmprintf(char *format, ...)
return ret;
}
-void xflush(void)
+// if !flush just check for error on stdout without flushing
+void xflush(int flush)
{
- if (fflush(stdout) || ferror(stdout)) perror_exit("write");
+ if ((flush && fflush(0)) || ferror(stdout))
+ if (!toys.exitval) perror_msg("write");
}
void xprintf(char *format, ...)
@@ -151,7 +153,7 @@ void xprintf(char *format, ...)
vprintf(format, va);
va_end(va);
- xflush();
+ xflush(0);
}
// Put string with length (does not append newline)
@@ -164,7 +166,7 @@ void xputsl(char *s, int len)
len -= out;
s += out;
}
- xflush();
+ xflush(0);
}
// xputs with no newline
@@ -177,13 +179,13 @@ void xputsn(char *s)
void xputs(char *s)
{
puts(s);
- xflush();
+ xflush(0);
}
void xputc(char c)
{
if (EOF == fputc(c, stdout)) perror_exit("write");
- xflush();
+ xflush(0);
}
// This is called through the XVFORK macro because parent/child of vfork
diff --git a/toys/other/hexedit.c b/toys/other/hexedit.c
index 06a96a22..809340a1 100644
--- a/toys/other/hexedit.c
+++ b/toys/other/hexedit.c
@@ -180,7 +180,7 @@ void hexedit_main(void)
// Display cursor and flush output
highlight(x, y, ro ? 3 : side);
- xflush();
+ xflush(1);
// Wait for next key
key = scan_key(keybuf, -1);
diff --git a/toys/pending/arping.c b/toys/pending/arping.c
index be43cab1..6007845b 100644
--- a/toys/pending/arping.c
+++ b/toys/pending/arping.c
@@ -166,7 +166,6 @@ static void recv_from(struct sockaddr_ll *from, int *recv_len)
gettimeofday(&tval, NULL);
delta = (tval.tv_sec * 1000000ULL + (tval.tv_usec)) - TT.sent_at;
xprintf(" %u.%03ums\n", delta / 1000, delta % 1000);
- xflush();
}
}
TT.rcvd_nr++;
diff --git a/toys/pending/vi.c b/toys/pending/vi.c
index 31516b55..2beedf49 100644
--- a/toys/pending/vi.c
+++ b/toys/pending/vi.c
@@ -543,7 +543,7 @@ void vi_main(void)
//we dont get scroll log full of junk
tty_esc("?1049h");
tty_esc("H");
- xflush();
+ xflush(1);
draw_page();
while(1) {
key = scan_key(keybuf, -1);
@@ -821,7 +821,7 @@ static void draw_page()
tty_esc("0m");
} else tty_jump(cx_scr, cy_scr);
- xflush();
+ xflush(1);
}
diff --git a/toys/posix/echo.c b/toys/posix/echo.c
index 639f87ab..63eb1981 100644
--- a/toys/posix/echo.c
+++ b/toys/posix/echo.c
@@ -65,7 +65,7 @@ void echo_main(void)
int slash = *(c++), n = unescape(slash);
if (n) out = n;
- else if (slash=='c') goto done;
+ else if (slash=='c') return;
else if (slash=='0') {
out = 0;
while (*c>='0' && *c<='7' && n++<3) out = (out*8)+*(c++)-'0';
@@ -90,6 +90,4 @@ void echo_main(void)
// Output "\n" if no -n
if (!(toys.optflags&FLAG_n)) putchar('\n');
-done:
- xflush();
}
diff --git a/toys/posix/head.c b/toys/posix/head.c
index 7e34a714..38ab919e 100644
--- a/toys/posix/head.c
+++ b/toys/posix/head.c
@@ -40,7 +40,6 @@ static void do_head(int fd, char *name)
// Print an extra newline for all but the first file
if (TT.file_no) xprintf("\n");
xprintf("==> %s <==\n", name);
- xflush();
}
while ((toys.optflags&FLAG_c) ? bytes : lines) {