xref: /openbsd-src/sys/lib/libsa/memmove.c (revision 7a2189da75c696c4404ba58cd970151833ffe027)
1*7a2189daSderaadt /*	$OpenBSD: memmove.c,v 1.1 2019/04/20 22:59:04 deraadt Exp $	*/
2*7a2189daSderaadt 
3*7a2189daSderaadt /*-
4*7a2189daSderaadt  * Copyright (c) 1993
5*7a2189daSderaadt  *	The Regents of the University of California.  All rights reserved.
6*7a2189daSderaadt  *
7*7a2189daSderaadt  * Redistribution and use in source and binary forms, with or without
8*7a2189daSderaadt  * modification, are permitted provided that the following conditions
9*7a2189daSderaadt  * are met:
10*7a2189daSderaadt  * 1. Redistributions of source code must retain the above copyright
11*7a2189daSderaadt  *    notice, this list of conditions and the following disclaimer.
12*7a2189daSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13*7a2189daSderaadt  *    notice, this list of conditions and the following disclaimer in the
14*7a2189daSderaadt  *    documentation and/or other materials provided with the distribution.
15*7a2189daSderaadt  * 3. Neither the name of the University nor the names of its contributors
16*7a2189daSderaadt  *    may be used to endorse or promote products derived from this software
17*7a2189daSderaadt  *    without specific prior written permission.
18*7a2189daSderaadt  *
19*7a2189daSderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*7a2189daSderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*7a2189daSderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*7a2189daSderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*7a2189daSderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*7a2189daSderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*7a2189daSderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*7a2189daSderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*7a2189daSderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*7a2189daSderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*7a2189daSderaadt  * SUCH DAMAGE.
30*7a2189daSderaadt  */
31*7a2189daSderaadt 
32*7a2189daSderaadt #include <sys/types.h>
33*7a2189daSderaadt #include <sys/systm.h>
34*7a2189daSderaadt 
35*7a2189daSderaadt #undef memmove
36*7a2189daSderaadt 
37*7a2189daSderaadt /*
38*7a2189daSderaadt  * This is designed to be small, not fast.
39*7a2189daSderaadt  */
40*7a2189daSderaadt void *
memmove(void * s1,const void * s2,size_t n)41*7a2189daSderaadt memmove(void *s1, const void *s2, size_t n)
42*7a2189daSderaadt {
43*7a2189daSderaadt 	const char *f = s2;
44*7a2189daSderaadt 	char *t = s1;
45*7a2189daSderaadt 
46*7a2189daSderaadt 	if (f < t) {
47*7a2189daSderaadt 		f += n;
48*7a2189daSderaadt 		t += n;
49*7a2189daSderaadt 		while (n-- > 0)
50*7a2189daSderaadt 			*--t = *--f;
51*7a2189daSderaadt 	} else
52*7a2189daSderaadt 		while (n-- > 0)
53*7a2189daSderaadt 			*t++ = *f++;
54*7a2189daSderaadt 	return s1;
55*7a2189daSderaadt }
56