blob: 335b003dddf9b220dbf10b50a845f71be21acf9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * getopt.c
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 * 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.
22 *
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.
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}