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.
5 * Misc librarized functions for cmdline poking.
7 #include <linux/kernel.h>
8 #include <linux/string.h>
9 #include <linux/ctype.h>
10 #include <asm/setup.h>
12 static inline int myisspace(u8 c)
14 return c <= ' '; /* Close enough approximation */
18 * Find a boolean option (like quiet,noapic,nosmp....)
20 * @cmdline: the cmdline string
21 * @option: option string to look for
23 * Returns the position of that @option (starts counting with 1)
24 * or 0 on not found. @option will only be found if it is found
25 * as an entire word in @cmdline. For instance, if @option="car"
26 * then a cmdline which contains "cart" will not match.
29 __cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
33 int pos = 0, wstart = 0;
34 const char *opptr = NULL;
36 st_wordstart = 0, /* Start of word/after whitespace */
37 st_wordcmp, /* Comparing this word */
38 st_wordskip, /* Miscompare, skip */
39 } state = st_wordstart;
42 return -1; /* No command line */
45 * This 'pos' check ensures we do not overrun
46 * a non-NULL-terminated 'cmdline'
48 while (pos < max_cmdline_size) {
49 c = *(char *)cmdline++;
56 else if (myisspace(c))
67 * We matched all the way to the end of the
68 * option we were looking for. If the
69 * command-line has a space _or_ ends, then
72 if (!c || myisspace(c))
75 * We hit the end of the option, but _not_
76 * the end of a word on the cmdline. Not
81 * Hit the NULL terminator on the end of
85 } else if (c == *opptr++) {
87 * We are currently matching, so continue
88 * to the next character on the cmdline.
98 else if (myisspace(c))
104 return 0; /* Buffer overrun */
108 * Find a non-boolean option (i.e. option=argument). In accordance with
109 * standard Linux practice, if this option is repeated, this returns the
110 * last instance on the command line.
112 * @cmdline: the cmdline string
113 * @max_cmdline_size: the maximum size of cmdline
114 * @option: option string to look for
115 * @buffer: memory buffer to return the option argument
116 * @bufsize: size of the supplied memory buffer
118 * Returns the length of the argument (regardless of if it was
119 * truncated to fit in the buffer), or -1 on not found.
122 __cmdline_find_option(const char *cmdline, int max_cmdline_size,
123 const char *option, char *buffer, int bufsize)
126 int pos = 0, len = -1;
127 const char *opptr = NULL;
128 char *bufptr = buffer;
130 st_wordstart = 0, /* Start of word/after whitespace */
131 st_wordcmp, /* Comparing this word */
132 st_wordskip, /* Miscompare, skip */
133 st_bufcpy, /* Copying this to buffer */
134 } state = st_wordstart;
137 return -1; /* No command line */
140 * This 'pos' check ensures we do not overrun
141 * a non-NULL-terminated 'cmdline'
143 while (pos++ < max_cmdline_size) {
144 c = *(char *)cmdline++;
158 if ((c == '=') && !*opptr) {
160 * We matched all the way to the end of the
161 * option we were looking for, prepare to
168 } else if (c == *opptr++) {
170 * We are currently matching, so continue
171 * to the next character on the cmdline.
180 state = st_wordstart;
185 state = st_wordstart;
188 * Increment len, but don't overrun the
189 * supplied buffer and leave room for the
205 int cmdline_find_option_bool(const char *cmdline, const char *option)
207 return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
210 int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
213 return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,