strncasecmp.c 2.2 KB

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