strequal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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. int curl_strequal(const char *first, const char *second)
  27. {
  28. #if defined(HAVE_STRCASECMP)
  29. return !(strcasecmp)(first, second);
  30. #elif defined(HAVE_STRICMP)
  31. return !(stricmp)(first, second);
  32. #elif defined(HAVE_STRCMPI)
  33. return !(strcmpi)(first, second);
  34. #else
  35. while (*first && *second) {
  36. if (toupper(*first) != toupper(*second)) {
  37. break;
  38. }
  39. first++;
  40. second++;
  41. }
  42. return toupper(*first) == toupper(*second);
  43. #endif
  44. }
  45. int curl_strnequal(const char *first, const char *second, size_t max)
  46. {
  47. #if defined(HAVE_STRCASECMP)
  48. return !strncasecmp(first, second, max);
  49. #elif defined(HAVE_STRICMP)
  50. return !strnicmp(first, second, max);
  51. #elif defined(HAVE_STRCMPI)
  52. return !strncmpi(first, second, max);
  53. #else
  54. while (*first && *second && max) {
  55. if (toupper(*first) != toupper(*second)) {
  56. break;
  57. }
  58. max--;
  59. first++;
  60. second++;
  61. }
  62. if(0 == max)
  63. return 1; /* they are equal this far */
  64. return toupper(*first) == toupper(*second);
  65. #endif
  66. }
  67. #ifndef HAVE_STRLCAT
  68. /*
  69. * The strlcat() function appends the NUL-terminated string src to the end
  70. * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
  71. * nating the result.
  72. *
  73. * The strlcpy() and strlcat() functions return the total length of the
  74. * string they tried to create. For strlcpy() that means the length of src.
  75. * For strlcat() that means the initial length of dst plus the length of
  76. * src. While this may seem somewhat confusing it was done to make trunca-
  77. * tion detection simple.
  78. *
  79. *
  80. */
  81. size_t Curl_strlcat(char *dst, const char *src, size_t siz)
  82. {
  83. char *d = dst;
  84. const char *s = src;
  85. size_t n = siz;
  86. size_t dlen;
  87. /* Find the end of dst and adjust bytes left but don't go past end */
  88. while (n-- != 0 && *d != '\0')
  89. d++;
  90. dlen = d - dst;
  91. n = siz - dlen;
  92. if (n == 0)
  93. return(dlen + strlen(s));
  94. while (*s != '\0') {
  95. if (n != 1) {
  96. *d++ = *s;
  97. n--;
  98. }
  99. s++;
  100. }
  101. *d = '\0';
  102. return(dlen + (s - src)); /* count does not include NUL */
  103. }
  104. #endif
  105. /*
  106. * local variables:
  107. * eval: (load-file "../curl-mode.el")
  108. * end:
  109. * vim600: fdm=marker
  110. * vim: et sw=2 ts=2 sts=2 tw=78
  111. */