On Jan 16, 2008 10:35 PM, Sebastian Reitenbach
<[EMAIL PROTECTED]> wrote:
> Hi,
>
> I run into troubles with getopt(3). the test program below shows the
> problem. It produces different output on Linux and OpenBSD, when it is
> called like this on Linux it looks like this:
>
> ./a.out asdf -n
> option char: 110, n
>
> on OpenBSD, getopt returns -1 and no output is shown.
> what would be the best way to make it work on OpenBSD?

Define POSIXLY_CORRECT in your Linux program to make it work like on OpenBSD ;)
See the getopt(3) on your Linux system for more details, but
basically, OpenBSD getopt(3) stops when it encounters the first
non-option argument, while GNU getopt(3) parses the whole argv array.
If you really need your "asdf" argument before the option, you can
parse it and do the argv++, argc-- dance before calling getopt(3).
Otherwise, just use ./a.out -n asdf.

>
> cheers
> Sebastian
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> extern char *optarg;
> extern int opterr;
> extern int optind;
> extern int optopt;
> extern int optreset;
>
> int main (int argc, char **argv)
> {
>   char *optstr = "mpn";
>   int option_char;
>   do
>   {
>     option_char = getopt(argc, argv, optstr);
>     if (option_char == -1) {
>       break;
>     }
>     switch (option_char) {
>       case 'm':
>       case 'p':
>       case 'n':
>       default:
>                 printf("option char: %i, %c\n",
>                         option_char, option_char);
>                 break;
>      }
>   } while (1);
>
>   return 0;
> }
>
>

-- 
Pierre Riteau

Reply via email to