{"id":5280,"date":"2022-12-20T18:37:16","date_gmt":"2022-12-20T21:37:16","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getsubopt-man3p\/"},"modified":"2022-12-20T18:37:16","modified_gmt":"2022-12-20T21:37:16","slug":"getsubopt-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getsubopt-man3p\/","title":{"rendered":"GETSUBOPT (man3p)"},"content":{"rendered":"<h1 align=\"center\">GETSUBOPT<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">getsubopt \u2014 parse suboption arguments from a string<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <stdlib.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int getsubopt(char **<i>optionp<\/i>, char bodies manpages.csv script_extrae_body.sh script.sh usr const *<i>keylistp<\/i>, char **<i>valuep<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getsubopt<\/i>() function shall parse suboption arguments in a flag argument. Such options often result from the use of <i>getopt<\/i>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getsubopt<\/i>() argument <i>optionp<\/i> is a pointer to a pointer to the option argument string. The suboption arguments shall be separated by <comma> characters and each may consist of either a single token, or a token-value pair separated by an <equals-sign>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>keylistp<\/i> argument shall be a pointer to a vector of strings. The end of the vector is identified by a null pointer. Each entry in the vector is one of the possible tokens that might be found in *<i>optionp<\/i>. Since <comma> characters delimit suboption arguments in <i>optionp<\/i>, they should not appear in any of the strings pointed to by <i>keylistp<\/i>. Similarly, because an <equals-sign> separates a token from its value, the application should not include an <equals-sign> in any of the strings pointed to by <i>keylistp<\/i>. The <i>getsubopt<\/i>() function shall not modify the <i>keylistp<\/i> vector.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>valuep<\/i> argument is the address of a value string pointer.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If a <comma> appears in <i>optionp<\/i>, it shall be interpreted as a suboption separator. After <comma> characters have been processed, if there are one or more <equals-sign> characters in a suboption string, the first <equals-sign> in any suboption string shall be interpreted as a separator between a token and a value. Subsequent <equals-sign> characters in a suboption string shall be interpreted as part of the value.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the string at *<i>optionp<\/i> contains only one suboption argument (equivalently, no <comma> characters), <i>getsubopt<\/i>() shall update *<i>optionp<\/i> to point to the null character at the end of the string. Otherwise, it shall isolate the suboption argument by replacing the <comma> separator with a null character, and shall update *<i>optionp<\/i> to point to the start of the next suboption argument. If the suboption argument has an associated value (equivalently, contains an <equals-sign>), <i>getsubopt<\/i>() shall update *<i>valuep<\/i> to point to the value\u2019s first character. Otherwise, it shall set *<i>valuep<\/i> to a null pointer. The calling application may use this information to determine whether the presence or absence of a value for the suboption is an error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Additionally, when <i>getsubopt<\/i>() fails to match the suboption argument with a token in the <i>keylistp<\/i> array, the calling application should decide if this is an error, or if the unrecognized option should be processed in another way.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getsubopt<\/i>() function shall return the index of the matched token string, or \u22121 if no token strings were matched.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">No errors are defined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i><b>Parsing Suboptions<\/b><\/i> <br \/> The following example uses the <i>getsubopt<\/i>() function to parse a <i>value<\/i> argument in the <i>optarg<\/i> external variable returned by a call to <i>getopt<\/i>().<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <unistd.h><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int do_all; <br \/> const char *type; <br \/> int read_size; <br \/> int write_size; <br \/> int read_only;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">enum <br \/> { <br \/> RO_OPTION = 0, <br \/> RW_OPTION, <br \/> READ_SIZE_OPTION, <br \/> WRITE_SIZE_OPTION <br \/> };<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">const char *mount_opts[] = <br \/> { <br \/> [RO_OPTION] = &#8220;ro&#8221;, <br \/> [RW_OPTION] = &#8220;rw&#8221;, <br \/> [READ_SIZE_OPTION] = &#8220;rsize&#8221;, <br \/> [WRITE_SIZE_OPTION] = &#8220;wsize&#8221;, <br \/> NULL <br \/> };<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> char *subopts, *value; <br \/> int opt;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">while ((opt = getopt(argc, argv, &#8220;at:o:&#8221;)) != -1) <br \/> switch(opt) <br \/> { <br \/> case &#8216;a&#8217;: <br \/> do_all = 1; <br \/> break; <br \/> case &#8216;t&#8217;: <br \/> type = optarg; <br \/> break; <br \/> case &#8216;o&#8217;: <br \/> subopts = optarg; <br \/> while (*subopts != &#8216; &#8216;) <br \/> { <br \/> char *saved = subopts; <br \/> switch(getsubopt(&#038;subopts, (char **)mount_opts, <br \/> &#038;value)) <br \/> { <br \/> case RO_OPTION: <br \/> read_only = 1; <br \/> break; <br \/> case RW_OPTION: <br \/> read_only = 0; <br \/> break; <br \/> case READ_SIZE_OPTION: <br \/> if (value == NULL) <br \/> abort(); <br \/> read_size = atoi(value); <br \/> break; <br \/> case WRITE_SIZE_OPTION: <br \/> if (value == NULL) <br \/> abort(); <br \/> write_size = atoi(value); <br \/> break; <br \/> default: <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Unknown suboption. bodies\/ usr\/ <br \/> printf(&#8220;Unknown suboption \u2018%s&#8217;n&#8221;, saved); <br \/> abort(); <br \/> } <br \/> } <br \/> break; <br \/> default: <br \/> abort(); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* Do the real work. *\/<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">return 0; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the above example is invoked with:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">program -o ro,rsize=512<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">then after option parsing, the variable <i>do_all<\/i> will be 0, <i>type<\/i> will be a null pointer, <i>read_size<\/i> will be 512, <i>write_size<\/i> will be 0, and <i>read_only<\/i> will be 1. If it is invoked with:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">program -o oops<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">it will print:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">&#8220;Unknown suboption \u2018oops'&#8221;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">before aborting.<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The value of *<i>valuep<\/i> when <i>getsubopt<\/i>() returns \u22121 is unspecified. Historical implementations provide various incompatible extensions to allow an application to access the suboption text that was not found in the <i>keylistp<\/i> array.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>keylistp<\/i> argument of <i>getsubopt<\/i>() is typed as <b>char bodies manpages.csv script_extrae_body.sh script.sh usr const *<\/b> to match historical practice. However, the standard is clear that implementations will not modify either the array or the strings contained in the array, as if the argument had been typed <b>const char bodies manpages.csv script_extrae_body.sh script.sh usr const *<\/b>.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>getopt<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><stdlib.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  getsubopt \u2014 parse suboption arguments from a string <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[2169,1594],"class_list":["post-5280","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-getsubopt","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5280"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5280\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}