Subject: bin/8062: [PATCH] Atc: use getopt
To: None <gnats-bugs@gnats.netbsd.org>
From: Joseph Myers <jsm28@cam.ac.uk>
List: netbsd-bugs
Date: 07/24/1999 09:30:56
>Number:         8062
>Category:       bin
>Synopsis:       [PATCH] Atc: use getopt
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sat Jul 24 08:20:01 1999
>Last-Modified:
>Originator:     Joseph S. Myers
>Organization:
Trinity College, University of Cambridge, UK
>Release:        NetBSD-current of 1999-07-24
>Environment:
[
System: Linux decomino 2.2.10 #1 Mon Jun 14 07:48:53 UTC 1999 i686 unknown
Architecture: i686
]
>Description:

This patch makes atc(6) use getopt(3) for its option parsing rather
than rolling its own.

>How-To-Repeat:

>Fix:

diff -ruN atc/main.c atc+/main.c
--- atc/main.c	Sun Jul 18 11:11:09 1999
+++ atc+/main.c	Sat Jul 24 13:33:07 1999
@@ -72,7 +72,7 @@
 	int			f_usage = 0, f_list = 0, f_showscore = 0;
 	int			f_printpath = 0;
 	const char		*file = NULL;
-	char			*name, *ptr;
+	int			ch;
 	struct sigaction	sa;
 #ifdef BSD
 	struct itimerval	itv;
@@ -84,54 +84,40 @@
 
 	start_time = seed = time(0);
 
-	name = *av++;
-	while (*av) {
-#ifndef SAVEDASH
-		if (**av == '-') 
-			++*av;
-		else
+	while ((ch = getopt(ac, av, "ulstpg:f:r:")) != -1) {
+		switch (ch) {
+		case '?':
+		case 'u':
+		default: 
+			f_usage++;
+			break;
+		case 'l':
+			f_list++;
+			break;
+		case 's':
+		case 't':
+			f_showscore++;
+			break;
+		case 'p':
+			f_printpath++;
+			break;
+		case 'r':
+			seed = atoi(optarg);
+			break;
+		case 'f':
+		case 'g':
+			file = optarg;
 			break;
-#endif
-		ptr = *av++;
-		while (*ptr) {
-			switch (*ptr) {
-			case '?':
-			case 'u':
-				f_usage++;
-				break;
-			case 'l':
-				f_list++;
-				break;
-			case 's':
-			case 't':
-				f_showscore++;
-				break;
-			case 'p':
-				f_printpath++;
-				break;
-			case 'r':
-				seed = atoi(*av);
-				av++;
-				break;
-			case 'f':
-			case 'g':
-				file = *av;
-				av++;
-				break;
-			default: 
-				warnx("unknown option '%c'\n", *ptr);
-				f_usage++;
-				break;
-			}
-			ptr++;
 		}
 	}
+	if (optind < ac)
+		f_usage++;
 	srandom(seed);
 
 	if (f_usage)
 		fprintf(stderr, 
 		    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
-			name);
+			av[0]);
 	if (f_showscore)
 		log_score(1);
 	if (f_list)
>Audit-Trail:
>Unformatted: