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.
28 int cmdline_find_option_bool(const char *cmdline, const char *option)
31 int pos = 0, wstart = 0;
32 const char *opptr = NULL;
34 st_wordstart = 0, /* Start of word/after whitespace */
35 st_wordcmp, /* Comparing this word */
36 st_wordskip, /* Miscompare, skip */
37 } state = st_wordstart;
40 return -1; /* No command line */
46 * This 'pos' check ensures we do not overrun
47 * a non-NULL-terminated 'cmdline'
49 while (pos < COMMAND_LINE_SIZE) {
50 c = *(char *)cmdline++;
57 else if (myisspace(c))
68 * We matched all the way to the end of the
69 * option we were looking for. If the
70 * command-line has a space _or_ ends, then
73 if (!c || myisspace(c))
79 * Hit the NULL terminator on the end of
83 } else if (c != *opptr++) {
91 else if (myisspace(c))
97 return 0; /* Buffer overrun */
101 * Find a non-boolean option (i.e. option=argument). In accordance with
102 * standard Linux practice, if this option is repeated, this returns the
103 * last instance on the command line.
105 * @cmdline: the cmdline string
106 * @max_cmdline_size: the maximum size of cmdline
107 * @option: option string to look for
108 * @buffer: memory buffer to return the option argument
109 * @bufsize: size of the supplied memory buffer
111 * Returns the length of the argument (regardless of if it was
112 * truncated to fit in the buffer), or -1 on not found.
115 __cmdline_find_option(const char *cmdline, int max_cmdline_size,
116 const char *option, char *buffer, int bufsize)
119 int pos = 0, len = -1;
120 const char *opptr = NULL;
121 char *bufptr = buffer;
123 st_wordstart = 0, /* Start of word/after whitespace */
124 st_wordcmp, /* Comparing this word */
125 st_wordskip, /* Miscompare, skip */
126 st_bufcpy, /* Copying this to buffer */
127 } state = st_wordstart;
130 return -1; /* No command line */
133 * This 'pos' check ensures we do not overrun
134 * a non-NULL-terminated 'cmdline'
136 while (pos++ < max_cmdline_size) {
137 c = *(char *)cmdline++;
151 if ((c == '=') && !*opptr) {
153 * We matched all the way to the end of the
154 * option we were looking for, prepare to
161 } else if (c == *opptr++) {
163 * We are currently matching, so continue
164 * to the next character on the cmdline.
173 state = st_wordstart;
178 state = st_wordstart;
181 * Increment len, but don't overrun the
182 * supplied buffer and leave room for the
198 int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
201 return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,