cosmopolitan/third_party/getopt
Justine Tunney f4f4caab0e Add x86_64-linux-gnu emulator
I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
2020-08-25 04:43:42 -07:00
..
README.txt Initial import 2020-06-15 07:18:57 -07:00
getopt.3 Initial import 2020-06-15 07:18:57 -07:00
getopt.c Add x86_64-linux-gnu emulator 2020-08-25 04:43:42 -07:00
getopt.h Initial import 2020-06-15 07:18:57 -07:00
getopt.mk Add x86_64-linux-gnu emulator 2020-08-25 04:43:42 -07:00
getopt_long.3 Initial import 2020-06-15 07:18:57 -07:00
getopt_long.c Initial import 2020-06-15 07:18:57 -07:00
initgetopt.S Add x86_64-linux-gnu emulator 2020-08-25 04:43:42 -07:00

README.txt

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

GETOPT(3)         Cosmopolitan Library Functions Manual              GETOPT(3)

๐๐€๐Œ๐„
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜ โ€” get option character from command line argument list

๐’๐˜๐๐Ž๐๐’๐ˆ๐’
     #๐—ถ๐—ป๐—ฐ๐—น๐˜‚๐—ฑ๐—ฒ <๐˜‚๐—ป๐—ถ๐˜€๐˜๐—ฑ.๐—ต>

     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ cฬฒhฬฒaฬฒrฬฒ *ฬฒoฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒeฬฒrฬฒrฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒrฬฒeฬฒsฬฒeฬฒtฬฒ;ฬฒ

     iฬฒnฬฒtฬฒ
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜(iฬฒnฬฒtฬฒ aฬฒrฬฒgฬฒcฬฒ, cฬฒhฬฒaฬฒrฬฒ *ฬฒ cฬฒoฬฒnฬฒsฬฒtฬฒ *ฬฒaฬฒrฬฒgฬฒvฬฒ, cฬฒoฬฒnฬฒsฬฒtฬฒ cฬฒhฬฒaฬฒrฬฒ *ฬฒoฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ);

๐ƒ๐„๐’๐‚๐‘๐ˆ๐๐“๐ˆ๐Ž๐
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function incrementally parses a command line argument
     list aฬฒrฬฒgฬฒvฬฒ and returns the next kฬฒnฬฒoฬฒwฬฒnฬฒ option character.  An option
     character is kฬฒnฬฒoฬฒwฬฒnฬฒ if it has been specified in the string of
     accepted option characters, oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ.

     The option string oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ may contain the following elements:
     individual characters, characters followed by a colon, and characโ€
     ters followed by two colons.  A character followed by a single
     colon indicates that an argument is to follow the option on the
     command line.  Two colons indicates that the argument is optional -
     this is an extension not covered by POSIX.  For example, an option
     string "x" recognizes an option -๐˜…, and an option string "x:" recโ€
     ognizes an option and argument -๐˜… aฬฒrฬฒgฬฒuฬฒmฬฒeฬฒnฬฒtฬฒ.  It does not matter to
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() if a following argument has leading whitespace; except in
     the case where the argument is optional, denoted with two colons,
     no leading whitespace is permitted.

     On return from ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜(), oฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ points to an option argument, if it
     is anticipated, and the variable oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ contains the index to the
     next aฬฒrฬฒgฬฒvฬฒ argument for a subsequent call to ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜().

     The variables oฬฒpฬฒtฬฒeฬฒrฬฒrฬฒ and oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ are both initialized to 1.  The
     oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ variable may be set to another value larger than 0 before a
     set of calls to ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() in order to skip over more or less aฬฒrฬฒgฬฒvฬฒ
     entries.  An oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ value of 0 is reserved for compatibility with
     GNU ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜().

     In order to use ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() to evaluate multiple sets of arguments, or
     to evaluate a single set of arguments multiple times, the variable
     oฬฒpฬฒtฬฒrฬฒeฬฒsฬฒeฬฒtฬฒ must be set to 1 before the second and each additional set
     of calls to ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜(), and the variable oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ must be reinitialโ€
     ized.

     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function returns -1 when the argument list is
     exhausted.  The interpretation of options in the argument list may
     be cancelled by the option โ€˜--โ€™ (double dash) which causes ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜()
     to signal the end of argument processing and return -1.  When all
     options have been processed (i.e., up to the first non-option arguโ€
     ment), ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() returns -1.

๐‘๐„๐“๐”๐‘๐ ๐•๐€๐‹๐”๐„๐’
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function returns the next known option character in
     oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ.  If ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() encounters a character not found in
     oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ or if it detects a missing option argument, it returns
     โ€˜?โ€™ (question mark).  If oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ has a leading โ€˜:โ€™ then a missing
     option argument causes โ€˜:โ€™ to be returned instead of โ€˜?โ€™.  In
     either case, the variable oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ is set to the character that
     caused the error.  The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function returns -1 when the arguโ€
     ment list is exhausted.

๐„๐—๐€๐Œ๐๐‹๐„๐’
     The following code accepts the options -๐—ฏ and -๐—ณ aฬฒrฬฒgฬฒuฬฒmฬฒeฬฒnฬฒtฬฒ and
     adjusts aฬฒrฬฒgฬฒcฬฒ and aฬฒrฬฒgฬฒvฬฒ after option argument processing has comโ€
     pleted.

           int bflag, ch, fd;

           bflag = 0;
           while ((ch = getopt(argc, argv, "bf:")) != -1) {
                   switch (ch) {
                   case 'b':
                           bflag = 1;
                           break;
                   case 'f':
                           if ((fd = open(optarg, O_RDONLY, 0)) == -1)
                                   err(1, "%s", optarg);
                           break;
                   default:
                           usage();
                   }
           }
           argc -= optind;
           argv += optind;

๐ƒ๐ˆ๐€๐†๐๐Ž๐’๐“๐ˆ๐‚๐’
     If the ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function encounters a character not found in the
     string oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ or detects a missing option argument, it writes an
     error message to sฬฒtฬฒdฬฒeฬฒrฬฒrฬฒ and returns โ€˜?โ€™.  Setting oฬฒpฬฒtฬฒeฬฒrฬฒrฬฒ to a zero
     will disable these error messages.  If oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ has a leading โ€˜:โ€™
     then a missing option argument causes a โ€˜:โ€™ to be returned in addiโ€
     tion to suppressing any error messages.

     Option arguments are allowed to begin with โ€˜-โ€™; this is reasonable
     but reduces the amount of error checking possible.

๐’๐„๐„ ๐€๐‹๐’๐Ž
     getopt(1), getopt_long(3), getsubopt(3)

๐’๐“๐€๐๐ƒ๐€๐‘๐ƒ๐’
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function implements a superset of the functionality
     specified by IEEE Std 1003.1 (โ€œPOSIX.1โ€).

     The following extensions are supported:

     ยท   The oฬฒpฬฒtฬฒrฬฒeฬฒsฬฒeฬฒtฬฒ variable was added to make it possible to call the
         ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function multiple times.

     ยท   If the oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ variable is set to 0, ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() will behave as if
         the oฬฒpฬฒtฬฒrฬฒeฬฒsฬฒeฬฒtฬฒ variable has been set.  This is for compatibility
         with GNU ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜().  New code should use oฬฒpฬฒtฬฒrฬฒeฬฒsฬฒeฬฒtฬฒ instead.

     ยท   If the first character of oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ is a plus sign (โ€˜+โ€™), it
         will be ignored.  This is for compatibility with GNU ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜().

     ยท   If the first character of oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ is a dash (โ€˜-โ€™), non-
         options will be returned as arguments to the option character
         โ€˜\1โ€™.  This is for compatibility with GNU ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜().

     ยท   A single dash (โ€˜-โ€™) may be specified as a character in
         oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ, however it should nฬฒeฬฒvฬฒeฬฒrฬฒ have an argument associated
         with it.  This allows ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() to be used with programs that
         expect โ€˜-โ€™ as an option flag.  This practice is wrong, and
         should not be used in any current development.  It is provided
         for backward compatibility oฬฒnฬฒlฬฒyฬฒ.  Care should be taken not to
         use โ€˜-โ€™ as the first character in oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ to avoid a semantic
         conflict with GNU ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() semantics (see above).  By default,
         a single dash causes ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() to return -1.

     Historic BSD versions of ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() set oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ to the last option
     character processed.  However, this conflicts with IEEE Std 1003.1
     (โ€œPOSIX.1โ€) which stipulates that oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ be set to the last characโ€
     ter that caused an error.

๐‡๐ˆ๐’๐“๐Ž๐‘๐˜
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function appeared in 4.3BSD.

๐๐”๐†๐’
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() function was once specified to return EOF instead of
     -1.  This was changed by IEEE Std 1003.2-1992 (โ€œPOSIX.2โ€) to decouโ€
     ple ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() from <sฬฒtฬฒdฬฒiฬฒoฬฒ.ฬฒhฬฒ>.

     It is possible to handle digits as option letters.  This allows
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() to be used with programs that expect a number (โ€œ-3โ€) as an
     option.  This practice is wrong, and should not be used in any curโ€
     rent development.  It is provided for backward compatibility oฬฒnฬฒlฬฒyฬฒ.
     The following code fragment works in most cases and can handle
     mixed number and letter arguments.

           int aflag = 0, bflag = 0, ch, lastch = '\0';
           int length = -1, newarg = 1, prevoptind = 1;

           while ((ch = getopt(argc, argv, "0123456789ab")) != -1) {
                   switch (ch) {
                   case '0': case '1': case '2': case '3': case '4':
                   case '5': case '6': case '7': case '8': case '9':
                           if (newarg || !isdigit(lastch))
                                   length = 0;
                           else if (length > INT_MAX / 10)
                                   usage();
                           length = (length * 10) + (ch - '0');
                           break;
                   case 'a':
                           aflag = 1;
                           break;
                   case 'b':
                           bflag = 1;
                           break;
                   default:
                           usage();
                   }
                   lastch = ch;
                   newarg = optind != prevoptind;
                   prevoptind = optind;
           }

COSMOPOLITAN                    January 4, 2016                            BSD

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

GETOPT_LONG(3)      Cosmopolitan Library Functions Manual       GETOPT_LONG(3)

๐๐€๐Œ๐„
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด, ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด_๐—ผ๐—ป๐—น๐˜† โ€” get long options from command line
     argument list

๐’๐˜๐๐Ž๐๐’๐ˆ๐’
     #๐—ถ๐—ป๐—ฐ๐—น๐˜‚๐—ฑ๐—ฒ <๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜.๐—ต>

     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ cฬฒhฬฒaฬฒrฬฒ *ฬฒoฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒeฬฒrฬฒrฬฒ;ฬฒ
     eฬฒxฬฒtฬฒeฬฒrฬฒnฬฒ iฬฒnฬฒtฬฒ oฬฒpฬฒtฬฒrฬฒeฬฒsฬฒeฬฒtฬฒ;ฬฒ

     iฬฒnฬฒtฬฒ
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด(iฬฒnฬฒtฬฒ aฬฒrฬฒgฬฒcฬฒ, cฬฒhฬฒaฬฒrฬฒ *ฬฒ cฬฒoฬฒnฬฒsฬฒtฬฒ *ฬฒaฬฒrฬฒgฬฒvฬฒ, cฬฒoฬฒnฬฒsฬฒtฬฒ cฬฒhฬฒaฬฒrฬฒ *ฬฒoฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ,
         cฬฒoฬฒnฬฒsฬฒtฬฒ sฬฒtฬฒrฬฒuฬฒcฬฒtฬฒ oฬฒpฬฒtฬฒiฬฒoฬฒnฬฒ *ฬฒlฬฒoฬฒnฬฒgฬฒoฬฒpฬฒtฬฒsฬฒ, iฬฒnฬฒtฬฒ *ฬฒlฬฒoฬฒnฬฒgฬฒiฬฒnฬฒdฬฒeฬฒxฬฒ);

     iฬฒnฬฒtฬฒ
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด_๐—ผ๐—ป๐—น๐˜†(iฬฒnฬฒtฬฒ aฬฒrฬฒgฬฒcฬฒ, cฬฒhฬฒaฬฒrฬฒ *ฬฒ cฬฒoฬฒnฬฒsฬฒtฬฒ *ฬฒaฬฒrฬฒgฬฒvฬฒ,
         cฬฒoฬฒnฬฒsฬฒtฬฒ cฬฒhฬฒaฬฒrฬฒ *ฬฒoฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ, cฬฒoฬฒnฬฒsฬฒtฬฒ sฬฒtฬฒrฬฒuฬฒcฬฒtฬฒ oฬฒpฬฒtฬฒiฬฒoฬฒnฬฒ *ฬฒlฬฒoฬฒnฬฒgฬฒoฬฒpฬฒtฬฒsฬฒ,
         iฬฒnฬฒtฬฒ *ฬฒlฬฒoฬฒnฬฒgฬฒiฬฒnฬฒdฬฒeฬฒxฬฒ);

๐ƒ๐„๐’๐‚๐‘๐ˆ๐๐“๐ˆ๐Ž๐
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() function is similar to getopt(3) but it accepts
     options in two forms: words and characters.  The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด()
     function provides a superset of the functionality of getopt(3).
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() can be used in two ways.  In the first way, every
     long option understood by the program has a corresponding short
     option, and the option structure is only used to translate from
     long options to short options.  When used in this fashion,
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() behaves identically to getopt(3).  This is a good way
     to add long option processing to an existing program with the miniโ€
     mum of rewriting.

     In the second mechanism, a long option sets a flag in the oฬฒpฬฒtฬฒiฬฒoฬฒnฬฒ
     structure passed, or will store a pointer to the command line arguโ€
     ment in the oฬฒpฬฒtฬฒiฬฒoฬฒnฬฒ structure passed to it for options that take
     arguments.  Additionally, the long option's argument may be speciโ€
     fied as a single argument with an equal sign, e.g.

           $ myprogram --myoption=somevalue

     When a long option is processed, the call to ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() will
     return 0.  For this reason, long option processing without shortโ€
     cuts is not backwards compatible with getopt(3).

     It is possible to combine these methods, providing for long options
     processing with short option equivalents for some options.  Less
     frequently used options would be processed as long options only.

     Abbreviated long option names are accepted when ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() proโ€
     cesses long options if the abbreviation is unique.  An exact match
     is always preferred for a defined long option.

     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() call requires an array to be initialized describโ€
     ing the long options.  Each element of the array is a structure:

           struct option {
                   char *name;
                   int has_arg;
                   int *flag;
                   int val;
           };

     The nฬฒaฬฒmฬฒeฬฒ field should contain the option name without the leading
     double dash.

     The hฬฒaฬฒsฬฒ_aฬฒrฬฒgฬฒ field should be one of:

           no_argument        no argument to the option is expected.
           required_argument  an argument to the option is required.
           optional_argument  an argument to the option may be preโ€
                              sented.

     If fฬฒlฬฒaฬฒgฬฒ is not NULL, then the integer pointed to by it will be set
     to the value in the vฬฒaฬฒlฬฒ field.  If the fฬฒlฬฒaฬฒgฬฒ field is NULL, then the
     vฬฒaฬฒlฬฒ field will be returned.  Setting fฬฒlฬฒaฬฒgฬฒ to NULL and setting vฬฒaฬฒlฬฒ
     to the corresponding short option will make this function act just
     like getopt(3).

     If the lฬฒoฬฒnฬฒgฬฒiฬฒnฬฒdฬฒeฬฒxฬฒ field is not NULL, then the integer pointed to by
     it will be set to the index of the long option relative to
     lฬฒoฬฒnฬฒgฬฒoฬฒpฬฒtฬฒsฬฒ.

     The last element of the lฬฒoฬฒnฬฒgฬฒoฬฒpฬฒtฬฒsฬฒ array has to be filled with
     zeroes.

     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด_๐—ผ๐—ป๐—น๐˜†() function behaves identically to
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() with the exception that long options may start with
     โ€˜-โ€™ in addition to โ€˜--โ€™.  If an option starting with โ€˜-โ€™ does not
     match a long option but does match a single-character option, the
     single-character option is returned.

๐‘๐„๐“๐”๐‘๐ ๐•๐€๐‹๐”๐„๐’
     If the fฬฒlฬฒaฬฒgฬฒ field in struct option is NULL, ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() and
     ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด_๐—ผ๐—ป๐—น๐˜†() return the value specified in the vฬฒaฬฒlฬฒ field,
     which is usually just the corresponding short option.  If fฬฒlฬฒaฬฒgฬฒ is
     not NULL, these functions return 0 and store vฬฒaฬฒlฬฒ in the location
     pointed to by fฬฒlฬฒaฬฒgฬฒ.  These functions return โ€˜:โ€™ if there was a
     missing option argument, โ€˜?โ€™ if the user specified an unknown or
     ambiguous option, and -1 when the argument list has been exhausted.

๐ˆ๐Œ๐๐‹๐„๐Œ๐„๐๐“๐€๐“๐ˆ๐Ž๐ ๐ƒ๐ˆ๐…๐…๐„๐‘๐„๐๐‚๐„๐’
     This section describes differences to the GNU implementation found
     in glibc-2.1.3:

     ยท   handling of โ€˜-โ€™ within the option string (not the first characโ€
         ter):

         GNU      treats a โ€˜-โ€™ on the command line as a non-argument.

         OpenBSD  a โ€˜-โ€™ within the option string matches a โ€˜-โ€™ (single
                  dash) on the command line.  This functionality is proโ€
                  vided for backward compatibility with programs, such
                  as su(1), that use โ€˜-โ€™ as an option flag.  This pracโ€
                  tice is wrong, and should not be used in any current
                  development.

     ยท   handling of โ€˜::โ€™ in the option string in the presence of
         POSIXLY_CORRECT:

         Both     GNU and OpenBSD ignore POSIXLY_CORRECT here and take
                  โ€˜::โ€™ to mean the preceding option takes an optional
                  argument.

     ยท   return value in case of missing argument if first character
         (after โ€˜+โ€™ or โ€˜-โ€™) in the option string is not โ€˜:โ€™:

         GNU      returns โ€˜?โ€™

         OpenBSD  returns โ€˜:โ€™ (since OpenBSD's getopt(3) does).

     ยท   handling of โ€˜--aโ€™ in getopt(3):

         GNU      parses this as option โ€˜-โ€™, option โ€˜aโ€™.

         OpenBSD  parses this as โ€˜--โ€™, and returns -1 (ignoring the โ€˜aโ€™)
                  (because the original ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜() did.)

     ยท   setting of oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ for long options with fฬฒlฬฒaฬฒgฬฒ non-NULL:

         GNU      sets oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ to vฬฒaฬฒlฬฒ.

         OpenBSD  sets oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ to 0 (since vฬฒaฬฒlฬฒ would never be returned).

     ยท   handling of โ€˜-Wโ€™ with โ€˜W;โ€™ in the option string in getopt(3)
         (not ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด()):

         GNU      causes a segmentation fault.

         OpenBSD  no special handling is done; โ€˜W;โ€™ is interpreted as
                  two separate options, neither of which take an arguโ€
                  ment.

     ยท   setting of oฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ for long options without an argument that are
         invoked via โ€˜-Wโ€™ (with โ€˜W;โ€™ in the option string):

         GNU      sets oฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ to the option name (the argument of โ€˜-Wโ€™).

         OpenBSD  sets oฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ to NULL (the argument of the long option).

     ยท   handling of โ€˜-Wโ€™ with an argument that is not (a prefix to) a
         known long option (with โ€˜W;โ€™ in the option string):

         GNU      returns โ€˜-Wโ€™ with oฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ set to the unknown option.

         OpenBSD  treats this as an error (unknown option) and returns
                  โ€˜?โ€™ with oฬฒpฬฒtฬฒoฬฒpฬฒtฬฒ set to 0 and oฬฒpฬฒtฬฒaฬฒrฬฒgฬฒ set to NULL (as
                  GNU's man page documents).

     ยท   The error messages are different.

     ยท   OpenBSD does not permute the argument vector at the same points
         in the calling sequence as GNU does.  The aspects normally used
         by the caller (ordering after -1 is returned, value of oฬฒpฬฒtฬฒiฬฒnฬฒdฬฒ
         relative to current positions) are the same, though.  (We do
         fewer variable swaps.)

๐„๐๐•๐ˆ๐‘๐Ž๐๐Œ๐„๐๐“
     POSIXLY_CORRECT  If set, option processing stops when the first
                      non-option is found and a leading โ€˜+โ€™ in the
                      oฬฒpฬฒtฬฒsฬฒtฬฒrฬฒiฬฒnฬฒgฬฒ is ignored.

๐„๐—๐€๐Œ๐๐‹๐„๐’
     int bflag, ch, fd;
     int daggerset;

     /* options descriptor */
     static struct option longopts[] = {
             { "buffy",      no_argument,            NULL,           'b' },
             { "fluoride",   required_argument,      NULL,           'f' },
             { "daggerset",  no_argument,            &daggerset,     1 },
             { NULL,         0,                      NULL,           0 }
     };

     bflag = 0;
     while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
             switch (ch) {
             case 'b':
                     bflag = 1;
                     break;
             case 'f':
                     if ((fd = open(optarg, O_RDONLY, 0)) == -1)
                             err(1, "unable to open %s", optarg);
                     break;
             case 0:
                     if (daggerset)
                             fprintf(stderr, "Buffy will use her dagger to "
                                 "apply fluoride to dracula's teeth\n");
                     break;
             default:
                     usage();
             }
     argc -= optind;
     argv += optind;

๐’๐„๐„ ๐€๐‹๐’๐Ž
     getopt(3)

๐‡๐ˆ๐’๐“๐Ž๐‘๐˜
     The ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด() and ๐—ด๐—ฒ๐˜๐—ผ๐—ฝ๐˜_๐—น๐—ผ๐—ป๐—ด_๐—ผ๐—ป๐—น๐˜†() functions first appeared
     in GNU libiberty.  This implementation first appeared in
     OpenBSD 3.3.

๐๐”๐†๐’
     The aฬฒrฬฒgฬฒvฬฒ argument is not really const as its elements may be perโ€
     muted (unless POSIXLY_CORRECT is set).

COSMOPOLITAN                    January 4, 2016                            BSD