summaryrefslogtreecommitdiffstats
path: root/private/crt32/mbstring/mbsncpy.c
blob: 0ce2b471b7fe019061bf0dfe0762b054b61b7d12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*** 
*mbsncpy.c - Copy one string to another, n chars only (MBCS)
*
*	Copyright (c) 1985-1993, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	Copy one string to another, n chars only (MBCS)
*
*Revision History:
*	11-19-92  KRS	Ported from 16-bit sources.
*	08-03-93  KRS	Fix logic bug.
*
*******************************************************************************/

#ifdef _MBCS
#include <cruntime.h>
#include <mbdata.h>
#include <mbctype.h>
#include <mbstring.h>

/***
* _mbsncpy - Copy one string to another, n chars only (MBCS)
*
*Purpose:
*	Copies exactly cnt character from src to dst.  If strlen(src) < cnt, the
*	remaining character are padded with null bytes.  If strlen >= cnt, no
*	terminating null byte is added.  2-byte MBCS characters are handled
*       correctly.
*
*Entry:
*	unsigned char *dst = destination for copy
*	unsigned char *src = source for copy
*	int cnt = number of characters to copy
*
*Exit:
*	returns dst = destination of copy
*
*Exceptions:
*
*******************************************************************************/

unsigned char * _CRTAPI1 _mbsncpy(dst, src, cnt)
unsigned char *dst;
const unsigned char *src;
size_t cnt;
{

	unsigned char *start = dst;

	while (cnt) {

		cnt--;
		if (_ISLEADBYTE(*src)) {
			*dst++ = *src++;
			if ((*dst++ = *src++) == '\0') {
				dst[-2] = '\0';
				break;
                        }
                }

		else
			if ((*dst++ = *src++) == '\0')
				break;

	}

	/* pad with nulls as needed */

	while (cnt--)
		*dst++ = '\0';

	return start;
}
#endif	/* _MBCS */