strequal.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, Daniel Stenberg, <[email protected]>, 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. #ifdef HAVE_STRINGS_H
  27. #include <strings.h>
  28. #endif
  29. #include "strequal.h"
  30. #if defined(HAVE_STRCASECMP) && defined(__STRICT_ANSI__)
  31. /* this is for "-ansi -Wall -pedantic" to stop complaining! */
  32. extern int (strcasecmp)(const char *s1, const char *s2);
  33. extern int (strncasecmp)(const char *s1, const char *s2, size_t n);
  34. #endif
  35. int curl_strequal(const char *first, const char *second)
  36. {
  37. #if defined(HAVE_STRCASECMP)
  38. return !(strcasecmp)(first, second);
  39. #elif defined(HAVE_STRCMPI)
  40. return !(strcmpi)(first, second);
  41. #elif defined(HAVE_STRICMP)
  42. return !(stricmp)(first, second);
  43. #else
  44. while (*first && *second) {
  45. if (toupper(*first) != toupper(*second)) {
  46. break;
  47. }
  48. first++;
  49. second++;
  50. }
  51. return toupper(*first) == toupper(*second);
  52. #endif
  53. }
  54. int curl_strnequal(const char *first, const char *second, size_t max)
  55. {
  56. #if defined(HAVE_STRCASECMP)
  57. return !strncasecmp(first, second, max);
  58. #elif defined(HAVE_STRCMPI)
  59. return !strncmpi(first, second, max);
  60. #elif defined(HAVE_STRICMP)
  61. return !strnicmp(first, second, max);
  62. #else
  63. while (*first && *second && max) {
  64. if (toupper(*first) != toupper(*second)) {
  65. break;
  66. }
  67. max--;
  68. first++;
  69. second++;
  70. }
  71. if(0 == max)
  72. return 1; /* they are equal this far */
  73. return toupper(*first) == toupper(*second);
  74. #endif
  75. }
  76. /*
  77. * Curl_strcasestr() finds the first occurrence of the substring needle in the
  78. * string haystack. The terminating `\0' characters are not compared. The
  79. * matching is done CASE INSENSITIVE, which thus is the difference between
  80. * this and strstr().
  81. */
  82. char *Curl_strcasestr(const char *haystack, const char *needle)
  83. {
  84. size_t nlen = strlen(needle);
  85. size_t hlen = strlen(haystack);
  86. while(hlen-- >= nlen) {
  87. if(curl_strnequal(haystack, needle, nlen))
  88. return (char *)haystack;
  89. haystack++;
  90. }
  91. return NULL;
  92. }
  93. #ifndef HAVE_STRLCAT
  94. /*
  95. * The strlcat() function appends the NUL-terminated string src to the end
  96. * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
  97. * nating the result.
  98. *
  99. * The strlcpy() and strlcat() functions return the total length of the
  100. * string they tried to create. For strlcpy() that means the length of src.
  101. * For strlcat() that means the initial length of dst plus the length of
  102. * src. While this may seem somewhat confusing it was done to make trunca-
  103. * tion detection simple.
  104. *
  105. *
  106. */
  107. size_t Curl_strlcat(char *dst, const char *src, size_t siz)
  108. {
  109. char *d = dst;
  110. const char *s = src;
  111. size_t n = siz;
  112. size_t dlen;
  113. /* Find the end of dst and adjust bytes left but don't go past end */
  114. while (n-- != 0 && *d != '\0')
  115. d++;
  116. dlen = d - dst;
  117. n = siz - dlen;
  118. if (n == 0)
  119. return(dlen + strlen(s));
  120. while (*s != '\0') {
  121. if (n != 1) {
  122. *d++ = *s;
  123. n--;
  124. }
  125. s++;
  126. }
  127. *d = '\0';
  128. return(dlen + (s - src)); /* count does not include NUL */
  129. }
  130. #endif