strequal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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: strequal.c,v 1.25 2004/01/29 13:56:45 bagder Exp $
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include "strequal.h"
  27. #ifdef HAVE_STRCASECMP
  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. #ifndef HAVE_STRLCAT
  74. /*
  75. * The strlcat() function appends the NUL-terminated string src to the end
  76. * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
  77. * nating the result.
  78. *
  79. * The strlcpy() and strlcat() functions return the total length of the
  80. * string they tried to create. For strlcpy() that means the length of src.
  81. * For strlcat() that means the initial length of dst plus the length of
  82. * src. While this may seem somewhat confusing it was done to make trunca-
  83. * tion detection simple.
  84. *
  85. *
  86. */
  87. size_t Curl_strlcat(char *dst, const char *src, size_t siz)
  88. {
  89. char *d = dst;
  90. const char *s = src;
  91. size_t n = siz;
  92. size_t dlen;
  93. /* Find the end of dst and adjust bytes left but don't go past end */
  94. while (n-- != 0 && *d != '\0')
  95. d++;
  96. dlen = d - dst;
  97. n = siz - dlen;
  98. if (n == 0)
  99. return(dlen + strlen(s));
  100. while (*s != '\0') {
  101. if (n != 1) {
  102. *d++ = *s;
  103. n--;
  104. }
  105. s++;
  106. }
  107. *d = '\0';
  108. return(dlen + (s - src)); /* count does not include NUL */
  109. }
  110. #endif