string.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/tools/lib/string.c
  4. *
  5. * Copied from linux/lib/string.c, where it is:
  6. *
  7. * Copyright (C) 1991, 1992 Linus Torvalds
  8. *
  9. * More specifically, the first copied function was strtobool, which
  10. * was introduced by:
  11. *
  12. * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
  13. * Author: Jonathan Cameron <jic23@cam.ac.uk>
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <linux/string.h>
  19. #include <linux/compiler.h>
  20. /**
  21. * memdup - duplicate region of memory
  22. *
  23. * @src: memory region to duplicate
  24. * @len: memory region length
  25. */
  26. void *memdup(const void *src, size_t len)
  27. {
  28. void *p = malloc(len);
  29. if (p)
  30. memcpy(p, src, len);
  31. return p;
  32. }
  33. /**
  34. * strtobool - convert common user inputs into boolean values
  35. * @s: input string
  36. * @res: result
  37. *
  38. * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
  39. * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
  40. * pointed to by res is updated upon finding a match.
  41. */
  42. int strtobool(const char *s, bool *res)
  43. {
  44. if (!s)
  45. return -EINVAL;
  46. switch (s[0]) {
  47. case 'y':
  48. case 'Y':
  49. case '1':
  50. *res = true;
  51. return 0;
  52. case 'n':
  53. case 'N':
  54. case '0':
  55. *res = false;
  56. return 0;
  57. case 'o':
  58. case 'O':
  59. switch (s[1]) {
  60. case 'n':
  61. case 'N':
  62. *res = true;
  63. return 0;
  64. case 'f':
  65. case 'F':
  66. *res = false;
  67. return 0;
  68. default:
  69. break;
  70. }
  71. default:
  72. break;
  73. }
  74. return -EINVAL;
  75. }
  76. /**
  77. * strlcpy - Copy a C-string into a sized buffer
  78. * @dest: Where to copy the string to
  79. * @src: Where to copy the string from
  80. * @size: size of destination buffer
  81. *
  82. * Compatible with *BSD: the result is always a valid
  83. * NUL-terminated string that fits in the buffer (unless,
  84. * of course, the buffer size is zero). It does not pad
  85. * out the result like strncpy() does.
  86. *
  87. * If libc has strlcpy() then that version will override this
  88. * implementation:
  89. */
  90. #ifdef __clang__
  91. #pragma clang diagnostic push
  92. #pragma clang diagnostic ignored "-Wignored-attributes"
  93. #endif
  94. size_t __weak strlcpy(char *dest, const char *src, size_t size)
  95. {
  96. size_t ret = strlen(src);
  97. if (size) {
  98. size_t len = (ret >= size) ? size - 1 : ret;
  99. memcpy(dest, src, len);
  100. dest[len] = '\0';
  101. }
  102. return ret;
  103. }
  104. #ifdef __clang__
  105. #pragma clang diagnostic pop
  106. #endif