0

The code block is listed below

/*************** Micro getopt() *********************************************/
#define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
                (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
                &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
#define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
                (_O=4,(char*)0):(char*)0)
#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
#define ARG(c,v)    (c?(--c,*v++):(char*)0)

static int _O = 0;      /* Internal state */
/*************** Micro getopt() *********************************************/

I know this macros are used for get the args of main function,but not fully understand it. Now the question is what's the _O mean in this block,and how it works.

  • It's just a normal static variable. It gets assigned different values in the different macros. And as for its use? It's *internal* and nothing you really need to worry about. – Some programmer dude Jul 02 '13 at 07:15
  • 1
    http://en.wikipedia.org/wiki/Getopt – hit Jul 02 '13 at 07:17
  • project git repo is [here](git://ozlabs.org/~paulus/ppp.git) @alk – user2541485 Jul 02 '13 at 07:38
  • and [here](http://ppp.samba.org/download.html) – user2541485 Jul 02 '13 at 07:45
  • It's a macro implementation of getopt(), used in mico (https://github.com/raghunayak/mico/blob/master/src/admin/mkdepend.cc) and in the ppp chat implementation (ftp://ftp.samba.org/pub/unpacked/ppp/chat/chat.c) the OP probably found it in. – ProphetV Jul 02 '13 at 07:47

1 Answers1

0

Like the comment says, it's internal state the macros use to remember where they are and what they're doing. Nothing you need to worry about unless you are reimplementing the code, in which case the complete lack of documentation apart from that comment and the general coding style suggest you might have a problem.

As it's static it is only accessible from that file (God help you if that code is from a header) and it has a lifetime the same as the executing program.

But basically, you should not touch _O in your code.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61