Subject: bin/36822: /bin/sh: incorrect exit status with file redirection error
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <oki@NetBSD.org>
List: netbsd-bugs
Date: 08/22/2007 19:25:01
>Number:         36822
>Category:       bin
>Synopsis:       /bin/sh: incorrect exit status with file redirection error
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Aug 22 19:25:01 +0000 2007
>Originator:     oki@NetBSD.org
>Release:        NetBSD 3.1 (and -current 20070822)
>Organization:
	
>Environment:
System: NetBSD chiharu.e-neta.jp 3.99.15 NetBSD 3.99.15 (CHIHARU.MP) #25: Thu Feb 2 18:12:52 JST 2006 oki@chiharu.e-neta.jp:/usr/src/sys/arch/i386/compile/CHIHARU.MP i386
Architecture: i386
Machine: i386
>Description:
	if file redirection is failed, the exit status ($?) for the command
        is 0, that is incorrect.

        In Shell and Utilities volume (XCU)
        http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_08_02
        2.8.2 Exit Status for Commands
         ...
        If a command fails during word expansion or redirection,
        its exit status shall be greater than zero.

>How-To-Repeat:
	$ ls < no_such_file; echo $?
	sh: cannot open no_such_file: no such file
	0
	$

>Fix:
Index: redir.c
===================================================================
RCS file: /cvsroot/src/bin/sh/redir.c,v
retrieving revision 1.29
diff -u -r1.29 redir.c
--- redir.c	8 Jul 2004 03:57:33 -0000	1.29
+++ redir.c	22 Aug 2007 16:56:10 -0000
@@ -241,8 +241,10 @@
 	INTON;
 	return;
 ecreate:
+	exerrno = 1;
 	error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
 eopen:
+	exerrno = 1;
 	error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
 }