xref: /onnv-gate/usr/src/common/util/memmove.c (revision 12391:e5884cf20c37)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56812Sraf  * Common Development and Distribution License (the "License").
66812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216812Sraf 
220Sstevel@tonic-gate /*
23*12391SSurya.Prakki@Sun.COM  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #if !defined(_KMDB) && !defined(_KERNEL)
300Sstevel@tonic-gate 
31*12391SSurya.Prakki@Sun.COM #include "lint.h"
32*12391SSurya.Prakki@Sun.COM 
33*12391SSurya.Prakki@Sun.COM #endif /* !_KMDB && !_KERNEL */
340Sstevel@tonic-gate 
35*12391SSurya.Prakki@Sun.COM /*
36*12391SSurya.Prakki@Sun.COM  * The SunStudio compiler may generate calls to _memmove; So we
37*12391SSurya.Prakki@Sun.COM  * need to make sure that the correct symbol exists for these calls.
38*12391SSurya.Prakki@Sun.COM  */
39*12391SSurya.Prakki@Sun.COM #pragma weak _memmove = memmove
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #if defined(_KERNEL)
440Sstevel@tonic-gate #include <sys/systm.h>
450Sstevel@tonic-gate #else
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <memory.h>
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Copy s0 to s, always copy n bytes.
520Sstevel@tonic-gate  * Return s
530Sstevel@tonic-gate  * Copying between objects that overlap will take place correctly
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate void *
memmove(void * s,const void * s0,size_t n)560Sstevel@tonic-gate memmove(void *s, const void *s0, size_t n)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	if (n != 0) {
590Sstevel@tonic-gate 		char *s1 = s;
600Sstevel@tonic-gate 		const char *s2 = s0;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 		if (s1 <= s2) {
630Sstevel@tonic-gate 			do {
640Sstevel@tonic-gate 				*s1++ = *s2++;
650Sstevel@tonic-gate 			} while (--n != 0);
660Sstevel@tonic-gate 		} else {
670Sstevel@tonic-gate 			s2 += n;
680Sstevel@tonic-gate 			s1 += n;
690Sstevel@tonic-gate 			do {
700Sstevel@tonic-gate 				*--s1 = *--s2;
710Sstevel@tonic-gate 			} while (--n != 0);
720Sstevel@tonic-gate 		}
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 	return (s);
750Sstevel@tonic-gate }
76