sh: Detect and flag write errors on stdout in builtins.

If there is a write error on stdout, a message will be printed (to stderr)
and the exit status will be changed to 2 if it would have been 0 or 1.

PR:		bin/158206
This commit is contained in:
Jilles Tjoelker 2012-12-12 22:01:10 +00:00
parent 9d93dba489
commit d6d66cfc00
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=244162
4 changed files with 25 additions and 0 deletions

View File

@ -1070,6 +1070,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
}
handler = &jmploc;
redirect(cmd->ncmd.redirect, mode);
outclearerror(out1);
/*
* If there is no command word, redirection errors should
* not be fatal but assignment errors should.
@ -1085,6 +1086,11 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
builtin_flags = flags;
exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
flushall();
if (outiserror(out1)) {
warning("write error on stdout");
if (exitstatus == 0 || exitstatus == 1)
exitstatus = 2;
}
cmddone:
if (argc > 0)
bltinunsetlocale();

View File

@ -239,6 +239,20 @@ freestdout(void)
}
int
outiserror(struct output *file)
{
return (file->flags & OUTPUT_ERR);
}
void
outclearerror(struct output *file)
{
file->flags &= ~OUTPUT_ERR;
}
void
outfmt(struct output *file, const char *fmt, ...)
{

View File

@ -66,6 +66,8 @@ void emptyoutbuf(struct output *);
void flushall(void);
void flushout(struct output *);
void freestdout(void);
int outiserror(struct output *);
void outclearerror(struct output *);
void outfmt(struct output *, const char *, ...) __printflike(2, 3);
void out1fmt(const char *, ...) __printflike(1, 2);
void out2fmt_flush(const char *, ...) __printflike(1, 2);

View File

@ -0,0 +1,3 @@
# $FreeBSD$
! echo >&- 2>/dev/null