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