Subject: bin/22311: rpcgen should check the contents of environment variable "CPP"
To: None <gnats-bugs@gnats.netbsd.org>
From: None <hiramatu@boreas.dti.ne.jp>
List: netbsd-bugs
Date: 07/31/2003 07:00:23
>Number:         22311
>Category:       bin
>Synopsis:       rpcgen should check the contents of environment variable "CPP"
>Confidential:   no
>Severity:       non-critical
>Priority:       high
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Jul 30 22:00:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Hiramatsu Yoshifumi
>Release:        NetBSD 1.6U
>Organization:
	
>Environment:
	
	
System: NetBSD orinoco.my.domain 1.6U NetBSD 1.6U (Orinoco) #3: Mon Jul 28 15:22:58 JST 2003 hiramatsu@orinoco.my.domain:/sys/arch/i386/compile/Orinoco i386
Architecture: i386
Machine: i386
>Description:
rpcgen executes cpp to process its input files, and it tries to find cpp
from environment variable CPP.

However, rpcgen will execute where $CPP points, even if the contents of
$CPP is wrong.

>How-To-Repeat:
% export CPP=cpp
% rpcgen drac.x
cannot find any C preprocessor (cpp)
cannot find any C preprocessor (cpp)
cannot find any C preprocessor (cpp)
cannot find any C preprocessor (cpp)

>Fix:
This patch implements a fix of this behavior, which is suggested in this mail.
http://mail-index.netbsd.org/tech-pkg/2003/07/19/0009.html

diff -buNr rpcgen.orig/rpc_main.c rpcgen/rpc_main.c
--- rpcgen.orig/rpc_main.c	2003-07-23 23:38:51.000000000 +0900
+++ rpcgen/rpc_main.c	2003-07-23 23:52:46.000000000 +0900
@@ -156,6 +156,7 @@
 static int parseargs __P((int, char *[], struct commandline *));
 static void usage __P((void));
 static void options_usage __P((void));
+static char *padvance(const char **, const char *);
 
 
 int
@@ -321,20 +322,78 @@
 find_cpp()
 {
 	struct stat buf;
+	const char *path;
+	char *cmdname;
 
-	if (stat(CPP, &buf) < 0) {	/* SVR4 or explicit cpp does not exist */
+	if (CPP[0] == '/') {
+		if (stat(CPP, &buf) < 0) {
+			/* SVR4 or explicit cpp does not exist */
 		if (cppDefined) {
-			fprintf(stderr, "cannot find C preprocessor: %s\n", CPP);
+				fprintf(stderr,
+				    "cannot find C preprocessor: %s\n", CPP);
 			crash();
 		} else {	/* try the other one */
 			CPP = SUNOS_CPP;
-			if (stat(CPP, &buf) < 0) {	/* can't find any cpp */
-				fprintf(stderr, "cannot find any C preprocessor (cpp)\n");
+				if (stat(CPP, &buf) < 0) {
+					/* can't find any cpp */
+					fprintf(stderr,
+					    "cannot find any C preprocessor (cpp)\n");
 				crash();
 			}
 		}
 	}
+	} else {
+		path = getenv("PATH");
+		while((cmdname = padvance(&path, "cpp")) != NULL) {
+			if (stat(cmdname, &buf) < 0) {
+				/* not found in this path */
+				free(cmdname