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

Sean Hefty sean.hefty at intel.com
Tue Feb 24 16:23:19 PST 2009


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.  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.

- Sean




More information about the ofw mailing list