2008-09-26 19:44:53 +02:00
|
|
|
/*
|
|
|
|
* libudev - interface to udev device information
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
|
|
|
|
*
|
2009-03-26 19:29:36 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-09-26 19:44:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2009-06-15 17:09:43 +02:00
|
|
|
/**
|
|
|
|
* SECTION:libudev-list
|
|
|
|
* @short_description: list operation
|
|
|
|
*
|
|
|
|
* Libudev list operations.
|
|
|
|
*/
|
|
|
|
|
2009-06-15 13:22:38 +02:00
|
|
|
/**
|
|
|
|
* udev_list_entry:
|
|
|
|
*
|
2009-06-15 17:09:43 +02:00
|
|
|
* Opaque object representing one entry in a list. An entry contains
|
|
|
|
* contains a name, and optionally a value.
|
2009-06-15 13:22:38 +02:00
|
|
|
*/
|
2008-09-28 01:34:55 +02:00
|
|
|
struct udev_list_entry {
|
2008-10-09 22:24:43 +02:00
|
|
|
struct udev_list_node node;
|
2011-08-04 22:59:58 +02:00
|
|
|
struct udev_list *list;
|
2008-09-26 19:44:53 +02:00
|
|
|
char *name;
|
|
|
|
char *value;
|
2011-04-24 00:22:23 +02:00
|
|
|
int num;
|
2008-09-26 19:44:53 +02:00
|
|
|
};
|
|
|
|
|
2011-04-24 00:22:23 +02:00
|
|
|
/* the list's head points to itself if empty */
|
2011-08-04 22:59:58 +02:00
|
|
|
void udev_list_node_init(struct udev_list_node *list)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
|
|
|
list->next = list;
|
|
|
|
list->prev = list;
|
|
|
|
}
|
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
int udev_list_node_is_empty(struct udev_list_node *list)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
|
|
|
return list->next == list;
|
|
|
|
}
|
|
|
|
|
2008-10-17 01:39:10 +02:00
|
|
|
static void udev_list_node_insert_between(struct udev_list_node *new,
|
2008-10-17 18:59:08 +02:00
|
|
|
struct udev_list_node *prev,
|
|
|
|
struct udev_list_node *next)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
|
|
|
next->prev = new;
|
|
|
|
new->next = next;
|
|
|
|
new->prev = prev;
|
|
|
|
prev->next = new;
|
|
|
|
}
|
|
|
|
|
2008-10-17 18:59:08 +02:00
|
|
|
void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
|
|
|
|
{
|
|
|
|
udev_list_node_insert_between(new, list->prev, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void udev_list_node_remove(struct udev_list_node *entry)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
2008-10-09 22:24:43 +02:00
|
|
|
struct udev_list_node *prev = entry->prev;
|
|
|
|
struct udev_list_node *next = entry->next;
|
2008-09-26 19:44:53 +02:00
|
|
|
|
|
|
|
next->prev = prev;
|
|
|
|
prev->next = next;
|
2008-09-28 01:34:55 +02:00
|
|
|
|
|
|
|
entry->prev = NULL;
|
|
|
|
entry->next = NULL;
|
2008-09-26 19:44:53 +02:00
|
|
|
}
|
|
|
|
|
2008-09-28 01:34:55 +02:00
|
|
|
/* return list entry which embeds this node */
|
2008-10-09 22:24:43 +02:00
|
|
|
static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
|
2008-09-28 01:34:55 +02:00
|
|
|
{
|
|
|
|
char *list;
|
2008-09-26 19:44:53 +02:00
|
|
|
|
2008-09-28 01:34:55 +02:00
|
|
|
list = (char *)node;
|
|
|
|
list -= offsetof(struct udev_list_entry, node);
|
|
|
|
return (struct udev_list_entry *)list;
|
|
|
|
}
|
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
|
2008-09-28 01:34:55 +02:00
|
|
|
{
|
2011-08-04 22:59:58 +02:00
|
|
|
memset(list, 0x00, sizeof(struct udev_list));
|
|
|
|
list->udev = udev;
|
|
|
|
list->unique = unique;
|
|
|
|
udev_list_node_init(&list->node);
|
2008-09-28 01:34:55 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
/* insert entry into a list as the last element */
|
|
|
|
void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list)
|
2008-11-04 20:19:01 +01:00
|
|
|
{
|
2011-08-04 22:59:58 +02:00
|
|
|
/* inserting before the list head make the node the last node in the list */
|
|
|
|
udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
|
|
|
|
new->list = list;
|
2008-11-04 20:19:01 +01:00
|
|
|
}
|
|
|
|
|
2008-09-28 01:34:55 +02:00
|
|
|
/* insert entry into a list, before a given existing entry */
|
2008-11-04 20:19:01 +01:00
|
|
|
void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
|
2008-09-28 01:34:55 +02:00
|
|
|
{
|
2008-10-17 01:39:10 +02:00
|
|
|
udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
|
2008-09-28 01:34:55 +02:00
|
|
|
new->list = entry->list;
|
|
|
|
}
|
2008-09-26 19:44:53 +02:00
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
/* binary search in sorted array */
|
|
|
|
static int list_search(struct udev_list *list, const char *name)
|
2008-09-28 01:34:55 +02:00
|
|
|
{
|
2011-08-04 22:59:58 +02:00
|
|
|
unsigned int first, last;
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
last = list->entries_cur;
|
|
|
|
while (first < last) {
|
|
|
|
unsigned int i;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
i = (first + last)/2;
|
|
|
|
cmp = strcmp(name, list->entries[i]->name);
|
|
|
|
if (cmp < 0)
|
|
|
|
last = i;
|
|
|
|
else if (cmp > 0)
|
|
|
|
first = i+1;
|
|
|
|
else
|
|
|
|
return i;
|
2011-04-24 00:22:23 +02:00
|
|
|
}
|
2008-09-26 19:44:53 +02:00
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
/* not found, return negative insertion-index+1 */
|
|
|
|
return -(first+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
struct udev_list_entry *entry;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (list->unique) {
|
|
|
|
/* lookup existing name or insertion-index */
|
|
|
|
i = list_search(list, name);
|
|
|
|
if (i >= 0) {
|
|
|
|
entry = list->entries[i];
|
|
|
|
|
|
|
|
dbg(list->udev, "'%s' is already in the list\n", name);
|
|
|
|
free(entry->value);
|
|
|
|
if (value == NULL) {
|
|
|
|
entry->value = NULL;
|
|
|
|
dbg(list->udev, "'%s' value unset\n", name);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
entry->value = strdup(value);
|
|
|
|
if (entry->value == NULL)
|
|
|
|
return NULL;
|
|
|
|
dbg(list->udev, "'%s' value replaced with '%s'\n", name, value);
|
|
|
|
return entry;
|
2008-09-26 19:44:53 +02:00
|
|
|
}
|
2011-04-24 00:22:23 +02:00
|
|
|
}
|
2008-09-26 19:44:53 +02:00
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
/* add new name */
|
|
|
|
entry = calloc(1, sizeof(struct udev_list_entry));
|
|
|
|
if (entry == NULL)
|
2008-09-26 19:44:53 +02:00
|
|
|
return NULL;
|
2011-08-04 22:59:58 +02:00
|
|
|
entry->name = strdup(name);
|
|
|
|
if (entry->name == NULL) {
|
|
|
|
free(entry);
|
2008-09-26 19:44:53 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (value != NULL) {
|
2011-08-04 22:59:58 +02:00
|
|
|
entry->value = strdup(value);
|
|
|
|
if (entry->value == NULL) {
|
|
|
|
free(entry->name);
|
|
|
|
free(entry);
|
2008-09-26 19:44:53 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-08-04 22:59:58 +02:00
|
|
|
|
|
|
|
if (list->unique) {
|
|
|
|
/* allocate or enlarge sorted array if needed */
|
|
|
|
if (list->entries_cur >= list->entries_max) {
|
|
|
|
unsigned int add;
|
|
|
|
|
|
|
|
add = list->entries_max;
|
|
|
|
if (add < 1)
|
|
|
|
add = 64;
|
|
|
|
list->entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
|
|
|
|
if (list->entries == NULL) {
|
|
|
|
free(entry->name);
|
|
|
|
free(entry->value);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list->entries_max += add;
|
|
|
|
}
|
2011-04-24 00:22:23 +02:00
|
|
|
|
2011-08-28 22:41:46 +02:00
|
|
|
/* the negative i returned the insertion index */
|
2011-08-04 22:59:58 +02:00
|
|
|
i = (-i)-1;
|
2011-08-28 22:41:46 +02:00
|
|
|
|
|
|
|
/* insert into sorted list */
|
|
|
|
if ((unsigned int)i < list->entries_cur)
|
|
|
|
udev_list_entry_insert_before(entry, list->entries[i]);
|
|
|
|
else
|
|
|
|
udev_list_entry_append(entry, list);
|
|
|
|
|
|
|
|
/* insert into sorted array */
|
2011-08-04 22:59:58 +02:00
|
|
|
memmove(&list->entries[i+1], &list->entries[i],
|
|
|
|
(list->entries_cur - i) * sizeof(struct udev_list_entry *));
|
|
|
|
list->entries[i] = entry;
|
|
|
|
list->entries_cur++;
|
2011-08-28 22:41:46 +02:00
|
|
|
} else {
|
|
|
|
udev_list_entry_append(entry, list);
|
2011-08-04 22:59:58 +02:00
|
|
|
}
|
2011-04-24 00:22:23 +02:00
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
dbg(list->udev, "'%s=%s' added\n", entry->name, entry->value);
|
|
|
|
return entry;
|
2008-09-26 19:44:53 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 20:19:01 +01:00
|
|
|
void udev_list_entry_delete(struct udev_list_entry *entry)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
2011-08-04 22:59:58 +02:00
|
|
|
if (entry->list->entries != NULL) {
|
|
|
|
int i;
|
|
|
|
struct udev_list *list = entry->list;
|
|
|
|
|
|
|
|
/* remove entry from sorted array */
|
|
|
|
i = list_search(list, entry->name);
|
|
|
|
if (i >= 0) {
|
|
|
|
memmove(&list->entries[i], &list->entries[i+1],
|
|
|
|
((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
|
|
|
|
list->entries_cur--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 01:39:10 +02:00
|
|
|
udev_list_node_remove(&entry->node);
|
2008-10-16 13:33:13 +02:00
|
|
|
free(entry->name);
|
|
|
|
free(entry->value);
|
|
|
|
free(entry);
|
2008-09-26 19:44:53 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
void udev_list_cleanup(struct udev_list *list)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
2008-09-28 01:34:55 +02:00
|
|
|
struct udev_list_entry *entry_loop;
|
|
|
|
struct udev_list_entry *entry_tmp;
|
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
free(list->entries);
|
|
|
|
list->entries = NULL;
|
|
|
|
list->entries_cur = 0;
|
|
|
|
list->entries_max = 0;
|
2008-10-16 20:12:16 +02:00
|
|
|
udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
|
2008-11-04 20:19:01 +01:00
|
|
|
udev_list_entry_delete(entry_loop);
|
2008-10-16 21:12:08 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
2011-08-04 22:59:58 +02:00
|
|
|
if (udev_list_node_is_empty(&list->node))
|
2008-09-26 19:44:53 +02:00
|
|
|
return NULL;
|
2011-08-04 22:59:58 +02:00
|
|
|
return list_node_to_entry(list->node.next);
|
2008-09-26 19:44:53 +02:00
|
|
|
}
|
|
|
|
|
2009-06-15 13:22:38 +02:00
|
|
|
/**
|
|
|
|
* udev_list_entry_get_next:
|
|
|
|
* @list_entry: current entry
|
|
|
|
*
|
|
|
|
* Returns: the next entry from the list, #NULL is no more entries are found.
|
|
|
|
*/
|
2011-05-20 14:41:10 +02:00
|
|
|
UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
2008-10-09 22:24:43 +02:00
|
|
|
struct udev_list_node *next;
|
2008-09-26 19:44:53 +02:00
|
|
|
|
2008-09-28 01:34:55 +02:00
|
|
|
if (list_entry == NULL)
|
|
|
|
return NULL;
|
2008-09-26 19:44:53 +02:00
|
|
|
next = list_entry->node.next;
|
2008-09-28 01:34:55 +02:00
|
|
|
/* empty list or no more entries */
|
2011-08-04 22:59:58 +02:00
|
|
|
if (next == &list_entry->list->node)
|
2008-09-26 19:44:53 +02:00
|
|
|
return NULL;
|
2008-09-28 01:34:55 +02:00
|
|
|
return list_node_to_entry(next);
|
2008-09-26 19:44:53 +02:00
|
|
|
}
|
|
|
|
|
2009-06-15 13:22:38 +02:00
|
|
|
/**
|
|
|
|
* udev_list_entry_get_by_name:
|
|
|
|
* @list_entry: current entry
|
|
|
|
* @name: name string to match
|
|
|
|
*
|
|
|
|
* Returns: the entry where @name matched, #NULL if no matching entry is found.
|
|
|
|
*/
|
2011-05-20 14:41:10 +02:00
|
|
|
UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
|
2008-09-28 17:39:31 +02:00
|
|
|
{
|
2011-08-04 22:59:58 +02:00
|
|
|
int i;
|
2008-09-28 17:39:31 +02:00
|
|
|
|
2011-08-04 22:59:58 +02:00
|
|
|
if (list_entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!list_entry->list->unique)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = list_search(list_entry->list, name);
|
|
|
|
if (i < 0)
|
|
|
|
return NULL;
|
|
|
|
return list_entry->list->entries[i];
|
2008-09-28 17:39:31 +02:00
|
|
|
}
|
|
|
|
|
2009-06-15 13:22:38 +02:00
|
|
|
/**
|
|
|
|
* udev_list_entry_get_name:
|
|
|
|
* @list_entry: current entry
|
|
|
|
*
|
|
|
|
* Returns: the name string of this entry.
|
|
|
|
*/
|
2011-05-20 14:41:10 +02:00
|
|
|
UDEV_EXPORT const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
|
|
|
if (list_entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
return list_entry->name;
|
|
|
|
}
|
|
|
|
|
2009-06-15 13:22:38 +02:00
|
|
|
/**
|
|
|
|
* udev_list_entry_get_value:
|
|
|
|
* @list_entry: current entry
|
|
|
|
*
|
|
|
|
* Returns: the value string of this entry.
|
|
|
|
*/
|
2011-05-20 14:41:10 +02:00
|
|
|
UDEV_EXPORT const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
|
2008-09-26 19:44:53 +02:00
|
|
|
{
|
|
|
|
if (list_entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
return list_entry->value;
|
|
|
|
}
|
2008-10-11 18:40:04 +02:00
|
|
|
|
2011-04-24 00:22:23 +02:00
|
|
|
int udev_list_entry_get_num(struct udev_list_entry *list_entry)
|
2008-10-11 18:40:04 +02:00
|
|
|
{
|
|
|
|
if (list_entry == NULL)
|
|
|
|
return -EINVAL;
|
2011-04-24 00:22:23 +02:00
|
|
|
return list_entry->num;
|
2008-10-11 18:40:04 +02:00
|
|
|
}
|
|
|
|
|
2011-04-24 00:22:23 +02:00
|
|
|
void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
|
2008-10-11 18:40:04 +02:00
|
|
|
{
|
|
|
|
if (list_entry == NULL)
|
|
|
|
return;
|
2011-04-24 00:22:23 +02:00
|
|
|
list_entry->num = num;
|
2008-10-11 18:40:04 +02:00
|
|
|
}
|