The getsubopt() function parses suboptions in a flag argument that was initially parsed by getopt(3C). The suboptions are separated by commas and may consist of either a single token or a token-value pair separated by an equal sign. Since commas delimit suboptions in
the option string, they are not allowed to be part of the suboption or the value of a suboption; if present in the option input string, they are changed to null characters. White spaces within tokens or
token-value pairs must be protected from the shell by quotes.
The syntax described above is used in the following example by the mount(1M),
utility, which allows the user to specify mount parameters with the -o option as follows:
mount -o rw,hard,bg,wsize=1024 speed:/usr /usr
In this example there are four suboptions: rw, hard, bg, and wsize, the last of which has an associated value of 1024.
The getsubopt() function takes the address of a pointer to the option string, a vector of possible tokens, and the address of a value string pointer. It returns the index of the
token that matched the suboption in the input string, or -1 if there was no match. If the option string pointed to by optionp contains only one subobtion, getsubopt() updates optionp to point to the null character at the end of the string; otherwise it isolates the suboption by replacing the comma separator with a null character,
and updates optionp to point to the start of the next suboption. If the suboption has an associated value, getsubopt() updates valuep to
point to the value's first character. Otherwise it sets valuep to NULL.
The token vector is organized as a series of pointers to null strings. The end of the token vector is identified by a null pointer.
When getsubopt() returns, a non-null value for valuep indicates that the suboption that was processed included a value. The calling program may use this
information to determine if the presence or absence of a value for this subobtion is an error.
When getsubopt() fails to match the suboption with the tokens in the tokens array, the calling program should decide if this is an error, or if the unrecognized
option should be passed to another program.