📄 getsubopt.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>getsubopt</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="getsubopt"></a> <a name="tag_03_253"></a><!-- getsubopt --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_253_01"></a>NAME</h4><blockquote>getsubopt - parse suboption arguments from a string</blockquote><h4><a name="tag_03_253_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/stdlib.h.html">stdlib.h</a>><br><br> int getsubopt(char **</tt><i>optionp</i><tt>, char * const *</tt><i>keylistp</i><tt>, char **</tt><i>valuep</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_253_03"></a>DESCRIPTION</h4><blockquote><p>The <i>getsubopt</i>() function shall parse suboption arguments in a flag argument. Such options often result from the use of <ahref="../functions/getopt.html"><i>getopt</i>()</a>.</p><p>The <i>getsubopt</i>() argument <i>optionp</i> is a pointer to a pointer to the option argument string. The suboption argumentsshall be separated by commas and each may consist of either a single token, or a token-value pair separated by an equal sign.</p><p>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 commas delimit suboptionarguments in <i>optionp</i>, they should not appear in any of the strings pointed to by <i>keylistp</i>. Similarly, because anequal sign separates a token from its value, the application should not include an equal sign in any of the strings pointed to by<i>keylistp</i>.</p><p>The <i>valuep</i> argument is the address of a value string pointer.</p><p>If a comma appears in <i>optionp</i>, it shall be interpreted as a suboption separator. After commas have been processed, ifthere are one or more equal signs in a suboption string, the first equal sign in any suboption string shall be interpreted as aseparator between a token and a value. Subsequent equal signs in a suboption string shall be interpreted as part of the value.</p><p>If the string at *<i>optionp</i> contains only one suboption argument (equivalently, no commas), <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 byreplacing the comma separator with a null character, and shall update *<i>optionp</i> to point to the start of the next suboptionargument. If the suboption argument has an associated value (equivalently, contains an equal sign), <i>getsubopt</i>() shall update*<i>valuep</i> to point to the value's first character. Otherwise, it shall set *<i>valuep</i> to a null pointer. The callingapplication may use this information to determine whether the presence or absence of a value for the suboption is an error.</p><p>Additionally, when <i>getsubopt</i>() fails to match the suboption argument with a token in the <i>keylistp</i> array, thecalling application should decide if this is an error, or if the unrecognized option should be processed in another way.</p></blockquote><h4><a name="tag_03_253_04"></a>RETURN VALUE</h4><blockquote><p>The <i>getsubopt</i>() function shall return the index of the matched token string, or -1 if no token strings were matched.</p></blockquote><h4><a name="tag_03_253_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_253_06"></a>EXAMPLES</h4><blockquote><pre><tt>#include <stdio.h>#include <stdlib.h><br>int do_all;const char *type;int read_size;int write_size;int read_only;<br>enum{ RO_OPTION = 0, RW_OPTION, READ_SIZE_OPTION, WRITE_SIZE_OPTION};<br>const char *mount_opts[] ={ [RO_OPTION] = "ro", [RW_OPTION] = "rw", [READ_SIZE_OPTION] = "rsize", [WRITE_SIZE_OPTION] = "wsize", NULL};<br>intmain(int argc, char *argv[]){ char *subopts, *value; int opt;<br> while ((opt = getopt(argc, argv, "at:o:")) != -1) switch(opt) { case 'a': do_all = 1; break; case 't': type = optarg; break; case 'o': subopts = optarg; while (*subopts != '\0') switch(getsubopt(&subopts, mount_opts, &value)) { case RO_OPTION: read_only = 1; break; case RW_OPTION: read_only = 0; break; case READ_SIZE_OPTION: if (value == NULL) abort(); read_size = atoi(value); break; case WRITE_SIZE_OPTION: if (value == NULL) abort(); write_size = atoi(value); break; default: /* Unknown suboption. */ printf("Unknown suboption `%s'\n", value); break; } break; default: abort(); }<br> /* Do the real work. */<br> return 0;}</tt></pre><h5><a name="tag_03_253_06_01"></a>Parsing Suboptions</h5><p>The following example uses the <i>getsubopt</i>() function to parse a <i>value</i> argument in the <i>optarg</i> externalvariable returned by a call to <a href="../functions/getopt.html"><i>getopt</i>()</a>.</p><pre><tt>#include <stdlib.h>...char *tokens[] = {"HOME", "PATH", "LOGNAME", (char *) NULL };char *value;int opt, index;<br>while ((opt = getopt(argc, argv, "e:")) != -1) { switch(opt) { case 'e' : while ((index = getsubopt(&optarg, tokens, &value)) != -1) { switch(index) {... } break;... }}...</tt></pre></blockquote><h4><a name="tag_03_253_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_253_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_253_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_253_10"></a>SEE ALSO</h4><blockquote><p><a href="getopt.html"><i>getopt</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/stdlib.h.html"><i><stdlib.h></i></a></p></blockquote><h4><a name="tag_03_253_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4, Version 2.</p></blockquote><h4><a name="tag_03_253_12"></a>Issue 5</h4><blockquote><p>Moved from X/OPEN UNIX extension to BASE.</p></blockquote><h4><a name="tag_03_253_13"></a>Issue 6</h4><blockquote><p>IEEE Std 1003.1-2001/Cor 1-2002, item XSH/TC1/D6/26 is applied, correcting an editorial error in the SYNOPSIS.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -