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-1998 by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 /* LINTLIBRARY */
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 "
39 "1995/05/18 20:55:05 ant Exp $";
40 #endif
41 #endif
42
43 #include <private.h>
44
45 static void reverse(void **, unsigned int, unsigned int);
46
47 /*
48 * Move range start..finish inclusive before the given location.
49 * Return -1 if the region to move is out of bounds or the target
50 * falls within the region; 0 for success.
51 *
52 * (See Software Tools chapter 6.)
53 */
54 int
__m_ptr_move(void ** array,unsigned int length,unsigned int start,unsigned int finish,unsigned int to)55 __m_ptr_move(void **array, unsigned int length,
56 unsigned int start, unsigned int finish, unsigned int to)
57 {
58 if (finish < start || length <= finish)
59 return (-1);
60
61 if (to < start) {
62 reverse(array, to, start-1);
63 reverse(array, start, finish);
64 reverse(array, to, finish);
65 } else if (finish < to && to <= length) {
66 reverse(array, start, finish);
67 reverse(array, finish+1, to-1);
68 reverse(array, start, to-1);
69 } else {
70 return (-1);
71 }
72
73 return (0);
74 }
75
76 /*
77 * Reverse range a..b inclusive.
78 */
79 static void
reverse(void ** ptr,unsigned int a,unsigned int b)80 reverse(void **ptr, unsigned int a, unsigned int b)
81 {
82 void *temp;
83 void **a_ptr = &ptr[a];
84 void **b_ptr = &ptr[b];
85
86 while (a_ptr < b_ptr) {
87 temp = *a_ptr;
88 *a_ptr++ = *b_ptr;
89 *b_ptr-- = temp;
90 }
91 }
92