xref: /openbsd-src/sys/lib/libkern/memmove.c (revision 77f321a54c747e5d40bc22f3f68b77b421ee4582)
1*77f321a5Sjsg /*	$OpenBSD: memmove.c,v 1.3 2021/05/16 04:45:58 jsg Exp $	*/
26c32d98cSderaadt 
36c32d98cSderaadt /*-
46c32d98cSderaadt  * Copyright (c) 1993
56c32d98cSderaadt  *	The Regents of the University of California.  All rights reserved.
66c32d98cSderaadt  *
76c32d98cSderaadt  * Redistribution and use in source and binary forms, with or without
86c32d98cSderaadt  * modification, are permitted provided that the following conditions
96c32d98cSderaadt  * are met:
106c32d98cSderaadt  * 1. Redistributions of source code must retain the above copyright
116c32d98cSderaadt  *    notice, this list of conditions and the following disclaimer.
126c32d98cSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
136c32d98cSderaadt  *    notice, this list of conditions and the following disclaimer in the
146c32d98cSderaadt  *    documentation and/or other materials provided with the distribution.
156c32d98cSderaadt  * 3. Neither the name of the University nor the names of its contributors
166c32d98cSderaadt  *    may be used to endorse or promote products derived from this software
176c32d98cSderaadt  *    without specific prior written permission.
186c32d98cSderaadt  *
196c32d98cSderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
206c32d98cSderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
216c32d98cSderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
226c32d98cSderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
236c32d98cSderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
246c32d98cSderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
256c32d98cSderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
266c32d98cSderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
276c32d98cSderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
286c32d98cSderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
296c32d98cSderaadt  * SUCH DAMAGE.
306c32d98cSderaadt  */
316c32d98cSderaadt 
326c32d98cSderaadt #include <sys/types.h>
336c32d98cSderaadt #include <sys/systm.h>
346c32d98cSderaadt 
35249415e0Skettenis #undef memmove
36249415e0Skettenis 
376c32d98cSderaadt /*
386c32d98cSderaadt  * This is designed to be small, not fast.
396c32d98cSderaadt  */
406c32d98cSderaadt void *
memmove(void * s1,const void * s2,size_t n)416c32d98cSderaadt memmove(void *s1, const void *s2, size_t n)
426c32d98cSderaadt {
43*77f321a5Sjsg 	const char *f = s2;
44*77f321a5Sjsg 	char *t = s1;
456c32d98cSderaadt 
466c32d98cSderaadt 	if (f < t) {
476c32d98cSderaadt 		f += n;
486c32d98cSderaadt 		t += n;
496c32d98cSderaadt 		while (n-- > 0)
506c32d98cSderaadt 			*--t = *--f;
516c32d98cSderaadt 	} else
526c32d98cSderaadt 		while (n-- > 0)
536c32d98cSderaadt 			*t++ = *f++;
546c32d98cSderaadt 	return s1;
556c32d98cSderaadt }
56