Source code of Windows XP (NT5)
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.

124 lines
3.0 KiB

  1. /***
  2. *mbsncat.c - concatenate string2 onto string1, max length n
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines mbsncat() - concatenate maximum of n characters
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 08-20-93 CFW Update _MBCS_OS support.
  12. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  13. * 04-15-93 CFW Add _MB_CP_LOCK.
  14. * 05-09-94 CFW Optimize for SBCS.
  15. * 05-19-94 CFW Enable non-Win32.
  16. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  17. * 04-15-98 GJF Revised multithread support based on threadmbcinfo
  18. * structs
  19. *
  20. *******************************************************************************/
  21. #ifdef _MBCS
  22. #include <mtdll.h>
  23. #include <cruntime.h>
  24. #include <string.h>
  25. #include <mbdata.h>
  26. #include <mbctype.h>
  27. #include <mbstring.h>
  28. /***
  29. * _mbsncat - concatenate max cnt characters onto dst
  30. *
  31. *Purpose:
  32. * Concatenates src onto dst, with a maximum of cnt characters copied.
  33. * Handles 2-byte MBCS characters correctly.
  34. *
  35. *Entry:
  36. * unsigned char *dst - string to concatenate onto
  37. * unsigned char *src - string to concatenate from
  38. * int cnt - number of characters to copy
  39. *
  40. *Exit:
  41. * returns dst, with src (at least part) concatenated on
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46. unsigned char * __cdecl _mbsncat(
  47. unsigned char *dst,
  48. const unsigned char *src,
  49. size_t cnt
  50. )
  51. {
  52. unsigned char *start;
  53. #ifdef _MT
  54. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  55. if ( ptmbci != __ptmbcinfo )
  56. ptmbci = __updatetmbcinfo();
  57. #endif
  58. if (!cnt)
  59. return(dst);
  60. #ifdef _MT
  61. if ( _ISNOTMBCP_MT(ptmbci) )
  62. #else
  63. if ( _ISNOTMBCP )
  64. #endif
  65. return strncat(dst, src, cnt);
  66. start = dst;
  67. while (*dst++)
  68. ;
  69. --dst; // dst now points to end of dst string
  70. /* if last char in string is a lead byte, back up pointer */
  71. #ifdef _MT
  72. if ( __ismbslead_mt(ptmbci, start, dst) )
  73. #else
  74. if ( _ismbslead(start, dst) )
  75. #endif
  76. --dst;
  77. /* copy over the characters */
  78. while (cnt--) {
  79. #ifdef _MT
  80. if ( __ismbblead_mt(ptmbci, *src) ) {
  81. #else
  82. if ( _ismbblead(*src) ) {
  83. #endif
  84. *dst++ = *src++;
  85. if ((*dst++ = *src++) == '\0') {
  86. dst[-2] = '\0';
  87. break;
  88. }
  89. }
  90. else if ((*dst++ = *src++) == '\0')
  91. break;
  92. }
  93. /* enter final nul, if necessary */
  94. #ifdef _MT
  95. if ( __mbsbtype_mt(ptmbci, start, (int) ((dst - start) - 1)) ==
  96. _MBC_LEAD )
  97. #else
  98. if ( _mbsbtype(start, (int) ((dst - start) - 1)) == _MBC_LEAD )
  99. #endif
  100. dst[-1] = '\0';
  101. else
  102. *dst = '\0';
  103. return(start);
  104. }
  105. #endif /* _MBCS */