Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.7 KiB

  1. /***
  2. *strncoll.c - Collate locale strings
  3. *
  4. * Copyright (c) 1994-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Compare two strings using the locale LC_COLLATE information.
  8. * Compares at most n characters of two strings.
  9. *
  10. *Revision History:
  11. * 05-09-94 CFW Created from strnicol.c.
  12. * 05-26-94 CFW If count is zero, return EQUAL.
  13. * 09-06-94 CFW Remove _INTL switch.
  14. * 10-24-94 GJF Sped up C locale, multi-thread case.
  15. * 12-29-94 CFW Merge non-Win32.
  16. * 09-26-95 GJF New locking macro, and scheme, for functions which
  17. * reference the locale.
  18. * 10-30-95 GJF Specify SORT_STRINGSORT to CompareString.
  19. * 07-16-96 SKS Added missing call to _unlock_locale()
  20. * 11-24-97 GJF Removed bogus codepage determination.
  21. * 01-12-98 GJF Use _lc_collate_cp codepage.
  22. * 08-11-98 GJF Revised multithread support based on threadlocinfo
  23. * struct.
  24. * 01-04-99 GJF Changes for 64-bit size_t.
  25. * 04-30-99 PML Minor cleanup as part of 64-bit merge.
  26. * 05-17-99 PML Remove all Macintosh support.
  27. *
  28. *******************************************************************************/
  29. #include <cruntime.h>
  30. #include <string.h>
  31. #include <windows.h>
  32. #include <stdlib.h>
  33. #include <malloc.h>
  34. #include <limits.h>
  35. #include <locale.h>
  36. #include <setlocal.h>
  37. #include <mtdll.h>
  38. #include <errno.h>
  39. #include <awint.h>
  40. /***
  41. *int _strncoll() - Collate locale strings
  42. *
  43. *Purpose:
  44. * Compare two strings using the locale LC_COLLATE information.
  45. * Compares at most n characters of two strings.
  46. *
  47. *Entry:
  48. * const char *s1 = pointer to the first string
  49. * const char *s2 = pointer to the second string
  50. * size_t count - maximum number of characters to compare
  51. *
  52. *Exit:
  53. * Less than 0 = first string less than second string
  54. * 0 = strings are equal
  55. * Greater than 0 = first string greater than second string
  56. *
  57. *Exceptions:
  58. * _NLSCMPERROR = error
  59. * errno = EINVAL
  60. *
  61. *******************************************************************************/
  62. int __cdecl _strncoll (
  63. const char *_string1,
  64. const char *_string2,
  65. size_t count
  66. )
  67. {
  68. #if !defined(_NTSUBSET_)
  69. int ret;
  70. #ifdef _MT
  71. pthreadlocinfo ptloci;
  72. #endif
  73. if ( !count )
  74. return 0;
  75. if ( count > INT_MAX ) {
  76. errno = EINVAL;
  77. return _NLSCMPERROR;
  78. }
  79. #ifdef _MT
  80. ptloci = _getptd()->ptlocinfo;
  81. if ( ptloci != __ptlocinfo )
  82. ptloci = __updatetlocinfo();
  83. if ( ptloci->lc_handle[LC_COLLATE] == _CLOCALEHANDLE )
  84. #else
  85. if ( __lc_handle[LC_COLLATE] == _CLOCALEHANDLE )
  86. #endif
  87. return strncmp(_string1, _string2, count);
  88. #ifdef _MT
  89. if ( 0 == (ret = __crtCompareStringA( ptloci->lc_handle[LC_COLLATE],
  90. #else
  91. if ( 0 == (ret = __crtCompareStringA( __lc_handle[LC_COLLATE],
  92. #endif
  93. SORT_STRINGSORT,
  94. _string1,
  95. (int)count,
  96. _string2,
  97. (int)count,
  98. #ifdef _MT
  99. ptloci->lc_collate_cp )) )
  100. #else
  101. __lc_collate_cp )) )
  102. #endif
  103. {
  104. errno = EINVAL;
  105. return _NLSCMPERROR;
  106. }
  107. return (ret - 2);
  108. #else
  109. return strncmp(_string1, _string2, count);
  110. #endif
  111. }