patch to add getopt parsing to expr

Joerg Sonnenberger joerg at britannica.bec.de
Tue Jan 20 05:06:31 PST 2009


Patch.

On Tue, Jan 20, 2009 at 02:02:28PM +0100, Joerg Sonnenberger wrote:
> On Mon, Jan 19, 2009 at 11:30:55PM -0500, Joe Talbott wrote:
> > +	while ((ch = getopt(argc, argv, "-")) != -1) {
> 
> The use of getopt here is wrong. Besides, using getopt is taking a
> sledgehammer to crack a nut. Attached is the corresponding fix for
> NetBSD, the explicit check for having at least one argument is better
> anyway.
> 
> Joerg
Index: expr.y
===================================================================
RCS file: /home/joerg/repo/netbsd/src/bin/expr/expr.y,v
retrieving revision 1.34
diff -u -p -r1.34 expr.y
--- expr.y	30 Apr 2008 13:39:13 -0000	1.34
+++ expr.y	20 Jan 2009 12:56:52 -0000
@@ -440,19 +440,25 @@ yyerror(const char *fmt, ...)
 	va_end(arg);
 }
 
+static void
+usage(void)
+{
+	(void)fprintf(stderr, "usage: %s expression\n", getprogname());
+	exit(2);
+}
+
 int
 main(int argc, const char * const *argv)
 {
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
 
-	if (argc == 1) {
-		(void)fprintf(stderr, "usage: %s expression\n",
-		    getprogname());
-		exit(2);
-	}
-
-	av = argv + 1;
+	if (argc > 1 && strcmp(argv[1], "--"))
+		av = argv + 1;
+	else if (argc > 2)
+		av = argv + 2;
+	else
+		usage();
 
 	exit(yyparse());
 	/* NOTREACHED */




More information about the Bugs mailing list