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.

101 lines
2.5 KiB

  1. /***
  2. *mbsncpy.c - Copy one string to another, n chars only (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Copy one string to another, n chars only (MBCS)
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 08-03-93 KRS Fix logic bug.
  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-16-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. * _mbsncpy - Copy one string to another, n chars only (MBCS)
  30. *
  31. *Purpose:
  32. * Copies exactly cnt character from src to dst. If strlen(src) < cnt, the
  33. * remaining character are padded with null bytes. If strlen >= cnt, no
  34. * terminating null byte is added. 2-byte MBCS characters are handled
  35. * correctly.
  36. *
  37. *Entry:
  38. * unsigned char *dst = destination for copy
  39. * unsigned char *src = source for copy
  40. * int cnt = number of characters to copy
  41. *
  42. *Exit:
  43. * returns dst = destination of copy
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48. unsigned char * __cdecl _mbsncpy(
  49. unsigned char *dst,
  50. const unsigned char *src,
  51. size_t cnt
  52. )
  53. {
  54. unsigned char *start = dst;
  55. #ifdef _MT
  56. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  57. if ( ptmbci != __ptmbcinfo )
  58. ptmbci = __updatetmbcinfo();
  59. if ( _ISNOTMBCP_MT(ptmbci) )
  60. #else
  61. if ( _ISNOTMBCP )
  62. #endif
  63. return strncpy(dst, src, cnt);
  64. while (cnt) {
  65. cnt--;
  66. #ifdef _MT
  67. if ( __ismbblead_mt(ptmbci, *src) ) {
  68. #else
  69. if ( _ismbblead(*src) ) {
  70. #endif
  71. *dst++ = *src++;
  72. if ((*dst++ = *src++) == '\0') {
  73. dst[-2] = '\0';
  74. break;
  75. }
  76. }
  77. else
  78. if ((*dst++ = *src++) == '\0')
  79. break;
  80. }
  81. /* pad with nulls as needed */
  82. while (cnt--)
  83. *dst++ = '\0';
  84. return start;
  85. }
  86. #endif /* _MBCS */