cmdline.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:3k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. /**
  18.  * get_option - Parse integer from an option string
  19.  * @str: option string
  20.  * @pint: (output) integer value parsed from @str
  21.  *
  22.  * Read an int from an option string; if available accept a subsequent
  23.  * comma as well.
  24.  *
  25.  * Return values:
  26.  * 0 : no int in string
  27.  * 1 : int found, no subsequent comma
  28.  * 2 : int found including a subsequent comma
  29.  */
  30. int get_option (char **str, int *pint)
  31. {
  32. char *cur = *str;
  33. if (!cur || !(*cur))
  34. return 0;
  35. *pint = simple_strtol (cur, str, 0);
  36. if (cur == *str)
  37. return 0;
  38. if (**str == ',') {
  39. (*str)++;
  40. return 2;
  41. }
  42. return 1;
  43. }
  44. /**
  45.  * get_options - Parse a string into a list of integers
  46.  * @str: String to be parsed
  47.  * @nints: size of integer array
  48.  * @ints: integer array
  49.  *
  50.  * This function parses a string containing a comma-separated
  51.  * list of integers.  The parse halts when the array is
  52.  * full, or when no more numbers can be retrieved from the
  53.  * string.
  54.  *
  55.  * Return value is the character in the string which caused
  56.  * the parse to end (typically a null terminator, if @str is
  57.  * completely parseable).
  58.  */
  59.  
  60. char *get_options (char *str, int nints, int *ints)
  61. {
  62. int res, i = 1;
  63. while (i < nints) {
  64. res = get_option (&str, ints + i);
  65. if (res == 0)
  66. break;
  67. i++;
  68. if (res == 1)
  69. break;
  70. }
  71. ints[0] = i - 1;
  72. return (str);
  73. }
  74. /**
  75.  * memparse - parse a string with mem suffixes into a number
  76.  * @ptr: Where parse begins
  77.  * @retptr: (output) Pointer to next char after parse completes
  78.  *
  79.  * Parses a string into a number.  The number stored at @ptr is
  80.  * potentially suffixed with %K (for kilobytes, or 1024 bytes),
  81.  * %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
  82.  * 1073741824).  If the number is suffixed with K, M, or G, then
  83.  * the return value is the number multiplied by one kilobyte, one
  84.  * megabyte, or one gigabyte, respectively.
  85.  */
  86. unsigned long long memparse (char *ptr, char **retptr)
  87. {
  88. unsigned long long ret = simple_strtoull (ptr, retptr, 0);
  89. switch (**retptr) {
  90. case 'G':
  91. case 'g':
  92. ret <<= 10;
  93. case 'M':
  94. case 'm':
  95. ret <<= 10;
  96. case 'K':
  97. case 'k':
  98. ret <<= 10;
  99. (*retptr)++;
  100. default:
  101. break;
  102. }
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(memparse);
  106. EXPORT_SYMBOL(get_option);
  107. EXPORT_SYMBOL(get_options);