strcasecmp.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifdef HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #include <ctype.h>
  18. #include <string.h>
  19. #ifndef _LIBC
  20. # define __strcasecmp strcasecmp
  21. # define TOLOWER(Ch) tolower (Ch)
  22. #else
  23. # include <locale/localeinfo.h>
  24. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  25. # define __strcasecmp __strcasecmp_l
  26. # endif
  27. # define TOLOWER(Ch) __tolower_l ((Ch), loc)
  28. #endif
  29. #ifdef USE_IN_EXTENDED_LOCALE_MODEL
  30. # define LOCALE_PARAM , locale_t loc
  31. #else
  32. # define LOCALE_PARAM
  33. #endif
  34. /* Compare S1 and S2, ignoring case, returning less than, equal to or
  35. greater than zero if S1 is lexicographically less than,
  36. equal to or greater than S2. */
  37. int
  38. __strcasecmp (const char *s1, const char *s2 LOCALE_PARAM)
  39. {
  40. #if defined _LIBC && !defined USE_IN_EXTENDED_LOCALE_MODEL
  41. locale_t loc = _NL_CURRENT_LOCALE;
  42. #endif
  43. const unsigned char *p1 = (const unsigned char *) s1;
  44. const unsigned char *p2 = (const unsigned char *) s2;
  45. int result;
  46. if (p1 == p2)
  47. return 0;
  48. while ((result = TOLOWER (*p1) - TOLOWER (*p2++)) == 0)
  49. if (*p1++ == '\0')
  50. break;
  51. return result;
  52. }
  53. #ifndef __strcasecmp
  54. libc_hidden_def (__strcasecmp)
  55. weak_alias (__strcasecmp, strcasecmp)
  56. #endif