string.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * libc string functions
  3. *
  4. * This code is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Library General Public License version 2.
  6. */
  7. #include "libcflat.h"
  8. unsigned long strlen(const char *buf)
  9. {
  10. unsigned long len = 0;
  11. while (*buf++)
  12. ++len;
  13. return len;
  14. }
  15. char *strcat(char *dest, const char *src)
  16. {
  17. char *p = dest;
  18. while (*p)
  19. ++p;
  20. while ((*p++ = *src++) != 0)
  21. ;
  22. return dest;
  23. }
  24. char *strcpy(char *dest, const char *src)
  25. {
  26. *dest = 0;
  27. return strcat(dest, src);
  28. }
  29. int strncmp(const char *a, const char *b, size_t n)
  30. {
  31. for (; n--; ++a, ++b)
  32. if (*a != *b || *a == '\0')
  33. return *a - *b;
  34. return 0;
  35. }
  36. int strcmp(const char *a, const char *b)
  37. {
  38. return strncmp(a, b, SIZE_MAX);
  39. }
  40. char *strchr(const char *s, int c)
  41. {
  42. while (*s != (char)c)
  43. if (*s++ == '\0')
  44. return NULL;
  45. return (char *)s;
  46. }
  47. char *strstr(const char *s1, const char *s2)
  48. {
  49. size_t l1, l2;
  50. l2 = strlen(s2);
  51. if (!l2)
  52. return (char *)s1;
  53. l1 = strlen(s1);
  54. while (l1 >= l2) {
  55. l1--;
  56. if (!memcmp(s1, s2, l2))
  57. return (char *)s1;
  58. s1++;
  59. }
  60. return NULL;
  61. }
  62. void *memset(void *s, int c, size_t n)
  63. {
  64. size_t i;
  65. char *a = s;
  66. for (i = 0; i < n; ++i)
  67. a[i] = c;
  68. return s;
  69. }
  70. void *memcpy(void *dest, const void *src, size_t n)
  71. {
  72. size_t i;
  73. char *a = dest;
  74. const char *b = src;
  75. for (i = 0; i < n; ++i)
  76. a[i] = b[i];
  77. return dest;
  78. }
  79. int memcmp(const void *s1, const void *s2, size_t n)
  80. {
  81. const unsigned char *a = s1, *b = s2;
  82. int ret = 0;
  83. while (n--) {
  84. ret = *a - *b;
  85. if (ret)
  86. break;
  87. ++a, ++b;
  88. }
  89. return ret;
  90. }
  91. void *memmove(void *dest, const void *src, size_t n)
  92. {
  93. const unsigned char *s = src;
  94. unsigned char *d = dest;
  95. if (d <= s) {
  96. while (n--)
  97. *d++ = *s++;
  98. } else {
  99. d += n, s += n;
  100. while (n--)
  101. *--d = *--s;
  102. }
  103. return dest;
  104. }
  105. void *memchr(const void *s, int c, size_t n)
  106. {
  107. const unsigned char *str = s, chr = (unsigned char)c;
  108. while (n--)
  109. if (*str++ == chr)
  110. return (void *)(str - 1);
  111. return NULL;
  112. }
  113. long atol(const char *ptr)
  114. {
  115. long acc = 0;
  116. const char *s = ptr;
  117. int neg, c;
  118. while (*s == ' ' || *s == '\t')
  119. s++;
  120. if (*s == '-'){
  121. neg = 1;
  122. s++;
  123. } else {
  124. neg = 0;
  125. if (*s == '+')
  126. s++;
  127. }
  128. while (*s) {
  129. if (*s < '0' || *s > '9')
  130. break;
  131. c = *s - '0';
  132. acc = acc * 10 + c;
  133. s++;
  134. }
  135. if (neg)
  136. acc = -acc;
  137. return acc;
  138. }
  139. extern char **environ;
  140. char *getenv(const char *name)
  141. {
  142. char **envp = environ, *delim;
  143. while (*envp) {
  144. delim = strchr(*envp, '=');
  145. if (delim && strncmp(name, *envp, delim - *envp) == 0)
  146. return delim + 1;
  147. ++envp;
  148. }
  149. return NULL;
  150. }
  151. /* Very simple glob matching. Allows '*' at beginning and end of pattern. */
  152. bool simple_glob(const char *text, const char *pattern)
  153. {
  154. bool star_start = false;
  155. bool star_end = false;
  156. size_t n = strlen(pattern);
  157. char copy[n + 1];
  158. if (pattern[0] == '*') {
  159. pattern += 1;
  160. n -= 1;
  161. star_start = true;
  162. }
  163. strcpy(copy, pattern);
  164. if (n > 0 && pattern[n - 1] == '*') {
  165. n -= 1;
  166. copy[n] = '\0';
  167. star_end = true;
  168. }
  169. if (star_start && star_end)
  170. return strstr(text, copy);
  171. if (star_end)
  172. return strstr(text, copy) == text;
  173. if (star_start) {
  174. size_t text_len = strlen(text);
  175. const char *suffix;
  176. if (n > text_len)
  177. return false;
  178. suffix = text + text_len - n;
  179. return !strcmp(suffix, copy);
  180. }
  181. return !strcmp(text, copy);
  182. }