Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * getopt.c |
| 3 | */ |
| 4 | |
Joe Perches | b41f8b8 | 2014-04-08 16:04:14 -0700 | [diff] [blame] | 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/string.h> |
| 9 | |
| 10 | #include <asm/errno.h> |
| 11 | |
| 12 | #include "getopt.h" |
| 13 | |
| 14 | /** |
| 15 | * ncp_getopt - option parser |
| 16 | * @caller: name of the caller, for error messages |
| 17 | * @options: the options string |
| 18 | * @opts: an array of &struct option entries controlling parser operations |
| 19 | * @optopt: output; will contain the current option |
| 20 | * @optarg: output; will contain the value (if one exists) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * @value: output; may be NULL; will be overwritten with the integer value |
| 22 | * of the current argument. |
| 23 | * |
| 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. |
| 27 | */ |
| 28 | int ncp_getopt(const char *caller, char **options, const struct ncp_option *opts, |
| 29 | char **optopt, char **optarg, unsigned long *value) |
| 30 | { |
| 31 | char *token; |
| 32 | char *val; |
| 33 | |
| 34 | do { |
| 35 | if ((token = strsep(options, ",")) == NULL) |
| 36 | return 0; |
| 37 | } while (*token == '\0'); |
| 38 | if (optopt) |
| 39 | *optopt = token; |
| 40 | |
| 41 | if ((val = strchr (token, '=')) != NULL) { |
| 42 | *val++ = 0; |
| 43 | } |
| 44 | *optarg = val; |
| 45 | for (; opts->name; opts++) { |
| 46 | if (!strcmp(opts->name, token)) { |
| 47 | if (!val) { |
| 48 | if (opts->has_arg & OPT_NOPARAM) { |
| 49 | return opts->val; |
| 50 | } |
Joe Perches | b41f8b8 | 2014-04-08 16:04:14 -0700 | [diff] [blame] | 51 | pr_info("%s: the %s option requires an argument\n", |
| 52 | caller, token); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return -EINVAL; |
| 54 | } |
| 55 | if (opts->has_arg & OPT_INT) { |
Fabian Frederick | 8091b89 | 2014-06-04 16:12:21 -0700 | [diff] [blame] | 56 | int rc = kstrtoul(val, 0, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Fabian Frederick | 8091b89 | 2014-06-04 16:12:21 -0700 | [diff] [blame] | 58 | if (rc) { |
| 59 | pr_info("%s: invalid numeric value in %s=%s\n", |
| 60 | caller, token, val); |
| 61 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
Fabian Frederick | 8091b89 | 2014-06-04 16:12:21 -0700 | [diff] [blame] | 63 | return opts->val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | if (opts->has_arg & OPT_STRING) { |
| 66 | return opts->val; |
| 67 | } |
Joe Perches | b41f8b8 | 2014-04-08 16:04:14 -0700 | [diff] [blame] | 68 | pr_info("%s: unexpected argument %s to the %s option\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | caller, val, token); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | } |
Joe Perches | b41f8b8 | 2014-04-08 16:04:14 -0700 | [diff] [blame] | 73 | pr_info("%s: Unrecognized mount option %s\n", caller, token); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return -EOPNOTSUPP; |
| 75 | } |