xref: /illumos-gate/usr/src/lib/libcurses/screen/newwin.c (revision 48edc7cf07b5dccc3ad84bf2dafe4150bd666d60)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 /* allocate space for and set up defaults for a new _window */
45 
46 #include	<stdlib.h>
47 #include	<sys/types.h>
48 #include	"curses_inc.h"
49 
50 WINDOW	*
51 newwin(int nlines, int ncols, int by, int bx)
52 {
53 	WINDOW	*win;
54 	int		counter = 0;
55 
56 	if (nlines <= 0)
57 		nlines = LINES - by;
58 	if (ncols <= 0)
59 		ncols = COLS - bx;
60 
61 	if ((by < 0) || (bx < 0) || ((win = _makenew(nlines, ncols, by,
62 	    bx)) == (WINDOW *) NULL) || (_image(win) == ERR)) {
63 		return ((WINDOW *) NULL);
64 	}
65 
66 	while (counter < nlines) {
67 		memSset(&win->_y[counter][0], (chtype) ' ', ncols);
68 #ifdef	_VR3_COMPAT_CODE
69 		if (_y16update) {
70 			int	i = ncols;
71 
72 			while (i--)
73 				win->_y16[counter][i] = (_ochtype) ' ';
74 		}
75 #endif	/* _VR3_COMPAT_CODE */
76 		counter++;
77 	}
78 
79 	win->_yoffset = SP->Yabove;
80 	return (win);
81 }
82 
83 int
84 _image(WINDOW *win)
85 {
86 			int	i, nlines = win->_maxy;
87 #ifdef	_VR3_COMPAT_CODE
88 			size_t oscols = win->_maxx * sizeof (_ochtype);
89 #endif	/* _VR3_COMPAT_CODE */
90 			size_t scols = win->_maxx * sizeof (chtype);
91 			chtype	**_y = win->_y;
92 #ifdef	_VR3_COMPAT_CODE
93 			_ochtype	**_y16 = win->_y16;
94 #endif	/* _VR3_COMPAT_CODE */
95 
96 	for (i = 0; i < nlines; i++) {
97 #ifdef	_VR3_COMPAT_CODE
98 		if (((_y[i] = (chtype *) malloc(scols)) == NULL) ||
99 		    ((_y16update) && ((_y16[i] = (_ochtype *)
100 		    malloc(oscols)) == NULL)))
101 #else	/* _VR3_COMPAT_CODE */
102 			if ((_y[i] = (chtype *) malloc(scols)) ==
103 			    NULL)
104 #endif	/* _VR3_COMPAT_CODE */
105 				{
106 				int	j;
107 
108 				curs_errno = CURS_BAD_MALLOC;
109 #ifdef	DEBUG
110 				strcpy(curs_parm_err, "_image");
111 #endif	/* DEBUG */
112 #ifdef	_VR3_COMPAT_CODE
113 				for (j = 0; j <= i; j++) {
114 					if (_y[j] != NULL)
115 					free((char *) _y[j]);
116 					if ((_y16update) && (_y16[j] != NULL))
117 						free((char *) _y16[j]);
118 				}
119 #else	/* _VR3_COMPAT_CODE */
120 				for (j = 0; j < i; j++)
121 					free((char *) _y[j]);
122 #endif	/* _VR3_COMPAT_CODE */
123 
124 				free((char *) win->_firstch);
125 				free((char *) win->_y);
126 #ifdef	_VR3_COMPAT_CODE
127 				if ((_y16update) && (win->_y16 != NULL))
128 					free((char *) win->_y16);
129 #endif	/* _VR3_COMPAT_CODE */
130 				free((char *) win);
131 				return (ERR);
132 			}
133 	}
134 	return (OK);
135 }
136