1 // SPDX-License-Identifier: GPL-2.0
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/string.h>
11 #include <asm/errno.h>
16 * ncp_getopt - option parser
17 * @caller: name of the caller, for error messages
18 * @options: the options string
19 * @opts: an array of &struct option entries controlling parser operations
20 * @optopt: output; will contain the current option
21 * @optarg: output; will contain the value (if one exists)
22 * @value: output; may be NULL; will be overwritten with the integer value
23 * of the current argument.
25 * Helper to parse options on the format used by mount ("a=b,c=d,e,f").
26 * Returns opts->val if a matching entry in the 'opts' array is found,
27 * 0 when no more tokens are found, -1 if an error is encountered.
29 int ncp_getopt(const char *caller
, char **options
, const struct ncp_option
*opts
,
30 char **optopt
, 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 pr_info("%s: the %s option requires an argument\n",
56 if (opts
->has_arg
& OPT_INT
) {
57 int rc
= kstrtoul(val
, 0, value
);
60 pr_info("%s: invalid numeric value in %s=%s\n",
66 if (opts
->has_arg
& OPT_STRING
) {
69 pr_info("%s: unexpected argument %s to the %s option\n",
74 pr_info("%s: Unrecognized mount option %s\n", caller
, token
);