xref: /titanic_52/usr/src/lib/libnisdb/db_entry.cc (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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  *	db_entry.cc
24  *
25  *	Copyright (c) 1988-2001 by Sun Microsystems, Inc.
26  *	All Rights Reserved.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include "db_headers.h"
37 #include "db_table.h"  /* must come before db_entry */
38 #include "db_entry.h"
39 #include "nisdb_mt.h"
40 
41 #define	PRINT_WIDTH 32
42 
43 void
44 print_entry(entryp location, entry_object *e)
45 {
46 	printf("entry at location %d: \n", location);
47 
48 	if (e == NULL) {
49 		printf("\tnull object\n");
50 		return;
51 	}
52 
53 	int size = e->en_cols.en_cols_len, i, j, col_width;
54 	entry_col * entry = e->en_cols.en_cols_val;
55 
56 	printf("\ttype: %s\n", e->en_type ? e->en_type : "none");
57 	printf("\tnumber of columns: %d\n", size);
58 
59 	for (i = 0; i < size; i++) {
60 		printf("\t\t%d: flags=0x%x, length=%d, value=",
61 			i, entry[i].ec_flags, entry[i].ec_value.ec_value_len);
62 		col_width = ((entry[i].ec_value.ec_value_len > PRINT_WIDTH) ?
63 				PRINT_WIDTH : entry[i].ec_value.ec_value_len);
64 		for (j = 0; j < col_width; j++) {
65 			if (entry[i].ec_value.ec_value_val[j] < 32) {
66 				putchar('^');
67 				putchar(entry[i].ec_value.ec_value_val[j]+32);
68 			} else {
69 				putchar(entry[i].ec_value.ec_value_val[j]);
70 			}
71 		}
72 
73 		putchar('\n');
74 	}
75 }
76 
77 entry_object*
78 new_entry(entry_object *old)
79 {
80 	entry_object* newobj = new entry_object;
81 	if (newobj == NULL)
82 	    FATAL3("new_entry:: cannot allocate space", DB_MEMORY_LIMIT,
83 			NULL);
84 
85 	if (copy_entry(old, newobj))
86 		return (newobj);
87 	else {
88 	    delete newobj;
89 	    return (NULL);
90 	}
91 }
92 
93 bool_t
94 copy_entry(entry_object * old, entry_object *nb)
95 {
96 	int tlen, j, i;
97 	int num_cols = 0;
98 	entry_col *cols, *newcols = NULL;
99 
100 	if (old == NULL) return FALSE;
101 
102 	if (old->en_type == NULL)
103 		nb->en_type = NULL;
104 	else {
105 		nb->en_type = strdup(old->en_type);
106 		if (nb->en_type == NULL)
107 			FATAL3(
108 			    "copy_entry: cannot allocate space for entry type",
109 			    DB_MEMORY_LIMIT, FALSE);
110 	}
111 
112 	num_cols = old->en_cols.en_cols_len;
113 	cols = old->en_cols.en_cols_val;
114 	if (num_cols == 0)
115 		nb->en_cols.en_cols_val = NULL;
116 	else {
117 		newcols = new entry_col[num_cols];
118 		if (newcols == NULL) {
119 			if (nb->en_type)
120 			delete nb->en_type;
121 			FATAL3("copy_entry: cannot allocate space for columns",
122 				DB_MEMORY_LIMIT, FALSE);
123 		}
124 		for (j = 0; j < num_cols; j++) {
125 			newcols[j].ec_flags = cols[j].ec_flags;
126 			tlen = newcols[j].ec_value.ec_value_len =
127 				cols[j].ec_value.ec_value_len;
128 			newcols[j].ec_value.ec_value_val = new char[ tlen ];
129 			if (newcols[j].ec_value.ec_value_val == NULL) {
130 				// cleanup space already allocated
131 				if (nb->en_type)
132 					delete nb->en_type;
133 				for (i = 0; i < j; i++)
134 					delete newcols[i].ec_value.ec_value_val;
135 				delete newcols;
136 				FATAL3(
137 			"copy_entry: cannot allocate space for column value",
138 			DB_MEMORY_LIMIT, FALSE);
139 			}
140 			memcpy(newcols[j].ec_value.ec_value_val,
141 				cols[j].ec_value.ec_value_val,
142 				tlen);
143 		}
144 	}
145 	nb->en_cols.en_cols_len = num_cols;
146 	nb->en_cols.en_cols_val = newcols;
147 	return (TRUE);
148 }
149 
150 void
151 free_entry(entry_object * obj)
152 {
153 	int i;
154 	int num_cols;
155 	entry_col *cols;
156 
157 	if (obj != NULL) {
158 		num_cols = obj->en_cols.en_cols_len;
159 		cols = obj->en_cols.en_cols_val;
160 		for (i = 0; i < num_cols; i++)
161 			if (cols[i].ec_value.ec_value_val != NULL)
162 				delete cols[i].ec_value.ec_value_val;
163 		if (cols)
164 			delete cols;
165 		if (obj->en_type)
166 			delete obj->en_type;
167 		delete obj;
168 	}
169 }
170 
171 bool_t
172 sameEntry(entry_object *a, entry_object *b) {
173 	uint_t	i;
174 
175 	if (a == 0)
176 		return (b == 0);
177 	if (b == 0)
178 		return (FALSE);
179 
180 	if (a->en_type != 0 && b->en_type != 0) {
181 		if (strcmp(a->en_type, b->en_type) != 0)
182 			return (FALSE);
183 	} else if (a->en_type != b->en_type) {
184 		return (FALSE);
185 	}
186 
187 	if (a->en_cols.en_cols_len != b->en_cols.en_cols_len)
188 		return (FALSE);
189 
190 	for (i = 0; i < a->en_cols.en_cols_len; i++) {
191 		if (a->en_cols.en_cols_val[i].ec_flags !=
192 				b->en_cols.en_cols_val[i].ec_flags)
193 			return (FALSE);
194 		if (a->en_cols.en_cols_val[i].ec_value.ec_value_len !=
195 			b->en_cols.en_cols_val[i].ec_value.ec_value_len)
196 			return (FALSE);
197 		if (memcmp(a->en_cols.en_cols_val[i].ec_value.ec_value_val,
198 			b->en_cols.en_cols_val[i].ec_value.ec_value_val,
199 			a->en_cols.en_cols_val[i].ec_value.ec_value_len) != 0)
200 			return (FALSE);
201 	}
202 
203 	return (TRUE);
204 }
205