File: node.c

package info (click to toggle)
usbmgr 0.4.8-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 344 kB
  • ctags: 318
  • sloc: ansic: 2,067; sh: 539; makefile: 198
file content (108 lines) | stat: -rw-r--r-- 1,810 bytes parent folder | download | duplicates (3)
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
/*
 *  
 *  Copyright (c) by Shuu Yamaguchi <shuu@wondernetworkresources.com>
 *
 *  $Id: node.c,v 4.2 2001/01/04 15:14:38 shuu Exp $
 *
 *  Can be freely distributed and used under the terms of the GNU GPL.
 */
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

#include	"node.h"

/*
 * create node , allocate data and link to base
 */
struct node *
create_node_data_link(struct node **base,void *data,int size)
{
	struct node *np;
	unsigned int alloc_size;

	if (size > 0)
		alloc_size = sizeof(struct node) + size + 1;
	else
		alloc_size = sizeof(struct node);
	np = (struct node *)Malloc(alloc_size);
	if (np == NULL)
		return NULL;
	INIT_NODE(np);
	link_node(base,np);
	if (size > 0) {
		(char *)np->data = (char *)(&(np->data)+1);
		memcpy((char *)np->data,(char *)data,size);
	} else {
		np->data = data;
	}

	return np;
}

void
delete_node_data_link(struct node **base,struct node *np)
{
	unlink_node(base,np);
	Free(np);
}

/*
 * link node
 */
void
link_node(struct node **linkp,struct node *np)
{
	if (*linkp == NULL)
		*linkp = np;
	else
		add_node(*linkp,np);
}

/*
 * unlink node
 */
void
unlink_node(struct node **base,struct node *np)
{
	if (np == *base) {	/* np is first node */
		if (np->prev == np)		/* np is only one node */
			*base = NULL;
		else
			*base = np->next;
	}
	np->next->prev = np->prev;
	np->prev->next = np->next;
}


/*
 * add node
 */
void
add_node(struct node *link,struct node *np)
{
	np->next = link;
	np->prev = link->prev;
	link->prev->next = np;
	link->prev = np;
}

/*
 * find data in node
 */
struct node *
find_string_node(struct node *start,char *name)
{
	struct node *np;

	if (start == NULL)
		return NULL;
	for (np = start; ;np = np->next) {
		if (strcmp((char *)np->data,name) == 0)
			return np;
		if (np->next == start)
			break;
	}
	return NULL;
}