getopt.c 1.6 KB

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