agent.c revision 194087
1158115Sume/*- 2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru> 3158115Sume * All rights reserved. 4158115Sume * 5158115Sume * Redistribution and use in source and binary forms, with or without 6158115Sume * modification, are permitted provided that the following conditions 7158115Sume * are met: 8158115Sume * 1. Redistributions of source code must retain the above copyright 9158115Sume * notice, this list of conditions and the following disclaimer. 10158115Sume * 2. Redistributions in binary form must reproduce the above copyright 11158115Sume * notice, this list of conditions and the following disclaimer in the 12158115Sume * documentation and/or other materials provided with the distribution. 13158115Sume * 14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17158115Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24158115Sume * SUCH DAMAGE. 25158115Sume * 26158115Sume */ 27158115Sume 28158115Sume#include <sys/cdefs.h> 29158115Sume__FBSDID("$FreeBSD: head/usr.sbin/nscd/agent.c 194087 2009-06-12 23:39:05Z des $"); 30158115Sume 31158115Sume#include <assert.h> 32158115Sume#include <string.h> 33158115Sume#include <stdlib.h> 34158115Sume#include "agent.h" 35158115Sume#include "debug.h" 36158115Sume 37158115Sumestatic int 38158115Sumeagent_cmp_func(const void *a1, const void *a2) 39158115Sume{ 40158115Sume struct agent const *ap1 = *((struct agent const **)a1); 41158115Sume struct agent const *ap2 = *((struct agent const **)a2); 42158115Sume int res; 43158115Sume 44158115Sume res = strcmp(ap1->name, ap2->name); 45158115Sume if (res == 0) { 46158115Sume if (ap1->type == ap2->type) 47158115Sume res = 0; 48158115Sume else if (ap1->type < ap2->type) 49158115Sume res = -1; 50158115Sume else 51158115Sume res = 1; 52158115Sume } 53158115Sume 54158115Sume return (res); 55158115Sume} 56158115Sume 57158115Sumestruct agent_table * 58194087Sdesinit_agent_table(void) 59158115Sume{ 60158115Sume struct agent_table *retval; 61158115Sume 62158115Sume TRACE_IN(init_agent_table); 63183770Sdelphij retval = (struct agent_table *)calloc(1, sizeof(struct agent_table)); 64158115Sume assert(retval != NULL); 65158115Sume 66158115Sume TRACE_OUT(init_agent_table); 67158115Sume return (retval); 68158115Sume} 69158115Sume 70158115Sumevoid 71158115Sumeregister_agent(struct agent_table *at, struct agent *a) 72158115Sume{ 73158115Sume struct agent **new_agents; 74158115Sume size_t new_agents_num; 75158115Sume 76158115Sume TRACE_IN(register_agent); 77158115Sume assert(at != NULL); 78158115Sume assert(a != NULL); 79158115Sume new_agents_num = at->agents_num + 1; 80158115Sume new_agents = (struct agent **)malloc(sizeof(struct agent *) * 81158115Sume new_agents_num); 82158115Sume assert(new_agents != NULL); 83158115Sume memcpy(new_agents, at->agents, at->agents_num * sizeof(struct agent *)); 84158115Sume new_agents[new_agents_num - 1] = a; 85158115Sume qsort(new_agents, new_agents_num, sizeof(struct agent *), 86158115Sume agent_cmp_func); 87158115Sume 88158115Sume free(at->agents); 89158115Sume at->agents = new_agents; 90158115Sume at->agents_num = new_agents_num; 91158115Sume TRACE_OUT(register_agent); 92158115Sume} 93158115Sume 94158115Sumestruct agent * 95158115Sumefind_agent(struct agent_table *at, const char *name, enum agent_type type) 96158115Sume{ 97158115Sume struct agent **res; 98158115Sume struct agent model, *model_p; 99158115Sume 100158115Sume TRACE_IN(find_agent); 101158115Sume model.name = (char *)name; 102158115Sume model.type = type; 103158115Sume model_p = &model; 104158115Sume res = bsearch(&model_p, at->agents, at->agents_num, 105158115Sume sizeof(struct agent *), agent_cmp_func); 106158115Sume 107158115Sume TRACE_OUT(find_agent); 108158115Sume return ( res == NULL ? NULL : *res); 109158115Sume} 110158115Sume 111158115Sumevoid 112158115Sumedestroy_agent_table(struct agent_table *at) 113158115Sume{ 114158115Sume size_t i; 115158115Sume 116158115Sume TRACE_IN(destroy_agent_table); 117158115Sume assert(at != NULL); 118158115Sume for (i = 0; i < at->agents_num; ++i) { 119158115Sume free(at->agents[i]->name); 120158115Sume free(at->agents[i]); 121158115Sume } 122158115Sume 123158115Sume free(at->agents); 124158115Sume free(at); 125158115Sume TRACE_OUT(destroy_agent_table); 126158115Sume} 127