[vox-tech] writing free getopt, ran into a dilemma...

Mark K. Kim vox-tech@lists.lugod.org
Thu, 28 Mar 2002 17:19:46 -0800 (PST)


On 28 Mar 2002, Micah Cowan wrote:

> Permuting is probably the easiest way to go; but another alternative
> would be to create another function, increment_optind(), instead of
> doing ++optind.  This function could then automatically skip options.

increment_optind() sounds excellent!  Thanks!

> Oh, but you mentioned it doesn't currently even handle the middle
> options (which is what I thought the point was for your getopt()?).

I'm not sure what you mean by "middle options"...  It currently behaves
exactly like GNU getopt as far as required arguments (as in "o:") and
non-option arguments (as in "-") except it doesn't permute[1] argv and
optind doesn't work like GNU getopt.  I don't use optional arguments
(which I believe is a GNU extension) but I'll implement it if I knew how
to use it :)  And... I don't think there's anything else, is there???

Oh, my code does handle "--" :)

> You'd still need
> increment_optind()---or you could break getopt()'s parameter list by
> passing back the allocated string (make sure it gets freed
> somewhere...).

My original version of getopt allocated an array that is the same size as
argc+1, but I realized freeing it becomes a problem (don't wanna use
atexit(), and wanna keep compatibility so don't wanna use a cleanup
function call).  I modified the program to not use allocation at all.

> If you email me the code, I'd be happy to comment on it (comp.lang.c is
> a hobby of mine, though one I haven't gotten around to lately).  I won't
> get to it this weekend, though, since I'll be out of town (and away from
> email).

I'm gonna work on the code some more and send you a copy for a review.
Maybe I'll just put up a copy on my website.  Thanks, Micah!

-Mark

[1] - Thank you for bringing up the proper vocabulary :)

--
Mark K. Kim
http://www.cbreak.org/
PGP key available upon request.