strncasecmp.c 1.7 KB

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