getsubopt(3) Library Functions Manual getsubopt(3) getsubopt - LIBRARY Standard C library (libc, -lc) #include int getsubopt(char **restrict optionp, char *const *restrict tokens, char **restrict valuep); glibc (. feature_test_macros(7)): getsubopt(): _XOPEN_SOURCE >= 500 || /* glibc 2.12: */ _POSIX_C_SOURCE >= 200809L getsubopt() , , optionp ( , , getopt(3) ; , -o mount(8)). , . , optionp: ro,name=xyz tokens -- ( NULL) , getsubopt() optionp. ( null), , , . getsubopt() optionp. ( ) . ( ) . tokens, -, getsubopt() *valuep . optionp null, *valuep -- <<->> . , , *valuep NULL. When getsubopt() returns, optionp points to the next suboption, or to the null byte ('\0') at the end of the string if the last suboption was just processed. optionp , getsubopt() tokens . -1 *valuep name[=value]. *optionp , getsubopt() ( ) , getsubopt(). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getsubopt() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ POSIX.1-2008. POSIX.1-2001. getsubopt() , *optionp, ; . <<-o>>. #define _XOPEN_SOURCE 500 #include #include #include int main(int argc, char *argv[]) { 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; while ((opt = getopt(argc, argv, "o:")) != -1) { switch (opt) { case 'o': subopts = optarg; while (*subopts != '\0' && !errfnd) { switch (getsubopt(&subopts, token, &value)) { case RO_OPT: readonly = 1; break; case RW_OPT: readwrite = 1; break; case NAME_OPT: if (value == NULL) { fprintf(stderr, "Missing value for suboption '%s'\n", token[NAME_OPT]); errfnd = 1; continue; } name = value; break; default: fprintf(stderr, "No match found for token: /%s/\n", value); errfnd = 1; break; } } if (readwrite && readonly) { fprintf(stderr, "Only one of '%s' and '%s' can be specified\n", token[RO_OPT], token[RW_OPT]); errfnd = 1; } break; default: errfnd = 1; } } if (errfnd || argc == 1) { fprintf(stderr, "\nUsage: %s -o \n", argv[0]); fprintf(stderr, "suboptions are 'ro', 'rw', and 'name='\n"); exit(EXIT_FAILURE); } /* Remainder of program... */ exit(EXIT_SUCCESS); } . getopt(3) Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . getsubopt(3)