Add linux-next specific files for 20110716
[linux-2.6/next.git] / fs / ncpfs / getopt.c
blob0af3349de8512b83c50b9ad406af972de7180186
1 /*
2 * getopt.c
3 */
5 #include <linux/kernel.h>
6 #include <linux/string.h>
8 #include <asm/errno.h>
10 #include "getopt.h"
12 /**
13 * ncp_getopt - option parser
14 * @caller: name of the caller, for error messages
15 * @options: the options string
16 * @opts: an array of &struct option entries controlling parser operations
17 * @optopt: output; will contain the current option
18 * @optarg: output; will contain the value (if one exists)
19 * @value: output; may be NULL; will be overwritten with the integer value
20 * of the current argument.
22 * Helper to parse options on the format used by mount ("a=b,c=d,e,f").
23 * Returns opts->val if a matching entry in the 'opts' array is found,
24 * 0 when no more tokens are found, -1 if an error is encountered.
26 int ncp_getopt(const char *caller, char **options, const struct ncp_option *opts,
27 char **optopt, char **optarg, unsigned long *value)
29 char *token;
30 char *val;
32 do {
33 if ((token = strsep(options, ",")) == NULL)
34 return 0;
35 } while (*token == '\0');
36 if (optopt)
37 *optopt = token;
39 if ((val = strchr (token, '=')) != NULL) {
40 *val++ = 0;
42 *optarg = val;
43 for (; opts->name; opts++) {
44 if (!strcmp(opts->name, token)) {
45 if (!val) {
46 if (opts->has_arg & OPT_NOPARAM) {
47 return opts->val;
49 printk(KERN_INFO "%s: the %s option requires an argument\n",
50 caller, token);
51 return -EINVAL;
53 if (opts->has_arg & OPT_INT) {
54 char* v;
56 *value = simple_strtoul(val, &v, 0);
57 if (!*v) {
58 return opts->val;
60 printk(KERN_INFO "%s: invalid numeric value in %s=%s\n",
61 caller, token, val);
62 return -EDOM;
64 if (opts->has_arg & OPT_STRING) {
65 return opts->val;
67 printk(KERN_INFO "%s: unexpected argument %s to the %s option\n",
68 caller, val, token);
69 return -EINVAL;
72 printk(KERN_INFO "%s: Unrecognized mount option %s\n", caller, token);
73 return -EOPNOTSUPP;