Calling getopt with new arguments

S

Shaun Jackman

I'd like to call getopt with one set of arguments, and once that's
completely done call it again with an entirely different set of
arguments. I've found though that at least with the getopt
implementation common to both glibc and newlib, getopt has to be
"cleaned up" in between. The glibc source code shows that to clean it
up either optind must be set to zero or __getopt_initialized must be
set to non-zero. The latter is clearly non-standard, is the former
possibly more standard? It seems to me this issue must have been
encountered and addressed sometime before.

I ran into a bug with busybox because it set optind to 1 to clean up
getopt, but this was insufficient with newlib (and glibc) and was
causing errors when parsing options.

Thanks,
Shaun
 
C

Chris Croughton

I'd like to call getopt with one set of arguments, and once that's
completely done call it again with an entirely different set of
arguments. I've found though that at least with the getopt
implementation common to both glibc and newlib, getopt has to be
"cleaned up" in between. The glibc source code shows that to clean it
up either optind must be set to zero or __getopt_initialized must be
set to non-zero. The latter is clearly non-standard, is the former
possibly more standard? It seems to me this issue must have been
encountered and addressed sometime before.

It is not standard in C at all, the C specification says nothing about
getopt(). It is defined in IEEE Std 1003.1-2001, but that says nothing
about re-initialising it.

http://www.opengroup.org/onlinepubs/009695399/functions/getopt.html
I ran into a bug with busybox because it set optind to 1 to clean up
getopt, but this was insufficient with newlib (and glibc) and was
causing errors when parsing options.

Since it is not defined that there is any standard way of doing it, you
will have to read other documentation (or the code), or code your own
version (as I have; I don't know that mine alows restarting either, I
don't remember designing it in). The latter (having your own version)
is probably best, then you won't be dependent on the OS libraries
supporting it.

Chris C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top