man-pages/man3/getsubopt.3

241 lines
6.2 KiB
Groff
Raw Normal View History

2007-09-20 06:52:22 +00:00
.\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
.\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
2007-05-04 22:21:25 +00:00
.\"
.\" %%%LICENSE_START(PERMISSIVE_MISC)
2007-05-04 22:21:25 +00:00
.\" Permission is hereby granted, free of charge, to any person obtaining
.\" a copy of this software and associated documentation files (the
.\" "Software"), to deal in the Software without restriction, including
.\" without limitation the rights to use, copy, modify, merge, publish,
.\" distribute, sublicense, and/or sell copies of the Software, and to
.\" permit persons to whom the Software is furnished to do so, subject to
.\" the following conditions:
.\"
.\" The above copyright notice and this permission notice shall be
.\" included in all copies or substantial portions of the Software.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
.\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
.\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
.\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
.\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
.\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.\" %%%LICENSE_END
2007-05-04 22:21:25 +00:00
.\"
mmap.2, a64l.3, abort.3, abs.3, acos.3, acosh.3, addseverity.3, adjtime.3, aio_cancel.3, aio_error.3, aio_fsync.3, aio_read.3, aio_return.3, aio_suspend.3, aio_write.3, alloca.3, argz_add.3, asin.3, asinh.3, assert.3, assert_perror.3, atan.3, atan2.3, atanh.3, atof.3, atoi.3, backtrace.3, basename.3, bcmp.3, bcopy.3, bindresvport.3, bsd_signal.3, btowc.3, byteorder.3, bzero.3, cabs.3, cacos.3, cacosh.3, canonicalize_file_name.3, carg.3, casin.3, casinh.3, catan.3, catanh.3, catgets.3, catopen.3, cbrt.3, ceil.3, cfree.3, cimag.3, clock_getcpuclockid.3, clog.3, clog10.3, closedir.3, confstr.3, conj.3, copysign.3, cos.3, cosh.3, cpow.3, cproj.3, creal.3, crypt.3, ctermid.3, daemon.3, des_crypt.3, difftime.3, dirfd.3, div.3, drand48.3, drand48_r.3, dysize.3, ecvt.3, ecvt_r.3, envz_add.3, erf.3, erfc.3, euidaccess.3, exec.3, exit.3, exp.3, exp10.3, exp2.3, expm1.3, fabs.3, fclose.3, fcloseall.3, fdim.3, fenv.3, ferror.3, fflush.3, ffs.3, fgetc.3, fgetwc.3, fgetws.3, finite.3, flockfile.3, floor.3, fma.3, fmax.3, fmin.3, fmod.3, fmtmsg.3, fnmatch.3, fopen.3, fopencookie.3, fpclassify.3, fread.3, frexp.3, ftime.3, ftok.3, futimes.3, gamma.3, getauxval.3, getcontext.3, getcwd.3, getdate.3, getdirentries.3, getdtablesize.3, getenv.3, getfsent.3, getgrent.3, getgrnam.3, getgrouplist.3, getloadavg.3, getlogin.3, getopt.3, getpass.3, getpt.3, getpwent.3, getpwnam.3, gets.3, getsubopt.3, getttyent.3, getusershell.3, getutmp.3, getw.3, gnu_get_libc_version.3, grantpt.3, hsearch.3, iconv.3, iconv_open.3, if_nametoindex.3, ilogb.3, index.3, inet.3, inet_pton.3, isalpha.3, isatty.3, isgreater.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, j0.3, ldexp.3, lio_listio.3, localeconv.3, lockf.3, log.3, log10.3, log1p.3, log2.3, logb.3, lrint.3, lround.3, lseek64.3, makecontext.3, makedev.3, malloc.3, malloc_usable_size.3, matherr.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbstowcs.3, mbtowc.3, memccpy.3, memchr.3, memcmp.3, memcpy.3, memfrob.3, memmem.3, memmove.3, mempcpy.3, memset.3, mkdtemp.3, mkfifo.3, mkstemp.3, modf.3, mq_close.3, mq_getattr.3, mq_open.3, mq_receive.3, mq_send.3, mq_unlink.3, mtrace.3, nan.3, nextafter.3, nl_langinfo.3, opendir.3, posix_fallocate.3, posix_openpt.3, pow.3, pow10.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_equal.3, pthread_exit.3, pthread_getcpuclockid.3, pthread_kill.3, pthread_kill_other_threads_np.3, pthread_self.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, pthread_sigmask.3, pthread_sigqueue.3, pthread_testcancel.3, pthread_yield.3, ptsname.3, putenv.3, puts.3, putwchar.3, qecvt.3, raise.3, rand.3, random.3, random_r.3, re_comp.3, readdir.3, realpath.3, regex.3, remainder.3, remove.3, remquo.3, rewinddir.3, rexec.3, rint.3, round.3, rtime.3, scalb.3, scalbln.3, sched_getcpu.3, seekdir.3, sem_destroy.3, sem_getvalue.3, sem_init.3, sem_post.3, sem_unlink.3, sem_wait.3, setbuf.3, setenv.3, siginterrupt.3, signbit.3, sigpause.3, sigqueue.3, sigset.3, sigsetops.3, sigvec.3, sigwait.3, sin.3, sincos.3, sinh.3, sockatmark.3, sqrt.3, stdarg.3, stdio_ext.3, stpcpy.3, stpncpy.3, strcasecmp.3, strcat.3, strchr.3, strcmp.3, strcpy.3, strerror.3, strfmon.3, strfry.3, strftime.3, strlen.3, strnlen.3, strpbrk.3, strptime.3, strsep.3, strspn.3, strstr.3, strtod.3, strtoimax.3, strtok.3, strtol.3, strtoul.3, strverscmp.3, strxfrm.3, swab.3, system.3, sysv_signal.3, tan.3, tanh.3, tcgetpgrp.3, tcgetsid.3, telldir.3, tempnam.3, termios.3, tgamma.3, timegm.3, tmpfile.3, tmpnam.3, toascii.3, toupper.3, towctrans.3, towlower.3, towupper.3, trunc.3, tsearch.3, ttyname.3, ttyslot.3, ualarm.3, ungetwc.3, unlockpt.3, usleep.3, wcpcpy.3, wcpncpy.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcspbrk.3, wcsrchr.3, wcsspn.3, wcsstr.3, wcstoimax.3, wcstok.3, wcswidth.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, y0.3, attributes.7, man-pages.7, standards.7: tstamp Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2015-03-02 15:12:18 +00:00
.TH GETSUBOPT 3 2015-03-02 "GNU" "Linux Programmer's Manual"
2007-05-04 22:21:25 +00:00
.SH NAME
getsubopt \- parse suboption arguments from a string
.SH SYNOPSIS
.B #include <stdlib.h>
2008-05-06 07:47:45 +00:00
.BI "int getsubopt(char **"optionp ", char * const *" tokens \
", char **" valuep );
.sp
.in -4n
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.in
.sp
.BR getsubopt ():
.ad l
.RS 4
.PD 0
_XOPEN_SOURCE\ >= 500 ||
_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
.br
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
.PD
.RE
.ad
2007-05-04 22:21:25 +00:00
.SH DESCRIPTION
.BR getsubopt ()
parses the list of comma-separated suboptions provided in
.IR optionp .
2007-06-21 22:55:04 +00:00
(Such a suboption list is typically produced when
2007-05-04 22:21:25 +00:00
.BR getopt (3)
is used to parse a command line;
2007-05-04 22:35:31 +00:00
see for example the \fI-o\fP option of
2007-05-04 22:21:25 +00:00
.BR mount (8).)
Each suboption may include an associated value,
which is separated from the suboption name by an equal sign.
2007-06-21 22:55:04 +00:00
The following is an example of the kind of string
that might be passed in
2007-05-04 22:21:25 +00:00
.IR optionp :
.sp
.in +4n
2007-05-04 22:21:25 +00:00
.B ro,name=xyz
.in
2007-05-04 22:21:25 +00:00
2007-06-21 22:55:04 +00:00
The
2007-05-04 22:21:25 +00:00
.I tokens
argument is a pointer to a NULL-terminated array of pointers to the tokens that
.BR getsubopt ()
2007-06-21 22:55:04 +00:00
will look for in
2007-05-04 22:21:25 +00:00
.IR optionp .
The tokens should be distinct, null-terminated strings containing at
least one character, with no embedded equal signs or commas.
Each call to
.BR getsubopt ()
returns information about the next unprocessed suboption in
.IR optionp .
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
.IR tokens ,
and a value string was found,
.BR getsubopt ()
sets
2007-12-22 22:10:40 +00:00
.I *valuep
2007-05-04 22:21:25 +00:00
to the address of that string.
The first comma in
.I optionp
is overwritten with a null byte, so
2007-12-22 22:10:40 +00:00
.I *valuep
2007-05-04 22:21:25 +00:00
is precisely the "value string" for that suboption.
If the suboption is recognized, but no value string was found,
2007-12-22 22:10:40 +00:00
.I *valuep
2007-05-04 22:21:25 +00:00
is set to NULL.
When
.BR getsubopt ()
returns,
.I optionp
points to the next suboption,
or to the null byte (\(aq\\0\(aq) at the end of the
2007-05-04 22:21:25 +00:00
string if the last suboption was just processed.
.SH RETURN VALUE
If the first suboption in
.I optionp
is recognized,
2007-05-04 22:21:25 +00:00
.BR getsubopt ()
returns the index of the matching suboption element in
2007-11-18 07:28:32 +00:00
.IR tokens .
2007-05-04 22:21:25 +00:00
Otherwise, \-1 is returned and
2007-12-22 22:10:40 +00:00
.I *valuep
2007-05-04 22:21:25 +00:00
is the entire
.IB name [= value ]
string.
Since
2007-12-22 22:10:40 +00:00
.I *optionp
2007-05-04 22:21:25 +00:00
is changed, the first suboption before the call to
.BR getsubopt ()
is not (necessarily) the same as the first suboption after
.BR getsubopt ().
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lb lb lb
l l l.
Interface Attribute Value
T{
.BR getsubopt ()
T} Thread safety MT-Safe
.TE
2007-05-04 22:21:25 +00:00
.SH CONFORMING TO
POSIX.1-2001.
.SH NOTES
Since
.BR getsubopt ()
overwrites any commas it finds in the string
2007-12-22 22:10:40 +00:00
.IR *optionp ,
2007-05-04 22:21:25 +00:00
that string must be writable; it cannot be a string constant.
.SH EXAMPLE
2007-11-17 04:27:41 +00:00
The following program expects suboptions following a "\-o" option.
2007-05-04 22:21:25 +00:00
.nf
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
int
main(int argc, char **argv)
2007-05-04 22:21:25 +00:00
{
enum {
RO_OPT = 0,
RW_OPT,
NAME_OPT
};
char *const token[] = {
[RO_OPT] = "ro",
[RW_OPT] = "rw",
[NAME_OPT] = "name",
NULL
};
char *subopts;
char *value;
int opt;
int readonly = 0;
int readwrite = 0;
char *name = NULL;
int errfnd = 0;
2007-06-20 21:39:45 +00:00
while ((opt = getopt(argc, argv, "o:")) != \-1) {
2007-06-20 20:52:14 +00:00
switch (opt) {
2008-06-09 15:49:35 +00:00
case \(aqo\(aq:
2007-05-04 22:21:25 +00:00
subopts = optarg;
2008-06-09 15:49:35 +00:00
while (*subopts != \(aq\\0\(aq && !errfnd) {
2007-05-04 22:21:25 +00:00
switch (getsubopt(&subopts, token, &value)) {
2007-06-22 04:59:15 +00:00
case RO_OPT:
readonly = 1;
break;
case RW_OPT:
readwrite = 1;
break;
case NAME_OPT:
if (value == NULL) {
fprintf(stderr, "Missing value for "
2008-06-09 15:49:35 +00:00
"suboption \(aq%s\(aq\\n", token[NAME_OPT]);
2007-05-04 22:21:25 +00:00
errfnd = 1;
2007-06-22 04:59:15 +00:00
continue;
2007-05-04 22:21:25 +00:00
}
2007-06-22 04:59:15 +00:00
name = value;
break;
default:
fprintf(stderr, "No match found "
"for token: /%s/\\n", value);
2007-05-04 22:21:25 +00:00
errfnd = 1;
2007-06-22 04:59:15 +00:00
break;
2007-05-04 22:21:25 +00:00
}
2007-06-22 04:59:15 +00:00
}
if (readwrite && readonly) {
2008-06-09 15:49:35 +00:00
fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
2007-06-22 04:59:15 +00:00
"specified\\n", token[RO_OPT], token[RW_OPT]);
errfnd = 1;
}
break;
2007-05-04 22:21:25 +00:00
default:
errfnd = 1;
}
}
if (errfnd || argc == 1) {
2007-06-20 21:39:45 +00:00
fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
2008-06-09 15:49:35 +00:00
fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
"and \(aqname=<value>\(aq\\n");
2007-05-04 22:21:25 +00:00
exit(EXIT_FAILURE);
}
/* Remainder of program... */
exit(EXIT_SUCCESS);
}
.fi
.SH SEE ALSO
.BR getopt (3)