cmdline.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * This file is part of the Linux kernel, and is made available under
  3. * the terms of the GNU General Public License version 2.
  4. *
  5. * Misc librarized functions for cmdline poking.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/ctype.h>
  10. #include <asm/setup.h>
  11. static inline int myisspace(u8 c)
  12. {
  13. return c <= ' '; /* Close enough approximation */
  14. }
  15. /**
  16. * Find a boolean option (like quiet,noapic,nosmp....)
  17. *
  18. * @cmdline: the cmdline string
  19. * @option: option string to look for
  20. *
  21. * Returns the position of that @option (starts counting with 1)
  22. * or 0 on not found.
  23. */
  24. int cmdline_find_option_bool(const char *cmdline, const char *option)
  25. {
  26. char c;
  27. int len, pos = 0, wstart = 0;
  28. const char *opptr = NULL;
  29. enum {
  30. st_wordstart = 0, /* Start of word/after whitespace */
  31. st_wordcmp, /* Comparing this word */
  32. st_wordskip, /* Miscompare, skip */
  33. } state = st_wordstart;
  34. if (!cmdline)
  35. return -1; /* No command line */
  36. len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);
  37. if (!len)
  38. return 0;
  39. while (len--) {
  40. c = *(char *)cmdline++;
  41. pos++;
  42. switch (state) {
  43. case st_wordstart:
  44. if (!c)
  45. return 0;
  46. else if (myisspace(c))
  47. break;
  48. state = st_wordcmp;
  49. opptr = option;
  50. wstart = pos;
  51. /* fall through */
  52. case st_wordcmp:
  53. if (!*opptr)
  54. if (!c || myisspace(c))
  55. return wstart;
  56. else
  57. state = st_wordskip;
  58. else if (!c)
  59. return 0;
  60. else if (c != *opptr++)
  61. state = st_wordskip;
  62. else if (!len) /* last word and is matching */
  63. return wstart;
  64. break;
  65. case st_wordskip:
  66. if (!c)
  67. return 0;
  68. else if (myisspace(c))
  69. state = st_wordstart;
  70. break;
  71. }
  72. }
  73. return 0; /* Buffer overrun */
  74. }