strncasecmp.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include "_string.h"
  8. #include <ctype.h>
  9. #include <locale.h>
  10. #ifdef WANT_WIDE
  11. # define strncasecmp wcsncasecmp
  12. # define strncasecmp_l wcsncasecmp_l
  13. # ifdef __UCLIBC_DO_XLOCALE
  14. # define TOLOWER(C) towlower_l((C), locale_arg)
  15. # else
  16. # define TOLOWER(C) towlower((C))
  17. # endif
  18. #else
  19. # ifdef __UCLIBC_DO_XLOCALE
  20. # define TOLOWER(C) tolower_l((C), locale_arg)
  21. # else
  22. #if !defined __UCLIBC_HAS_XLOCALE__ && defined __UCLIBC_HAS_CTYPE_TABLES__
  23. #endif
  24. # define TOLOWER(C) tolower((C))
  25. # endif
  26. #endif
  27. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  28. int strncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  29. {
  30. return strncasecmp_l(s1, s2, n, __UCLIBC_CURLOCALE);
  31. }
  32. libc_hidden_def(strncasecmp)
  33. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  34. int __XL_NPP(strncasecmp)(register const Wchar *s1, register const Wchar *s2,
  35. size_t n __LOCALE_PARAM )
  36. {
  37. #ifdef WANT_WIDE
  38. while (n && ((*s1 == *s2) || (TOLOWER(*s1) == TOLOWER(*s2)))) {
  39. if (!*s1++) {
  40. return 0;
  41. }
  42. ++s2;
  43. --n;
  44. }
  45. return (n == 0)
  46. ? 0
  47. : ((((Wuchar)TOLOWER(*s1)) < ((Wuchar)TOLOWER(*s2))) ? -1 : 1);
  48. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  49. #else
  50. int r = 0;
  51. while ( n
  52. && ((s1 == s2) ||
  53. !(r = ((int)( TOLOWER(*((unsigned char *)s1))))
  54. - TOLOWER(*((unsigned char *)s2))))
  55. && (--n, ++s2, *s1++));
  56. return r;
  57. #endif
  58. }
  59. libc_hidden_def(__XL_NPP(strncasecmp))
  60. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */