1 
2 /*
3 * Licensed Materials - Property of IBM
4 *
5 * trousers - An open source TCG Software Stack
6 *
7 * (C) Copyright International Business Machines Corp. 2006
8 *
9 */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <list.h>
14 
15 #include "tsplog.h"
16 
list_new(void)17 list_ptr list_new( void) {
18 	list_ptr list = (list_ptr)malloc( sizeof( list_struct));
19 
20 	if( list == NULL) return NULL;
21 	list->head = NULL;
22 	return list;
23 }
24 
list_add(list_ptr list,void * obj)25 void list_add(list_ptr list, void *obj) {
26 	list->current = (node_t *) malloc (sizeof(struct _list_t));
27 	if (list->current == NULL) {
28 		LogError("[list_add] malloc of %d bytes failed", sizeof(struct _list_t));
29 		return;
30 	}
31 	if( list->head == NULL) {
32 		list->head = list->current;
33 	} else
34 		list->previous->next = list->current;
35 	list->current->obj = obj;
36 	list->current->next = NULL;
37 	list->previous = list->current;
38 }
39 
list_dump(list_ptr list)40 void list_dump(list_ptr list) {
41 	node_t *current;
42 
43 	if( list->head == NULL) // if head has not been altered
44 		puts("no data"); // list is empty
45 	else {
46 		current = list->head; // go to first node
47 		do {
48 			printf("%d\n", (int)current->obj); // print value at current node
49 			current = current->next; // traverse through the list
50 		} while(current != NULL); // until current node is NULL
51 	}
52 }
53 
list_freeall(list_ptr list)54 void list_freeall(list_ptr list) {
55 	node_t *current = list->head; // go to first node
56 	node_t *next;
57 
58 	if( list->head != NULL) {
59 		current = list->head; // go to first node
60 		do {
61 			next = current->next;
62 			free(current);
63 			current = next;
64 		} while(current != NULL); // until current node is NULL
65 	}
66 }
67