patch to add getopt parsing to expr

Joe Talbott josepht at cstone.net
Mon Jan 19 20:31:25 PST 2009


On Mon, Jan 19, 2009 at 11:27:18PM -0500, Joe Talbott wrote:
> Here's a patch to add getopt(3) support to expr so that -- is accepted as
> required by POSIX.  This was pointed out by vstemen on IRC.  I'll
> commit sometime after the release if there are no objections.
> 
> Joe

Forgot the patch.

Joe
diff --git a/bin/expr/expr.y b/bin/expr/expr.y
index 0bcd6e0..05564a7 100644
--- a/bin/expr/expr.y
+++ b/bin/expr/expr.y
@@ -18,6 +18,7 @@
 #include <errno.h>
 #include <regex.h>
 #include <limits.h>
+#include <unistd.h>
   
 enum valtype {
 	integer, numeric_string, string
@@ -244,12 +245,31 @@ is_zero_or_null(struct val *vp)
 	/* NOTREACHED */
 }
 
+static void
+usage(void)
+{
+        fprintf(stderr,
+                "usage: expr <expression>\n");
+        exit(EXIT_FAILURE);
+}
+
 int
-main (int argc __unused, char **argv)
+main (int argc, char **argv)
 {
+	int ch;
 	setlocale (LC_ALL, "");
 
-	av = argv + 1;
+	while ((ch = getopt(argc, argv, "-")) != -1) {
+		switch (ch) {
+		default:
+			usage();
+			/* NOTREACED */
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	av = argv;
 
 	yyparse ();
 




More information about the Bugs mailing list