GETSUBOPT(3)               POSIX Programmer's Manual              GETSUBOPT(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       getsubopt -- parse suboption arguments from a string

SSYYNNOOPPSSIISS
       #include <stdlib.h>

       int getsubopt(char **_o_p_t_i_o_n_p, char * const *_k_e_y_l_i_s_t_p, char **_v_a_l_u_e_p);

DDEESSCCRRIIPPTTIIOONN
       The _g_e_t_s_u_b_o_p_t() function shall parse suboption arguments in a flag
       argument. Such options often result from the use of _g_e_t_o_p_t().

       The _g_e_t_s_u_b_o_p_t() argument _o_p_t_i_o_n_p 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>.

       The _k_e_y_l_i_s_t_p 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 *_o_p_t_i_o_n_p.
       Since <comma> characters delimit suboption arguments in _o_p_t_i_o_n_p, they
       should not appear in any of the strings pointed to by _k_e_y_l_i_s_t_p.
       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 _k_e_y_l_i_s_t_p.  The _g_e_t_s_u_b_o_p_t() function shall not
       modify the _k_e_y_l_i_s_t_p vector.

       The _v_a_l_u_e_p argument is the address of a value string pointer.

       If a <comma> appears in _o_p_t_i_o_n_p, 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.

       If the string at *_o_p_t_i_o_n_p contains only one suboption argument
       (equivalently, no <comma> characters), _g_e_t_s_u_b_o_p_t() shall update
       *_o_p_t_i_o_n_p 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 *_o_p_t_i_o_n_p to
       point to the start of the next suboption argument. If the suboption
       argument has an associated value (equivalently, contains an <equals-
       sign>), _g_e_t_s_u_b_o_p_t() shall update *_v_a_l_u_e_p to point to the value's first
       character.  Otherwise, it shall set *_v_a_l_u_e_p 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.

       Additionally, when _g_e_t_s_u_b_o_p_t() fails to match the suboption argument
       with a token in the _k_e_y_l_i_s_t_p array, the calling application should
       decide if this is an error, or if the unrecognized option should be
       processed in another way.

RREETTUURRNN VVAALLUUEE
       The _g_e_t_s_u_b_o_p_t() function shall return the index of the matched token
       string, or -1 if no token strings were matched.

EERRRROORRSS
       No errors are defined.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   PPaarrssiinngg SSuubbooppttiioonnss
       The following example uses the _g_e_t_s_u_b_o_p_t() function to parse a _v_a_l_u_e
       argument in the _o_p_t_a_r_g external variable returned by a call to
       _g_e_t_o_p_t().


           #include <stdio.h>
           #include <stdlib.h>
           #include <unistd.h>

           int do_all;
           const char *type;
           int read_size;
           int write_size;
           int read_only;

           enum
           {
               RO_OPTION = 0,
               RW_OPTION,
               READ_SIZE_OPTION,
               WRITE_SIZE_OPTION
           };

           const char *mount_opts[] =
           {
               [RO_OPTION] = "ro",
               [RW_OPTION] = "rw",
               [READ_SIZE_OPTION] = "rsize",
               [WRITE_SIZE_OPTION] = "wsize",
               NULL
           };

           int
           main(int argc, char *argv[])
           {
               char *subopts, *value;
               int opt;

               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 != ' ')
                           {
                               char *saved = subopts;
                               switch(getsubopt(&subopts, (char **)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", saved);
                                   abort();
                               }
                           }
                           break;
                       default:
                           abort();
                       }

               /* Do the real work. */

               return 0;
           }

       If the above example is invoked with:


           program -o ro,rsize=512

       then after option parsing, the variable _d_o___a_l_l will be 0, _t_y_p_e will be
       a null pointer, _r_e_a_d___s_i_z_e will be 512, _w_r_i_t_e___s_i_z_e will be 0, and
       _r_e_a_d___o_n_l_y will be 1. If it is invoked with:


           program -o oops

       it will print:


           "Unknown suboption `oops'"

       before aborting.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The value of *_v_a_l_u_e_p when _g_e_t_s_u_b_o_p_t() returns -1 is unspecified.
       Historical implementations provide various incompatible extensions to
       allow an application to access the suboption text that was not found in
       the _k_e_y_l_i_s_t_p array.

RRAATTIIOONNAALLEE
       The _k_e_y_l_i_s_t_p argument of _g_e_t_s_u_b_o_p_t() is typed as cchhaarr ** ccoonnsstt ** 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 ccoonnsstt cchhaarr **
       ccoonnsstt **.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _g_e_t_o_p_t()

       The Base Definitions volume of POSIX.1-2017, <<ssttddlliibb..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       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 .

       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 .



IEEE/The Open Group                  2017                         GETSUBOPT(3)
