xref: /onnv-gate/usr/src/lib/libnisdb/db_entry.cc (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	db_entry.cc
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  *	Copyright (c) 1988-2001 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  *	All Rights Reserved.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include "db_headers.h"
37*0Sstevel@tonic-gate #include "db_table.h"  /* must come before db_entry */
38*0Sstevel@tonic-gate #include "db_entry.h"
39*0Sstevel@tonic-gate #include "nisdb_mt.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #define	PRINT_WIDTH 32
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate void
print_entry(entryp location,entry_object * e)44*0Sstevel@tonic-gate print_entry(entryp location, entry_object *e)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	printf("entry at location %d: \n", location);
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	if (e == NULL) {
49*0Sstevel@tonic-gate 		printf("\tnull object\n");
50*0Sstevel@tonic-gate 		return;
51*0Sstevel@tonic-gate 	}
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	int size = e->en_cols.en_cols_len, i, j, col_width;
54*0Sstevel@tonic-gate 	entry_col * entry = e->en_cols.en_cols_val;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	printf("\ttype: %s\n", e->en_type ? e->en_type : "none");
57*0Sstevel@tonic-gate 	printf("\tnumber of columns: %d\n", size);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
60*0Sstevel@tonic-gate 		printf("\t\t%d: flags=0x%x, length=%d, value=",
61*0Sstevel@tonic-gate 			i, entry[i].ec_flags, entry[i].ec_value.ec_value_len);
62*0Sstevel@tonic-gate 		col_width = ((entry[i].ec_value.ec_value_len > PRINT_WIDTH) ?
63*0Sstevel@tonic-gate 				PRINT_WIDTH : entry[i].ec_value.ec_value_len);
64*0Sstevel@tonic-gate 		for (j = 0; j < col_width; j++) {
65*0Sstevel@tonic-gate 			if (entry[i].ec_value.ec_value_val[j] < 32) {
66*0Sstevel@tonic-gate 				putchar('^');
67*0Sstevel@tonic-gate 				putchar(entry[i].ec_value.ec_value_val[j]+32);
68*0Sstevel@tonic-gate 			} else {
69*0Sstevel@tonic-gate 				putchar(entry[i].ec_value.ec_value_val[j]);
70*0Sstevel@tonic-gate 			}
71*0Sstevel@tonic-gate 		}
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 		putchar('\n');
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate entry_object*
new_entry(entry_object * old)78*0Sstevel@tonic-gate new_entry(entry_object *old)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	entry_object* newobj = new entry_object;
81*0Sstevel@tonic-gate 	if (newobj == NULL)
82*0Sstevel@tonic-gate 	    FATAL3("new_entry:: cannot allocate space", DB_MEMORY_LIMIT,
83*0Sstevel@tonic-gate 			NULL);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	if (copy_entry(old, newobj))
86*0Sstevel@tonic-gate 		return (newobj);
87*0Sstevel@tonic-gate 	else {
88*0Sstevel@tonic-gate 	    delete newobj;
89*0Sstevel@tonic-gate 	    return (NULL);
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate bool_t
copy_entry(entry_object * old,entry_object * nb)94*0Sstevel@tonic-gate copy_entry(entry_object * old, entry_object *nb)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	int tlen, j, i;
97*0Sstevel@tonic-gate 	int num_cols = 0;
98*0Sstevel@tonic-gate 	entry_col *cols, *newcols = NULL;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	if (old == NULL) return FALSE;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (old->en_type == NULL)
103*0Sstevel@tonic-gate 		nb->en_type = NULL;
104*0Sstevel@tonic-gate 	else {
105*0Sstevel@tonic-gate 		nb->en_type = strdup(old->en_type);
106*0Sstevel@tonic-gate 		if (nb->en_type == NULL)
107*0Sstevel@tonic-gate 			FATAL3(
108*0Sstevel@tonic-gate 			    "copy_entry: cannot allocate space for entry type",
109*0Sstevel@tonic-gate 			    DB_MEMORY_LIMIT, FALSE);
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	num_cols = old->en_cols.en_cols_len;
113*0Sstevel@tonic-gate 	cols = old->en_cols.en_cols_val;
114*0Sstevel@tonic-gate 	if (num_cols == 0)
115*0Sstevel@tonic-gate 		nb->en_cols.en_cols_val = NULL;
116*0Sstevel@tonic-gate 	else {
117*0Sstevel@tonic-gate 		newcols = new entry_col[num_cols];
118*0Sstevel@tonic-gate 		if (newcols == NULL) {
119*0Sstevel@tonic-gate 			if (nb->en_type)
120*0Sstevel@tonic-gate 			delete nb->en_type;
121*0Sstevel@tonic-gate 			FATAL3("copy_entry: cannot allocate space for columns",
122*0Sstevel@tonic-gate 				DB_MEMORY_LIMIT, FALSE);
123*0Sstevel@tonic-gate 		}
124*0Sstevel@tonic-gate 		for (j = 0; j < num_cols; j++) {
125*0Sstevel@tonic-gate 			newcols[j].ec_flags = cols[j].ec_flags;
126*0Sstevel@tonic-gate 			tlen = newcols[j].ec_value.ec_value_len =
127*0Sstevel@tonic-gate 				cols[j].ec_value.ec_value_len;
128*0Sstevel@tonic-gate 			newcols[j].ec_value.ec_value_val = new char[ tlen ];
129*0Sstevel@tonic-gate 			if (newcols[j].ec_value.ec_value_val == NULL) {
130*0Sstevel@tonic-gate 				// cleanup space already allocated
131*0Sstevel@tonic-gate 				if (nb->en_type)
132*0Sstevel@tonic-gate 					delete nb->en_type;
133*0Sstevel@tonic-gate 				for (i = 0; i < j; i++)
134*0Sstevel@tonic-gate 					delete newcols[i].ec_value.ec_value_val;
135*0Sstevel@tonic-gate 				delete newcols;
136*0Sstevel@tonic-gate 				FATAL3(
137*0Sstevel@tonic-gate 			"copy_entry: cannot allocate space for column value",
138*0Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
139*0Sstevel@tonic-gate 			}
140*0Sstevel@tonic-gate 			memcpy(newcols[j].ec_value.ec_value_val,
141*0Sstevel@tonic-gate 				cols[j].ec_value.ec_value_val,
142*0Sstevel@tonic-gate 				tlen);
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 	nb->en_cols.en_cols_len = num_cols;
146*0Sstevel@tonic-gate 	nb->en_cols.en_cols_val = newcols;
147*0Sstevel@tonic-gate 	return (TRUE);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate void
free_entry(entry_object * obj)151*0Sstevel@tonic-gate free_entry(entry_object * obj)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	int i;
154*0Sstevel@tonic-gate 	int num_cols;
155*0Sstevel@tonic-gate 	entry_col *cols;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (obj != NULL) {
158*0Sstevel@tonic-gate 		num_cols = obj->en_cols.en_cols_len;
159*0Sstevel@tonic-gate 		cols = obj->en_cols.en_cols_val;
160*0Sstevel@tonic-gate 		for (i = 0; i < num_cols; i++)
161*0Sstevel@tonic-gate 			if (cols[i].ec_value.ec_value_val != NULL)
162*0Sstevel@tonic-gate 				delete cols[i].ec_value.ec_value_val;
163*0Sstevel@tonic-gate 		if (cols)
164*0Sstevel@tonic-gate 			delete cols;
165*0Sstevel@tonic-gate 		if (obj->en_type)
166*0Sstevel@tonic-gate 			delete obj->en_type;
167*0Sstevel@tonic-gate 		delete obj;
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate bool_t
sameEntry(entry_object * a,entry_object * b)172*0Sstevel@tonic-gate sameEntry(entry_object *a, entry_object *b) {
173*0Sstevel@tonic-gate 	uint_t	i;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (a == 0)
176*0Sstevel@tonic-gate 		return (b == 0);
177*0Sstevel@tonic-gate 	if (b == 0)
178*0Sstevel@tonic-gate 		return (FALSE);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if (a->en_type != 0 && b->en_type != 0) {
181*0Sstevel@tonic-gate 		if (strcmp(a->en_type, b->en_type) != 0)
182*0Sstevel@tonic-gate 			return (FALSE);
183*0Sstevel@tonic-gate 	} else if (a->en_type != b->en_type) {
184*0Sstevel@tonic-gate 		return (FALSE);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (a->en_cols.en_cols_len != b->en_cols.en_cols_len)
188*0Sstevel@tonic-gate 		return (FALSE);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	for (i = 0; i < a->en_cols.en_cols_len; i++) {
191*0Sstevel@tonic-gate 		if (a->en_cols.en_cols_val[i].ec_flags !=
192*0Sstevel@tonic-gate 				b->en_cols.en_cols_val[i].ec_flags)
193*0Sstevel@tonic-gate 			return (FALSE);
194*0Sstevel@tonic-gate 		if (a->en_cols.en_cols_val[i].ec_value.ec_value_len !=
195*0Sstevel@tonic-gate 			b->en_cols.en_cols_val[i].ec_value.ec_value_len)
196*0Sstevel@tonic-gate 			return (FALSE);
197*0Sstevel@tonic-gate 		if (memcmp(a->en_cols.en_cols_val[i].ec_value.ec_value_val,
198*0Sstevel@tonic-gate 			b->en_cols.en_cols_val[i].ec_value.ec_value_val,
199*0Sstevel@tonic-gate 			a->en_cols.en_cols_val[i].ec_value.ec_value_len) != 0)
200*0Sstevel@tonic-gate 			return (FALSE);
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	return (TRUE);
204*0Sstevel@tonic-gate }
205