strncasecmp.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. # define TOLOWER(C) tolower((C))
  23. # endif
  24. #endif
  25. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  26. int strncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  27. {
  28. return strncasecmp_l(s1, s2, n, __UCLIBC_CURLOCALE);
  29. }
  30. libc_hidden_def(strncasecmp)
  31. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  32. int __XL_NPP(strncasecmp)(register const Wchar *s1, register const Wchar *s2,
  33. size_t n __LOCALE_PARAM )
  34. {
  35. #ifdef WANT_WIDE
  36. while (n && ((*s1 == *s2) || (TOLOWER(*s1) == TOLOWER(*s2)))) {
  37. if (!*s1++) {
  38. return 0;
  39. }
  40. ++s2;
  41. --n;
  42. }
  43. return (n == 0)
  44. ? 0
  45. : ((((Wuchar)TOLOWER(*s1)) < ((Wuchar)TOLOWER(*s2))) ? -1 : 1);
  46. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  47. #else
  48. int r = 0;
  49. while ( n
  50. && ((s1 == s2) ||
  51. !(r = ((int)( TOLOWER(*((unsigned char *)s1))))
  52. - TOLOWER(*((unsigned char *)s2))))
  53. && (--n, ++s2, *s1++));
  54. return r;
  55. #endif
  56. }
  57. libc_hidden_def(__XL_NPP(strncasecmp))
  58. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */