File: list.c

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (97 lines) | stat: -rw-r--r-- 1,804 bytes parent folder | download | duplicates (4)
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
/*
 * libifupdown/list.c
 * Purpose: linked lists
 *
 * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
 * Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
 *
 * 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>
#include <stdlib.h>
#include "libifupdown/list.h"

void
lif_list_free_nodes(struct lif_list *list)
{
	if (list == NULL)
		return;

	struct lif_node *iter, *iter_next;

	LIF_LIST_FOREACH_SAFE(iter, iter_next, list->head)
	{
		free (iter);
	}

	free (list);
}

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;
}