mausezahn: use getopt_long instead of getopt
[netsniff-ng.git] / staging / llist.c
blob9ef2d1a2530ecd9d3b93652d48fc616c9115c90e
1 /*
2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2010 Herbert Haas
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
20 #include "mz.h"
21 #include "cli.h"
22 #include "mops.h"
23 #include "llist.h"
25 /* PURPOSE:
26 * General doubly linked list with management functions.
28 * NOTE:
29 * There is no dummy head element. Every element may contain data!
30 * Therefore there is only one general "create_new_element" function.
32 * You cannot delete the head element except you want to delete the whole list.
33 * Usually you delete the head element at last.
35 * head->refcount always contains the number of elements.
37 * Each element has a unique index number.
39 * The user must assign her/his data to (void*) elem->data.
41 */
43 struct mz_ll *packet_sequences;
44 struct mz_ll *cli_seq; // currently edited packet sequence used by CLI
46 // Create new list element - may be the first one (list==NULL)
48 struct mz_ll * mz_ll_create_new_element(struct mz_ll *list)
50 struct mz_ll *new_element;
51 new_element = (struct mz_ll*) malloc (sizeof(struct mz_ll));
52 if (new_element==NULL) return NULL;
53 _mz_ll_set_default(new_element);
54 if (list==NULL) {
55 new_element->next=new_element;
56 new_element->prev=new_element;
57 new_element->head=new_element;
58 new_element->refcount=1;
59 new_element->index=0;
60 new_element->index_last=0;
61 } else {
62 new_element->prev=list->prev;
63 new_element->next=list;
64 new_element->prev->next=new_element;
65 list->prev = new_element;
66 new_element->head=list;
67 list->refcount++;
68 list->index_last++;
69 new_element->index=list->index_last;
72 return new_element;
75 // Delete ONE list element.
76 int mz_ll_delete_element (struct mz_ll *cur)
78 if ((cur==NULL)||(cur==cur->head)) return -1; // don't delete head!
79 if (cur->data!=NULL) { free(cur->data); cur->data=NULL; }
81 if ((cur->next!=cur)&&(cur->prev!=cur)) {
82 cur->prev->next=cur->next;
83 cur->next->prev=cur->prev;
85 cur->head->refcount--;
86 if (cur!=NULL) { free(cur); cur=NULL; }
87 return 0;
91 int mz_ll_delete_list (struct mz_ll *list)
93 struct mz_ll *cur=list,
94 *tmp;
96 if (cur==NULL) return 1;
97 while (cur!=cur->next) {
98 tmp=cur->next;
99 mz_ll_delete_element(cur);
100 cur=tmp;
102 // Finally free list head:
103 if (list->data!=NULL) { free(list->data); list->data=NULL; }
104 free(list);
105 list=NULL;
106 return 0;
109 struct mz_ll * mz_ll_search_name (struct mz_ll *list, char *str)
111 struct mz_ll *cur=list;
112 do {
113 if (strncmp(cur->name, str, MZ_LL_NAME_LEN)==0) return cur;
114 cur=cur->next;
116 while (cur!=list);
117 return NULL;
120 struct mz_ll * mz_ll_search_index (struct mz_ll *list, int i)
122 struct mz_ll *cur=list;
123 do {
124 if (cur->index==i) return cur;
125 cur=cur->next;
127 while (cur!=list);
128 return NULL;
131 int mz_ll_size(struct mz_ll *list)
133 int i=0;
134 struct mz_ll *cur=list;
136 if (list==NULL) return 0;
138 do {
139 i++;
140 cur=cur->next;
142 while (cur!=list);
143 if (i!=list->refcount) fprintf(stderr, "MZ_LL_SIZE: Anomalous situation. Report this.\n");
144 return i;
148 int mz_ll_dump_all(struct mz_ll *list)
150 int i=0;
151 struct mz_ll *cur=list;
153 if (list==NULL) return 0;
155 do {
156 i++;
157 fprintf(stdout, "Element %i: '%s', index=%i\n",i,cur->name, cur->index);
158 cur=cur->next;
160 while (cur!=list);
161 return i;
166 // ------ PRIVATE: initialize list-element
167 void _mz_ll_set_default (struct mz_ll *cur)
169 cur->refcount = 0;
170 cur->data = NULL;
171 cur->name[0]='\0';
172 cur->index=0;
173 cur->state=0;