summaryrefslogtreecommitdiff
path: root/udev/lib/libudev-list.c
blob: 76c2f7e93bd1847ded8fb4eb3250b02cd656b063 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * libudev - interface to udev device information
 *
 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "libudev.h"
#include "libudev-private.h"

struct udev_list_entry {
	struct udev *udev;
	struct udev_list_node node;
	struct udev_list_node *list;
	char *name;
	char *value;
	int flag;
};

/* list head point to itself if empty */
void udev_list_init(struct udev_list_node *list)
{
	list->next = list;
	list->prev = list;
}

static int udev_list_is_empty(struct udev_list_node *list)
{
	return list->next == list;
}

static void udev_list_node_insert_between(struct udev_list_node *new,
				     struct udev_list_node *prev,
				     struct udev_list_node *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

static void udev_list_node_remove(struct udev_list_node *entry)
{
	struct udev_list_node *prev = entry->prev;
	struct udev_list_node *next = entry->next;

	next->prev = prev;
	prev->next = next;

	entry->prev = NULL;
	entry->next = NULL;
}

/* return list entry which embeds this node */
static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
{
	char *list;

	list = (char *)node;
	list -= offsetof(struct udev_list_entry, node);
	return (struct udev_list_entry *)list;
}

/* insert entry into a list as the last element  */
static void list_entry_append(struct udev_list_entry *new, struct udev_list_node *list)
{
	/* inserting before the list head make the node the last node in the list */
	udev_list_node_insert_between(&new->node, list->prev, list);
	new->list = list;
}

/* insert entry into a list, before a given existing entry */
static void list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
{
	udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
	new->list = entry->list;
}

struct udev_list_entry *udev_list_entry_add(struct udev *udev, struct udev_list_node *list,
					    const char *name, const char *value,
					    int unique, int sort)
{
	struct udev_list_entry *entry_loop = NULL;
	struct udev_list_entry *entry_new;

	if (unique)
		udev_list_entry_foreach(entry_loop, udev_list_get_entry(list)) {
			if (strcmp(entry_loop->name, name) == 0) {
				info(udev, "'%s' is already in the list\n", name);
				free(entry_loop->value);
				if (value == NULL) {
					entry_loop->value = NULL;
					info(udev, "'%s' value unset\n", name);
					return entry_loop;
				}
				entry_loop->value = strdup(value);
				if (entry_loop->value == NULL)
					return NULL;
				info(udev, "'%s' value replaced with '%s'\n", name, value);
				return entry_loop;
			}
		}

	if (sort)
		udev_list_entry_foreach(entry_loop, udev_list_get_entry(list)) {
			if (strcmp(entry_loop->name, name) > 0)
				break;
		}

	entry_new = malloc(sizeof(struct udev_list_entry));
	if (entry_new == NULL)
		return NULL;
	memset(entry_new, 0x00, sizeof(struct udev_list_entry));
	entry_new->udev = udev;
	entry_new->name = strdup(name);
	if (entry_new->name == NULL) {
		free(entry_new);
		return NULL;
	}
	if (value != NULL) {
		entry_new->value = strdup(value);
		if (entry_new->value == NULL) {
			free(entry_new->name);
			free(entry_new);
			return NULL;
		}
	}
	if (entry_loop != NULL)
		list_entry_insert_before(entry_new, entry_loop);
	else
		list_entry_append(entry_new, list);
	dbg(udev, "'%s=%s' added\n", entry_new->name, entry_new->value);
	return entry_new;
}

void udev_list_entry_remove(struct udev_list_entry *entry)
{
	udev_list_node_remove(&entry->node);
	free(entry->name);
	free(entry->value);
	free(entry);
}

void udev_list_cleanup(struct udev *udev, struct udev_list_node *list)
{
	struct udev_list_entry *entry_loop;
	struct udev_list_entry *entry_tmp;

	udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
		udev_list_entry_remove(entry_loop);
}

void udev_list_entry_move_to_end(struct udev_list_entry *list_entry)
{
	udev_list_node_remove(&list_entry->node);
	udev_list_node_insert_between(&list_entry->node, list_entry->list->prev, list_entry->list);
}

void udev_list_entry_move_before(struct udev_list_entry *list_entry, struct udev_list_entry *entry)
{
	udev_list_node_remove(&list_entry->node);
	udev_list_node_insert_between(&list_entry->node, entry->node.prev, &entry->node);
	list_entry->list = entry->list;
}

struct udev_list_entry *udev_list_get_entry(struct udev_list_node *list)
{
	if (udev_list_is_empty(list))
		return NULL;
	return list_node_to_entry(list->next);
}

struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
{
	struct udev_list_node *next;

	if (list_entry == NULL)
		return NULL;
	next = list_entry->node.next;
	/* empty list or no more entries */
	if (next == list_entry->list)
		return NULL;
	return list_node_to_entry(next);
}

struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
{
	struct udev_list_entry *entry;

	udev_list_entry_foreach(entry, list_entry) {
		if (strcmp(udev_list_entry_get_name(entry), name) == 0) {
			dbg(entry->udev, "found '%s=%s'\n", entry->name, entry->value);
			return entry;
		}
	}
	return NULL;
}

const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
{
	if (list_entry == NULL)
		return NULL;
	return list_entry->name;
}

const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
{
	if (list_entry == NULL)
		return NULL;
	return list_entry->value;
}

extern int udev_list_entry_get_flag(struct udev_list_entry *list_entry)
{
	if (list_entry == NULL)
		return -EINVAL;
	return list_entry->flag;
}

extern void udev_list_entry_set_flag(struct udev_list_entry *list_entry, int flag)
{
	if (list_entry == NULL)
		return;
	list_entry->flag = flag;
}