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 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Routines for manipulating iidesc_t structures 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 35*7c478bd9Sstevel@tonic-gate #include <strings.h> 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include "ctftools.h" 38*7c478bd9Sstevel@tonic-gate #include "memory.h" 39*7c478bd9Sstevel@tonic-gate #include "list.h" 40*7c478bd9Sstevel@tonic-gate #include "hash.h" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate typedef struct iidesc_find { 43*7c478bd9Sstevel@tonic-gate iidesc_t *iif_tgt; 44*7c478bd9Sstevel@tonic-gate iidesc_t *iif_ret; 45*7c478bd9Sstevel@tonic-gate } iidesc_find_t; 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate iidesc_t * 48*7c478bd9Sstevel@tonic-gate iidesc_new(char *name) 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate iidesc_t *ii; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate ii = xcalloc(sizeof (iidesc_t)); 53*7c478bd9Sstevel@tonic-gate if (name) 54*7c478bd9Sstevel@tonic-gate ii->ii_name = xstrdup(name); 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate return (ii); 57*7c478bd9Sstevel@tonic-gate } 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate int 60*7c478bd9Sstevel@tonic-gate iidesc_hash(int nbuckets, void *arg) 61*7c478bd9Sstevel@tonic-gate { 62*7c478bd9Sstevel@tonic-gate iidesc_t *ii = arg; 63*7c478bd9Sstevel@tonic-gate int h = 0; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate if (ii->ii_name) 66*7c478bd9Sstevel@tonic-gate return (hash_name(nbuckets, ii->ii_name)); 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate return (h); 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate static int 72*7c478bd9Sstevel@tonic-gate iidesc_cmp(iidesc_t *src, iidesc_find_t *find) 73*7c478bd9Sstevel@tonic-gate { 74*7c478bd9Sstevel@tonic-gate iidesc_t *tgt = find->iif_tgt; 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate if (src->ii_type != tgt->ii_type || 77*7c478bd9Sstevel@tonic-gate !streq(src->ii_name, tgt->ii_name)) 78*7c478bd9Sstevel@tonic-gate return (0); 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate find->iif_ret = src; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate return (-1); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate void 86*7c478bd9Sstevel@tonic-gate iidesc_add(hash_t *hash, iidesc_t *new) 87*7c478bd9Sstevel@tonic-gate { 88*7c478bd9Sstevel@tonic-gate iidesc_find_t find; 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate find.iif_tgt = new; 91*7c478bd9Sstevel@tonic-gate find.iif_ret = NULL; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate (void) hash_match(hash, new, (int (*)())iidesc_cmp, &find); 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if (find.iif_ret != NULL) { 96*7c478bd9Sstevel@tonic-gate iidesc_t *old = find.iif_ret; 97*7c478bd9Sstevel@tonic-gate iidesc_t tmp; 98*7c478bd9Sstevel@tonic-gate /* replacing existing one */ 99*7c478bd9Sstevel@tonic-gate bcopy(old, &tmp, sizeof (tmp)); 100*7c478bd9Sstevel@tonic-gate bcopy(new, old, sizeof (*old)); 101*7c478bd9Sstevel@tonic-gate bcopy(&tmp, new, sizeof (*new)); 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate iidesc_free(new, NULL); 104*7c478bd9Sstevel@tonic-gate return; 105*7c478bd9Sstevel@tonic-gate } 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate hash_add(hash, new); 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate iidesc_t * 111*7c478bd9Sstevel@tonic-gate iidesc_dup(iidesc_t *src) 112*7c478bd9Sstevel@tonic-gate { 113*7c478bd9Sstevel@tonic-gate iidesc_t *tgt; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate tgt = xmalloc(sizeof (iidesc_t)); 116*7c478bd9Sstevel@tonic-gate bcopy(src, tgt, sizeof (iidesc_t)); 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL; 119*7c478bd9Sstevel@tonic-gate tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL; 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate if (tgt->ii_nargs) { 122*7c478bd9Sstevel@tonic-gate tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs); 123*7c478bd9Sstevel@tonic-gate bcopy(src->ii_args, tgt->ii_args, 124*7c478bd9Sstevel@tonic-gate sizeof (tdesc_t *) * tgt->ii_nargs); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate return (tgt); 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate iidesc_t * 131*7c478bd9Sstevel@tonic-gate iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner) 132*7c478bd9Sstevel@tonic-gate { 133*7c478bd9Sstevel@tonic-gate iidesc_t *tgt = iidesc_dup(src); 134*7c478bd9Sstevel@tonic-gate free(tgt->ii_name); 135*7c478bd9Sstevel@tonic-gate free(tgt->ii_owner); 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate tgt->ii_name = name ? xstrdup(name) : NULL; 138*7c478bd9Sstevel@tonic-gate tgt->ii_owner = owner ? xstrdup(owner) : NULL; 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate return (tgt); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 144*7c478bd9Sstevel@tonic-gate void 145*7c478bd9Sstevel@tonic-gate iidesc_free(iidesc_t *idp, void *private) 146*7c478bd9Sstevel@tonic-gate { 147*7c478bd9Sstevel@tonic-gate if (idp->ii_name) 148*7c478bd9Sstevel@tonic-gate free(idp->ii_name); 149*7c478bd9Sstevel@tonic-gate if (idp->ii_nargs) 150*7c478bd9Sstevel@tonic-gate free(idp->ii_args); 151*7c478bd9Sstevel@tonic-gate if (idp->ii_owner) 152*7c478bd9Sstevel@tonic-gate free(idp->ii_owner); 153*7c478bd9Sstevel@tonic-gate free(idp); 154*7c478bd9Sstevel@tonic-gate } 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate int 157*7c478bd9Sstevel@tonic-gate iidesc_dump(iidesc_t *ii) 158*7c478bd9Sstevel@tonic-gate { 159*7c478bd9Sstevel@tonic-gate printf("type: %d name %s\n", ii->ii_type, 160*7c478bd9Sstevel@tonic-gate (ii->ii_name ? ii->ii_name : "(anon)")); 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate return (0); 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate int 166*7c478bd9Sstevel@tonic-gate iidesc_count_type(void *data, void *private) 167*7c478bd9Sstevel@tonic-gate { 168*7c478bd9Sstevel@tonic-gate iidesc_t *ii = data; 169*7c478bd9Sstevel@tonic-gate iitype_t match = (iitype_t)private; 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate return (ii->ii_type == match); 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate void 175*7c478bd9Sstevel@tonic-gate iidesc_stats(hash_t *ii) 176*7c478bd9Sstevel@tonic-gate { 177*7c478bd9Sstevel@tonic-gate printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n", 178*7c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_GFUN), 179*7c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_SFUN), 180*7c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_GVAR), 181*7c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_SVAR), 182*7c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_TYPE), 183*7c478bd9Sstevel@tonic-gate hash_iter(ii, iidesc_count_type, (void *)II_SOU)); 184*7c478bd9Sstevel@tonic-gate } 185