strequal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. ***************************************************************************/
  22. #include "setup.h"
  23. #include <string.h>
  24. #include <ctype.h>
  25. #ifdef HAVE_STRINGS_H
  26. #include <strings.h>
  27. #endif
  28. #include "strequal.h"
  29. int curl_strequal(const char *first, const char *second)
  30. {
  31. #if defined(HAVE_STRCASECMP)
  32. return !(strcasecmp)(first, second);
  33. #elif defined(HAVE_STRCMPI)
  34. return !(strcmpi)(first, second);
  35. #elif defined(HAVE_STRICMP)
  36. return !(stricmp)(first, second);
  37. #else
  38. while(*first && *second) {
  39. if(toupper(*first) != toupper(*second)) {
  40. break;
  41. }
  42. first++;
  43. second++;
  44. }
  45. return toupper(*first) == toupper(*second);
  46. #endif
  47. }
  48. int curl_strnequal(const char *first, const char *second, size_t max)
  49. {
  50. #if defined(HAVE_STRNCASECMP)
  51. return !strncasecmp(first, second, max);
  52. #elif defined(HAVE_STRNCMPI)
  53. return !strncmpi(first, second, max);
  54. #elif defined(HAVE_STRNICMP)
  55. return !strnicmp(first, second, max);
  56. #else
  57. while(*first && *second && max) {
  58. if(toupper(*first) != toupper(*second)) {
  59. break;
  60. }
  61. max--;
  62. first++;
  63. second++;
  64. }
  65. if(0 == max)
  66. return 1; /* they are equal this far */
  67. return toupper(*first) == toupper(*second);
  68. #endif
  69. }
  70. #ifndef HAVE_STRLCAT
  71. /*
  72. * The strlcat() function appends the NUL-terminated string src to the end
  73. * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
  74. * nating the result.
  75. *
  76. * The strlcpy() and strlcat() functions return the total length of the
  77. * string they tried to create. For strlcpy() that means the length of src.
  78. * For strlcat() that means the initial length of dst plus the length of
  79. * src. While this may seem somewhat confusing it was done to make trunca-
  80. * tion detection simple.
  81. *
  82. *
  83. */
  84. size_t Curl_strlcat(char *dst, const char *src, size_t siz)
  85. {
  86. char *d = dst;
  87. const char *s = src;
  88. size_t n = siz;
  89. union {
  90. ssize_t sig;
  91. size_t uns;
  92. } 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.sig = d - dst;
  97. n = siz - dlen.uns;
  98. if(n == 0)
  99. return(dlen.uns + 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.uns + (s - src)); /* count does not include NUL */
  109. }
  110. #endif