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
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