[Openswan dev] improving tncfg argument handling

D. Hugh Redelmeier hugh at mimosa.com
Thu Dec 17 14:42:26 EST 2009


I was looking at tncfg to eliminate some new GCC diagnotics.

In the process, I found some weaknesses in option handling.  Here is a
patch to improve this handling.  UNTESTED.

- "tncfg --help" should terminate with an exit status of 0, not 1

- whenever you specify an incorrect option, you probably don't want
  all the usage information.  Instead, tncfg will how show how to get
  usage information.

- detect and diagnose when extra arguments are provided to the command

- fix quoting in one message.

These changes are low priority.  I'm posting them in the hope that
they might be adopted when there is some time before a critical
release or the automated testing system is working.

================ diffs ================
--- /home/hugh/rpmbuilds/BUILD/openswan-2.6.24rc5/programs/tncfg/tncfg.c.BAK2	2009-12-17 12:05:49.000000000 -0500
+++ /home/hugh/rpmbuilds/BUILD/openswan-2.6.24rc5/programs/tncfg/tncfg.c	2009-12-17 14:24:58.000000000 -0500
@@ -71,7 +71,6 @@
 		name);
 	fprintf(stdout, "        [ --debug ] is optional to any %s command.\n", name);
 	fprintf(stdout, "        [ --label <label> ] is optional to any %s command.\n", name);
-	exit(1);
 }
 
 static struct option const longopts[] =
@@ -210,7 +209,7 @@
 			break;
 		case 'h':
 			usage(progname);
-			break;
+			exit(0);
 		case 'v':
 			if(optarg) {
 				fprintf(stderr, "%s: warning; '-v' and '--version' options don't expect arguments, arg '%s' found, perhaps unintended.\n",
@@ -251,12 +250,19 @@
 			/* no return on error */
 			break;
 		default:
-			usage(progname);
-			break;
+			fprintf(stderr, "%s: unrecognized option.  Try `%s --help' for usage information.\n"
+				, progname, progname);
+			exit(1);
 		}
 		previous = c;
 	}
 
+	if (optind != argcount) {
+		fprintf(stderr, "%s: unexpected operand.  Try `%s --help' for usage information.\n"
+			, progname, progname);
+		exit(1);
+	}
+
 	if(argcount == 1) {
 		exit(system("cat /proc/net/ipsec_tncfg"));
 	}
@@ -293,7 +299,7 @@
 		break;
 	default:
 		fprintf(stderr, "%s: exactly one of '--attach', '--detach' or '--clear' options must be specified.\n"
-			"Try %s --help' for usage information.\n",
+			"Try `%s --help' for usage information.\n",
 			progname, progname);
 		exit(1);
 	}

================ end ================


More information about the Dev mailing list