getopt.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:2k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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