2 * lifted from fs/ncpfs/getopt.c
10 #include <sys/libkern.h>
14 * krping_getopt - option parser
15 * @caller: name of the caller, for error messages
16 * @options: the options string
17 * @opts: an array of &struct option entries controlling parser operations
18 * @optopt: output; will contain the current option
19 * @optarg: output; will contain the value (if one exists)
20 * @flag: output; may be NULL; should point to a long for or'ing flags
21 * @value: output; may be NULL; will be overwritten with the integer value
22 * of the current argument.
24 * Helper to parse options on the format used by mount ("a=b,c=d,e,f").
25 * Returns opts->val if a matching entry in the 'opts' array is found,
26 * 0 when no more tokens are found, -1 if an error is encountered.
28 int krping_getopt(const char *caller, char **options,
29 const struct krping_option *opts, char **optopt,
30 char **optarg, unsigned long *value)
36 if ((token = strsep(options, ",")) == NULL)
38 } while (*token == '\0');
42 if ((val = strchr (token, '=')) != NULL) {
46 for (; opts->name; opts++) {
47 if (!strcmp(opts->name, token)) {
49 if (opts->has_arg & OPT_NOPARAM) {
52 printf("%s: the %s option requires "
53 "an argument\n", caller, token);
56 if (opts->has_arg & OPT_INT) {
59 *value = strtoul(val, &v, 0);
63 printf("%s: invalid numeric value "
64 "in %s=%s\n", caller, token, val);
67 if (opts->has_arg & OPT_STRING) {
70 printf("%s: unexpected argument %s to the "
71 "%s option\n", caller, val, token);
75 printf("%s: Unrecognized option %s\n", caller, token);