summaryrefslogblamecommitdiffstats
path: root/libtar/libtar_list.c
blob: 2c5febb01047499b6930b7ba318a1bbe529e7306 (plain) (tree)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458









































































































































































































































































































































































































































































                                                                        
/* listhash/libtar_list.c.  Generated from list.c.in by configure. */

/*
**  Copyright 1998-2002 University of Illinois Board of Trustees
**  Copyright 1998-2002 Mark D. Roth
**  All rights reserved.
**
**  libtar_list.c - linked list routines
**
**  Mark D. Roth <roth@uiuc.edu>
**  Campus Information Technologies and Educational Services
**  University of Illinois at Urbana-Champaign
*/

#include <config.h>
#include <compat.h>

#include <libtar_listhash.h>

#include <stdio.h>
#include <errno.h>
#include <sys/param.h>

#ifdef STDC_HEADERS
# include <string.h>
# include <stdlib.h>
#endif


/*
** libtar_listptr_reset() - reset a list pointer
*/
void
libtar_listptr_reset(libtar_listptr_t *lp)
{
	*lp = NULL;
}


/*
** libtar_listptr_data() - retrieve the data pointed to by lp
*/
void *
libtar_listptr_data(libtar_listptr_t *lp)
{
	return (*lp)->data;
}


/*
** libtar_list_new() - create a new, empty list
*/
libtar_list_t *
libtar_list_new(int flags, libtar_cmpfunc_t cmpfunc)
{
	libtar_list_t *newlist;

#ifdef DS_DEBUG
	printf("in libtar_list_new(%d, 0x%lx)\n", flags, cmpfunc);
#endif

	if (flags != LIST_USERFUNC
	    && flags != LIST_STACK
	    && flags != LIST_QUEUE)
	{
		errno = EINVAL;
		return NULL;
	}

	newlist = (libtar_list_t *)calloc(1, sizeof(libtar_list_t));
	if (cmpfunc != NULL)
		newlist->cmpfunc = cmpfunc;
	else
		newlist->cmpfunc = (libtar_cmpfunc_t)strcmp;
	newlist->flags = flags;

	return newlist;
}


/*
** libtar_list_iterate() - call a function for every element
**				      in a list
*/
int
libtar_list_iterate(libtar_list_t *l,
			       libtar_iterate_func_t plugin,
			       void *state)
{
	libtar_listptr_t n;

	if (l == NULL)
		return -1;

	for (n = l->first; n != NULL; n = n->next)
	{
		if ((*plugin)(n->data, state) == -1)
			return -1;
	}

	return 0;
}


/*
** libtar_list_empty() - empty the list
*/
void
libtar_list_empty(libtar_list_t *l, libtar_freefunc_t freefunc)
{
	libtar_listptr_t n;

	for (n = l->first; n != NULL; n = l->first)
	{
		l->first = n->next;
		if (freefunc != NULL)
			(*freefunc)(n->data);
		free(n);
	}

	l->nents = 0;
}


/*
** libtar_list_free() - remove and free() the whole list
*/
void
libtar_list_free(libtar_list_t *l, libtar_freefunc_t freefunc)
{
	libtar_list_empty(l, freefunc);
	free(l);
}


/*
** libtar_list_nents() - return number of elements in the list
*/
unsigned int
libtar_list_nents(libtar_list_t *l)
{
	return l->nents;
}


/*
** libtar_list_add() - adds an element to the list
** returns:
**	0			success
**	-1 (and sets errno)	failure
*/
int
libtar_list_add(libtar_list_t *l, void *data)
{
	libtar_listptr_t n, m;

#ifdef DS_DEBUG
	printf("==> libtar_list_add(\"%s\")\n", (char *)data);
#endif

	n = (libtar_listptr_t)malloc(sizeof(struct libtar_node));
	if (n == NULL)
		return -1;
	n->data = data;
	l->nents++;

#ifdef DS_DEBUG
	printf("    libtar_list_add(): allocated data\n");
#endif

	/* if the list is empty */
	if (l->first == NULL)
	{
		l->last = l->first = n;
		n->next = n->prev = NULL;
#ifdef DS_DEBUG
		printf("<== libtar_list_add(): list was empty; "
		       "added first element and returning 0\n");
#endif
		return 0;
	}

#ifdef DS_DEBUG
	printf("    libtar_list_add(): list not empty\n");
#endif

	if (l->flags == LIST_STACK)
	{
		n->prev = NULL;
		n->next = l->first;
		if (l->first != NULL)
			l->first->prev = n;
		l->first = n;
#ifdef DS_DEBUG
		printf("<== libtar_list_add(): LIST_STACK set; "
		       "added in front\n");
#endif
		return 0;
	}

	if (l->flags == LIST_QUEUE)
	{
		n->prev = l->last;
		n->next = NULL;
		if (l->last != NULL)
			l->last->next = n;
		l->last = n;
#ifdef DS_DEBUG
		printf("<== libtar_list_add(): LIST_QUEUE set; "
		       "added at end\n");
#endif
		return 0;
	}

	for (m = l->first; m != NULL; m = m->next)
		if ((*(l->cmpfunc))(data, m->data) < 0)
		{
			/*
			** if we find one that's bigger,
			** insert data before it
			*/
#ifdef DS_DEBUG
			printf("    libtar_list_add(): gotcha..."
			       "inserting data\n");
#endif
			if (m == l->first)
			{
				l->first = n;
				n->prev = NULL;
				m->prev = n;
				n->next = m;
#ifdef DS_DEBUG
				printf("<== libtar_list_add(): "
				       "added first, returning 0\n");
#endif
				return 0;
			}
			m->prev->next = n;
			n->prev = m->prev;
			m->prev = n;
			n->next = m;
#ifdef DS_DEBUG
			printf("<== libtar_list_add(): added middle,"
			       " returning 0\n");
#endif
			return 0;
		}

#ifdef DS_DEBUG
	printf("    libtar_list_add(): new data larger than current "
	       "list elements\n");
#endif

	/* if we get here, data is bigger than everything in the list */
	l->last->next = n;
	n->prev = l->last;
	l->last = n;
	n->next = NULL;
#ifdef DS_DEBUG
	printf("<== libtar_list_add(): added end, returning 0\n");
#endif
	return 0;
}


/*
** libtar_list_del() - remove the element pointed to by n
**				  from the list l
*/
void
libtar_list_del(libtar_list_t *l, libtar_listptr_t *n)
{
	libtar_listptr_t m;

#ifdef DS_DEBUG
	printf("==> libtar_list_del()\n");
#endif

	l->nents--;

	m = (*n)->next;

	if ((*n)->prev)
		(*n)->prev->next = (*n)->next;
	else
		l->first = (*n)->next;
	if ((*n)->next)
		(*n)->next->prev = (*n)->prev;
	else
		l->last = (*n)->prev;

	free(*n);
	*n = m;
}


/*
** libtar_list_next() - get the next element in the list
** returns:
**	1			success
**	0			end of list
*/
int
libtar_list_next(libtar_list_t *l,
			    libtar_listptr_t *n)
{
	if (*n == NULL)
		*n = l->first;
	else
		*n = (*n)->next;

	return (*n != NULL ? 1 : 0);
}


/*
** libtar_list_prev() - get the previous element in the list
** returns:
**	1			success
**	0			end of list
*/
int
libtar_list_prev(libtar_list_t *l,
			    libtar_listptr_t *n)
{
	if (*n == NULL)
		*n = l->last;
	else
		*n = (*n)->prev;

	return (*n != NULL ? 1 : 0);
}


/*
** libtar_str_match() - string matching function
** returns:
**	1			match
**	0			no match
*/
int
libtar_str_match(char *check, char *data)
{
	return !strcmp(check, data);
}


/*
** libtar_list_add_str() - splits string str into delim-delimited
**				      elements and adds them to list l
** returns:
**	0			success
**	-1 (and sets errno)	failure
*/
int
libtar_list_add_str(libtar_list_t *l,
			       char *str, char *delim)
{
	char tmp[10240];
	char *tokp, *nextp = tmp;

	strlcpy(tmp, str, sizeof(tmp));
	while ((tokp = strsep(&nextp, delim)) != NULL)
	{
		if (*tokp == '\0')
			continue;
		if (libtar_list_add(l, strdup(tokp)))
			return -1;
	}

	return 0;
}


/*
** libtar_list_search() - find an entry in a list
** returns:
**	1			match found
**	0			no match
*/
int
libtar_list_search(libtar_list_t *l,
			      libtar_listptr_t *n, void *data,
			      libtar_matchfunc_t matchfunc)
{
#ifdef DS_DEBUG
	printf("==> libtar_list_search(l=0x%lx, n=0x%lx, \"%s\")\n",
	       l, n, (char *)data);
#endif

	if (matchfunc == NULL)
		matchfunc = (libtar_matchfunc_t)libtar_str_match;

	if (*n == NULL)
		*n = l->first;
	else
		*n = (*n)->next;

	for (; *n != NULL; *n = (*n)->next)
	{
#ifdef DS_DEBUG
		printf("checking against \"%s\"\n", (char *)(*n)->data);
#endif
		if ((*(matchfunc))(data, (*n)->data) != 0)
			return 1;
	}

#ifdef DS_DEBUG
	printf("no matches found\n");
#endif
	return 0;
}


/*
** libtar_list_dup() - copy an existing list
*/
libtar_list_t *
libtar_list_dup(libtar_list_t *l)
{
	libtar_list_t *newlist;
	libtar_listptr_t n;

	newlist = libtar_list_new(l->flags, l->cmpfunc);
	for (n = l->first; n != NULL; n = n->next)
		libtar_list_add(newlist, n->data);

#ifdef DS_DEBUG
	printf("returning from libtar_list_dup()\n");
#endif
	return newlist;
}


/*
** libtar_list_merge() - merge two lists into a new list
*/
libtar_list_t *
libtar_list_merge(libtar_cmpfunc_t cmpfunc, int flags,
			     libtar_list_t *list1,
			     libtar_list_t *list2)
{
	libtar_list_t *newlist;
	libtar_listptr_t n;

	newlist = libtar_list_new(flags, cmpfunc);

	n = NULL;
	while (libtar_list_next(list1, &n) != 0)
		libtar_list_add(newlist, n->data);
	n = NULL;
	while (libtar_list_next(list2, &n) != 0)
		libtar_list_add(newlist, n->data);

	return newlist;
}