1
2
3
4
5#include <linux/kernel.h>
6#include <linux/string.h>
7
8#include <asm/errno.h>
9
10#include "getopt.h"
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27int ncp_getopt(const char *caller, char **options, const struct ncp_option *opts,
28 char **optopt, char **optarg, unsigned long *value)
29{
30 char *token;
31 char *val;
32
33 do {
34 if ((token = strsep(options, ",")) == NULL)
35 return 0;
36 } while (*token == '\0');
37 if (optopt)
38 *optopt = token;
39
40 if ((val = strchr (token, '=')) != NULL) {
41 *val++ = 0;
42 }
43 *optarg = val;
44 for (; opts->name; opts++) {
45 if (!strcmp(opts->name, token)) {
46 if (!val) {
47 if (opts->has_arg & OPT_NOPARAM) {
48 return opts->val;
49 }
50 printk(KERN_INFO "%s: the %s option requires an argument\n",
51 caller, token);
52 return -EINVAL;
53 }
54 if (opts->has_arg & OPT_INT) {
55 char* v;
56
57 *value = simple_strtoul(val, &v, 0);
58 if (!*v) {
59 return opts->val;
60 }
61 printk(KERN_INFO "%s: invalid numeric value in %s=%s\n",
62 caller, token, val);
63 return -EDOM;
64 }
65 if (opts->has_arg & OPT_STRING) {
66 return opts->val;
67 }
68 printk(KERN_INFO "%s: unexpected argument %s to the %s option\n",
69 caller, val, token);
70 return -EINVAL;
71 }
72 }
73 printk(KERN_INFO "%s: Unrecognized mount option %s\n", caller, token);
74 return -EOPNOTSUPP;
75}
76