File: llist.c

package info (click to toggle)
ganglia-monitor-core 2.5.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,300 kB
  • ctags: 3,900
  • sloc: ansic: 27,889; sh: 8,492; makefile: 148
file content (129 lines) | stat: -rw-r--r-- 3,654 bytes parent folder | download | duplicates (4)
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
/*
 * $Id: llist.c,v 1.2 2002/06/20 03:23:24 massie Exp $
 * "Copyright (c) 1999 by Brent N. Chun and The Regents of the University 
 * of California.  All rights reserved."
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <ganglia/llist.h>

/*
 * ==========================================================
 *
 * Exported functions
 *
 * ==========================================================
 */

/* llist_add: add e to list llist */
void llist_add(llist_entry **llist, llist_entry *e)
{
    if ((*llist) != NULL) {
        e->prev = NULL;
        e->next = (*llist);
        (*llist)->prev = e;
        (*llist) = e;
    }
    else {
        e->prev = NULL;
        e->next = NULL;
        (*llist) = e;
    }
}

/* llist_remove: remove e from list llist */
int llist_remove(llist_entry **llist, llist_entry *e)
{
    llist_entry *ei;

    for (ei = (*llist); ei != NULL; ei = ei->next) {
        if (ei == e) {
            if ((e == (*llist)) && (e->next == NULL)) { 
                (*llist) = NULL;                
            }
            else if ((e == (*llist)) && (e->next != NULL)) { 
                e->next->prev = NULL;
                (*llist) = e->next;
            }
            else if (e->next == NULL) {
                e->prev->next = NULL;
            }
            else {
                e->prev->next = e->next;
                e->next->prev = e->prev;
            }
            return 0;
        }
    }
    return -1;
}

/* 
 * llist_search: search for entry with val that matches
 * according to compare_function in list llist. Return
 * match in e.
 */
int llist_search(llist_entry **llist, void *val, 
                 int (*compare_function)(void *, void *), 
                 llist_entry **e)
{
    llist_entry *ei;

    for (ei = (*llist); ei != NULL; ei = ei->next)
        if (compare_function(ei->val, val) == 0) {
            (*e) = ei;
            return 0;
        }
    return -1;
}

int
llist_print(llist_entry **llist)
{
   llist_entry *ei;

   for(ei = (*llist); ei != NULL; ei = ei->next)
      {
         printf("%s\n", (char *)ei->val);
      }
   return 0;
}
   

int
llist_sort(llist_entry *llist, int (*compare_function)(llist_entry *, llist_entry *))
{
    llist_entry     *lle1, *lle2;
    void            *tmp_val;

    for (lle1 = llist; lle1 != NULL; lle1 = lle1->next) {
        for (lle2 = lle1->next; lle2 != NULL; lle2 = lle2->next) {
            if (compare_function(lle1, lle2) == 1) {
                tmp_val = lle1->val;
                lle1->val = lle2->val;
                lle2->val = tmp_val;
            }
        }
    }
    return 0;
}