1/*
2Copyright (c) 2009 Nick Clifton <nickc@redhat.com>
3 */
4	.file	"strncat.S"
5
6	.section .text
7	.global  _strncat
8	.type	 _strncat,@function
9_strncat:
10	;; On entry: r1 => Destination
11	;;           r2 => Source
12	;; 	     r3 => Max number of bytes to copy
13#ifdef __RX_DISALLOW_STRING_INSNS__
14	cmp	#0, r3		; If max is zero we have nothing to do.
15	beq	2f
16
17	mov 	r1, r4		; Leave the desintation pointer intact for the return value.
18
191:	mov.b	[r4+], r5	; Find the NUL byte at the end of the destination.
20	cmp	#0, r5
21	bne	1b
22
23	sub	#1, r4
24
253:	mov.b	[r2+], r5	; Copy bytes from the source into the destination ...
26	mov.b	r5, [r4+]
27	cmp	#0, r5		; ... until we reach a NUL byte ...
28	beq 	2f
29	sub	#1, r3
30	bne	3b		; ... or we have copied N bytes.
31
322:	rts
33#else
34	mov 	r1, r4		; Save a copy of the dest pointer.
35	mov 	r2, r5		; Save a copy of the source pointer.
36	mov 	r3, r14		; Save a copy of the byte count.
37
38	mov	#0,  r2		; Search for the NUL byte.
39	mov 	#-1, r3		; Search until we run out of memory.
40	suntil.b		; Find the end of the destination string.
41	sub	#1, r1		; suntil.b leaves r1 pointing to the byte beyond the NUL.
42
43	mov	r14, r3		; Restore the limit on the number of bytes copied.
44	mov	r5, r2		; Restore the source pointer.
45	mov	r1, r5		; Save a copy of the dest pointer.
46	smovu			; Copy source to destination.
47
48	add	#0, r14, r3	; Restore the number of bytes to copy (again), but this time set the Z flag as well.
49	beq	1f  		; If we copied 0 bytes then we already know that the dest string is NUL terminated, so we do not have to do anything.
50	mov	#0, r2		; Otherwise we must check to see if a NUL byte
51	mov	r5, r1		;  was included in the bytes that were copied.
52	suntil.b
53	beq	1f		; Z flag is set if a match was found.
54	add	r14, r5		; Point at byte after end of copied bytes.
55	mov.b	#0, [r5]	; Store a NUL there.
561:
57	mov	r4, r1		; Return the original dest pointer.
58	rts
59#endif
60	.size _strncat, . - _strncat
61
62