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