Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cmdline.c
Go to the documentation of this file.
1 /*
2  * linux/lib/cmdline.c
3  * Helper functions generally used for parsing kernel command line
4  * and module options.
5  *
6  * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
7  *
8  * This source code is licensed under the GNU General Public License,
9  * Version 2. See the file COPYING for more details.
10  *
11  * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
12  *
13  */
14 
15 #include <linux/export.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 
19 /*
20  * If a hyphen was found in get_option, this will handle the
21  * range of numbers, M-N. This will expand the range and insert
22  * the values[M, M+1, ..., N] into the ints array in get_options.
23  */
24 
25 static int get_range(char **str, int *pint)
26 {
27  int x, inc_counter, upper_range;
28 
29  (*str)++;
30  upper_range = simple_strtol((*str), NULL, 0);
31  inc_counter = upper_range - *pint;
32  for (x = *pint; x < upper_range; x++)
33  *pint++ = x;
34  return inc_counter;
35 }
36 
52 int get_option (char **str, int *pint)
53 {
54  char *cur = *str;
55 
56  if (!cur || !(*cur))
57  return 0;
58  *pint = simple_strtol (cur, str, 0);
59  if (cur == *str)
60  return 0;
61  if (**str == ',') {
62  (*str)++;
63  return 2;
64  }
65  if (**str == '-')
66  return 3;
67 
68  return 1;
69 }
70 
88 char *get_options(const char *str, int nints, int *ints)
89 {
90  int res, i = 1;
91 
92  while (i < nints) {
93  res = get_option ((char **)&str, ints + i);
94  if (res == 0)
95  break;
96  if (res == 3) {
97  int range_nums;
98  range_nums = get_range((char **)&str, ints + i);
99  if (range_nums < 0)
100  break;
101  /*
102  * Decrement the result by one to leave out the
103  * last number in the range. The next iteration
104  * will handle the upper number in the range
105  */
106  i += (range_nums - 1);
107  }
108  i++;
109  if (res == 1)
110  break;
111  }
112  ints[0] = i - 1;
113  return (char *)str;
114 }
115 
129 unsigned long long memparse(const char *ptr, char **retptr)
130 {
131  char *endptr; /* local pointer to end of parsed string */
132 
133  unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
134 
135  switch (*endptr) {
136  case 'G':
137  case 'g':
138  ret <<= 10;
139  case 'M':
140  case 'm':
141  ret <<= 10;
142  case 'K':
143  case 'k':
144  ret <<= 10;
145  endptr++;
146  default:
147  break;
148  }
149 
150  if (retptr)
151  *retptr = endptr;
152 
153  return ret;
154 }
155 
156