[comp.lang.c] using getopt

harrison@csl.dl.nec.com (Mark Harrison) (05/10/91)

We have a main() which uses getopt() to parse the command line options,
and then call a subroutine.  The subroutine receives parameters in an
argc/argv fashion, and uses getopt() to parse them.

The subroutine, however, receives -1 on its first invocation of getopt.
It turns out that the external int optind has been set to 3 by 
the main program's invocation of getopt.  resetting "optarg = 1;"
in the subroutine seems to fix things.

Our questions, which are not addressed by the FM, as far as we can tell:

1.  Is it legal to call getopt() multiple times in a program?
2.  Is resetting optarg to 1 a reliable way of handling the problem?
3.  If not, how best to handle this?

This is on SunOS 4.1.1, if it matters.
-- 
Mark Harrison           | Note: harrison@ssd.dl.nec.com and
harrison@csl.dl.nec.com | necssd!harrison are not operating at
(214)518-5050           | present.  Please forward mail through the
                        | above address.  Sorry for the inconvenience.