string.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ctype.h>
  3. #include <linux/kernel.h>
  4. #include <linux/errno.h>
  5. #undef CONFIG_KASAN
  6. #undef CONFIG_KASAN_GENERIC
  7. #include "../lib/string.c"
  8. int strncmp(const char *cs, const char *ct, size_t count)
  9. {
  10. unsigned char c1, c2;
  11. while (count) {
  12. c1 = *cs++;
  13. c2 = *ct++;
  14. if (c1 != c2)
  15. return c1 < c2 ? -1 : 1;
  16. if (!c1)
  17. break;
  18. count--;
  19. }
  20. return 0;
  21. }
  22. char *skip_spaces(const char *str)
  23. {
  24. while (isspace(*str))
  25. ++str;
  26. return (char *)str;
  27. }
  28. char *strim(char *s)
  29. {
  30. size_t size;
  31. char *end;
  32. size = strlen(s);
  33. if (!size)
  34. return s;
  35. end = s + size - 1;
  36. while (end >= s && isspace(*end))
  37. end--;
  38. *(end + 1) = '\0';
  39. return skip_spaces(s);
  40. }
  41. /* Works only for digits and letters, but small and fast */
  42. #define TOLOWER(x) ((x) | 0x20)
  43. static unsigned int simple_guess_base(const char *cp)
  44. {
  45. if (cp[0] == '0') {
  46. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  47. return 16;
  48. else
  49. return 8;
  50. } else {
  51. return 10;
  52. }
  53. }
  54. /**
  55. * simple_strtoull - convert a string to an unsigned long long
  56. * @cp: The start of the string
  57. * @endp: A pointer to the end of the parsed string will be placed here
  58. * @base: The number base to use
  59. */
  60. unsigned long long simple_strtoull(const char *cp, char **endp,
  61. unsigned int base)
  62. {
  63. unsigned long long result = 0;
  64. if (!base)
  65. base = simple_guess_base(cp);
  66. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  67. cp += 2;
  68. while (isxdigit(*cp)) {
  69. unsigned int value;
  70. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  71. if (value >= base)
  72. break;
  73. result = result * base + value;
  74. cp++;
  75. }
  76. if (endp)
  77. *endp = (char *)cp;
  78. return result;
  79. }
  80. long simple_strtol(const char *cp, char **endp, unsigned int base)
  81. {
  82. if (*cp == '-')
  83. return -simple_strtoull(cp + 1, endp, base);
  84. return simple_strtoull(cp, endp, base);
  85. }
  86. int kstrtobool(const char *s, bool *res)
  87. {
  88. if (!s)
  89. return -EINVAL;
  90. switch (s[0]) {
  91. case 'y':
  92. case 'Y':
  93. case '1':
  94. *res = true;
  95. return 0;
  96. case 'n':
  97. case 'N':
  98. case '0':
  99. *res = false;
  100. return 0;
  101. case 'o':
  102. case 'O':
  103. switch (s[1]) {
  104. case 'n':
  105. case 'N':
  106. *res = true;
  107. return 0;
  108. case 'f':
  109. case 'F':
  110. *res = false;
  111. return 0;
  112. default:
  113. break;
  114. }
  115. default:
  116. break;
  117. }
  118. return -EINVAL;
  119. }