xref: /original-bsd/lib/libc/vax/string/strncpy.s (revision c3e32dec)
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
9	.asciz "@(#)strncpy.s	8.1 (Berkeley) 06/04/93"
10#endif /* LIBC_SCCS and not lint */
11
12/*
13 * Copy string s2 over top of string s1.
14 * Truncate or null-pad to n bytes.
15 *
16 * char *
17 * strncpy(s1, s2, n)
18 *	char *s1, *s2;
19 */
20#include "DEFS.h"
21
22ENTRY(strncpy, R6)
23	movl	12(ap),r6	# r6 = n
24	bleq	done		# n <= 0
25	movl	4(ap),r3	# r3 = s1
26	movl	8(ap),r1	# r1 = s2
271:
28	movzwl	$65535,r2	# r2 = bytes in first chunk
29	cmpl	r6,r2		# r2 = min(bytes in chunk, n);
30	jgeq	2f
31	movl	r6,r2
322:
33	subl2	r2,r6		# update n
34	locc	$0,r2,(r1)	# '\0' found?
35	jneq	3f
36	subl2	r2,r1		# back up pointer updated by locc
37	movc3	r2,(r1),(r3)	# copy in next piece
38	tstl	r6		# run out of space?
39	jneq	1b
40	jbr	done
413:				# copy up to '\0' logic
42	addl2	r0,r6		# r6 = number of null-pad bytes
43	subl2	r0,r2		# r2 = number of bytes to move
44	subl2	r2,r1		# back up pointer updated by locc
45	movc3	r2,(r1),(r3)	# copy in last piece
464:				# null-pad logic
47	movzwl	$65535,r2	# r2 = bytes in first chunk
48	cmpl	r6,r2		# r2 = min(bytes in chunk, n);
49	jgeq	5f
50	movl	r6,r2
515:
52	subl2	r2,r6		# update n
53	movc5	$0,(r3),$0,r2,(r3)# pad with '\0's
54	tstl	r6		# finished padding?
55	jneq	4b
56done:
57	movl	4(ap),r0	# return s1
58	ret
59