getopt differs from c/c++

I’ve two programs, one written in C, and onother one written in C++.
After some time I’ve noticed that the getopt function that gets
linked in a C program differs from the one linked within the C++ one.

The getopt in c supports the “-oVALUE” style. The SAME program linked
with QCC or qcc -lang-c++ will return an error when a parameter like
this is found.

Both funtions NEVER returns the ‘:’ when a parameter is missing.

Has someone experienced these problems?


Wave++

Wave++ <wavexx@apexmail.com> wrote:

I’ve two programs, one written in C, and onother one written in C++.
After some time I’ve noticed that the getopt function that gets
linked in a C program differs from the one linked within the C++ one.

The getopt in c supports the “-oVALUE” style. The SAME program linked
with QCC or qcc -lang-c++ will return an error when a parameter like
this is found.

Both funtions NEVER returns the ‘:’ when a parameter is missing.

Has someone experienced these problems?

Can you post an example, along with compile and link parameters?


cburgess@qnx.com

Colin Burgess <cburgess@qnx.com> wrote:

Can you post an example, along with compile and link parameters?

after trying to replicate the problem I’ve noticed that the real
problem is more subtle:

getopt.c – begin

#include <unistd.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
int brk = 1, c = 0;

opterr = 0;
while(brk && ( c = getopt( argc, argv, “ab”)) != -1)
switch( c )
{
case ‘a’: printf(“a\n”);
break;
case ‘b’: printf(“b\n”);
break;
case ‘:’: printf(“will never reach this\n”);
break;
case ‘?’: printf(“unsupported option\n”);
brk = 0;
break;
}

opterr = optind = 1;

while(( c = getopt( argc, argv, “abc:”)) != -1)
switch( c )
{
case ‘a’: printf(“a2\n”);
break;
case ‘b’: printf(“b2\n”);
break;
case ‘c’: printf(“c2 == %s\n”, optarg);
break;
case ‘:’: printf(“will never reach this (2)\n”);
break;
}

return 0;
}

getopt.c --end

I compile it using qcc -o getopt getopt.c
and ran this as follows:

$ ./getopt -c
unsupported option
getopt: option requires an argument – c
$ ./getopt -c 2
unsupported option
c2 == 2
$ ./getopt -c2
unsupported option
getopt: illegal option – 2
$

getopt here seems to use another internal static data that
doesn’t get reset on the next function call.
this data resets only when getopt terminates with -1.

You will notice however that ‘:’ never gets returned even
when setting opterr to 0 or ‘:’ as documented.


cburgess@qnx.com


Wave++

Wave++ <wavexx@apexmail.com> wrote:

Colin Burgess <> cburgess@qnx.com> > wrote:
Can you post an example, along with compile and link parameters?

after trying to replicate the problem I’ve noticed that the real
problem is more subtle:

getopt.c – begin

#include <unistd.h
#include <stdio.h

int main(int argc, char* argv[])
{
int brk = 1, c = 0;

opterr = 0;
while(brk && ( c = getopt( argc, argv, “ab”)) != -1)
switch( c )
{
case ‘a’: printf(“a\n”);
break;
case ‘b’: printf(“b\n”);
break;
case ‘:’: printf(“will never reach this\n”);
break;
case ‘?’: printf(“unsupported option\n”);
brk = 0;
break;
}

opterr = optind = 1;

while(( c = getopt( argc, argv, “abc:”)) != -1)
switch( c )
{
case ‘a’: printf(“a2\n”);
break;
case ‘b’: printf(“b2\n”);
break;
case ‘c’: printf(“c2 == %s\n”, optarg);
break;
case ‘:’: printf(“will never reach this (2)\n”);
break;
}

return 0;
}

getopt.c --end

I compile it using qcc -o getopt getopt.c
and ran this as follows:

$ ./getopt -c
unsupported option
getopt: option requires an argument – c
$ ./getopt -c 2
unsupported option
c2 == 2
$ ./getopt -c2
unsupported option
getopt: illegal option – 2
$

getopt here seems to use another internal static data that
doesn’t get reset on the next function call.
this data resets only when getopt terminates with -1.

You will notice however that ‘:’ never gets returned even
when setting opterr to 0 or ‘:’ as documented.

Here’s the magic line from the docs…

If getopt() encounters an option character that isn’t contained in
optstring, it returns the ? character. If it detects a missing
option-argument, it returns : if the first character of optstring
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
is a colon, or ? otherwise. In both cases, getopt() sets optopt to
^^^^^^^^^^
the option character that caused the error.


cburgess@qnx.com

Colin Burgess <cburgess@qnx.com> wrote:

Here’s the magic line from the docs…

If getopt() encounters an option character that isn’t contained in
optstring, it returns the ? character. If it detects a missing
option-argument, it returns : if the first character of optstring
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
is a colon, or ? otherwise. In both cases, getopt() sets optopt to
^^^^^^^^^^
the option character that caused the error.

uhm. Yes. I should change my lens :slight_smile:).

another magic line from the docs:

  • argv[] (reset optind to 1 if you want to use getopt() to process
    ^^^^^^^^^^^^^^^^^
  • additional argument sets).

In the example shown before, resetting optind to 1 doesn’t suffice.
actually to reset the internal data of getopt I have to:
optind = 1;
getopt(0, NULL, NULL);

When optind is 1, getopt should automatically reset its internal
data.


cburgess@qnx.com


Wave++