xref: /csrg-svn/lib/libc/vax/string/strncpy.s (revision 17329)
1/*	strncpy.s	4.2	84/11/01	*/
2
3/*
4 * Copy string s2 over top of string s1.
5 * Truncate or null-pad to n bytes.
6 *
7 * char *
8 * strncpy(s1, s2, n)
9 *	char *s1, *s2;
10 */
11#include "DEFS.h"
12
13ENTRY(strncpy, R6)
14	movl	12(ap),r6	# r6 = n
15	bleq	done		# n <= 0
16	movl	4(ap),r3	# r3 = s1
17	movl	8(ap),r1	# r1 = s2
181:
19	movzwl	$65535,r2	# r2 = bytes in first chunk
20	cmpl	r6,r2		# r2 = min(bytes in chunk, n);
21	jgeq	2f
22	movl	r6,r2
232:
24	subl2	r2,r6		# update n
25	locc	$0,r2,(r1)	# '\0' found?
26	jneq	3f
27	subl2	r2,r1		# back up pointer updated by locc
28	movc3	r2,(r1),(r3)	# copy in next piece
29	tstl	r6		# run out of space?
30	jneq	1b
31	jbr	done
323:				# copy up to '\0' logic
33	addl2	r0,r6		# r6 = number of null-pad bytes
34	subl2	r0,r2		# r2 = number of bytes to move
35	subl2	r2,r1		# back up pointer updated by locc
36	movc3	r2,(r1),(r3)	# copy in last piece
374:				# null-pad logic
38	movzwl	$65535,r2	# r2 = bytes in first chunk
39	cmpl	r6,r2		# r2 = min(bytes in chunk, n);
40	jgeq	5f
41	movl	r6,r2
425:
43	subl2	r2,r6		# update n
44	movc5	$0,(r3),$0,r2,(r3)# pad with '\0's
45	tstl	r6		# finished padding?
46	jneq	4b
47done:
48	movl	4(ap),r0	# return s1
49	ret
50