strequal.c 4.1 KB

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