strings.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1993 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * File: strings.c
  28. * Author: Robert V. Baron, Carnegie Mellon University
  29. * Date: ??/92
  30. *
  31. * String functions.
  32. */
  33. #include <string.h>
  34. #ifdef strcpy
  35. #undef strcmp
  36. #undef strncmp
  37. #undef strcpy
  38. #undef strncpy
  39. #undef strlen
  40. #endif
  41. /*
  42. * Abstract:
  43. * strcmp (s1, s2) compares the strings "s1" and "s2".
  44. * It returns 0 if the strings are identical. It returns
  45. * > 0 if the first character that differs in the two strings
  46. * is larger in s1 than in s2 or if s1 is longer than s2 and
  47. * the contents are identical up to the length of s2.
  48. * It returns < 0 if the first differing character is smaller
  49. * in s1 than in s2 or if s1 is shorter than s2 and the
  50. * contents are identical up to the length of s1.
  51. */
  52. int __attribute__ ((pure))
  53. strcmp(
  54. const char *s1,
  55. const char *s2)
  56. {
  57. unsigned int a, b;
  58. do {
  59. a = *s1++;
  60. b = *s2++;
  61. if (a != b)
  62. return a-b; /* includes case when
  63. 'a' is zero and 'b' is not zero
  64. or vice versa */
  65. } while (a != '\0');
  66. return 0; /* both are zero */
  67. }
  68. /*
  69. * Abstract:
  70. * strncmp (s1, s2, n) compares the strings "s1" and "s2"
  71. * in exactly the same way as strcmp does. Except the
  72. * comparison runs for at most "n" characters.
  73. */
  74. int __attribute__ ((pure))
  75. strncmp(
  76. const char *s1,
  77. const char *s2,
  78. size_t n)
  79. {
  80. unsigned int a, b;
  81. while (n != 0) {
  82. a = *s1++;
  83. b = *s2++;
  84. if (a != b)
  85. return a-b; /* includes case when
  86. 'a' is zero and 'b' is not zero
  87. or vice versa */
  88. if (a == '\0')
  89. return 0; /* both are zero */
  90. n--;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Abstract:
  96. * strcpy copies the contents of the string "from" including
  97. * the null terminator to the string "to". A pointer to "to"
  98. * is returned.
  99. */
  100. char *
  101. strcpy(
  102. char *to,
  103. const char *from)
  104. {
  105. char *ret = to;
  106. while ((*to++ = *from++) != '\0')
  107. continue;
  108. return ret;
  109. }
  110. /*
  111. * Abstract:
  112. * strncpy copies "count" characters from the "from" string to
  113. * the "to" string. If "from" contains less than "count" characters
  114. * "to" will be padded with null characters until exactly "count"
  115. * characters have been written. The return value is a pointer
  116. * to the "to" string.
  117. */
  118. char *
  119. strncpy(
  120. char *to,
  121. const char *from,
  122. size_t count)
  123. {
  124. char *ret = to;
  125. while (count != 0) {
  126. count--;
  127. if ((*to++ = *from++) == '\0')
  128. break;
  129. }
  130. while (count != 0) {
  131. *to++ = '\0';
  132. count--;
  133. }
  134. return ret;
  135. }
  136. /*
  137. * Abstract:
  138. * strlen returns the number of characters in "string" preceding
  139. * the terminating null character.
  140. */
  141. size_t __attribute__ ((pure))
  142. strlen(
  143. const char *string)
  144. {
  145. const char *ret = string;
  146. while (*string++ != '\0')
  147. continue;
  148. return string - 1 - ret;
  149. }
  150. /*
  151. * Abstract:
  152. * memset writes value "c" in the "n" bytes starting at address "s".
  153. * The return value is a pointer to the "s" string.
  154. */
  155. #if 0
  156. void *
  157. memset(
  158. void *_s, int c, size_t n)
  159. {
  160. char *s = _s;
  161. size_t i;
  162. for (i = 0; i < n ; i++)
  163. s[i] = c;
  164. return _s;
  165. }
  166. #endif
  167. /*
  168. * Abstract:
  169. * strchr returns a pointer to the first occurrence of the character
  170. * "c" in the string "s". If "c" is not found, return NULL.
  171. */
  172. char *
  173. strchr(
  174. const char *s,
  175. int c)
  176. {
  177. while (*s != c) {
  178. if (*s == '\0') {
  179. return NULL;
  180. }
  181. s++;
  182. }
  183. return (char *)s;
  184. }
  185. /*
  186. * Abstract:
  187. * strsep extracts tokens from strings. If "*sp" is NULL, return NULL
  188. * and do nothing. Otherwise, find the first token in string "*sp".
  189. * Tokens are delimited by characters in the string "delim". If no
  190. * delimiter is found, the token is the entire string "*sp", and "*sp"
  191. * is made NULL. Otherwise, overwrite the delimiter with a null byte,
  192. * and make "*sp" point past it.
  193. */
  194. char *
  195. strsep(
  196. char **sp,
  197. const char *delim)
  198. {
  199. const char *d;
  200. char *s, *t;
  201. s = t = *sp;
  202. if (s == NULL) {
  203. return NULL;
  204. }
  205. for (;;) {
  206. if (*s == '\0') {
  207. *sp = NULL;
  208. return t;
  209. }
  210. d = delim;
  211. for (;;) {
  212. if (*d == '\0') {
  213. break;
  214. }
  215. if (*d == *s) {
  216. *s = '\0';
  217. *sp = s + 1;
  218. return t;
  219. }
  220. d++;
  221. }
  222. s++;
  223. }
  224. }
  225. /*
  226. * Abstract:
  227. * strstr returns a pointer to the first occurrence of the substring
  228. * "find" in the string "s". If no substring was found, return NULL.
  229. */
  230. char *
  231. strstr(
  232. const char *s,
  233. const char *find)
  234. {
  235. size_t len;
  236. len = strlen(find);
  237. if (len == 0) {
  238. return (char *)s;
  239. }
  240. for (;;) {
  241. if (*s == '\0') {
  242. return NULL;
  243. }
  244. if (strncmp(s, find, len) == 0) {
  245. return (char *)s;
  246. }
  247. s++;
  248. }
  249. }