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