1 /*
2  * blt(), also know as memmove(3), include file for Mathomatic.
3  *
4  * Copyright (C) 1987-2012 George Gesslein II.
5 
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10 
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 
20 The chief copyright holder can be contacted at gesslein@mathomatic.org, or
21 George Gesslein II, P.O. Box 224, Lansing, NY  14882-0224  USA.
22 
23  */
24 
25 #if	1
26 #define	blt(dest, src, cnt)	memmove((dest), (src), (cnt))	/* memory copy function; must allow overlapping of src and dest */
27 #else
28 /* If no fast or working memmove(3) routine exists use this one. */
29 static inline char *
blt(dest,src,cnt)30 blt(dest, src, cnt)
31 char		*dest;
32 const char	*src;
33 int		cnt;
34 {
35 	char		*tdest;
36 	const char	*tsrc;
37 	int		tcnt;
38 
39 	if (cnt <= 0) {
40 		if (cnt == 0) {
41 			return dest;
42 		} else {
43 			error_bug("blt() cnt < 0");
44 		}
45 	}
46 	if (src == dest) {
47 		return dest;
48 	}
49 
50 	tdest = dest;
51 	tsrc = src;
52 	tcnt = cnt;
53 
54 	if (tdest > tsrc) {
55 		tdest += tcnt;
56 		tsrc += tcnt;
57 		while (--tcnt >= 0)
58 			*--tdest = *--tsrc;
59 	} else {
60 		while (--tcnt >= 0)
61 			*tdest++ = *tsrc++;
62 	}
63 	return dest;
64 }
65 #endif
66