Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* A perhaps slow but I hope correct implementation of memmove */ |
| 3 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4 | extern char *memcpy(char *, char *, int); |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 5 | |
| 6 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 7 | memmove(char *dst, char *src, int n) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 8 | { |
| 9 | char *realdst = dst; |
| 10 | if (n <= 0) |
| 11 | return dst; |
| 12 | if (src >= dst+n || dst >= src+n) |
| 13 | return memcpy(dst, src, n); |
| 14 | if (src > dst) { |
| 15 | while (--n >= 0) |
| 16 | *dst++ = *src++; |
| 17 | } |
| 18 | else if (src < dst) { |
| 19 | src += n; |
| 20 | dst += n; |
| 21 | while (--n >= 0) |
| 22 | *--dst = *--src; |
| 23 | } |
| 24 | return realdst; |
| 25 | } |