2020-07-18 07:58:24 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/list.c
|
|
|
|
* Purpose: linked lists
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
2020-07-28 21:25:01 +00:00
|
|
|
* Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
|
2020-07-18 07:58:24 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2020-07-28 21:25:01 +00:00
|
|
|
#include <stdlib.h>
|
2020-07-18 07:58:24 +00:00
|
|
|
#include "libifupdown/list.h"
|
|
|
|
|
2020-07-28 21:25:01 +00:00
|
|
|
void
|
|
|
|
lif_list_free_nodes(struct lif_list **list)
|
|
|
|
{
|
|
|
|
if (*list == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
LIF_LIST_FOREACH (iter, (*list)->head)
|
|
|
|
{
|
|
|
|
free (iter->prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
free (iter);
|
|
|
|
free (*list);
|
2020-07-28 21:30:09 +00:00
|
|
|
*list = NULL;
|
2020-07-28 21:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 07:58:24 +00:00
|
|
|
void
|
|
|
|
lif_node_insert(struct lif_node *node, void *data, struct lif_list *list)
|
|
|
|
{
|
|
|
|
struct lif_node *tnode;
|
|
|
|
|
|
|
|
node->data = data;
|
|
|
|
|
|
|
|
if (list->head == NULL)
|
|
|
|
{
|
|
|
|
list->head = list->tail = node;
|
|
|
|
list->length = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tnode = list->head;
|
|
|
|
|
|
|
|
node->next = tnode;
|
|
|
|
tnode->prev = node;
|
|
|
|
|
|
|
|
list->head = node;
|
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lif_node_insert_tail(struct lif_node *node, void *data, struct lif_list *list)
|
|
|
|
{
|
|
|
|
struct lif_node *tnode;
|
|
|
|
|
|
|
|
node->data = data;
|
|
|
|
|
|
|
|
if (list->tail == NULL)
|
|
|
|
{
|
|
|
|
list->head = list->tail = node;
|
|
|
|
list->length = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tnode = list->tail;
|
|
|
|
|
|
|
|
node->prev = tnode;
|
|
|
|
tnode->next = node;
|
|
|
|
|
|
|
|
list->tail = node;
|
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lif_node_delete(struct lif_node *node, struct lif_list *list)
|
|
|
|
{
|
|
|
|
list->length--;
|
|
|
|
|
|
|
|
if (node->prev == NULL)
|
|
|
|
list->head = node->next;
|
|
|
|
else
|
|
|
|
node->prev->next = node->next;
|
|
|
|
|
|
|
|
if (node->next == NULL)
|
|
|
|
list->tail = node->prev;
|
|
|
|
else
|
|
|
|
node->next->prev = node->prev;
|
|
|
|
}
|