strequal.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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 "curl_setup.h"
  23. #ifdef HAVE_STRINGS_H
  24. #include <strings.h>
  25. #endif
  26. #include "strequal.h"
  27. /*
  28. * @unittest: 1301
  29. */
  30. int curl_strequal(const char *first, const char *second)
  31. {
  32. #if defined(HAVE_STRCASECMP)
  33. return !(strcasecmp)(first, second);
  34. #elif defined(HAVE_STRCMPI)
  35. return !(strcmpi)(first, second);
  36. #elif defined(HAVE_STRICMP)
  37. return !(stricmp)(first, second);
  38. #else
  39. while(*first && *second) {
  40. if(toupper(*first) != toupper(*second)) {
  41. break;
  42. }
  43. first++;
  44. second++;
  45. }
  46. return toupper(*first) == toupper(*second);
  47. #endif
  48. }
  49. /*
  50. * @unittest: 1301
  51. */
  52. int curl_strnequal(const char *first, const char *second, size_t max)
  53. {
  54. #if defined(HAVE_STRNCASECMP)
  55. return !strncasecmp(first, second, max);
  56. #elif defined(HAVE_STRNCMPI)
  57. return !strncmpi(first, second, max);
  58. #elif defined(HAVE_STRNICMP)
  59. return !strnicmp(first, second, max);
  60. #else
  61. while(*first && *second && max) {
  62. if(toupper(*first) != toupper(*second)) {
  63. break;
  64. }
  65. max--;
  66. first++;
  67. second++;
  68. }
  69. if(0 == max)
  70. return 1; /* they are equal this far */
  71. return toupper(*first) == toupper(*second);
  72. #endif
  73. }