Linux v2.6.16-rc1
[linux-2.6/next.git] / fs / ncpfs / getopt.c
blob335b003dddf9b220dbf10b50a845f71be21acf9f
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 * @flag: output; may be NULL; should point to a long for or'ing flags
20 * @value: output; may be NULL; will be overwritten with the integer value
21 * of the current argument.
23 * Helper to parse options on the format used by mount ("a=b,c=d,e,f").
24 * Returns opts->val if a matching entry in the 'opts' array is found,
25 * 0 when no more tokens are found, -1 if an error is encountered.
27 int ncp_getopt(const char *caller, char **options, const struct ncp_option *opts,
28 char **optopt, char **optarg, unsigned long *value)
30 char *token;
31 char *val;
33 do {
34 if ((token = strsep(options, ",")) == NULL)
35 return 0;
36 } while (*token == '\0');
37 if (optopt)
38 *optopt = token;
40 if ((val = strchr (token, '=')) != NULL) {
41 *val++ = 0;
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;
50 printk(KERN_INFO "%s: the %s option requires an argument\n",
51 caller, token);
52 return -EINVAL;
54 if (opts->has_arg & OPT_INT) {
55 char* v;
57 *value = simple_strtoul(val, &v, 0);
58 if (!*v) {
59 return opts->val;
61 printk(KERN_INFO "%s: invalid numeric value in %s=%s\n",
62 caller, token, val);
63 return -EDOM;
65 if (opts->has_arg & OPT_STRING) {
66 return opts->val;
68 printk(KERN_INFO "%s: unexpected argument %s to the %s option\n",
69 caller, val, token);
70 return -EINVAL;
73 printk(KERN_INFO "%s: Unrecognized mount option %s\n", caller, token);
74 return -EOPNOTSUPP;