cmdline.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. char *get_options(const char *str, int nints, int *ints)
  60. {
  61. int res, i = 1;
  62. while (i < nints) {
  63. res = get_option ((char **)&str, ints + i);
  64. if (res == 0)
  65. break;
  66. i++;
  67. if (res == 1)
  68. break;
  69. }
  70. ints[0] = i - 1;
  71. return (char *)str;
  72. }
  73. /**
  74. * memparse - parse a string with mem suffixes into a number
  75. * @ptr: Where parse begins
  76. * @retptr: (output) Pointer to next char after parse completes
  77. *
  78. * Parses a string into a number. The number stored at @ptr is
  79. * potentially suffixed with %K (for kilobytes, or 1024 bytes),
  80. * %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
  81. * 1073741824). If the number is suffixed with K, M, or G, then
  82. * the return value is the number multiplied by one kilobyte, one
  83. * megabyte, or one gigabyte, respectively.
  84. */
  85. unsigned long long memparse (char *ptr, char **retptr)
  86. {
  87. unsigned long long ret = simple_strtoull (ptr, retptr, 0);
  88. switch (**retptr) {
  89. case 'G':
  90. case 'g':
  91. ret <<= 10;
  92. case 'M':
  93. case 'm':
  94. ret <<= 10;
  95. case 'K':
  96. case 'k':
  97. ret <<= 10;
  98. (*retptr)++;
  99. default:
  100. break;
  101. }
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(memparse);
  105. EXPORT_SYMBOL(get_option);
  106. EXPORT_SYMBOL(get_options);