LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2004, 01:35 AM   #1
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
getopt(3)


i found out that you can reinitialize getopt by setting optind = 0. i tried it and it worked. the thing is i didn't see it documented on the man pages or the info pages. did i miss something, or is this just something that's "well-known but undocumented".
 
Old 06-14-2004, 08:43 AM   #2
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Hi,

optind, AFAIK is an internal counter telling getopt where to find it's next operand. That is, argv + optind is where it'll find it's next argument. So, resetting it will obviously make it look at arguments it has previously seen. It's probably not documented as it's not to be meddled with and not many situations demand this kind of a behaviour.

Setting it to 0 might not be such a good idea, as argv + optind = argv. So, argv[0] will be accessed and that is the program name. But, depending on when optind is internally incremented, you might find that even though you set it to 0, it might be incremented to 1.

Arvind

PS: I just checked the glibc implementation of getopt. optind is checked to see if it's 0. If it is, it's set to 1, and getopt is re-initialized. Nothing special.

PS: Have you tried setting it to a negative number?
 
Old 06-14-2004, 02:22 PM   #3
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Original Poster
Rep: Reputation: 165Reputation: 165
there's also a static variable nextchar (as mentioned in the man page) that's involved. for example, if you used a command of the form "command -abc", getopt would have optind = 1 while it parses all three options, and nextchar would be incremented as the options are retrieved. but, apparently, setting optind = 0 resets everything, including nextchar. that's good. but, i like to have everything documented in the man page; i don't like to discover things by tinkering with them.
 
Old 06-14-2004, 08:37 PM   #4
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Well, it's not meant to be known at all. You're given an interface to work with and that interface is documented. But, if you change an internal variable and something happens, then, you can hardly complain that it's undocumented behaviour. :-)

If this was in C++ or something, it'd be a private variable and you couldn't access it anyway. But, the abstraction facilities offered by C are not great. So, you can tinker with the internal variables of getopt. If such behaviour was wanted by many, then, a function called "_getopt_initialize" (as in the sources) would've been documented.

optind is meant purely for finding out which is the next non-option argument, and to use the rest of them.

Arvind
 
Old 06-14-2004, 10:43 PM   #5
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Original Poster
Rep: Reputation: 165Reputation: 165
yes, one would think. but, then -- and this is the reason i was asking -- when one looks at some of the GNU source code, one sees things like:

Code:
optind = 0;    /* this forces GNU getopt to reinitialize */
the above is from env.c in coreutils.

see what i mean?

Last edited by Berhanie; 06-14-2004 at 10:59 PM.
 
Old 06-15-2004, 05:20 AM   #6
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Yes, I suppose they set a bad example. As they're all GNU code, they know how each one will be implemented and so, they make use of that fact to (maybe) make it work a little faster. But, I'm sure you'll agree that it's not a very good idea to be so dependant on implementation. What happens if the getopt implementation changes?

For the record, the getopt file says, about optind:

Quote:
On entry to `getopt', zero means this is the first call; initialize.
Later, it says:
Quote:
Formerly, initialization of getopt depended on optind==0, which causes problems with re-calling getopt as programs generally don't know that.
Arvind
 
Old 06-15-2004, 12:15 PM   #7
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Original Poster
Rep: Reputation: 165Reputation: 165
yes, i agree with you that an undocumented feature shouldn't be used.
by the way, what you quoted had more information than was contained in the man pages. where did you find it?
 
Old 06-15-2004, 09:18 PM   #8
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
I found it in the glibc sources of getopt. Do you want me to send it to you? It's a small file, getopt.c.

There's a slightly modified version of getopt.c here: http://mia.ece.uic.edu/cgi-bin/lxr/h...v=poptop-1.1.4

Line numbers 130 to around 145 are what you're looking for.

Arvind
 
Old 06-15-2004, 09:50 PM   #9
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Original Poster
Rep: Reputation: 165Reputation: 165
arvind, thank you very much. no need to send it to me, though. i'm walking away from this with the conclusion that this feature is undocumented (unless one reads the source code itself -- because even the comments to the code leave room for speculation).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getopt availability Bostonian Programming 2 12-26-2004 10:37 AM
getopt in unix hari_s_82 Linux - Newbie 2 10-01-2004 04:19 AM
Help for perl--- Getopt::Declare arobinson74 Programming 1 08-20-2004 10:31 AM
Getopt() newbie... AMMullan Programming 6 02-11-2004 01:02 PM
Using getopt in cplus UltimaGuy Programming 3 08-28-2003 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration