This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
forked from wulf7/libudev-devd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudev-list.c
176 lines (139 loc) · 4.08 KB
/
udev-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) 2015, 2021 Vladimir Kondratyev <vladimir@kondratyev.su>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libudev.h"
#include "udev-list.h"
#include "udev-utils.h"
#include "utils.h"
struct udev_list_entry {
struct udev_list *list;
RB_ENTRY(udev_list_entry) link;
char *value;
char name[];
};
void udev_list_entry_free(struct udev_list_entry *ule);
RB_PROTOTYPE(udev_list, udev_list_entry, link, udev_list_entry_cmp);
void
udev_list_init(struct udev_list *ul)
{
RB_INIT(ul);
}
int
udev_list_insert(struct udev_list *ul, char const *name, char const *value)
{
struct udev_list_entry *ule, *old_ule;
size_t namelen, valuelen;
namelen = strlen(name) + 1;
valuelen = value == NULL ? 0 : strlen(value) + 1;
ule = calloc
(1, offsetof(struct udev_list_entry, name) + namelen + valuelen);
if (!ule)
return (-1);
ule->list = ul;
strcpy(ule->name, name);
ule->value = NULL;
if (value != NULL) {
ule->value = ule->name + namelen;
strcpy(ule->value, value);
}
old_ule = RB_FIND(udev_list, ul, ule);
if (old_ule != NULL) {
RB_REMOVE(udev_list, ul, old_ule);
udev_list_entry_free(old_ule);
}
RB_INSERT(udev_list, ul, ule);
return (0);
}
void
udev_list_free(struct udev_list *ul)
{
struct udev_list_entry *ule1, *ule2;
RB_FOREACH_SAFE (ule1, udev_list, ul, ule2) {
RB_REMOVE(udev_list, ul, ule1);
udev_list_entry_free(ule1);
}
RB_INIT(ul);
}
void
udev_list_entry_free(struct udev_list_entry *ule)
{
free(ule);
}
struct udev_list_entry *
udev_list_entry_get_first(struct udev_list *ul)
{
return (RB_MIN(udev_list, ul));
}
LIBUDEV_EXPORT struct udev_list_entry *
udev_list_entry_get_next(struct udev_list_entry *ule)
{
return (RB_NEXT(udev_list,, ule));
}
const char *
_udev_list_entry_get_name(struct udev_list_entry *ule)
{
return (ule->name);
}
LIBUDEV_EXPORT const char *
udev_list_entry_get_name(struct udev_list_entry *ule)
{
const char *name;
name = _udev_list_entry_get_name(ule);
TRC("() %s", name);
return (name);
}
const char *
_udev_list_entry_get_value(struct udev_list_entry *ule)
{
return (ule->value);
}
LIBUDEV_EXPORT const char *
udev_list_entry_get_value(struct udev_list_entry *ule)
{
const char *value;
value = _udev_list_entry_get_value(ule);
TRC("() %s", value);
return (value);
}
static int
udev_list_entry_cmp (struct udev_list_entry *le1, struct udev_list_entry *le2)
{
return (strcmp(le1->name, le2->name));
}
LIBUDEV_EXPORT struct udev_list_entry *
udev_list_entry_get_by_name(struct udev_list_entry *ule, const char *name)
{
struct udev_list_entry *find, *ret;
find = calloc
(1, offsetof(struct udev_list_entry, name) + strlen(name) + 1);
ret = RB_FIND(udev_list, ule->list, find);
udev_list_entry_free(find);
return (ret);
}
RB_GENERATE(udev_list, udev_list_entry, link, udev_list_entry_cmp);