From b6dd340d3c5e64e71932c3da8bfd9bca6bc760d3 Mon Sep 17 00:00:00 2001 From: kre Date: Tue, 16 Nov 2021 16:57:15 +0000 Subject: [PATCH] Make pwd (both /bin/pwd and the /bin/sh built-in version) check for write errors on stdout, and indicate an error if that happens. --- bin/pwd/pwd.c | 8 ++++++-- bin/sh/cd.c | 11 +++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bin/pwd/pwd.c b/bin/pwd/pwd.c index 27888ebf7eba..2ec3a804a050 100644 --- a/bin/pwd/pwd.c +++ b/bin/pwd/pwd.c @@ -1,4 +1,4 @@ -/* $NetBSD: pwd.c,v 1.22 2011/08/29 14:51:19 joerg Exp $ */ +/* $NetBSD: pwd.c,v 1.23 2021/11/16 16:57:15 kre Exp $ */ /* * Copyright (c) 1991, 1993, 1994 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\ #if 0 static char sccsid[] = "@(#)pwd.c 8.3 (Berkeley) 4/1/94"; #else -__RCSID("$NetBSD: pwd.c,v 1.22 2011/08/29 14:51:19 joerg Exp $"); +__RCSID("$NetBSD: pwd.c,v 1.23 2021/11/16 16:57:15 kre Exp $"); #endif #endif /* not lint */ @@ -107,6 +107,10 @@ main(int argc, char *argv[]) (void)printf("%s\n", p); + (void)fflush(stdout); + if (ferror(stdout)) + err(EXIT_FAILURE, "stdout"); + exit(EXIT_SUCCESS); /* NOTREACHED */ } diff --git a/bin/sh/cd.c b/bin/sh/cd.c index 178c44f4f7e3..66a9c5d009d2 100644 --- a/bin/sh/cd.c +++ b/bin/sh/cd.c @@ -1,4 +1,4 @@ -/* $NetBSD: cd.c,v 1.51 2021/10/31 02:12:01 kre Exp $ */ +/* $NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; #else -__RCSID("$NetBSD: cd.c,v 1.51 2021/10/31 02:12:01 kre Exp $"); +__RCSID("$NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $"); #endif #endif /* not lint */ @@ -364,8 +364,15 @@ pwdcmd(int argc, char **argv) if (curdir == NULL) error("Unable to find current directory"); } + + flushout(out1); /* make sure buffer is empty */ + clr_err(out1); /* and forget any earlier errors */ out1str(curdir); out1c('\n'); + flushout(out1); + if (io_err(out1)) + error("stdout: %s", strerror(errno)); + return 0; }