1/* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7#ifdef LIBC_SCCS 8 .asciz "@(#)strncpy.s 5.3 (Berkeley) 03/09/86" 9#endif LIBC_SCCS 10 11/* 12 * Copy string s2 over top of string s1. 13 * Truncate or null-pad to n bytes. 14 * 15 * char * 16 * strncpy(s1, s2, n) 17 * char *s1, *s2; 18 */ 19#include "DEFS.h" 20 21ENTRY(strncpy, R6) 22 movl 12(ap),r6 # r6 = n 23 bleq done # n <= 0 24 movl 4(ap),r3 # r3 = s1 25 movl 8(ap),r1 # r1 = s2 261: 27 movzwl $65535,r2 # r2 = bytes in first chunk 28 cmpl r6,r2 # r2 = min(bytes in chunk, n); 29 jgeq 2f 30 movl r6,r2 312: 32 subl2 r2,r6 # update n 33 locc $0,r2,(r1) # '\0' found? 34 jneq 3f 35 subl2 r2,r1 # back up pointer updated by locc 36 movc3 r2,(r1),(r3) # copy in next piece 37 tstl r6 # run out of space? 38 jneq 1b 39 jbr done 403: # copy up to '\0' logic 41 addl2 r0,r6 # r6 = number of null-pad bytes 42 subl2 r0,r2 # r2 = number of bytes to move 43 subl2 r2,r1 # back up pointer updated by locc 44 movc3 r2,(r1),(r3) # copy in last piece 454: # null-pad logic 46 movzwl $65535,r2 # r2 = bytes in first chunk 47 cmpl r6,r2 # r2 = min(bytes in chunk, n); 48 jgeq 5f 49 movl r6,r2 505: 51 subl2 r2,r6 # update n 52 movc5 $0,(r3),$0,r2,(r3)# pad with '\0's 53 tstl r6 # finished padding? 54 jneq 4b 55done: 56 movl 4(ap),r0 # return s1 57 ret 58