1 /*                                                     -*- linux-c -*-
2     Copyright (C) 2004 Tom Szilagyi
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18     $Id: trashlist.c 1237 2012-02-04 10:30:17Z assworth $
19 */
20 
21 #include <config.h>
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include "trashlist.h"
27 
28 
29 trashlist_t *
trashlist_new(void)30 trashlist_new(void) {
31 
32 	trashlist_t * root;
33 
34 	if ((root = (trashlist_t *)malloc(sizeof(trashlist_t))) == NULL) {
35 		fprintf(stderr, "trashlist.c : trashlist_new(): malloc error\n");
36 		return NULL;
37 	}
38 
39 	root->ptr = NULL;
40 	root->next = NULL;
41 
42 	return root;
43 }
44 
45 
46 void
trashlist_add(trashlist_t * root,void * ptr)47 trashlist_add(trashlist_t * root, void * ptr) {
48 
49 	trashlist_t * q_item = NULL;
50 	trashlist_t * q_prev;
51 
52 	if ((q_item = (trashlist_t *)malloc(sizeof(trashlist_t))) == NULL) {
53 		fprintf(stderr, "trashlist.c : trashlist_add(): malloc error\n");
54 		return;
55 	}
56 
57 	q_item->ptr = ptr;
58 	q_item->next = NULL;
59 
60 	q_prev = root;
61 	while (q_prev->next != NULL) {
62 		q_prev = q_prev->next;
63 	}
64 	q_prev->next = q_item;
65 }
66 
67 
68 void
trashlist_free(trashlist_t * root)69 trashlist_free(trashlist_t * root) {
70 
71 	trashlist_t * p;
72 	trashlist_t * q;
73 
74 	if (root == NULL)
75 		return;
76 
77 	p = root->next;
78 	while (p != NULL) {
79 		q = p->next;
80 		free(p->ptr);
81 		free(p);
82 		p = q;
83 	}
84 
85 	free(root);
86 }
87 
88 // vim: shiftwidth=8:tabstop=8:softtabstop=8 :
89 
90