1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino * are met:
8*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino *
14*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino * SUCH DAMAGE.
25*86d7f5d3SJohn Marino *
26*86d7f5d3SJohn Marino * $FreeBSD: src/usr.sbin/nscd/agent.c,v 1.3 2008/10/12 00:44:27 delphij Exp $
27*86d7f5d3SJohn Marino */
28*86d7f5d3SJohn Marino
29*86d7f5d3SJohn Marino #include <assert.h>
30*86d7f5d3SJohn Marino #include <string.h>
31*86d7f5d3SJohn Marino #include <stdlib.h>
32*86d7f5d3SJohn Marino #include "agent.h"
33*86d7f5d3SJohn Marino #include "debug.h"
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino static int
agent_cmp_func(const void * a1,const void * a2)36*86d7f5d3SJohn Marino agent_cmp_func(const void *a1, const void *a2)
37*86d7f5d3SJohn Marino {
38*86d7f5d3SJohn Marino struct agent const *ap1 = *((struct agent const **)a1);
39*86d7f5d3SJohn Marino struct agent const *ap2 = *((struct agent const **)a2);
40*86d7f5d3SJohn Marino int res;
41*86d7f5d3SJohn Marino
42*86d7f5d3SJohn Marino res = strcmp(ap1->name, ap2->name);
43*86d7f5d3SJohn Marino if (res == 0) {
44*86d7f5d3SJohn Marino if (ap1->type == ap2->type)
45*86d7f5d3SJohn Marino res = 0;
46*86d7f5d3SJohn Marino else if (ap1->type < ap2->type)
47*86d7f5d3SJohn Marino res = -1;
48*86d7f5d3SJohn Marino else
49*86d7f5d3SJohn Marino res = 1;
50*86d7f5d3SJohn Marino }
51*86d7f5d3SJohn Marino
52*86d7f5d3SJohn Marino return (res);
53*86d7f5d3SJohn Marino }
54*86d7f5d3SJohn Marino
55*86d7f5d3SJohn Marino struct agent_table *
init_agent_table(void)56*86d7f5d3SJohn Marino init_agent_table(void)
57*86d7f5d3SJohn Marino {
58*86d7f5d3SJohn Marino struct agent_table *retval;
59*86d7f5d3SJohn Marino
60*86d7f5d3SJohn Marino TRACE_IN(init_agent_table);
61*86d7f5d3SJohn Marino retval = (struct agent_table *)calloc(1, sizeof(struct agent_table));
62*86d7f5d3SJohn Marino assert(retval != NULL);
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino TRACE_OUT(init_agent_table);
65*86d7f5d3SJohn Marino return (retval);
66*86d7f5d3SJohn Marino }
67*86d7f5d3SJohn Marino
68*86d7f5d3SJohn Marino void
register_agent(struct agent_table * at,struct agent * a)69*86d7f5d3SJohn Marino register_agent(struct agent_table *at, struct agent *a)
70*86d7f5d3SJohn Marino {
71*86d7f5d3SJohn Marino struct agent **new_agents;
72*86d7f5d3SJohn Marino size_t new_agents_num;
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino TRACE_IN(register_agent);
75*86d7f5d3SJohn Marino assert(at != NULL);
76*86d7f5d3SJohn Marino assert(a != NULL);
77*86d7f5d3SJohn Marino new_agents_num = at->agents_num + 1;
78*86d7f5d3SJohn Marino new_agents = (struct agent **)malloc(sizeof(struct agent *) *
79*86d7f5d3SJohn Marino new_agents_num);
80*86d7f5d3SJohn Marino assert(new_agents != NULL);
81*86d7f5d3SJohn Marino memcpy(new_agents, at->agents, at->agents_num * sizeof(struct agent *));
82*86d7f5d3SJohn Marino new_agents[new_agents_num - 1] = a;
83*86d7f5d3SJohn Marino qsort(new_agents, new_agents_num, sizeof(struct agent *),
84*86d7f5d3SJohn Marino agent_cmp_func);
85*86d7f5d3SJohn Marino
86*86d7f5d3SJohn Marino free(at->agents);
87*86d7f5d3SJohn Marino at->agents = new_agents;
88*86d7f5d3SJohn Marino at->agents_num = new_agents_num;
89*86d7f5d3SJohn Marino TRACE_OUT(register_agent);
90*86d7f5d3SJohn Marino }
91*86d7f5d3SJohn Marino
92*86d7f5d3SJohn Marino struct agent *
find_agent(struct agent_table * at,const char * name,enum agent_type type)93*86d7f5d3SJohn Marino find_agent(struct agent_table *at, const char *name, enum agent_type type)
94*86d7f5d3SJohn Marino {
95*86d7f5d3SJohn Marino struct agent **res;
96*86d7f5d3SJohn Marino struct agent model, *model_p;
97*86d7f5d3SJohn Marino
98*86d7f5d3SJohn Marino TRACE_IN(find_agent);
99*86d7f5d3SJohn Marino model.name = (char *)name;
100*86d7f5d3SJohn Marino model.type = type;
101*86d7f5d3SJohn Marino model_p = &model;
102*86d7f5d3SJohn Marino res = bsearch(&model_p, at->agents, at->agents_num,
103*86d7f5d3SJohn Marino sizeof(struct agent *), agent_cmp_func);
104*86d7f5d3SJohn Marino
105*86d7f5d3SJohn Marino TRACE_OUT(find_agent);
106*86d7f5d3SJohn Marino return ( res == NULL ? NULL : *res);
107*86d7f5d3SJohn Marino }
108*86d7f5d3SJohn Marino
109*86d7f5d3SJohn Marino void
destroy_agent_table(struct agent_table * at)110*86d7f5d3SJohn Marino destroy_agent_table(struct agent_table *at)
111*86d7f5d3SJohn Marino {
112*86d7f5d3SJohn Marino size_t i;
113*86d7f5d3SJohn Marino
114*86d7f5d3SJohn Marino TRACE_IN(destroy_agent_table);
115*86d7f5d3SJohn Marino assert(at != NULL);
116*86d7f5d3SJohn Marino for (i = 0; i < at->agents_num; ++i) {
117*86d7f5d3SJohn Marino free(at->agents[i]->name);
118*86d7f5d3SJohn Marino free(at->agents[i]);
119*86d7f5d3SJohn Marino }
120*86d7f5d3SJohn Marino
121*86d7f5d3SJohn Marino free(at->agents);
122*86d7f5d3SJohn Marino free(at);
123*86d7f5d3SJohn Marino TRACE_OUT(destroy_agent_table);
124*86d7f5d3SJohn Marino }
125