xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/ptrmove.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * ptrmove.c
31  *
32  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/ptrmove.c 1.3 1995/05/18 20:55:05 ant Exp $";
39 #endif
40 #endif
41 
42 #include <private.h>
43 
44 static void reverse(void **, int, int);
45 
46 /*
47  * Move range start..finish inclusive before the given location.
48  * Return -1 if the region to move is out of bounds or the target
49  * falls within the region; 0 for success.
50  *
51  * (See Software Tools chapter 6.)
52  */
53 int
54 __m_ptr_move(array, length, start, finish, to)
55 void **array;
56 unsigned length, start, finish, to;
57 {
58 #ifdef M_CURSES_TRACE
59 	__m_trace(
60 		"__m_ptr_move(%p, %d, %d, %d, %d)",
61 		array, length, start, finish, to
62 	);
63 #endif
64 	if (finish < start || length <= finish)
65 		return __m_return_int("__m_ptr_move()", -1);
66 
67 	if (to < start) {
68 		reverse(array, to, start-1);
69 		reverse(array, start, finish);
70 		reverse(array, to, finish);
71 	} else if (finish < to && to <= length) {
72 		reverse(array, start, finish);
73 		reverse(array, finish+1, to-1);
74 		reverse(array, start, to-1);
75 	} else {
76 		return __m_return_int("__m_ptr_move()", -1);
77 	}
78 
79 	return __m_return_int("__m_ptr_move()", 0);
80 }
81 
82 /*
83  * Reverse range a..b inclusive.
84  */
85 static void
86 reverse(ptr, a, b)
87 void **ptr;
88 int a, b;
89 {
90 	register void *temp;
91 	register void **a_ptr = &ptr[a];
92 	register void **b_ptr = &ptr[b];
93 
94 	while (a_ptr < b_ptr) {
95 		temp = *a_ptr;
96 		*a_ptr++ = *b_ptr;
97 		*b_ptr-- = temp;
98 	}
99 }
100