325 lines
12 KiB
HTML
325 lines
12 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETSUBOPT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETSUBOPT</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getsubopt - parse suboption arguments from a string
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int getsubopt(char **optionp</B><I>, char * const *</I><B>tokens</B><I>, char **</I><B>valuep</B><I>);</I>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>getsubopt</B>():
|
|
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
|
|
_XOPEN_SOURCE >= 500
|
|
|
|
<BR>
|
|
|
|
<BR> || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
|
|
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>getsubopt</B>()
|
|
|
|
parses the list of comma-separated suboptions provided in
|
|
<I>optionp</I>.
|
|
|
|
(Such a suboption list is typically produced when
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getopt">getopt</A></B>(3)
|
|
|
|
is used to parse a command line;
|
|
see for example the <I>-o</I> option of
|
|
<B><A HREF="/cgi-bin/man/man2html?8+mount">mount</A></B>(8).)
|
|
|
|
Each suboption may include an associated value,
|
|
which is separated from the suboption name by an equal sign.
|
|
The following is an example of the kind of string
|
|
that might be passed in
|
|
<I>optionp</I>:
|
|
|
|
<P>
|
|
|
|
|
|
|
|
<B>ro,name=xyz</B>
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>tokens</I>
|
|
|
|
argument is a pointer to a NULL-terminated array of pointers to the tokens that
|
|
<B>getsubopt</B>()
|
|
|
|
will look for in
|
|
<I>optionp</I>.
|
|
|
|
The tokens should be distinct, null-terminated strings containing at
|
|
least one character, with no embedded equal signs or commas.
|
|
<P>
|
|
|
|
Each call to
|
|
<B>getsubopt</B>()
|
|
|
|
returns information about the next unprocessed suboption in
|
|
<I>optionp</I>.
|
|
|
|
The first equal sign in a suboption (if any) is interpreted as a
|
|
separator between the name and the value of that suboption.
|
|
The value extends to the next comma,
|
|
or (for the last suboption) to the end of the string.
|
|
If the name of the suboption matches a known name from
|
|
<I>tokens</I>,
|
|
|
|
and a value string was found,
|
|
<B>getsubopt</B>()
|
|
|
|
sets
|
|
<I>*valuep</I>
|
|
|
|
to the address of that string.
|
|
The first comma in
|
|
<I>optionp</I>
|
|
|
|
is overwritten with a null byte, so
|
|
<I>*valuep</I>
|
|
|
|
is precisely the "value string" for that suboption.
|
|
<P>
|
|
|
|
If the suboption is recognized, but no value string was found,
|
|
<I>*valuep</I>
|
|
|
|
is set to NULL.
|
|
<P>
|
|
|
|
When
|
|
<B>getsubopt</B>()
|
|
|
|
returns,
|
|
<I>optionp</I>
|
|
|
|
points to the next suboption,
|
|
or to the null byte ('\0') at the end of the
|
|
string if the last suboption was just processed.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
If the first suboption in
|
|
<I>optionp</I>
|
|
|
|
is recognized,
|
|
<B>getsubopt</B>()
|
|
|
|
returns the index of the matching suboption element in
|
|
<I>tokens</I>.
|
|
|
|
Otherwise, -1 is returned and
|
|
<I>*valuep</I>
|
|
|
|
is the entire
|
|
<I>name</I><B>[=</B><I>value</I><B>]</B>
|
|
|
|
string.
|
|
<P>
|
|
|
|
Since
|
|
<I>*optionp</I>
|
|
|
|
is changed, the first suboption before the call to
|
|
<B>getsubopt</B>()
|
|
|
|
is not (necessarily) the same as the first suboption after
|
|
<B>getsubopt</B>().
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getsubopt</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<P>
|
|
|
|
Since
|
|
<B>getsubopt</B>()
|
|
|
|
overwrites any commas it finds in the string
|
|
<I>*optionp</I>,
|
|
|
|
that string must be writable; it cannot be a string constant.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following program expects suboptions following a "-o" option.
|
|
<P>
|
|
|
|
|
|
#define _XOPEN_SOURCE 500
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/assert.h">assert.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
<BR> enum {
|
|
<BR> RO_OPT = 0,
|
|
<BR> RW_OPT,
|
|
<BR> NAME_OPT
|
|
<BR> };
|
|
<BR> char *const token[] = {
|
|
<BR> [RO_OPT] = "ro",
|
|
<BR> [RW_OPT] = "rw",
|
|
<BR> [NAME_OPT] = "name",
|
|
<BR> NULL
|
|
<BR> };
|
|
<BR> char *subopts;
|
|
<BR> char *value;
|
|
<BR> int opt;
|
|
<P>
|
|
<BR> int readonly = 0;
|
|
<BR> int readwrite = 0;
|
|
<BR> char *name = NULL;
|
|
<BR> int errfnd = 0;
|
|
<P>
|
|
<BR> while ((opt = getopt(argc, argv, "o:")) != -1) {
|
|
<BR> switch (opt) {
|
|
<BR> case 'o':
|
|
<BR> subopts = optarg;
|
|
<BR> while (*subopts != '\0' && !errfnd) {
|
|
<P>
|
|
<BR> switch (getsubopt(&subopts, token, &value)) {
|
|
<BR> case RO_OPT:
|
|
<BR> readonly = 1;
|
|
<BR> break;
|
|
<P>
|
|
<BR> case RW_OPT:
|
|
<BR> readwrite = 1;
|
|
<BR> break;
|
|
<P>
|
|
<BR> case NAME_OPT:
|
|
<BR> if (value == NULL) {
|
|
<BR> fprintf(stderr, "Missing value for "
|
|
<BR> "suboption '%s'\n", token[NAME_OPT]);
|
|
<BR> errfnd = 1;
|
|
<BR> continue;
|
|
<BR> }
|
|
<P>
|
|
<BR> name = value;
|
|
<BR> break;
|
|
<P>
|
|
<BR> default:
|
|
<BR> fprintf(stderr, "No match found "
|
|
<BR> "for token: /%s/\n", value);
|
|
<BR> errfnd = 1;
|
|
<BR> break;
|
|
<BR> }
|
|
<BR> }
|
|
<BR> if (readwrite && readonly) {
|
|
<BR> fprintf(stderr, "Only one of '%s' and '%s' can be "
|
|
<BR> "specified\n", token[RO_OPT], token[RW_OPT]);
|
|
<BR> errfnd = 1;
|
|
<BR> }
|
|
<BR> break;
|
|
<P>
|
|
<BR> default:
|
|
<BR> errfnd = 1;
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> if (errfnd || argc == 1) {
|
|
<BR> fprintf(stderr, "\nUsage: %s -o <suboptstring>\n", argv[0]);
|
|
<BR> fprintf(stderr, "suboptions are 'ro', 'rw', "
|
|
<BR> "and 'name=<value>'\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Remainder of program... */
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getopt">getopt</A></B>(3)
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="5"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="6"><A HREF="#lbAF">ATTRIBUTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="8"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="9"><A HREF="#lbAI">EXAMPLE</A><DD>
|
|
<DT id="10"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="11"><A HREF="#lbAK">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:44 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|