Unix Programming - Using getopt() more than once?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2007 > Using getopt() more than once?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Using getopt() more than once?
Kenny McCormack

2007-02-09, 7:19 pm

(since it uses global variables to track its internal state), getopt()
seems, by design, to be only usable once. But it is not too hard to
imagine situations where you'd want to call it multiple times in a given
program. Is this possible (in any at least psuedo-portable way) ?

(answering my own question), I have found that setting optind to 1,
seems to work. That is, I do:

#include <getopt.h>
...
do normal getopt() stuff;
...
optind = 1;
do getopt() again;

Loic Domaigne

2007-02-10, 7:18 pm

Hello Kenny,

> (since it uses global variables to track its internal state), getopt()
> seems, by design, to be only usable once. But it is not too hard to
> imagine situations where you'd want to call it multiple times in a given
> program. Is this possible (in any at least psuedo-portable way) ?
>
> (answering my own question), I have found that setting optind to 1,
> seems to work. That is, I do:
>
> #include <getopt.h>
> ...
> do normal getopt() stuff;
> ...
> optind = 1;
> do getopt() again;


Seems to be the way to go... You may want to reset opterr and optopt
as well.

Cheers,
Loic.

Roger Leigh

2007-02-11, 7:20 am

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com