cmdline.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. @option will only be found if it is found
  23. * as an entire word in @cmdline. For instance, if @option="car"
  24. * then a cmdline which contains "cart" will not match.
  25. */
  26. static int
  27. __cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
  28. const char *option)
  29. {
  30. char c;
  31. int pos = 0, wstart = 0;
  32. const char *opptr = NULL;
  33. enum {
  34. st_wordstart = 0, /* Start of word/after whitespace */
  35. st_wordcmp, /* Comparing this word */
  36. st_wordskip, /* Miscompare, skip */
  37. } state = st_wordstart;
  38. if (!cmdline)
  39. return -1; /* No command line */
  40. /*
  41. * This 'pos' check ensures we do not overrun
  42. * a non-NULL-terminated 'cmdline'
  43. */
  44. while (pos < max_cmdline_size) {
  45. c = *(char *)cmdline++;
  46. pos++;
  47. switch (state) {
  48. case st_wordstart:
  49. if (!c)
  50. return 0;
  51. else if (myisspace(c))
  52. break;
  53. state = st_wordcmp;
  54. opptr = option;
  55. wstart = pos;
  56. /* fall through */
  57. case st_wordcmp:
  58. if (!*opptr) {
  59. /*
  60. * We matched all the way to the end of the
  61. * option we were looking for. If the
  62. * command-line has a space _or_ ends, then
  63. * we matched!
  64. */
  65. if (!c || myisspace(c))
  66. return wstart;
  67. /*
  68. * We hit the end of the option, but _not_
  69. * the end of a word on the cmdline. Not
  70. * a match.
  71. */
  72. } else if (!c) {
  73. /*
  74. * Hit the NULL terminator on the end of
  75. * cmdline.
  76. */
  77. return 0;
  78. } else if (c == *opptr++) {
  79. /*
  80. * We are currently matching, so continue
  81. * to the next character on the cmdline.
  82. */
  83. break;
  84. }
  85. state = st_wordskip;
  86. /* fall through */
  87. case st_wordskip:
  88. if (!c)
  89. return 0;
  90. else if (myisspace(c))
  91. state = st_wordstart;
  92. break;
  93. }
  94. }
  95. return 0; /* Buffer overrun */
  96. }
  97. int cmdline_find_option_bool(const char *cmdline, const char *option)
  98. {
  99. return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
  100. }