Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
getopt.c
Go to the documentation of this file.
1 /*
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 
26 int ncp_getopt(const char *caller, char **options, const struct ncp_option *opts,
27  char **optopt, char **optarg, unsigned long *value)
28 {
29  char *token;
30  char *val;
31 
32  do {
33  if ((token = strsep(options, ",")) == NULL)
34  return 0;
35  } while (*token == '\0');
36  if (optopt)
37  *optopt = token;
38 
39  if ((val = strchr (token, '=')) != NULL) {
40  *val++ = 0;
41  }
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;
48  }
49  printk(KERN_INFO "%s: the %s option requires an argument\n",
50  caller, token);
51  return -EINVAL;
52  }
53  if (opts->has_arg & OPT_INT) {
54  char* v;
55 
56  *value = simple_strtoul(val, &v, 0);
57  if (!*v) {
58  return opts->val;
59  }
60  printk(KERN_INFO "%s: invalid numeric value in %s=%s\n",
61  caller, token, val);
62  return -EDOM;
63  }
64  if (opts->has_arg & OPT_STRING) {
65  return opts->val;
66  }
67  printk(KERN_INFO "%s: unexpected argument %s to the %s option\n",
68  caller, val, token);
69  return -EINVAL;
70  }
71  }
72  printk(KERN_INFO "%s: Unrecognized mount option %s\n", caller, token);
73  return -EOPNOTSUPP;
74 }