Linux 2.4.0-test7-pre5
[davej-history.git] / fs / smbfs / getopt.c
blob0c5d111b3fa8cc11f3adf2f20e3581d00b4dc2af
1 /*
2 * getopt.c
3 */
5 #include <linux/kernel.h>
6 #include <linux/string.h>
8 #include "getopt.h"
10 /**
11 * smb_getopt - option parser
12 * @caller: name of the caller, for error messages
13 * @options: the options string
14 * @opts: an array of &struct option entries controlling parser operations
15 * @optopt: output; will contain the current option
16 * @optarg: output; will contain the value (if one exists)
17 * @flag: output; may be NULL; should point to a long for or'ing flags
18 * @value: output; may be NULL; will be overwritten with the integer value
19 * of the current argument.
21 * Helper to parse options on the format used by mount ("a=b,c=d,e,f").
22 * Returns opts->val if a matching entry in the 'opts' array is found,
23 * 0 when no more tokens are found, -1 if an error is encountered.
25 int smb_getopt(char *caller, char **options, struct option *opts,
26 char **optopt, char **optarg, unsigned long *flag,
27 unsigned long *value)
29 char *token;
30 char *val;
31 int i;
33 if ( (token = strsep(options, ",")) == NULL)
34 return 0;
35 *optopt = token;
37 *optarg = NULL;
38 if ((val = strchr (token, '=')) != NULL) {
39 *val++ = 0;
40 if (value)
41 *value = simple_strtoul(val, NULL, 0);
42 *optarg = val;
45 for (i = 0; opts[i].name != NULL; i++) {
46 if (!strcmp(opts[i].name, token)) {
47 if (opts[i].has_arg && (!val || !*val)) {
48 printk("%s: the %s option requires an argument\n",
49 caller, token);
50 return -1;
53 if (flag && opts[i].flag)
54 *flag |= opts[i].flag;
56 return opts[i].val;
59 printk("%s: Unrecognized mount option %s\n", caller, token);
60 return -1;