strequal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include "strequal.h"
  27. #if defined(HAVE_STRCASECMP) && defined(__STRICT_ANSI__)
  28. /* this is for "-ansi -Wall -pedantic" to stop complaining! */
  29. extern int (strcasecmp)(const char *s1, const char *s2);
  30. extern int (strncasecmp)(const char *s1, const char *s2, size_t n);
  31. #endif
  32. int curl_strequal(const char *first, const char *second)
  33. {
  34. #if defined(HAVE_STRCASECMP)
  35. return !(strcasecmp)(first, second);
  36. #elif defined(HAVE_STRCMPI)
  37. return !(strcmpi)(first, second);
  38. #elif defined(HAVE_STRICMP)
  39. return !(stricmp)(first, second);
  40. #else
  41. while (*first && *second) {
  42. if (toupper(*first) != toupper(*second)) {
  43. break;
  44. }
  45. first++;
  46. second++;
  47. }
  48. return toupper(*first) == toupper(*second);
  49. #endif
  50. }
  51. int curl_strnequal(const char *first, const char *second, size_t max)
  52. {
  53. #if defined(HAVE_STRCASECMP)
  54. return !strncasecmp(first, second, max);
  55. #elif defined(HAVE_STRCMPI)
  56. return !strncmpi(first, second, max);
  57. #elif defined(HAVE_STRICMP)
  58. return !strnicmp(first, second, max);
  59. #else
  60. while (*first && *second && max) {
  61. if (toupper(*first) != toupper(*second)) {
  62. break;
  63. }
  64. max--;
  65. first++;
  66. second++;
  67. }
  68. if(0 == max)
  69. return 1; /* they are equal this far */
  70. return toupper(*first) == toupper(*second);
  71. #endif
  72. }
  73. /*
  74. * Curl_strcasestr() finds the first occurrence of the substring needle in the
  75. * string haystack. The terminating `\0' characters are not compared. The
  76. * matching is done CASE INSENSITIVE, which thus is the difference between
  77. * this and strstr().
  78. */
  79. char *Curl_strcasestr(const char *haystack, const char *needle)
  80. {
  81. size_t nlen = strlen(needle);
  82. size_t hlen = strlen(haystack);
  83. while(hlen-- >= nlen) {
  84. if(curl_strnequal(haystack, needle, nlen))
  85. return (char *)haystack;
  86. haystack++;
  87. }
  88. return NULL;
  89. }
  90. #ifndef HAVE_STRLCAT
  91. /*
  92. * The strlcat() function appends the NUL-terminated string src to the end
  93. * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
  94. * nating the result.
  95. *
  96. * The strlcpy() and strlcat() functions return the total length of the
  97. * string they tried to create. For strlcpy() that means the length of src.
  98. * For strlcat() that means the initial length of dst plus the length of
  99. * src. While this may seem somewhat confusing it was done to make trunca-
  100. * tion detection simple.
  101. *
  102. *
  103. */
  104. size_t Curl_strlcat(char *dst, const char *src, size_t siz)
  105. {
  106. char *d = dst;
  107. const char *s = src;
  108. size_t n = siz;
  109. size_t dlen;
  110. /* Find the end of dst and adjust bytes left but don't go past end */
  111. while (n-- != 0 && *d != '\0')
  112. d++;
  113. dlen = d - dst;
  114. n = siz - dlen;
  115. if (n == 0)
  116. return(dlen + strlen(s));
  117. while (*s != '\0') {
  118. if (n != 1) {
  119. *d++ = *s;
  120. n--;
  121. }
  122. s++;
  123. }
  124. *d = '\0';
  125. return(dlen + (s - src)); /* count does not include NUL */
  126. }
  127. #endif