1/* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific written prior permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 */ 12 13#if defined(SYSLIBC_SCCS) && !defined(lint) 14_sccsid:.asciz "@(#)strncpy.s 5.4 (Berkeley) 05/25/88" 15#endif /* SYSLIBC_SCCS and not lint */ 16 17/* 18 * Copy string s2 over top of string s1. 19 * Truncate or null-pad to n bytes. 20 * 21 * char * 22 * strncpy(s1, s2, n) 23 * char *s1, *s2; 24 */ 25#include "DEFS.h" 26 27ENTRY(strncpy, R6) 28 movl 12(ap),r6 # r6 = n 29 bleq done # n <= 0 30 movl 4(ap),r3 # r3 = s1 31 movl 8(ap),r1 # r1 = s2 321: 33 movzwl $65535,r2 # r2 = bytes in first chunk 34 cmpl r6,r2 # r2 = min(bytes in chunk, n); 35 jgeq 2f 36 movl r6,r2 372: 38 subl2 r2,r6 # update n 39 locc $0,r2,(r1) # '\0' found? 40 jneq 3f 41 subl2 r2,r1 # back up pointer updated by locc 42 movc3 r2,(r1),(r3) # copy in next piece 43 tstl r6 # run out of space? 44 jneq 1b 45 jbr done 463: # copy up to '\0' logic 47 addl2 r0,r6 # r6 = number of null-pad bytes 48 subl2 r0,r2 # r2 = number of bytes to move 49 subl2 r2,r1 # back up pointer updated by locc 50 movc3 r2,(r1),(r3) # copy in last piece 514: # null-pad logic 52 movzwl $65535,r2 # r2 = bytes in first chunk 53 cmpl r6,r2 # r2 = min(bytes in chunk, n); 54 jgeq 5f 55 movl r6,r2 565: 57 subl2 r2,r6 # update n 58 movc5 $0,(r3),$0,r2,(r3)# pad with '\0's 59 tstl r6 # finished padding? 60 jneq 4b 61done: 62 movl 4(ap),r0 # return s1 63 ret 64