[ofw] [PATCH] getopt: add use of const to interface

Smith, Stan stan.smith at intel.com
Tue Feb 24 17:19:14 PST 2009


Hefty, Sean wrote:
> Here's a snip of the getopt() code, with comments added.
>
>       // See if option takes an argument
>       if (loc[1] != ':') {
>               goto out;               // nope
>       }
>
>       // See if argument immediately follows option, i.e. "-oTheArg"
>       if (argv[optind][2] != '\0') {
>               optarg = &argv[optind][2];
>               goto out;
>       }
>
>       /* switch argument is optional (::) - be careful */
>       if (loc[2] == ':' && (argv[optind+1] && argv[optind+1][0] == '-'))
>               goto out;
>
>       // If we're here, option takes an argument and arg is next
>       // command line parameter.
>       optarg = argv[++optind];
>       while(optarg && *optarg && (*optarg == ' ' || *optarg == '      ')) {
>               optarg++;
>       }
>
>       // If we didn't get a argument, report an error.
>       if (!optarg || !(*optarg)) {
>               return '?';
>       }
>
> So, I think my change to return '?' at the end are correct.

I did not say your fix was incorrect; I just pointed out that it would break existing code.
BTW, your solution mimics Linux behavior.
All I'm saying is, if you choose the '?' return value for getopt, then you need to fix existing code.

>  Also, I don't see that the while() loop above is needed at all.
> argv[++optind] shouldn't return a pointer to a blank space or a tab.
> The loop looks like it could have been useful after setting optarg =
> &argv[optind][2] (2nd if statement), but we've handled that case
> separately.

Dumps whitespace prior to returning optarg; I did find this when fixing cmd-line arg processing of perftests\*; try '-h <sp><sp>arg'. I'm sure there's another place where the whitespace dump loop could be moved to.

>
> - Sean




More information about the ofw mailing list