This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Tried to make getopt() and friends more sus4-like in error handling
- From: Gregory Pietsch <gpietsch at comcast dot net>
- To: newlib at sourceware dot org
- Date: Sun, 26 Aug 2012 20:00:55 -0400
- Subject: Tried to make getopt() and friends more sus4-like in error handling
I finally got the a-ha for this. Okay to add? -- Gregory Pietsch
--- getopt.c.old 2012-08-26 19:24:27.466130300 -0400
+++ getopt.c 2012-08-26 19:54:39.118121500 -0400
@@ -71,6 +71,14 @@
value of ordering. In the case of RETURN_IN_ORDER, only `--' can cause
getopt() and friends to return EOF with optind != argc.
+2012-08-26: Tried to make the error handling more sus4-like. The functions
+return a colon if getopt() and friends detect a missing argument and the
+first character of shortopts/optstring starts with a colon (`:'). If
getopt()
+and friends detect a missing argument and shortopts/optstring does not
start
+with a colon, the function returns a question mark (`?'). If it was a
missing
+argument to a short option, optopt is set to the character in question. The
+colon goes in front of the ordering character (`+' or `-').
+
COPYRIGHT NOTICE AND DISCLAIMER:
Copyright (C) 1997 Gregory Pietsch
@@ -185,6 +193,7 @@
int has_arg = -1;
char *cp = 0;
int arg_next = 0;
+ int initial_colon = 0;
/* first, deal with silly parameters and easy stuff */
if (argc == 0 || argv == 0 || (shortopts == 0 && longopts == 0)
@@ -200,6 +209,13 @@
if (data->optind == 0)
data->optind = data->optwhere = 1;
+ /* check for initial colon in shortopts */
+ if (*shortopts == ':')
+ {
+ ++shortopts;
+ initial_colon = 1;
+ }
+
/* define ordering */
if (shortopts != 0 && (*shortopts == '-' || *shortopts == '+'))
{
@@ -359,12 +375,18 @@
{
fprintf (stderr, "%s: argument required for option `", argv[0]);
if (longopt_match >= 0)
+ {
fprintf (stderr, "--%s'\n", longopts[longopt_match].name);
+ data->optopt = initial_colon ? ':' : '\?';
+ }
else
+ {
fprintf (stderr, "-%c'\n", *cp);
+ data->optopt = *cp;
+ }
}
data->optind++;
- return (data->optopt = ':');
+ return initial_colon ? ':' : '\?';
}
else
{