1 /******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12 
13 #include <stddef.h>
14 
15 void *memcpy(void *dest, const void *src, size_t n);
16 void *memmove(void *dest, const void *src, size_t n);
memmove(void * dest,const void * src,size_t n)17 void *memmove(void *dest, const void *src, size_t n)
18 {
19 	/* Do the buffers overlap in a bad way? */
20 	if (src < dest && src + n >= dest) {
21 		char *cdest;
22 		const char *csrc;
23 		int i;
24 
25 		/* Copy from end to start */
26 		cdest = dest + n - 1;
27 		csrc = src + n - 1;
28 		for (i = 0; i < n; i++) {
29 			*cdest-- = *csrc--;
30 		}
31 		return dest;
32 	} else {
33 		/* Normal copy is possible */
34 		return memcpy(dest, src, n);
35 	}
36 }
37