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 /*
28  * ptrmove.c
29  *
30  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
31  *
32  */
33 
34 #ifdef M_RCSID
35 #ifndef lint
36 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/ptrmove.c 1.3 1995/05/18 20:55:05 ant Exp $";
37 #endif
38 #endif
39 
40 #include <private.h>
41 
42 static void reverse(void **, int, int);
43 
44 /*
45  * Move range start..finish inclusive before the given location.
46  * Return -1 if the region to move is out of bounds or the target
47  * falls within the region; 0 for success.
48  *
49  * (See Software Tools chapter 6.)
50  */
51 int
__m_ptr_move(array,length,start,finish,to)52 __m_ptr_move(array, length, start, finish, to)
53 void **array;
54 unsigned length, start, finish, to;
55 {
56 #ifdef M_CURSES_TRACE
57 	__m_trace(
58 		"__m_ptr_move(%p, %d, %d, %d, %d)",
59 		array, length, start, finish, to
60 	);
61 #endif
62 	if (finish < start || length <= finish)
63 		return __m_return_int("__m_ptr_move()", -1);
64 
65 	if (to < start) {
66 		reverse(array, to, start-1);
67 		reverse(array, start, finish);
68 		reverse(array, to, finish);
69 	} else if (finish < to && to <= length) {
70 		reverse(array, start, finish);
71 		reverse(array, finish+1, to-1);
72 		reverse(array, start, to-1);
73 	} else {
74 		return __m_return_int("__m_ptr_move()", -1);
75 	}
76 
77 	return __m_return_int("__m_ptr_move()", 0);
78 }
79 
80 /*
81  * Reverse range a..b inclusive.
82  */
83 static void
reverse(ptr,a,b)84 reverse(ptr, a, b)
85 void **ptr;
86 int a, b;
87 {
88 	register void *temp;
89 	register void **a_ptr = &ptr[a];
90 	register void **b_ptr = &ptr[b];
91 
92 	while (a_ptr < b_ptr) {
93 		temp = *a_ptr;
94 		*a_ptr++ = *b_ptr;
95 		*b_ptr-- = temp;
96 	}
97 }
98